2012-04-18 03:49:21 +04:00
|
|
|
package jwt
|
|
|
|
|
|
|
|
import (
|
2023-09-13 04:29:27 +03:00
|
|
|
"crypto"
|
2012-04-18 03:49:21 +04:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
)
|
|
|
|
|
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.
|
2023-09-13 04:29:27 +03:00
|
|
|
//
|
2023-09-13 16:39:21 +03:00
|
|
|
// The returned interface{} may be a single key or a VerificationKeySet containing
|
2023-09-13 04:29:27 +03:00
|
|
|
// multiple keys.
|
2014-08-04 22:26:53 +04:00
|
|
|
type Keyfunc func(*Token) (interface{}, error)
|
2012-07-07 03:16:34 +04:00
|
|
|
|
2023-09-13 04:29:27 +03:00
|
|
|
// VerificationKey represents a public or secret key for verifying a token's signature.
|
|
|
|
type VerificationKey interface {
|
|
|
|
crypto.PublicKey | []uint8
|
|
|
|
}
|
|
|
|
|
|
|
|
// VerificationKeySet is a set of public or secret keys. It is used by the parser to verify a token.
|
|
|
|
type VerificationKeySet struct {
|
|
|
|
Keys []VerificationKey
|
|
|
|
}
|
|
|
|
|
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-04-02 18:54:36 +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 in decoded form
|
|
|
|
Claims Claims // Claims is the second segment of the token in decoded form
|
|
|
|
Signature []byte // Signature is the third segment of the token in decoded form. Populated when you Parse a token
|
|
|
|
Valid bool // Valid specifies if the token is valid. Populated when you Parse/Verify a token
|
|
|
|
jsonEncoder JSONMarshalFunc // jsonEncoder is the custom json encoder/decoder
|
|
|
|
base64Encoder Base64EncodeFunc // base64Encoder is the custom base64 encoder/decoder
|
2012-04-18 03:49:21 +04:00
|
|
|
}
|
|
|
|
|
2023-03-24 21:13:09 +03:00
|
|
|
// New creates a new [Token] with the specified signing method and an empty map
|
|
|
|
// of claims. Additional options can be specified, but are currently unused.
|
|
|
|
func New(method SigningMethod, opts ...TokenOption) *Token {
|
|
|
|
return NewWithClaims(method, MapClaims{}, opts...)
|
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
|
2023-03-24 21:13:09 +03:00
|
|
|
// claims. Additional options can be specified, but are currently unused.
|
|
|
|
func NewWithClaims(method SigningMethod, claims Claims, opts ...TokenOption) *Token {
|
2023-04-02 18:54:36 +03:00
|
|
|
t := &Token{
|
2012-07-07 02:43:17 +04:00
|
|
|
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-04-02 18:54:36 +03:00
|
|
|
for _, opt := range opts {
|
|
|
|
opt(t)
|
|
|
|
}
|
|
|
|
return t
|
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
|
2023-03-31 14:19:48 +03:00
|
|
|
// using the SigningMethod specified in the token. Please refer to
|
|
|
|
// https://golang-jwt.github.io/jwt/usage/signing_methods/#signing-methods-and-key-types
|
|
|
|
// for an overview of the different signing methods and their respective key
|
|
|
|
// types.
|
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
|
|
|
|
2023-03-24 21:13:09 +03:00
|
|
|
return sstr + "." + t.EncodeSegment(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-04-02 18:54:36 +03:00
|
|
|
var marshal JSONMarshalFunc
|
|
|
|
if t.jsonEncoder != nil {
|
|
|
|
marshal = t.jsonEncoder
|
|
|
|
} else {
|
|
|
|
marshal = json.Marshal
|
|
|
|
}
|
|
|
|
|
|
|
|
h, err := marshal(t.Header)
|
2023-02-22 05:28:00 +03:00
|
|
|
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-04-02 18:54:36 +03:00
|
|
|
c, err := marshal(t.Claims)
|
2023-02-22 05:28:00 +03:00
|
|
|
if err != nil {
|
2022-02-03 14:47:58 +03:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-03-24 21:13:09 +03:00
|
|
|
return t.EncodeSegment(h) + "." + t.EncodeSegment(c), nil
|
2014-03-08 02:43:11 +04:00
|
|
|
}
|
|
|
|
|
2023-03-24 21:13:09 +03:00
|
|
|
// EncodeSegment encodes a JWT specific base64url encoding with padding
|
|
|
|
// stripped. In the future, this function might take into account a
|
|
|
|
// [TokenOption]. Therefore, this function exists as a method of [Token], rather
|
|
|
|
// than a global function.
|
2023-04-02 18:54:36 +03:00
|
|
|
func (t *Token) EncodeSegment(seg []byte) string {
|
|
|
|
var enc Base64EncodeFunc
|
|
|
|
if t.base64Encoder != nil {
|
|
|
|
enc = t.base64Encoder
|
|
|
|
} else {
|
|
|
|
enc = base64.RawURLEncoding.EncodeToString
|
|
|
|
}
|
|
|
|
|
|
|
|
return enc(seg)
|
2012-07-07 02:43:17 +04:00
|
|
|
}
|