2020-02-03 08:16:02 +00:00
|
|
|
package jwt
|
|
|
|
|
|
|
|
import (
|
2020-02-25 22:15:44 +00:00
|
|
|
"encoding/base64"
|
2020-02-03 08:16:02 +00:00
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/dgrijalva/jwt-go"
|
|
|
|
"github.com/micro/go-micro/v2/auth"
|
|
|
|
)
|
|
|
|
|
2020-02-25 22:15:44 +00:00
|
|
|
var (
|
|
|
|
// ErrInvalidPrivateKey is returned when the service provided an invalid private key
|
|
|
|
ErrInvalidPrivateKey = errors.New("An invalid private key was provided")
|
2020-02-03 08:16:02 +00:00
|
|
|
|
2020-02-25 22:15:44 +00:00
|
|
|
// ErrEncodingToken is returned when the service encounters an error during encoding
|
|
|
|
ErrEncodingToken = errors.New("An error occured while encoding the JWT")
|
2020-02-03 08:16:02 +00:00
|
|
|
|
2020-02-25 22:15:44 +00:00
|
|
|
// ErrInvalidToken is returned when the token provided is not valid
|
|
|
|
ErrInvalidToken = errors.New("An invalid token was provided")
|
2020-02-03 08:16:02 +00:00
|
|
|
|
2020-02-25 22:15:44 +00:00
|
|
|
// ErrMissingToken is returned when no token is provided
|
|
|
|
ErrMissingToken = errors.New("A valid JWT is required")
|
|
|
|
)
|
2020-02-19 08:51:43 +00:00
|
|
|
|
2020-02-03 08:16:02 +00:00
|
|
|
// NewAuth returns a new instance of the Auth service
|
|
|
|
func NewAuth(opts ...auth.Option) auth.Auth {
|
|
|
|
svc := new(svc)
|
|
|
|
svc.Init(opts...)
|
|
|
|
return svc
|
|
|
|
}
|
|
|
|
|
|
|
|
// svc is the JWT implementation of the Auth interface
|
|
|
|
type svc struct {
|
|
|
|
options auth.Options
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *svc) String() string {
|
|
|
|
return "jwt"
|
|
|
|
}
|
|
|
|
|
2020-02-10 08:26:28 +00:00
|
|
|
func (s *svc) Options() auth.Options {
|
|
|
|
return s.options
|
|
|
|
}
|
|
|
|
|
2020-02-03 08:16:02 +00:00
|
|
|
func (s *svc) Init(opts ...auth.Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&s.options)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AuthClaims to be encoded in the JWT
|
|
|
|
type AuthClaims struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
Roles []*auth.Role `json:"roles"`
|
|
|
|
Metadata map[string]string `json:"metadata"`
|
|
|
|
|
|
|
|
jwt.StandardClaims
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a new JWT
|
|
|
|
func (s *svc) Generate(id string, ops ...auth.GenerateOption) (*auth.Account, error) {
|
2020-02-25 22:15:44 +00:00
|
|
|
// decode the private key
|
|
|
|
priv, err := base64.StdEncoding.DecodeString(s.options.PrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := jwt.ParseRSAPrivateKeyFromPEM(priv)
|
2020-02-03 08:16:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, ErrEncodingToken
|
|
|
|
}
|
|
|
|
|
|
|
|
options := auth.NewGenerateOptions(ops...)
|
|
|
|
account := jwt.NewWithClaims(jwt.SigningMethodRS256, AuthClaims{
|
|
|
|
id, options.Roles, options.Metadata, jwt.StandardClaims{
|
2020-02-19 08:51:43 +00:00
|
|
|
Subject: id,
|
2020-02-03 08:16:02 +00:00
|
|
|
ExpiresAt: time.Now().Add(time.Hour * 24).Unix(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
token, err := account.SignedString(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &auth.Account{
|
|
|
|
Id: id,
|
|
|
|
Token: token,
|
|
|
|
Roles: options.Roles,
|
|
|
|
Metadata: options.Metadata,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Revoke an authorization account
|
|
|
|
func (s *svc) Revoke(token string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-25 22:15:44 +00:00
|
|
|
// Verify a JWT
|
|
|
|
func (s *svc) Verify(token string) (*auth.Account, error) {
|
2020-02-19 08:51:43 +00:00
|
|
|
if token == "" {
|
|
|
|
return nil, ErrMissingToken
|
|
|
|
}
|
|
|
|
|
2020-02-25 22:15:44 +00:00
|
|
|
// decode the public key
|
|
|
|
pub, err := base64.StdEncoding.DecodeString(s.options.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-03 08:16:02 +00:00
|
|
|
res, err := jwt.ParseWithClaims(token, &AuthClaims{}, func(token *jwt.Token) (interface{}, error) {
|
2020-02-25 22:15:44 +00:00
|
|
|
return jwt.ParseRSAPublicKeyFromPEM(pub)
|
2020-02-03 08:16:02 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !res.Valid {
|
|
|
|
return nil, ErrInvalidToken
|
|
|
|
}
|
|
|
|
|
2020-02-19 02:05:38 +03:00
|
|
|
claims, ok := res.Claims.(*AuthClaims)
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrInvalidToken
|
|
|
|
}
|
2020-02-03 08:16:02 +00:00
|
|
|
|
|
|
|
return &auth.Account{
|
|
|
|
Id: claims.Id,
|
|
|
|
Metadata: claims.Metadata,
|
|
|
|
Roles: claims.Roles,
|
|
|
|
}, nil
|
|
|
|
}
|