Skip to content

Commit dce8e4d

Browse files
slickwillioxisto
andauthored
Set token.Signature in ParseUnverified (#414)
* set token.Signature in ParseUnverified * parser.go: using the already decoded token signature --------- Co-authored-by: Christian Banse <christian.banse@aisec.fraunhofer.de>
1 parent 8889e20 commit dce8e4d

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

parser.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,6 @@ func (p *Parser) ParseWithClaims(tokenString string, claims Claims, keyFunc Keyf
7676
}
7777
}
7878

79-
// Decode signature
80-
token.Signature, err = p.DecodeSegment(parts[2])
81-
if err != nil {
82-
return token, newError("could not base64 decode signature", ErrTokenMalformed, err)
83-
}
84-
text := strings.Join(parts[0:2], ".")
85-
8679
// Lookup key(s)
8780
if keyFunc == nil {
8881
// keyFunc was not provided. short circuiting validation
@@ -94,11 +87,14 @@ func (p *Parser) ParseWithClaims(tokenString string, claims Claims, keyFunc Keyf
9487
return token, newError("error while executing keyfunc", ErrTokenUnverifiable, err)
9588
}
9689

90+
// Join together header and claims in order to verify them with the signature
91+
text := strings.Join(parts[0:2], ".")
9792
switch have := got.(type) {
9893
case VerificationKeySet:
9994
if len(have.Keys) == 0 {
10095
return token, newError("keyfunc returned empty verification key set", ErrTokenUnverifiable)
10196
}
97+
10298
// Iterate through keys and verify signature, skipping the rest when a match is found.
10399
// Return the last error if no match is found.
104100
for _, key := range have.Keys {
@@ -131,7 +127,7 @@ func (p *Parser) ParseWithClaims(tokenString string, claims Claims, keyFunc Keyf
131127
return token, nil
132128
}
133129

134-
// ParseUnverified parses the token but doesn't validate the signature.
130+
// ParseUnverified parses the token but does not validate the signature.
135131
//
136132
// WARNING: Don't use this method unless you know what you're doing.
137133
//
@@ -146,7 +142,7 @@ func (p *Parser) ParseUnverified(tokenString string, claims Claims) (token *Toke
146142

147143
token = &Token{Raw: tokenString}
148144

149-
// parse Header
145+
// Parse Header
150146
var headerBytes []byte
151147
if headerBytes, err = p.DecodeSegment(parts[0]); err != nil {
152148
return token, parts, newError("could not base64 decode header", ErrTokenMalformed, err)
@@ -155,7 +151,7 @@ func (p *Parser) ParseUnverified(tokenString string, claims Claims) (token *Toke
155151
return token, parts, newError("could not JSON decode header", ErrTokenMalformed, err)
156152
}
157153

158-
// parse Claims
154+
// Parse Claims
159155
token.Claims = claims
160156

161157
claimBytes, err := p.DecodeSegment(parts[1])
@@ -196,6 +192,12 @@ func (p *Parser) ParseUnverified(tokenString string, claims Claims) (token *Toke
196192
return token, parts, newError("signing method (alg) is unspecified", ErrTokenUnverifiable)
197193
}
198194

195+
// Parse token signature
196+
token.Signature, err = p.DecodeSegment(parts[2])
197+
if err != nil {
198+
return token, parts, newError("could not base64 decode signature", ErrTokenMalformed, err)
199+
}
200+
199201
return token, parts, nil
200202
}
201203

parser_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -632,9 +632,9 @@ func TestParser_ParseUnverified(t *testing.T) {
632632
// The 'Valid' field should not be set to true when invoking ParseUnverified()
633633
t.Errorf("[%v] Token.Valid field mismatch. Expecting false, got %v", data.name, token.Valid)
634634
}
635-
if len(token.Signature) != 0 {
636-
// The signature was not validated, hence the 'Signature' field is not populated.
637-
t.Errorf("[%v] Token.Signature field mismatch. Expecting '', got %v", data.name, token.Signature)
635+
if len(token.Signature) == 0 {
636+
// The 'Signature' should always be populated.
637+
t.Errorf("[%v] Token.Signature field mismatch. Expecting non-nil, got %v", data.name, token.Signature)
638638
}
639639
})
640640
}

0 commit comments

Comments
 (0)