// parseToken tries and parse a valid token from a string.
// A token ID and token secret are returned in case of success, an error otherwise.
430326
func parseToken(s string) (string, string, error) {
	split := tokenRegexp.FindStringSubmatch(s)
	if len(split) != 3 {
		return "", "", fmt.Errorf("token [%q] was not of form [%q]", s, tokenRegexpString)
	}
	return split[1], split[2], nil
}
1322571
func (s *GitHubCodeDestination) SetToken(v string) *GitHubCodeDestination {
	s.Token = &v
	return s
}
1289537
func Token(r *http.Request) string {
	if val, err := contextGet(r, tokenKey); err == nil {
		if maskedToken, ok := val.(string); ok {
			return maskedToken
		}
	}

	return ""
}
21089
func (p *parser) parseStringToken(tok *scanner.Token) (string, error) {
	var backslashes bool
	switch tok.Type {
	case scanner.LITERAL:
		backslashes = false
	case scanner.STRING:
		backslashes = true
	default:
		panic("unsupported string token type")
	}

	raw := []byte(tok.Content)
	buf := make([]byte, 0, len(raw))

	for i := 0; i < len(raw); i++ {
		b := raw[i]
		more := len(raw) > (i + 1)

		if b == '$' {
			if more && raw[i+1] == '$' {
				// skip over the second dollar sign
				i++
			}
		} else if backslashes && b == '\\' {
			if !more {
				return "", Errorf(
					ast.Pos{
						Column: tok.Pos.Column + utf8.RuneCount(raw[:i]),
						Line:   tok.Pos.Line,
					},
					`unfinished backslash escape sequence`,
				)
			}
			escapeType := raw[i+1]
			switch escapeType {
			case '\\':
				// skip over the second slash
				i++
			case 'n':
				b = '\n'
				i++
			case '"':
				b = '"'
				i++
			default:
				return "", Errorf(
					ast.Pos{
						Column: tok.Pos.Column + utf8.RuneCount(raw[:i]),
						Line:   tok.Pos.Line,
					},
					`invalid backslash escape sequence`,
				)
			}
		}

		buf = append(buf, b)
	}

	return string(buf), nil
}
1087796
func ParseTokenParam(paramString string, info SearchParamInfo) *TokenParam {
	t := &TokenParam{SearchParamInfo: info}
	splitCode := escapeFriendlySplit(paramString, '|')
	if len(splitCode) > 1 {
		t.System = unescape(splitCode[0])
		t.Code = unescape(splitCode[1])
	} else {
		t.AnySystem = true
		t.Code = unescape(splitCode[0])
	}
	return t
}
150427
func parseTokens(dirs []string) []token {
	if len(dirs) == 0 {
		return make([]token, 0)
	}

	switch dirs[0] {
	case "":
		if len(dirs) == 1 {
			return []token{&component{fns: []componentFn{substring("")}}}
		}
		return parseTokens(dirs[1:])
	case "**":

		rest := parseTokens(dirs[1:])
		if len(rest) == 0 {
			// If there are no remaining tokens, return a lone
			// doubleStar token.
			return []token{&doubleStar{
				Until: nil,
			}}
		}

		// Otherwise, return a doubleStar token that will match greedily
		// until the first component in the remainder of the pattern,
		// and then the remainder of the pattern.
		return append([]token{&doubleStar{
			Until: rest[0],
		}}, rest[1:]...)
	default:
		// Ordinarily, simply return the appropriate component, and
		// continue on.
		return append([]token{&component{
			fns: parseComponent(dirs[0]),
		}}, parseTokens(dirs[1:])...)
	}
}
675542
func SetForToken(document interface{}, decodedToken string, value interface{}) (interface{}, error) {
	return document, setSingleImpl(document, value, decodedToken, swag.DefaultJSONNameProvider)
}
1088895
func (dec *Decoder) tokenPrepareForDecode() error {
	// Note: Not calling peek before switch, to avoid
	// putting peek into the standard Decode path.
	// peek is only called when using the Token API.
	switch dec.tokenState {
	case tokenArrayComma:
		c, err := dec.peek()
		if err != nil {
			return err
		}
		if c != ',' {
			return &SyntaxError{"expected comma after array element", dec.offset()}
		}
		dec.scanp++
		dec.tokenState = tokenArrayValue
	case tokenObjectColon:
		c, err := dec.peek()
		if err != nil {
			return err
		}
		if c != ':' {
			return &SyntaxError{"expected colon after object key", dec.offset()}
		}
		dec.scanp++
		dec.tokenState = tokenObjectValue
	}
	return nil
}
1145927
func parseToken(secret []byte, token string) (*AccessToken, error) {
	t, err := jwtParse(secret, token)

	if err != nil {
		return nil, err
	}

	if !t.Valid {
		return nil, nil
	}

	return jwtToAccessToken(t)
}
1302183
func ParseOAuthToken(response string) (*OAuthToken, error) {
	val, err := url.ParseQuery(strings.TrimSpace(response))
	if err != nil {
		return nil, err
	}

	ret := &OAuthToken{}

	oauth_problem := val.Get("oauth_problem")
	if oauth_problem != "" {
		ret.OAuthProblem = oauth_problem
		return ret, flickErr.NewError(flickErr.OAuthTokenError, oauth_problem)
	}

	ret.OAuthToken = val.Get("oauth_token")
	ret.OAuthTokenSecret = val.Get("oauth_token_secret")
	ret.Fullname = val.Get("fullname")
	ret.UserNsid = val.Get("user_nsid")
	ret.Username = val.Get("username")

	return ret, nil
}
238396
func getFormatToken(format string) (token string, remain string, succ bool) {
	if len(format) == 0 {
		return "", "", true
	}

	// Just one character.
	if len(format) == 1 {
		if format[0] == '%' {
			return "", "", false
		}
		return format, "", true
	}

	// More than one character.
	if format[0] == '%' {
		return format[:2], format[2:], true
	}

	return format[:1], format[1:], true
}
1108406
func (p *Parser) parseTagKeyExpr() (Token, Literal, error) {
	var err error

	// Parse required WITH KEY tokens.
	if err := p.parseTokens([]Token{WITH, KEY}); err != nil {
		return 0, nil, err
	}

	// Parse required IN, EQ, or EQREGEX token.
	tok, pos, lit := p.ScanIgnoreWhitespace()
	if tok == IN {
		// Parse required ( token.
		if tok, pos, lit = p.ScanIgnoreWhitespace(); tok != LPAREN {
			return 0, nil, newParseError(tokstr(tok, lit), []string{"("}, pos)
		}

		// Parse tag key list.
		var tagKeys []string
		if tagKeys, err = p.ParseIdentList(); err != nil {
			return 0, nil, err
		}

		// Parse required ) token.
		if tok, pos, lit = p.ScanIgnoreWhitespace(); tok != RPAREN {
			return 0, nil, newParseError(tokstr(tok, lit), []string{")"}, pos)
		}
		return IN, &ListLiteral{Vals: tagKeys}, nil
	} else if tok == EQ || tok == NEQ {
		// Parse required tag key.
		ident, err := p.ParseIdent()
		if err != nil {
			return 0, nil, err
		}
		return tok, &StringLiteral{Val: ident}, nil
	} else if tok == EQREGEX || tok == NEQREGEX {
		re, err := p.parseRegex()
		if err != nil {
			return 0, nil, err
		} else if re == nil {
			// parseRegex can return an empty type, but we need it to be present
			tok, pos, lit := p.ScanIgnoreWhitespace()
			return 0, nil, newParseError(tokstr(tok, lit), []string{"regex"}, pos)
		}
		return tok, re, nil
	}
	return 0, nil, newParseError(tokstr(tok, lit), []string{"IN", "=", "=~"}, pos)
}
891229
func GenerateCryptoKey() (*CryptoKey, error) {
	var key CryptoKey
	_, err := io.ReadFull(prng, key[:])
	if err != nil {
		return nil, err
	}

	return &key, nil
}
466115
func (c *Client) SetToken(token string, tokenSecret string) {
	c.user = oauth1.NewToken(token, tokenSecret)
	c.client = nil
}
1219938
func signToken(secret []byte, token *AccessToken) (string, error) {
	t := accessTokenToJwt(token)
	return t.SignedString(secret)
}
233037
func parseTokenConstraints(a ast.Value) (constraints tokenConstraints, err error) {
	constraints.time = -1
	var o ast.Object
	var ok bool
	if o, ok = a.(ast.Object); !ok {
		err = fmt.Errorf("token constraints must be object")
		return
	}
	if err = o.Iter(func(k *ast.Term, v *ast.Term) (err error) {
		var handler tokenConstraintHandler
		var ok bool
		name := string(k.Value.(ast.String))
		if handler, ok = tokenConstraintTypes[name]; ok {
			if err = handler(v.Value, &constraints); err != nil {
				return
			}
		} else {
			// Anything unknown is rejected.
			err = fmt.Errorf("unknown token validation constraint: %s", name)
			return
		}
		return
	}); err != nil {
		return
	}
	return
}
792524
func (p *Parser) ParseTokens() ([]Sexp, error) {
	select {
	case out := <-p.ParsedOutput:
		// out is type []ParserReply
		r := make([]Sexp, 0)
		for _, k := range out {
			r = append(r, k.Expr...)
			if k.Err != nil {
				return r, k.Err
			}
		}
		return r, nil
	case <-p.reqStop:
		return nil, ErrShuttingDown
	}
}