Verify verifies the token and returns the claims. If issuer is non-empty, the token's issuer must match the issuer. If audience is non-empty, one of the token's audiences must match the audience. The current system timestamp is used as reference to verify not before, issued at and expiry.
(ctx context.Context, token *jwt.JSONWebToken, keyProvider PublicKeyProvider, issuer, audience string)
| 187 | // If audience is non-empty, one of the token's audiences must match the audience. |
| 188 | // The current system timestamp is used as reference to verify not before, issued at and expiry. |
| 189 | func Verify(ctx context.Context, token *jwt.JSONWebToken, keyProvider PublicKeyProvider, issuer, audience string) (TokenClaims, error) { |
| 190 | keys, err := keyProvider.PublicKeys(ctx) |
| 191 | if err != nil { |
| 192 | return TokenClaims{}, err |
| 193 | } |
| 194 | var claims TokenClaims |
| 195 | if err := token.Claims(keys, &claims); err != nil { |
| 196 | return TokenClaims{}, errOAuth2Token.WithCause(err) |
| 197 | } |
| 198 | |
| 199 | exp := jwt.Expected{ |
| 200 | Issuer: issuer, |
| 201 | Time: time.Now(), |
| 202 | } |
| 203 | if audience != "" { |
| 204 | exp.Audience = jwt.Audience{audience} |
| 205 | } |
| 206 | if err := claims.Validate(exp); err != nil { |
| 207 | return TokenClaims{}, errNotAuthorized.WithCause(err) |
| 208 | } |
| 209 | return claims, nil |
| 210 | } |
| 211 | |
| 212 | // CachePublicKey caches the result from the given PublicKeyProvider with the TTL. |
| 213 | func CachePublicKey(provider PublicKeyProvider, ttl time.Duration) PublicKeyProvider { |
no test coverage detected