2012-04-18 03:49:21 +04:00
|
|
|
package jwt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
2012-04-18 23:59:37 +04:00
|
|
|
"errors"
|
2012-04-18 23:18:31 +04:00
|
|
|
"net/http"
|
2012-04-18 23:59:37 +04:00
|
|
|
"strings"
|
|
|
|
"time"
|
2012-04-18 03:49:21 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
// A JWT Token
|
|
|
|
type Token struct {
|
|
|
|
Header map[string]interface{}
|
|
|
|
Claims map[string]interface{}
|
|
|
|
Method SigningMethod
|
2012-07-07 03:07:55 +04:00
|
|
|
// This is only populated when you Parse a token
|
2012-04-18 03:49:21 +04:00
|
|
|
Signature string
|
2012-07-07 03:12:33 +04:00
|
|
|
// This is only populated when you Parse/Verify a token
|
2012-04-18 03:49:21 +04:00
|
|
|
Valid bool
|
|
|
|
}
|
|
|
|
|
2012-07-07 02:43:17 +04:00
|
|
|
func New(method SigningMethod)*Token {
|
|
|
|
return &Token{
|
|
|
|
Header: map[string]interface{}{
|
|
|
|
"typ": "JWT",
|
|
|
|
"alg": method.Alg(),
|
|
|
|
},
|
|
|
|
Claims: make(map[string]interface{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-07 03:12:33 +04:00
|
|
|
// Get the complete, signed token
|
2012-07-07 03:07:55 +04:00
|
|
|
func (t *Token) SignedString(key []byte)(string, error) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-07-07 03:12:33 +04:00
|
|
|
// Generate 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 03:07:55 +04:00
|
|
|
func (t *Token) SigningString()(string, error) {
|
|
|
|
var err error
|
|
|
|
parts := make([]string, 2)
|
|
|
|
for i, _ := range parts {
|
|
|
|
var source map[string]interface{}
|
|
|
|
if i == 0 {
|
|
|
|
source = t.Header
|
|
|
|
} else {
|
|
|
|
source = t.Claims
|
|
|
|
}
|
|
|
|
|
|
|
|
var jsonValue []byte
|
|
|
|
if jsonValue, err = json.Marshal(source); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
parts[i] = EncodeSegment(jsonValue)
|
|
|
|
}
|
|
|
|
return strings.Join(parts, "."), nil
|
2012-07-07 02:43:17 +04:00
|
|
|
}
|
|
|
|
|
2012-04-18 10:25:22 +04:00
|
|
|
// Parse, validate, and return a token.
|
|
|
|
// keyFunc will receive the parsed token and should return the key for validating.
|
|
|
|
// If everything is kosher, err will be nil
|
2012-04-18 23:59:37 +04:00
|
|
|
func Parse(tokenString string, keyFunc func(*Token) ([]byte, error)) (token *Token, err error) {
|
2012-04-18 03:49:21 +04:00
|
|
|
parts := strings.Split(tokenString, ".")
|
|
|
|
if len(parts) == 3 {
|
|
|
|
token = new(Token)
|
|
|
|
// parse Header
|
|
|
|
var headerBytes []byte
|
2012-04-18 23:18:31 +04:00
|
|
|
if headerBytes, err = DecodeSegment(parts[0]); err != nil {
|
2012-04-18 03:49:21 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = json.Unmarshal(headerBytes, &token.Header); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2012-04-18 23:59:37 +04:00
|
|
|
|
2012-04-18 03:49:21 +04:00
|
|
|
// parse Claims
|
|
|
|
var claimBytes []byte
|
2012-04-18 23:18:31 +04:00
|
|
|
if claimBytes, err = DecodeSegment(parts[1]); err != nil {
|
2012-04-18 03:49:21 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = json.Unmarshal(claimBytes, &token.Claims); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2012-04-18 23:59:37 +04:00
|
|
|
|
2012-04-18 03:49:21 +04:00
|
|
|
// Lookup signature method
|
|
|
|
if method, ok := token.Header["alg"].(string); ok {
|
|
|
|
if token.Method, err = GetSigningMethod(method); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = errors.New("Signing method (alg) is unspecified.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check expiry times
|
2012-04-18 03:52:38 +04:00
|
|
|
if exp, ok := token.Claims["exp"].(float64); ok {
|
|
|
|
if time.Now().Unix() > int64(exp) {
|
|
|
|
err = errors.New("Token is expired")
|
|
|
|
}
|
|
|
|
}
|
2012-04-18 03:49:21 +04:00
|
|
|
|
|
|
|
// Lookup key
|
2012-04-18 03:58:52 +04:00
|
|
|
var key []byte
|
|
|
|
if key, err = keyFunc(token); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2012-04-18 23:59:37 +04:00
|
|
|
|
2012-04-18 03:58:52 +04:00
|
|
|
// Perform validation
|
2012-04-18 09:50:26 +04:00
|
|
|
if err = token.Method.Verify(strings.Join(parts[0:2], "."), parts[2], key); err == nil {
|
2012-04-18 03:58:52 +04:00
|
|
|
token.Valid = true
|
|
|
|
}
|
2012-04-18 23:59:37 +04:00
|
|
|
|
2012-04-18 03:49:21 +04:00
|
|
|
} else {
|
|
|
|
err = errors.New("Token contains an invalid number of segments")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2012-04-18 23:18:31 +04:00
|
|
|
|
2012-07-07 03:12:33 +04:00
|
|
|
// Try to find the token in an http.Request.
|
|
|
|
// Currently, it only looks in the Authorization header
|
2012-04-18 23:59:37 +04:00
|
|
|
func ParseFromRequest(req *http.Request, keyFunc func(*Token) ([]byte, error)) (token *Token, err error) {
|
2012-04-18 23:18:31 +04:00
|
|
|
|
2012-04-18 23:35:16 +04:00
|
|
|
// Look for an Authorization header
|
|
|
|
if ah := req.Header.Get("Authorization"); ah != "" {
|
|
|
|
// Should be a bearer token
|
|
|
|
if len(ah) > 6 && strings.ToUpper(ah[0:6]) == "BEARER" {
|
|
|
|
return Parse(ah[7:], keyFunc)
|
|
|
|
}
|
|
|
|
}
|
2012-04-18 23:59:37 +04:00
|
|
|
|
2012-04-18 23:35:16 +04:00
|
|
|
return nil, errors.New("No token present in request.")
|
2012-04-18 23:59:37 +04:00
|
|
|
|
2012-04-18 23:18:31 +04:00
|
|
|
}
|
|
|
|
|
2012-07-07 03:12:33 +04:00
|
|
|
// Encode JWT specific base64url encoding with padding stripped
|
2012-07-07 02:43:17 +04:00
|
|
|
func EncodeSegment(seg []byte)string {
|
|
|
|
return strings.TrimRight(base64.URLEncoding.EncodeToString(seg), "=")
|
|
|
|
}
|
|
|
|
|
2012-07-07 03:12:33 +04:00
|
|
|
// Decode JWT specific base64url encoding with padding stripped
|
2012-04-18 23:59:37 +04:00
|
|
|
func DecodeSegment(seg string) ([]byte, error) {
|
2012-04-18 23:18:31 +04:00
|
|
|
// len % 4
|
|
|
|
switch len(seg) % 4 {
|
2012-04-18 23:59:37 +04:00
|
|
|
case 2:
|
2012-04-18 23:18:31 +04:00
|
|
|
seg = seg + "=="
|
2012-04-18 23:59:37 +04:00
|
|
|
case 3:
|
2012-04-18 23:18:31 +04:00
|
|
|
seg = seg + "==="
|
|
|
|
}
|
2012-04-18 23:59:37 +04:00
|
|
|
|
2012-04-18 23:18:31 +04:00
|
|
|
return base64.URLEncoding.DecodeString(seg)
|
2012-04-18 23:59:37 +04:00
|
|
|
}
|