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
|
|
|
)
|
|
|
|
|
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
|
|
|
|
|
2022-12-09 20:04:03 +03:00
|
|
|
// DecodeStrict will switch the codec used for decoding JWTs into strict mode.
|
|
|
|
// 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.
|
|
|
|
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
|
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
|
2022-02-03 14:47:58 +03:00
|
|
|
var jsonValue []byte
|
2012-07-07 04:02:20 +04:00
|
|
|
|
2022-02-03 14:47:58 +03:00
|
|
|
if jsonValue, err = json.Marshal(t.Header); err != nil {
|
|
|
|
return "", err
|
2012-07-07 03:07:55 +04:00
|
|
|
}
|
2022-02-03 14:47:58 +03:00
|
|
|
header := EncodeSegment(jsonValue)
|
|
|
|
|
|
|
|
if jsonValue, err = json.Marshal(t.Claims); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
claim := EncodeSegment(jsonValue)
|
|
|
|
|
|
|
|
return strings.Join([]string{header, claim}, "."), 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
|
|
|
}
|
|
|
|
|
2022-11-08 17:43:45 +03:00
|
|
|
// ParseWithClaims is a shortcut for NewParser().ParseWithClaims().
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
// 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) {
|
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
|
|
|
}
|