2012-04-18 03:49:21 +04:00
|
|
|
package jwt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
2012-04-18 23:59:37 +04:00
|
|
|
"strings"
|
|
|
|
"time"
|
2012-04-18 03:49:21 +04:00
|
|
|
)
|
|
|
|
|
2021-11-06 14:21:20 +03:00
|
|
|
|
|
|
|
// DecodePaddingAllowed will switch the codec used for decoding JWTs respectively. Note that the JWS RFC7515
|
|
|
|
// states that the tokens will utilize a Base64url encoding with no padding. Unfortunately, some implementations
|
|
|
|
// of JWT are producing non-standard tokens, and thus require support for decoding. 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.
|
|
|
|
// To use the non-recommended decoding, set this boolean to `true` prior to using this package.
|
|
|
|
var DecodePaddingAllowed bool
|
|
|
|
|
2014-02-11 05:27:59 +04:00
|
|
|
// TimeFunc provides the current time when parsing token to validate "exp" claim (expiration time).
|
|
|
|
// You can override it to use another time value. This is useful for testing or if your
|
|
|
|
// server uses a different time zone than your tokens.
|
2014-02-06 03:07:40 +04:00
|
|
|
var TimeFunc = time.Now
|
|
|
|
|
2021-08-03 16:51:01 +03:00
|
|
|
// Keyfunc will be used by the Parse methods as a callback function to supply
|
2012-07-07 03:16:34 +04:00
|
|
|
// the key for verification. The function receives the parsed,
|
2016-05-27 16:22:32 +03:00
|
|
|
// but unverified Token. This allows you to use properties in the
|
2012-07-07 03:16:34 +04:00
|
|
|
// Header of the token (such as `kid`) to identify which key to use.
|
2014-08-04 22:26:53 +04:00
|
|
|
type Keyfunc func(*Token) (interface{}, error)
|
2012-07-07 03:16:34 +04:00
|
|
|
|
2021-08-03 16:51:01 +03:00
|
|
|
// Token represents a JWT Token. Different fields will be used depending on whether you're
|
2014-06-16 06:40:38 +04:00
|
|
|
// creating or parsing/verifying a token.
|
2012-04-18 03:49:21 +04:00
|
|
|
type Token struct {
|
2014-06-16 06:39:12 +04:00
|
|
|
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
|
2015-07-17 21:59:18 +03:00
|
|
|
Claims Claims // The second segment of the token
|
2014-06-16 06:39:12 +04:00
|
|
|
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
|
2012-04-18 03:49:21 +04:00
|
|
|
}
|
|
|
|
|
2021-10-15 15:48:31 +03:00
|
|
|
// New creates a new Token with the specified signing method and an empty map of claims.
|
2012-07-07 04:02:20 +04:00
|
|
|
func New(method SigningMethod) *Token {
|
2015-08-18 20:18:57 +03:00
|
|
|
return NewWithClaims(method, MapClaims{})
|
2012-07-07 02:43:17 +04:00
|
|
|
}
|
|
|
|
|
2021-10-15 15:48:31 +03:00
|
|
|
// NewWithClaims creates a new Token with the specified signing method and claims.
|
2015-07-17 21:59:18 +03:00
|
|
|
func NewWithClaims(method SigningMethod, claims Claims) *Token {
|
2012-07-07 02:43:17 +04:00
|
|
|
return &Token{
|
|
|
|
Header: map[string]interface{}{
|
|
|
|
"typ": "JWT",
|
|
|
|
"alg": method.Alg(),
|
|
|
|
},
|
2015-07-14 20:34:09 +03:00
|
|
|
Claims: claims,
|
2012-07-07 23:12:49 +04:00
|
|
|
Method: method,
|
2012-07-07 02:43:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-15 15:48:31 +03:00
|
|
|
// SignedString creates and returns a complete, signed JWT.
|
|
|
|
// The token is signed using the SigningMethod specified in the token.
|
2014-09-29 22:00:25 +04:00
|
|
|
func (t *Token) SignedString(key interface{}) (string, error) {
|
2012-07-07 03:07:55 +04:00
|
|
|
var sig, sstr string
|
|
|
|
var err error
|
|
|
|
if sstr, err = t.SigningString(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if sig, err = t.Method.Sign(sstr, key); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return strings.Join([]string{sstr, sig}, "."), nil
|
2012-07-07 02:43:17 +04:00
|
|
|
}
|
|
|
|
|
2021-08-03 16:51:01 +03:00
|
|
|
// SigningString generates the signing string. This is the
|
2012-07-07 03:12:33 +04:00
|
|
|
// most expensive part of the whole deal. Unless you
|
|
|
|
// need this for something special, just go straight for
|
|
|
|
// the SignedString.
|
2012-07-07 04:02:20 +04:00
|
|
|
func (t *Token) SigningString() (string, error) {
|
2012-07-07 03:07:55 +04:00
|
|
|
var err error
|
|
|
|
parts := make([]string, 2)
|
2021-05-28 02:26:21 +03:00
|
|
|
for i := range parts {
|
2015-07-13 22:14:12 +03:00
|
|
|
var jsonValue []byte
|
2012-07-07 03:07:55 +04:00
|
|
|
if i == 0 {
|
2015-07-13 22:14:12 +03:00
|
|
|
if jsonValue, err = json.Marshal(t.Header); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2012-07-07 03:07:55 +04:00
|
|
|
} else {
|
2015-07-13 22:14:12 +03:00
|
|
|
if jsonValue, err = json.Marshal(t.Claims); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2012-07-07 03:07:55 +04:00
|
|
|
}
|
2012-07-07 04:02:20 +04:00
|
|
|
|
2012-07-07 03:07:55 +04:00
|
|
|
parts[i] = EncodeSegment(jsonValue)
|
|
|
|
}
|
|
|
|
return strings.Join(parts, "."), nil
|
2012-07-07 02:43:17 +04:00
|
|
|
}
|
|
|
|
|
2021-10-15 15:48:31 +03:00
|
|
|
// Parse parses, validates, verifies the signature and returns the parsed token.
|
|
|
|
// keyFunc will receive the parsed token and should return the cryptographic key
|
|
|
|
// for verifying the signature.
|
|
|
|
// The caller is strongly encouraged to set the WithValidMethods option to
|
|
|
|
// validate the 'alg' claim in the token matches the expected algorithm.
|
|
|
|
// For more details about the importance of validating the 'alg' claim,
|
|
|
|
// see https://auth0.com/blog/critical-vulnerabilities-in-json-web-token-libraries/
|
2021-10-13 20:36:33 +03:00
|
|
|
func Parse(tokenString string, keyFunc Keyfunc, options ...ParserOption) (*Token, error) {
|
|
|
|
return NewParser(options...).Parse(tokenString, keyFunc)
|
2014-03-08 02:43:11 +04:00
|
|
|
}
|
|
|
|
|
2021-10-13 20:36:33 +03:00
|
|
|
func ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc, options ...ParserOption) (*Token, error) {
|
|
|
|
return NewParser(options...).ParseWithClaims(tokenString, claims, keyFunc)
|
2014-03-08 02:43:11 +04:00
|
|
|
}
|
|
|
|
|
2021-08-03 16:51:01 +03:00
|
|
|
// EncodeSegment encodes a JWT specific base64url encoding with padding stripped
|
|
|
|
//
|
|
|
|
// Deprecated: In a future release, we will demote this function to a non-exported function, since it
|
|
|
|
// should only be used internally
|
2012-07-07 04:02:20 +04:00
|
|
|
func EncodeSegment(seg []byte) string {
|
2021-07-13 09:31:42 +03:00
|
|
|
return base64.RawURLEncoding.EncodeToString(seg)
|
2012-07-07 02:43:17 +04:00
|
|
|
}
|
|
|
|
|
2021-08-03 16:51:01 +03:00
|
|
|
// DecodeSegment decodes a JWT specific base64url encoding with padding stripped
|
|
|
|
//
|
|
|
|
// Deprecated: In a future release, we will demote this function to a non-exported function, since it
|
|
|
|
// should only be used internally
|
2012-04-18 23:59:37 +04:00
|
|
|
func DecodeSegment(seg string) ([]byte, error) {
|
2021-11-06 14:21:20 +03:00
|
|
|
if DecodePaddingAllowed {
|
|
|
|
if l := len(seg) % 4; l > 0 {
|
|
|
|
seg += strings.Repeat("=", 4-l)
|
|
|
|
}
|
|
|
|
return base64.URLEncoding.DecodeString(seg)
|
|
|
|
}
|
|
|
|
|
2021-07-13 09:31:42 +03:00
|
|
|
return base64.RawURLEncoding.DecodeString(seg)
|
2012-04-18 23:59:37 +04:00
|
|
|
}
|