forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/golang-jwt/jwt/v4/claims.go

435 lines
8.6 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
package jwt
import (
"crypto/subtle"
"fmt"
"time"
)
// Claims must just have a Valid method that determines
2024-02-18 10:42:21 +00:00
// if the token is invalid for any supported reason
2024-02-18 10:42:21 +00:00
type Claims interface {
Valid() error
}
// RegisteredClaims are a structured version of the JWT Claims Set,
2024-02-18 10:42:21 +00:00
// restricted to Registered Claim Names, as referenced at
2024-02-18 10:42:21 +00:00
// https://datatracker.ietf.org/doc/html/rfc7519#section-4.1
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// This type can be used on its own, but then additional private and
2024-02-18 10:42:21 +00:00
// public claims embedded in the JWT will not be parsed. The typical usecase
2024-02-18 10:42:21 +00:00
// therefore is to embedded this in a user-defined claim type.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// See examples for how to use this with your own claim types.
2024-02-18 10:42:21 +00:00
type RegisteredClaims struct {
2024-02-18 10:42:21 +00:00
// the `iss` (Issuer) claim. See https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.1
2024-02-18 10:42:21 +00:00
Issuer string `json:"iss,omitempty"`
// the `sub` (Subject) claim. See https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.2
2024-02-18 10:42:21 +00:00
Subject string `json:"sub,omitempty"`
// the `aud` (Audience) claim. See https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.3
2024-02-18 10:42:21 +00:00
Audience ClaimStrings `json:"aud,omitempty"`
// the `exp` (Expiration Time) claim. See https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.4
2024-02-18 10:42:21 +00:00
ExpiresAt *NumericDate `json:"exp,omitempty"`
// the `nbf` (Not Before) claim. See https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.5
2024-02-18 10:42:21 +00:00
NotBefore *NumericDate `json:"nbf,omitempty"`
// the `iat` (Issued At) claim. See https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.6
2024-02-18 10:42:21 +00:00
IssuedAt *NumericDate `json:"iat,omitempty"`
// the `jti` (JWT ID) claim. See https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.7
2024-02-18 10:42:21 +00:00
ID string `json:"jti,omitempty"`
}
// Valid validates time based claims "exp, iat, nbf".
2024-02-18 10:42:21 +00:00
// There is no accounting for clock skew.
2024-02-18 10:42:21 +00:00
// As well, if any of the above claims are not in the token, it will still
2024-02-18 10:42:21 +00:00
// be considered a valid claim.
2024-02-18 10:42:21 +00:00
func (c RegisteredClaims) Valid() error {
2024-02-18 10:42:21 +00:00
vErr := new(ValidationError)
2024-02-18 10:42:21 +00:00
now := TimeFunc()
// The claims below are optional, by default, so if they are set to the
2024-02-18 10:42:21 +00:00
// default value in Go, let's not fail the verification for them.
2024-02-18 10:42:21 +00:00
if !c.VerifyExpiresAt(now, false) {
2024-02-18 10:42:21 +00:00
delta := now.Sub(c.ExpiresAt.Time)
2024-02-18 10:42:21 +00:00
vErr.Inner = fmt.Errorf("%s by %s", ErrTokenExpired, delta)
2024-02-18 10:42:21 +00:00
vErr.Errors |= ValidationErrorExpired
2024-02-18 10:42:21 +00:00
}
if !c.VerifyIssuedAt(now, false) {
2024-02-18 10:42:21 +00:00
vErr.Inner = ErrTokenUsedBeforeIssued
2024-02-18 10:42:21 +00:00
vErr.Errors |= ValidationErrorIssuedAt
2024-02-18 10:42:21 +00:00
}
if !c.VerifyNotBefore(now, false) {
2024-02-18 10:42:21 +00:00
vErr.Inner = ErrTokenNotValidYet
2024-02-18 10:42:21 +00:00
vErr.Errors |= ValidationErrorNotValidYet
2024-02-18 10:42:21 +00:00
}
if vErr.valid() {
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
return vErr
2024-02-18 10:42:21 +00:00
}
// VerifyAudience compares the aud claim against cmp.
2024-02-18 10:42:21 +00:00
// If required is false, this method will return true if the value matches or is unset
2024-02-18 10:42:21 +00:00
func (c *RegisteredClaims) VerifyAudience(cmp string, req bool) bool {
2024-02-18 10:42:21 +00:00
return verifyAud(c.Audience, cmp, req)
2024-02-18 10:42:21 +00:00
}
// VerifyExpiresAt compares the exp claim against cmp (cmp < exp).
2024-02-18 10:42:21 +00:00
// If req is false, it will return true, if exp is unset.
2024-02-18 10:42:21 +00:00
func (c *RegisteredClaims) VerifyExpiresAt(cmp time.Time, req bool) bool {
2024-02-18 10:42:21 +00:00
if c.ExpiresAt == nil {
2024-02-18 10:42:21 +00:00
return verifyExp(nil, cmp, req)
2024-02-18 10:42:21 +00:00
}
return verifyExp(&c.ExpiresAt.Time, cmp, req)
2024-02-18 10:42:21 +00:00
}
// VerifyIssuedAt compares the iat claim against cmp (cmp >= iat).
2024-02-18 10:42:21 +00:00
// If req is false, it will return true, if iat is unset.
2024-02-18 10:42:21 +00:00
func (c *RegisteredClaims) VerifyIssuedAt(cmp time.Time, req bool) bool {
2024-02-18 10:42:21 +00:00
if c.IssuedAt == nil {
2024-02-18 10:42:21 +00:00
return verifyIat(nil, cmp, req)
2024-02-18 10:42:21 +00:00
}
return verifyIat(&c.IssuedAt.Time, cmp, req)
2024-02-18 10:42:21 +00:00
}
// VerifyNotBefore compares the nbf claim against cmp (cmp >= nbf).
2024-02-18 10:42:21 +00:00
// If req is false, it will return true, if nbf is unset.
2024-02-18 10:42:21 +00:00
func (c *RegisteredClaims) VerifyNotBefore(cmp time.Time, req bool) bool {
2024-02-18 10:42:21 +00:00
if c.NotBefore == nil {
2024-02-18 10:42:21 +00:00
return verifyNbf(nil, cmp, req)
2024-02-18 10:42:21 +00:00
}
return verifyNbf(&c.NotBefore.Time, cmp, req)
2024-02-18 10:42:21 +00:00
}
// VerifyIssuer compares the iss claim against cmp.
2024-02-18 10:42:21 +00:00
// If required is false, this method will return true if the value matches or is unset
2024-02-18 10:42:21 +00:00
func (c *RegisteredClaims) VerifyIssuer(cmp string, req bool) bool {
2024-02-18 10:42:21 +00:00
return verifyIss(c.Issuer, cmp, req)
2024-02-18 10:42:21 +00:00
}
// StandardClaims are a structured version of the JWT Claims Set, as referenced at
2024-02-18 10:42:21 +00:00
// https://datatracker.ietf.org/doc/html/rfc7519#section-4. They do not follow the
2024-02-18 10:42:21 +00:00
// specification exactly, since they were based on an earlier draft of the
2024-02-18 10:42:21 +00:00
// specification and not updated. The main difference is that they only
2024-02-18 10:42:21 +00:00
// support integer-based date fields and singular audiences. This might lead to
2024-02-18 10:42:21 +00:00
// incompatibilities with other JWT implementations. The use of this is discouraged, instead
2024-02-18 10:42:21 +00:00
// the newer RegisteredClaims struct should be used.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Deprecated: Use RegisteredClaims instead for a forward-compatible way to access registered claims in a struct.
2024-02-18 10:42:21 +00:00
type StandardClaims struct {
Audience string `json:"aud,omitempty"`
ExpiresAt int64 `json:"exp,omitempty"`
Id string `json:"jti,omitempty"`
IssuedAt int64 `json:"iat,omitempty"`
Issuer string `json:"iss,omitempty"`
NotBefore int64 `json:"nbf,omitempty"`
Subject string `json:"sub,omitempty"`
2024-02-18 10:42:21 +00:00
}
// Valid validates time based claims "exp, iat, nbf". There is no accounting for clock skew.
2024-02-18 10:42:21 +00:00
// As well, if any of the above claims are not in the token, it will still
2024-02-18 10:42:21 +00:00
// be considered a valid claim.
2024-02-18 10:42:21 +00:00
func (c StandardClaims) Valid() error {
2024-02-18 10:42:21 +00:00
vErr := new(ValidationError)
2024-02-18 10:42:21 +00:00
now := TimeFunc().Unix()
// The claims below are optional, by default, so if they are set to the
2024-02-18 10:42:21 +00:00
// default value in Go, let's not fail the verification for them.
2024-02-18 10:42:21 +00:00
if !c.VerifyExpiresAt(now, false) {
2024-02-18 10:42:21 +00:00
delta := time.Unix(now, 0).Sub(time.Unix(c.ExpiresAt, 0))
2024-02-18 10:42:21 +00:00
vErr.Inner = fmt.Errorf("%s by %s", ErrTokenExpired, delta)
2024-02-18 10:42:21 +00:00
vErr.Errors |= ValidationErrorExpired
2024-02-18 10:42:21 +00:00
}
if !c.VerifyIssuedAt(now, false) {
2024-02-18 10:42:21 +00:00
vErr.Inner = ErrTokenUsedBeforeIssued
2024-02-18 10:42:21 +00:00
vErr.Errors |= ValidationErrorIssuedAt
2024-02-18 10:42:21 +00:00
}
if !c.VerifyNotBefore(now, false) {
2024-02-18 10:42:21 +00:00
vErr.Inner = ErrTokenNotValidYet
2024-02-18 10:42:21 +00:00
vErr.Errors |= ValidationErrorNotValidYet
2024-02-18 10:42:21 +00:00
}
if vErr.valid() {
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
return vErr
2024-02-18 10:42:21 +00:00
}
// VerifyAudience compares the aud claim against cmp.
2024-02-18 10:42:21 +00:00
// If required is false, this method will return true if the value matches or is unset
2024-02-18 10:42:21 +00:00
func (c *StandardClaims) VerifyAudience(cmp string, req bool) bool {
2024-02-18 10:42:21 +00:00
return verifyAud([]string{c.Audience}, cmp, req)
2024-02-18 10:42:21 +00:00
}
// VerifyExpiresAt compares the exp claim against cmp (cmp < exp).
2024-02-18 10:42:21 +00:00
// If req is false, it will return true, if exp is unset.
2024-02-18 10:42:21 +00:00
func (c *StandardClaims) VerifyExpiresAt(cmp int64, req bool) bool {
2024-02-18 10:42:21 +00:00
if c.ExpiresAt == 0 {
2024-02-18 10:42:21 +00:00
return verifyExp(nil, time.Unix(cmp, 0), req)
2024-02-18 10:42:21 +00:00
}
t := time.Unix(c.ExpiresAt, 0)
2024-02-18 10:42:21 +00:00
return verifyExp(&t, time.Unix(cmp, 0), req)
2024-02-18 10:42:21 +00:00
}
// VerifyIssuedAt compares the iat claim against cmp (cmp >= iat).
2024-02-18 10:42:21 +00:00
// If req is false, it will return true, if iat is unset.
2024-02-18 10:42:21 +00:00
func (c *StandardClaims) VerifyIssuedAt(cmp int64, req bool) bool {
2024-02-18 10:42:21 +00:00
if c.IssuedAt == 0 {
2024-02-18 10:42:21 +00:00
return verifyIat(nil, time.Unix(cmp, 0), req)
2024-02-18 10:42:21 +00:00
}
t := time.Unix(c.IssuedAt, 0)
2024-02-18 10:42:21 +00:00
return verifyIat(&t, time.Unix(cmp, 0), req)
2024-02-18 10:42:21 +00:00
}
// VerifyNotBefore compares the nbf claim against cmp (cmp >= nbf).
2024-02-18 10:42:21 +00:00
// If req is false, it will return true, if nbf is unset.
2024-02-18 10:42:21 +00:00
func (c *StandardClaims) VerifyNotBefore(cmp int64, req bool) bool {
2024-02-18 10:42:21 +00:00
if c.NotBefore == 0 {
2024-02-18 10:42:21 +00:00
return verifyNbf(nil, time.Unix(cmp, 0), req)
2024-02-18 10:42:21 +00:00
}
t := time.Unix(c.NotBefore, 0)
2024-02-18 10:42:21 +00:00
return verifyNbf(&t, time.Unix(cmp, 0), req)
2024-02-18 10:42:21 +00:00
}
// VerifyIssuer compares the iss claim against cmp.
2024-02-18 10:42:21 +00:00
// If required is false, this method will return true if the value matches or is unset
2024-02-18 10:42:21 +00:00
func (c *StandardClaims) VerifyIssuer(cmp string, req bool) bool {
2024-02-18 10:42:21 +00:00
return verifyIss(c.Issuer, cmp, req)
2024-02-18 10:42:21 +00:00
}
// ----- helpers
func verifyAud(aud []string, cmp string, required bool) bool {
2024-02-18 10:42:21 +00:00
if len(aud) == 0 {
2024-02-18 10:42:21 +00:00
return !required
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// use a var here to keep constant time compare when looping over a number of claims
2024-02-18 10:42:21 +00:00
result := false
var stringClaims string
2024-02-18 10:42:21 +00:00
for _, a := range aud {
2024-02-18 10:42:21 +00:00
if subtle.ConstantTimeCompare([]byte(a), []byte(cmp)) != 0 {
2024-02-18 10:42:21 +00:00
result = true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
stringClaims = stringClaims + a
2024-02-18 10:42:21 +00:00
}
// case where "" is sent in one or many aud claims
2024-02-18 10:42:21 +00:00
if len(stringClaims) == 0 {
2024-02-18 10:42:21 +00:00
return !required
2024-02-18 10:42:21 +00:00
}
return result
2024-02-18 10:42:21 +00:00
}
func verifyExp(exp *time.Time, now time.Time, required bool) bool {
2024-02-18 10:42:21 +00:00
if exp == nil {
2024-02-18 10:42:21 +00:00
return !required
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return now.Before(*exp)
2024-02-18 10:42:21 +00:00
}
func verifyIat(iat *time.Time, now time.Time, required bool) bool {
2024-02-18 10:42:21 +00:00
if iat == nil {
2024-02-18 10:42:21 +00:00
return !required
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return now.After(*iat) || now.Equal(*iat)
2024-02-18 10:42:21 +00:00
}
func verifyNbf(nbf *time.Time, now time.Time, required bool) bool {
2024-02-18 10:42:21 +00:00
if nbf == nil {
2024-02-18 10:42:21 +00:00
return !required
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return now.After(*nbf) || now.Equal(*nbf)
2024-02-18 10:42:21 +00:00
}
func verifyIss(iss string, cmp string, required bool) bool {
2024-02-18 10:42:21 +00:00
if iss == "" {
2024-02-18 10:42:21 +00:00
return !required
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return subtle.ConstantTimeCompare([]byte(iss), []byte(cmp)) != 0
2024-02-18 10:42:21 +00:00
}