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

174 lines
3.2 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
package jwt
import (
"encoding/base64"
"encoding/json"
"strings"
"time"
)
// 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
// Parse methods use this 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)
// 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
}
// Create a new Token. Takes a signing method
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
}
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
}
// Get the complete, signed 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
}
// Generate 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
parts := make([]string, 2)
2024-02-18 10:42:21 +00:00
for i := range parts {
2024-02-18 10:42:21 +00:00
var jsonValue []byte
2024-02-18 10:42:21 +00:00
if i == 0 {
2024-02-18 10:42:21 +00:00
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
} else {
2024-02-18 10:42:21 +00:00
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
}
parts[i] = EncodeSegment(jsonValue)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return strings.Join(parts, "."), nil
2024-02-18 10:42:21 +00:00
}
// Parse, validate, and return a token.
2024-02-18 10:42:21 +00:00
// keyFunc will receive the parsed token and should return the key for validating.
2024-02-18 10:42:21 +00:00
// If everything is kosher, err will be nil
2024-02-18 10:42:21 +00:00
func Parse(tokenString string, keyFunc Keyfunc) (*Token, error) {
2024-02-18 10:42:21 +00:00
return new(Parser).Parse(tokenString, keyFunc)
2024-02-18 10:42:21 +00:00
}
func ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc) (*Token, error) {
2024-02-18 10:42:21 +00:00
return new(Parser).ParseWithClaims(tokenString, claims, keyFunc)
2024-02-18 10:42:21 +00:00
}
// Encode JWT specific base64url encoding with padding stripped
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
}
// Decode JWT specific base64url encoding with padding stripped
2024-02-18 10:42:21 +00:00
func DecodeSegment(seg string) ([]byte, error) {
2024-02-18 10:42:21 +00:00
return base64.RawURLEncoding.DecodeString(seg)
2024-02-18 10:42:21 +00:00
}