Пример #1
0
func parseChallenge(challenge string) (parsed Challenges, err error) {
	traceFn, traceT := trace.M(traceId, trace.Trace)

	r := strings.NewReader(challenge)
	rec := lexrec.NewRecord(256, nil, func(l *lexrec.Lexer) {})

	var l *lexrec.Lexer
	l, err = lexrec.NewLexerRun("ParseChallenge", r, rec, emitWWWAuthenticate)
	if err != nil {
		return nil, err
	}

	for {
		item := l.NextItem()
		if item.Type == lexrec.ItemEOF {
			break
		} else if item.Type == lexrec.ItemError {
			err = fmt.Errorf("error at position %d: %s", item.Pos, item.Value)
			break
		}

		switch item.Type {
		case ItemDigest:
			parsed = append(parsed, &Challenge{Scheme: item.Value})
		case ItemBasic:
			parsed = append(parsed, &Challenge{Scheme: item.Value})
		case ItemRealm:
			if i := len(parsed) - 1; i >= 0 {
				parsed[i].Realm = item.Value[1 : len(item.Value)-1]
			}
		case ItemDomain:
			if i := len(parsed) - 1; i >= 0 {
				parsed[i].Domain = strings.Fields(item.Value[1 : len(item.Value)-1])
			}
		case ItemNonce:
			if i := len(parsed) - 1; i >= 0 {
				parsed[i].Nonce = item.Value[1 : len(item.Value)-1]
			}
		case ItemOpaque:
			if i := len(parsed) - 1; i >= 0 {
				parsed[i].Opaque = item.Value[1 : len(item.Value)-1]
			}
		case ItemStale:
			if i := len(parsed) - 1; i >= 0 {
				if item.Value == "true" {
					parsed[i].Stale = true
				} else {
					parsed[i].Stale = false
				}
			}
		case ItemAlgorithm:
			if i := len(parsed) - 1; i >= 0 {
				parsed[i].Algorithm = item.Value
			}
		case ItemQop:
			if i := len(parsed) - 1; i >= 0 {
				options := strings.Split(item.Value[1:len(item.Value)-1], ",")
				parsed[i].Qop = options
			}
		case ItemAuthParam:
			if traceT {
				trace.T(traceFn, "skipping unrecognized auth-param: %s", item.Value)
			}
		default:
			err = fmt.Errorf("unhandled item type %d at position %d: %v", item.Type, item.Pos, item.Value)
			return
		}
	}

	return
}