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

feat(idtoken): add ParsePayload returning unvalidated token payload #2136

Merged
merged 8 commits into from
Sep 14, 2023
30 changes: 29 additions & 1 deletion idtoken/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,34 @@ func Validate(ctx context.Context, idToken string, audience string) (*Payload, e
return defaultValidator.validate(ctx, idToken, audience)
}

// GetPayload just gets the payload part of the token.
noahdietz marked this conversation as resolved.
Show resolved Hide resolved
//
// WARNING: THIS FUNCTION DOES NOT VALIDATE THE TOKEN.
noahdietz marked this conversation as resolved.
Show resolved Hide resolved
//
// In fact, it explicitly skips the validation part. It should only be used to inspect the payload
// content of a token, perhaps for debugging purposes, as a means to try to figure out why the
// validation failed. Note that if Validate() succeeds, it already returns the exact payload that
// this function returns.
noahdietz marked this conversation as resolved.
Show resolved Hide resolved
func (v *Validator) GetPayload(ctx context.Context, idToken string) (*Payload, error) {
noahdietz marked this conversation as resolved.
Show resolved Hide resolved
jwt, err := parseJWT(idToken)
if err != nil {
return nil, err
}
return jwt.parsedPayload()
}

// GetPayload just gets the payload part of the token.
noahdietz marked this conversation as resolved.
Show resolved Hide resolved
//
// WARNING: THIS FUNCTION DOES NOT VALIDATE THE TOKEN.
noahdietz marked this conversation as resolved.
Show resolved Hide resolved
//
// In fact, it explicitly skips the validation part. It should only be used to inspect the payload
// content of a token, perhaps for debugging purposes, as a means to try to figure out why the
// validation failed. Note that if Validate() succeeds, it already returns the exact payload that
// this function returns.
noahdietz marked this conversation as resolved.
Show resolved Hide resolved
func GetPayload(ctx context.Context, idToken string) (*Payload, error) {
noahdietz marked this conversation as resolved.
Show resolved Hide resolved
return defaultValidator.GetPayload(ctx, idToken)
}

func (v *Validator) validate(ctx context.Context, idToken string, audience string) (*Payload, error) {
jwt, err := parseJWT(idToken)
if err != nil {
Expand All @@ -145,7 +173,7 @@ func (v *Validator) validate(ctx context.Context, idToken string, audience strin
}

if now().Unix() > payload.Expires {
return nil, fmt.Errorf("idtoken: token expired")
return nil, fmt.Errorf("idtoken: token expired: now=%v, expires=%v", now().Unix(), payload.Expires)
}

switch header.Algorithm {
Expand Down