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

240 lines
5.8 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
package jwt
import (
"encoding/base64"
"encoding/json"
"strings"
"time"
)
// DecodePaddingAllowed will switch the codec used for decoding JWTs respectively. Note that the JWS RFC7515
2024-02-18 10:42:21 +00:00
// states that the tokens will utilize a Base64url encoding with no padding. Unfortunately, some implementations
2024-02-18 10:42:21 +00:00
// of JWT are producing non-standard tokens, and thus require support for decoding. Note that this is a global
2024-02-18 10:42:21 +00:00
// variable, and updating it will change the behavior on a package level, and is also NOT go-routine safe.
2024-02-18 10:42:21 +00:00
// To use the non-recommended decoding, set this boolean to `true` prior to using this package.
2024-02-18 10:42:21 +00:00
var DecodePaddingAllowed bool
// DecodeStrict will switch the codec used for decoding JWTs into strict mode.
2024-02-18 10:42:21 +00:00
// In this mode, the decoder requires that trailing padding bits are zero, as described in RFC 4648 section 3.5.
2024-02-18 10:42:21 +00:00
// Note that this is a global variable, and updating it will change the behavior on a package level, and is also NOT go-routine safe.
2024-02-18 10:42:21 +00:00
// To use strict decoding, set this boolean to `true` prior to using this package.
2024-02-18 10:42:21 +00:00
var DecodeStrict bool
// TimeFunc provides the current time when parsing token to validate "exp" claim (expiration time).
2024-02-18 10:42:21 +00:00
// You can override it to use another time value. This is useful for testing or if your
2024-02-18 10:42:21 +00:00
// server uses a different time zone than your tokens.
2024-02-18 10:42:21 +00:00
var TimeFunc = time.Now
// Keyfunc will be used by the Parse methods as a callback function to supply
2024-02-18 10:42:21 +00:00
// the key for verification. The function receives the parsed,
2024-02-18 10:42:21 +00:00
// but unverified Token. This allows you to use properties in the
2024-02-18 10:42:21 +00:00
// Header of the token (such as `kid`) to identify which key to use.
2024-02-18 10:42:21 +00:00
type Keyfunc func(*Token) (interface{}, error)
// Token represents a JWT Token. Different fields will be used depending on whether you're
2024-02-18 10:42:21 +00:00
// creating or parsing/verifying a token.
2024-02-18 10:42:21 +00:00
type Token struct {
Raw string // The raw token. Populated when you Parse a token
Method SigningMethod // The signing method used or to be used
Header map[string]interface{} // The first segment of the token
Claims Claims // The second segment of the token
Signature string // The third segment of the token. Populated when you Parse a token
Valid bool // Is the token valid? Populated when you Parse/Verify a token
2024-02-18 10:42:21 +00:00
}
// New creates a new Token with the specified signing method and an empty map of claims.
2024-02-18 10:42:21 +00:00
func New(method SigningMethod) *Token {
2024-02-18 10:42:21 +00:00
return NewWithClaims(method, MapClaims{})
2024-02-18 10:42:21 +00:00
}
// NewWithClaims creates a new Token with the specified signing method and claims.
2024-02-18 10:42:21 +00:00
func NewWithClaims(method SigningMethod, claims Claims) *Token {
2024-02-18 10:42:21 +00:00
return &Token{
2024-02-18 10:42:21 +00:00
Header: map[string]interface{}{
2024-02-18 10:42:21 +00:00
"typ": "JWT",
2024-02-18 10:42:21 +00:00
"alg": method.Alg(),
},
2024-02-18 10:42:21 +00:00
Claims: claims,
2024-02-18 10:42:21 +00:00
Method: method,
}
2024-02-18 10:42:21 +00:00
}
// SignedString creates and returns a complete, signed JWT.
2024-02-18 10:42:21 +00:00
// The token is signed using the SigningMethod specified in the token.
2024-02-18 10:42:21 +00:00
func (t *Token) SignedString(key interface{}) (string, error) {
2024-02-18 10:42:21 +00:00
var sig, sstr string
2024-02-18 10:42:21 +00:00
var err error
2024-02-18 10:42:21 +00:00
if sstr, err = t.SigningString(); err != nil {
2024-02-18 10:42:21 +00:00
return "", err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if sig, err = t.Method.Sign(sstr, key); err != nil {
2024-02-18 10:42:21 +00:00
return "", err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return strings.Join([]string{sstr, sig}, "."), nil
2024-02-18 10:42:21 +00:00
}
// SigningString generates the signing string. This is the
2024-02-18 10:42:21 +00:00
// most expensive part of the whole deal. Unless you
2024-02-18 10:42:21 +00:00
// need this for something special, just go straight for
2024-02-18 10:42:21 +00:00
// the SignedString.
2024-02-18 10:42:21 +00:00
func (t *Token) SigningString() (string, error) {
2024-02-18 10:42:21 +00:00
var err error
2024-02-18 10:42:21 +00:00
var jsonValue []byte
if jsonValue, err = json.Marshal(t.Header); err != nil {
2024-02-18 10:42:21 +00:00
return "", err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
header := EncodeSegment(jsonValue)
if jsonValue, err = json.Marshal(t.Claims); err != nil {
2024-02-18 10:42:21 +00:00
return "", err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
claim := EncodeSegment(jsonValue)
return strings.Join([]string{header, claim}, "."), nil
2024-02-18 10:42:21 +00:00
}
// Parse parses, validates, verifies the signature and returns the parsed token.
2024-02-18 10:42:21 +00:00
// keyFunc will receive the parsed token and should return the cryptographic key
2024-02-18 10:42:21 +00:00
// for verifying the signature.
2024-02-18 10:42:21 +00:00
// The caller is strongly encouraged to set the WithValidMethods option to
2024-02-18 10:42:21 +00:00
// validate the 'alg' claim in the token matches the expected algorithm.
2024-02-18 10:42:21 +00:00
// For more details about the importance of validating the 'alg' claim,
2024-02-18 10:42:21 +00:00
// see https://auth0.com/blog/critical-vulnerabilities-in-json-web-token-libraries/
2024-02-18 10:42:21 +00:00
func Parse(tokenString string, keyFunc Keyfunc, options ...ParserOption) (*Token, error) {
2024-02-18 10:42:21 +00:00
return NewParser(options...).Parse(tokenString, keyFunc)
2024-02-18 10:42:21 +00:00
}
// ParseWithClaims is a shortcut for NewParser().ParseWithClaims().
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Note: If you provide a custom claim implementation that embeds one of the standard claims (such as RegisteredClaims),
2024-02-18 10:42:21 +00:00
// make sure that a) you either embed a non-pointer version of the claims or b) if you are using a pointer, allocate the
2024-02-18 10:42:21 +00:00
// proper memory for it before passing in the overall claims, otherwise you might run into a panic.
2024-02-18 10:42:21 +00:00
func ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc, options ...ParserOption) (*Token, error) {
2024-02-18 10:42:21 +00:00
return NewParser(options...).ParseWithClaims(tokenString, claims, keyFunc)
2024-02-18 10:42:21 +00:00
}
// EncodeSegment encodes a JWT specific base64url encoding with padding stripped
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Deprecated: In a future release, we will demote this function to a non-exported function, since it
2024-02-18 10:42:21 +00:00
// should only be used internally
2024-02-18 10:42:21 +00:00
func EncodeSegment(seg []byte) string {
2024-02-18 10:42:21 +00:00
return base64.RawURLEncoding.EncodeToString(seg)
2024-02-18 10:42:21 +00:00
}
// DecodeSegment decodes a JWT specific base64url encoding with padding stripped
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Deprecated: In a future release, we will demote this function to a non-exported function, since it
2024-02-18 10:42:21 +00:00
// should only be used internally
2024-02-18 10:42:21 +00:00
func DecodeSegment(seg string) ([]byte, error) {
2024-02-18 10:42:21 +00:00
encoding := base64.RawURLEncoding
if DecodePaddingAllowed {
2024-02-18 10:42:21 +00:00
if l := len(seg) % 4; l > 0 {
2024-02-18 10:42:21 +00:00
seg += strings.Repeat("=", 4-l)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
encoding = base64.URLEncoding
2024-02-18 10:42:21 +00:00
}
if DecodeStrict {
2024-02-18 10:42:21 +00:00
encoding = encoding.Strict()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return encoding.DecodeString(seg)
2024-02-18 10:42:21 +00:00
}