2012-04-18 03:49:21 +04:00
|
|
|
package jwt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
2012-04-18 23:59:37 +04:00
|
|
|
"strings"
|
2012-04-18 03:49:21 +04:00
|
|
|
)
|
|
|
|
|
2023-02-21 16:32:25 +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.
|
2021-11-06 14:21:20 +03:00
|
|
|
var DecodePaddingAllowed bool
|
|
|
|
|
2022-12-09 20:04:03 +03:00
|
|
|
// DecodeStrict will switch the codec used for decoding JWTs into strict mode.
|
2023-02-21 16:32:25 +03:00
|
|
|
// In this mode, the decoder requires that trailing padding bits are zero, as
|
|
|
|
// described in RFC 4648 section 3.5. 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 strict decoding, set this boolean to `true` prior to
|
|
|
|
// using this package.
|
2022-12-09 20:04:03 +03:00
|
|
|
var DecodeStrict bool
|
|
|
|
|
2021-08-03 16:51:01 +03:00
|
|
|
// Keyfunc will be used by the Parse methods as a callback function to supply
|
2023-02-21 16:32:25 +03:00
|
|
|
// the key for verification. The function receives the parsed, but unverified
|
|
|
|
// Token. This allows you to use properties in the 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
|
|
|
|
2023-02-21 16:32:25 +03:00
|
|
|
// Token represents a JWT Token. Different fields will be used depending on
|
|
|
|
// whether you're creating or parsing/verifying a token.
|
2012-04-18 03:49:21 +04:00
|
|
|
type Token struct {
|
2023-02-21 16:32:25 +03:00
|
|
|
Raw string // Raw contains the raw token. Populated when you [Parse] a token
|
|
|
|
Method SigningMethod // Method is the signing method used or to be used
|
|
|
|
Header map[string]interface{} // Header is the first segment of the token
|
|
|
|
Claims Claims // Claims is the second segment of the token
|
|
|
|
Signature string // Signature is the third segment of the token. Populated when you Parse a token
|
|
|
|
Valid bool // Valid specifies if the token is valid. Populated when you Parse/Verify a token
|
2012-04-18 03:49:21 +04:00
|
|
|
}
|
|
|
|
|
2023-02-21 16:32:25 +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
|
|
|
}
|
|
|
|
|
2023-02-21 16:32:25 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-21 16:32:25 +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) {
|
2023-02-22 05:28:00 +03:00
|
|
|
sstr, err := t.SigningString()
|
|
|
|
if err != nil {
|
2012-07-07 03:07:55 +04:00
|
|
|
return "", err
|
|
|
|
}
|
2023-02-22 05:28:00 +03:00
|
|
|
|
|
|
|
sig, err := t.Method.Sign(sstr, key)
|
|
|
|
if err != nil {
|
2012-07-07 03:07:55 +04:00
|
|
|
return "", err
|
|
|
|
}
|
2023-02-22 05:28:00 +03:00
|
|
|
|
|
|
|
return sstr + "." + sig, nil
|
2012-07-07 02:43:17 +04:00
|
|
|
}
|
|
|
|
|
2023-02-21 16:32:25 +03:00
|
|
|
// SigningString generates the signing string. This is the 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) {
|
2023-02-22 05:28:00 +03:00
|
|
|
h, err := json.Marshal(t.Header)
|
|
|
|
if err != nil {
|
2022-02-03 14:47:58 +03:00
|
|
|
return "", err
|
2012-07-07 03:07:55 +04:00
|
|
|
}
|
2022-02-03 14:47:58 +03:00
|
|
|
|
2023-02-22 05:28:00 +03:00
|
|
|
c, err := json.Marshal(t.Claims)
|
|
|
|
if err != nil {
|
2022-02-03 14:47:58 +03:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-02-22 05:28:00 +03:00
|
|
|
return EncodeSegment(h) + "." + EncodeSegment(c), 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
|
2023-02-21 16:32:25 +03:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2022-11-08 17:43:45 +03:00
|
|
|
// ParseWithClaims is a shortcut for NewParser().ParseWithClaims().
|
|
|
|
//
|
2023-02-21 16:32:25 +03:00
|
|
|
// Note: If you provide a custom claim implementation that embeds one of the
|
|
|
|
// standard claims (such as RegisteredClaims), make sure that a) you either
|
|
|
|
// embed a non-pointer version of the claims or b) if you are using a pointer,
|
|
|
|
// allocate the proper memory for it before passing in the overall claims,
|
|
|
|
// otherwise you might run into a panic.
|
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
|
|
|
|
//
|
2023-02-21 16:32:25 +03:00
|
|
|
// 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
|
|
|
|
//
|
2023-02-21 16:32:25 +03:00
|
|
|
// 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) {
|
2022-12-09 20:04:03 +03:00
|
|
|
encoding := base64.RawURLEncoding
|
|
|
|
|
2021-11-06 14:21:20 +03:00
|
|
|
if DecodePaddingAllowed {
|
|
|
|
if l := len(seg) % 4; l > 0 {
|
|
|
|
seg += strings.Repeat("=", 4-l)
|
|
|
|
}
|
2022-12-09 20:04:03 +03:00
|
|
|
encoding = base64.URLEncoding
|
2021-11-06 14:21:20 +03:00
|
|
|
}
|
|
|
|
|
2022-12-09 20:04:03 +03:00
|
|
|
if DecodeStrict {
|
|
|
|
encoding = encoding.Strict()
|
|
|
|
}
|
|
|
|
return encoding.DecodeString(seg)
|
2012-04-18 23:59:37 +04:00
|
|
|
}
|