Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid use of json.NewDecoder #313

Merged
merged 4 commits into from
Aug 15, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,28 +135,36 @@ func (p *Parser) ParseUnverified(tokenString string, claims Claims) (token *Toke
}
return token, parts, newError("could not base64 decode header", ErrTokenMalformed, err)
}
if err = json.Unmarshal(headerBytes, &token.Header); err != nil {
if err := json.Unmarshal(headerBytes, &token.Header); err != nil {
oxisto marked this conversation as resolved.
Show resolved Hide resolved
return token, parts, newError("could not JSON decode header", ErrTokenMalformed, err)
}

// parse Claims
var claimBytes []byte
token.Claims = claims

if claimBytes, err = p.DecodeSegment(parts[1]); err != nil {
claimBytes, err := p.DecodeSegment(parts[1])
if err != nil {
return token, parts, newError("could not base64 decode claim", ErrTokenMalformed, err)
}
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)

if !p.useJSONNumber {
oxisto marked this conversation as resolved.
Show resolved Hide resolved
// JSON Unmarshal. Special case for map type to avoid weird pointer behavior.
if c, ok := token.Claims.(MapClaims); ok {
err = json.Unmarshal(claimBytes, &c)
} else {
err = json.Unmarshal(claimBytes, &claims)
}
} else {
err = dec.Decode(&claims)
dec := json.NewDecoder(bytes.NewBuffer(claimBytes))
dec.UseNumber()

// JSON Decode. Special case for map type to avoid weird pointer behavior
oxisto marked this conversation as resolved.
Show resolved Hide resolved
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, newError("could not JSON decode claim", ErrTokenMalformed, err)
}
Expand Down