mirror of https://github.com/golang-jwt/jwt.git
Merge pull request #152 from pusher/parse-unverified
Introduce (*Parser).ParseUnverified
This commit is contained in:
commit
3265a9bebd
110
parser.go
110
parser.go
|
@ -21,55 +21,9 @@ func (p *Parser) Parse(tokenString string, keyFunc Keyfunc) (*Token, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Parser) ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc) (*Token, error) {
|
func (p *Parser) ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc) (*Token, error) {
|
||||||
parts := strings.Split(tokenString, ".")
|
token, parts, err := p.ParseUnverified(tokenString, claims)
|
||||||
if len(parts) != 3 {
|
|
||||||
return nil, NewValidationError("token contains an invalid number of segments", ValidationErrorMalformed)
|
|
||||||
}
|
|
||||||
|
|
||||||
var err error
|
|
||||||
token := &Token{Raw: tokenString}
|
|
||||||
|
|
||||||
// parse Header
|
|
||||||
var headerBytes []byte
|
|
||||||
if headerBytes, err = DecodeSegment(parts[0]); err != nil {
|
|
||||||
if strings.HasPrefix(strings.ToLower(tokenString), "bearer ") {
|
|
||||||
return token, NewValidationError("tokenstring should not contain 'bearer '", ValidationErrorMalformed)
|
|
||||||
}
|
|
||||||
return token, &ValidationError{Inner: err, Errors: ValidationErrorMalformed}
|
|
||||||
}
|
|
||||||
if err = json.Unmarshal(headerBytes, &token.Header); err != nil {
|
|
||||||
return token, &ValidationError{Inner: err, Errors: ValidationErrorMalformed}
|
|
||||||
}
|
|
||||||
|
|
||||||
// parse Claims
|
|
||||||
var claimBytes []byte
|
|
||||||
token.Claims = claims
|
|
||||||
|
|
||||||
if claimBytes, err = DecodeSegment(parts[1]); err != nil {
|
|
||||||
return token, &ValidationError{Inner: err, Errors: ValidationErrorMalformed}
|
|
||||||
}
|
|
||||||
dec := json.NewDecoder(bytes.NewBuffer(claimBytes))
|
|
||||||
if p.UseJSONNumber {
|
|
||||||
dec.UseNumber()
|
|
||||||
}
|
|
||||||
// JSON Decode. Special case for map type to avoid weird pointer behavior
|
|
||||||
if c, ok := token.Claims.(MapClaims); ok {
|
|
||||||
err = dec.Decode(&c)
|
|
||||||
} else {
|
|
||||||
err = dec.Decode(&claims)
|
|
||||||
}
|
|
||||||
// Handle decode error
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return token, &ValidationError{Inner: err, Errors: ValidationErrorMalformed}
|
return token, err
|
||||||
}
|
|
||||||
|
|
||||||
// Lookup signature method
|
|
||||||
if method, ok := token.Header["alg"].(string); ok {
|
|
||||||
if token.Method = GetSigningMethod(method); token.Method == nil {
|
|
||||||
return token, NewValidationError("signing method (alg) is unavailable.", ValidationErrorUnverifiable)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return token, NewValidationError("signing method (alg) is unspecified.", ValidationErrorUnverifiable)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify signing method is in the required set
|
// Verify signing method is in the required set
|
||||||
|
@ -132,3 +86,63 @@ func (p *Parser) ParseWithClaims(tokenString string, claims Claims, keyFunc Keyf
|
||||||
|
|
||||||
return token, vErr
|
return token, vErr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WARNING: Don't use this method unless you know what you're doing
|
||||||
|
//
|
||||||
|
// This method parses the token but doesn't validate the signature. It's only
|
||||||
|
// ever useful in cases where you know the signature is valid (because it has
|
||||||
|
// been checked previously in the stack) and you want to extract values from
|
||||||
|
// it.
|
||||||
|
func (p *Parser) ParseUnverified(tokenString string, claims Claims) (token *Token, parts []string, err error) {
|
||||||
|
parts = strings.Split(tokenString, ".")
|
||||||
|
if len(parts) != 3 {
|
||||||
|
return nil, parts, NewValidationError("token contains an invalid number of segments", ValidationErrorMalformed)
|
||||||
|
}
|
||||||
|
|
||||||
|
token = &Token{Raw: tokenString}
|
||||||
|
|
||||||
|
// parse Header
|
||||||
|
var headerBytes []byte
|
||||||
|
if headerBytes, err = DecodeSegment(parts[0]); err != nil {
|
||||||
|
if strings.HasPrefix(strings.ToLower(tokenString), "bearer ") {
|
||||||
|
return token, parts, NewValidationError("tokenstring should not contain 'bearer '", ValidationErrorMalformed)
|
||||||
|
}
|
||||||
|
return token, parts, &ValidationError{Inner: err, Errors: ValidationErrorMalformed}
|
||||||
|
}
|
||||||
|
if err = json.Unmarshal(headerBytes, &token.Header); err != nil {
|
||||||
|
return token, parts, &ValidationError{Inner: err, Errors: ValidationErrorMalformed}
|
||||||
|
}
|
||||||
|
|
||||||
|
// parse Claims
|
||||||
|
var claimBytes []byte
|
||||||
|
token.Claims = claims
|
||||||
|
|
||||||
|
if claimBytes, err = DecodeSegment(parts[1]); err != nil {
|
||||||
|
return token, parts, &ValidationError{Inner: err, Errors: ValidationErrorMalformed}
|
||||||
|
}
|
||||||
|
dec := json.NewDecoder(bytes.NewBuffer(claimBytes))
|
||||||
|
if p.UseJSONNumber {
|
||||||
|
dec.UseNumber()
|
||||||
|
}
|
||||||
|
// JSON Decode. Special case for map type to avoid weird pointer behavior
|
||||||
|
if c, ok := token.Claims.(MapClaims); ok {
|
||||||
|
err = dec.Decode(&c)
|
||||||
|
} else {
|
||||||
|
err = dec.Decode(&claims)
|
||||||
|
}
|
||||||
|
// Handle decode error
|
||||||
|
if err != nil {
|
||||||
|
return token, parts, &ValidationError{Inner: err, Errors: ValidationErrorMalformed}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lookup signature method
|
||||||
|
if method, ok := token.Header["alg"].(string); ok {
|
||||||
|
if token.Method = GetSigningMethod(method); token.Method == nil {
|
||||||
|
return token, parts, NewValidationError("signing method (alg) is unavailable.", ValidationErrorUnverifiable)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return token, parts, NewValidationError("signing method (alg) is unspecified.", ValidationErrorUnverifiable)
|
||||||
|
}
|
||||||
|
|
||||||
|
return token, parts, nil
|
||||||
|
}
|
||||||
|
|
|
@ -247,6 +247,46 @@ func TestParser_Parse(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestParser_ParseUnverified(t *testing.T) {
|
||||||
|
privateKey := test.LoadRSAPrivateKeyFromDisk("test/sample_key")
|
||||||
|
|
||||||
|
// Iterate over test data set and run tests
|
||||||
|
for _, data := range jwtTestData {
|
||||||
|
// If the token string is blank, use helper function to generate string
|
||||||
|
if data.tokenString == "" {
|
||||||
|
data.tokenString = test.MakeSampleToken(data.claims, privateKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse the token
|
||||||
|
var token *jwt.Token
|
||||||
|
var err error
|
||||||
|
var parser = data.parser
|
||||||
|
if parser == nil {
|
||||||
|
parser = new(jwt.Parser)
|
||||||
|
}
|
||||||
|
// Figure out correct claims type
|
||||||
|
switch data.claims.(type) {
|
||||||
|
case jwt.MapClaims:
|
||||||
|
token, _, err = parser.ParseUnverified(data.tokenString, jwt.MapClaims{})
|
||||||
|
case *jwt.StandardClaims:
|
||||||
|
token, _, err = parser.ParseUnverified(data.tokenString, &jwt.StandardClaims{})
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("[%v] Invalid token")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify result matches expectation
|
||||||
|
if !reflect.DeepEqual(data.claims, token.Claims) {
|
||||||
|
t.Errorf("[%v] Claims mismatch. Expecting: %v Got: %v", data.name, data.claims, token.Claims)
|
||||||
|
}
|
||||||
|
|
||||||
|
if data.valid && err != nil {
|
||||||
|
t.Errorf("[%v] Error while verifying token: %T:%v", data.name, err, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Helper method for benchmarking various methods
|
// Helper method for benchmarking various methods
|
||||||
func benchmarkSigning(b *testing.B, method jwt.SigningMethod, key interface{}) {
|
func benchmarkSigning(b *testing.B, method jwt.SigningMethod, key interface{}) {
|
||||||
t := jwt.New(method)
|
t := jwt.New(method)
|
||||||
|
|
Loading…
Reference in New Issue