2020-03-23 16:19:30 +00:00
|
|
|
package jwt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"time"
|
|
|
|
|
2021-08-16 23:56:50 +03:00
|
|
|
"github.com/golang-jwt/jwt/v4"
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/auth"
|
2020-11-18 16:50:41 +03:00
|
|
|
"github.com/unistack-org/micro/v3/metadata"
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/util/token"
|
2020-03-23 16:19:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// authClaims to be encoded in the JWT
|
|
|
|
type authClaims struct {
|
2020-11-18 16:50:41 +03:00
|
|
|
Metadata metadata.Metadata `json:"metadata"`
|
2021-09-28 23:43:43 +03:00
|
|
|
jwt.RegisteredClaims
|
2021-03-06 19:45:13 +03:00
|
|
|
Type string `json:"type"`
|
|
|
|
Scopes []string `json:"scopes"`
|
2020-03-23 16:19:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// JWT implementation of token provider
|
|
|
|
type JWT struct {
|
|
|
|
opts token.Options
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTokenProvider returns an initialized basic provider
|
|
|
|
func NewTokenProvider(opts ...token.Option) token.Provider {
|
|
|
|
return &JWT{
|
|
|
|
opts: token.NewOptions(opts...),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a new JWT
|
2020-04-01 14:25:00 +01:00
|
|
|
func (j *JWT) Generate(acc *auth.Account, opts ...token.GenerateOption) (*token.Token, error) {
|
2020-03-23 16:19:30 +00:00
|
|
|
// decode the private key
|
|
|
|
priv, err := base64.StdEncoding.DecodeString(j.opts.PrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse the private key
|
|
|
|
key, err := jwt.ParseRSAPrivateKeyFromPEM(priv)
|
|
|
|
if err != nil {
|
|
|
|
return nil, token.ErrEncodingToken
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse the options
|
|
|
|
options := token.NewGenerateOptions(opts...)
|
|
|
|
|
|
|
|
// generate the JWT
|
|
|
|
expiry := time.Now().Add(options.Expiry)
|
|
|
|
t := jwt.NewWithClaims(jwt.SigningMethodRS256, authClaims{
|
2021-09-28 23:43:43 +03:00
|
|
|
Type: acc.Type, Scopes: acc.Scopes, Metadata: acc.Metadata, RegisteredClaims: jwt.RegisteredClaims{
|
2020-04-01 14:25:00 +01:00
|
|
|
Subject: acc.ID,
|
2020-05-21 16:41:55 +01:00
|
|
|
Issuer: acc.Issuer,
|
2021-09-28 23:43:43 +03:00
|
|
|
ExpiresAt: jwt.NewNumericDate(expiry),
|
2020-03-23 16:19:30 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
tok, err := t.SignedString(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// return the token
|
2020-04-01 14:25:00 +01:00
|
|
|
return &token.Token{
|
|
|
|
Token: tok,
|
|
|
|
Expiry: expiry,
|
|
|
|
Created: time.Now(),
|
2020-03-23 16:19:30 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inspect a JWT
|
2020-04-01 14:25:00 +01:00
|
|
|
func (j *JWT) Inspect(t string) (*auth.Account, error) {
|
2020-03-23 16:19:30 +00:00
|
|
|
// decode the public key
|
|
|
|
pub, err := base64.StdEncoding.DecodeString(j.opts.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse the public key
|
|
|
|
res, err := jwt.ParseWithClaims(t, &authClaims{}, func(token *jwt.Token) (interface{}, error) {
|
|
|
|
return jwt.ParseRSAPublicKeyFromPEM(pub)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, token.ErrInvalidToken
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate the token
|
|
|
|
if !res.Valid {
|
|
|
|
return nil, token.ErrInvalidToken
|
|
|
|
}
|
|
|
|
claims, ok := res.Claims.(*authClaims)
|
|
|
|
if !ok {
|
|
|
|
return nil, token.ErrInvalidToken
|
|
|
|
}
|
|
|
|
|
|
|
|
// return the token
|
2020-04-01 14:25:00 +01:00
|
|
|
return &auth.Account{
|
2020-05-19 18:17:17 +01:00
|
|
|
ID: claims.Subject,
|
2020-05-21 16:41:55 +01:00
|
|
|
Issuer: claims.Issuer,
|
2020-05-19 18:17:17 +01:00
|
|
|
Type: claims.Type,
|
|
|
|
Scopes: claims.Scopes,
|
|
|
|
Metadata: claims.Metadata,
|
2020-03-23 16:19:30 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns JWT
|
|
|
|
func (j *JWT) String() string {
|
|
|
|
return "jwt"
|
|
|
|
}
|