2020-04-29 09:21:17 +01:00
|
|
|
package jwt
|
|
|
|
|
|
|
|
import (
|
2020-04-29 13:21:51 +01:00
|
|
|
"sync"
|
2020-05-14 13:56:51 +01:00
|
|
|
"time"
|
2020-04-29 09:21:17 +01:00
|
|
|
|
|
|
|
"github.com/micro/go-micro/v2/auth"
|
2020-05-20 11:59:01 +01:00
|
|
|
"github.com/micro/go-micro/v2/auth/rules"
|
2020-04-29 09:21:17 +01:00
|
|
|
"github.com/micro/go-micro/v2/auth/token"
|
|
|
|
jwtToken "github.com/micro/go-micro/v2/auth/token/jwt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewAuth returns a new instance of the Auth service
|
|
|
|
func NewAuth(opts ...auth.Option) auth.Auth {
|
|
|
|
j := new(jwt)
|
|
|
|
j.Init(opts...)
|
|
|
|
return j
|
|
|
|
}
|
|
|
|
|
|
|
|
type jwt struct {
|
|
|
|
options auth.Options
|
|
|
|
jwt token.Provider
|
2020-05-20 11:59:01 +01:00
|
|
|
rules []*auth.Rule
|
2020-04-29 13:21:51 +01:00
|
|
|
|
|
|
|
sync.Mutex
|
2020-04-29 09:21:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (j *jwt) String() string {
|
|
|
|
return "jwt"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *jwt) Init(opts ...auth.Option) {
|
2020-04-29 13:21:51 +01:00
|
|
|
j.Lock()
|
|
|
|
defer j.Unlock()
|
|
|
|
|
2020-04-29 09:21:17 +01:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&j.options)
|
|
|
|
}
|
|
|
|
|
|
|
|
j.jwt = jwtToken.NewTokenProvider(
|
2020-04-29 09:38:39 +01:00
|
|
|
token.WithPrivateKey(j.options.PrivateKey),
|
2020-04-29 09:21:17 +01:00
|
|
|
token.WithPublicKey(j.options.PublicKey),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *jwt) Options() auth.Options {
|
2020-04-29 13:21:51 +01:00
|
|
|
j.Lock()
|
|
|
|
defer j.Unlock()
|
2020-04-29 09:21:17 +01:00
|
|
|
return j.options
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *jwt) Generate(id string, opts ...auth.GenerateOption) (*auth.Account, error) {
|
2020-04-29 13:21:51 +01:00
|
|
|
options := auth.NewGenerateOptions(opts...)
|
|
|
|
account := &auth.Account{
|
2020-05-19 18:17:17 +01:00
|
|
|
ID: id,
|
|
|
|
Type: options.Type,
|
|
|
|
Scopes: options.Scopes,
|
|
|
|
Metadata: options.Metadata,
|
2020-05-21 16:41:55 +01:00
|
|
|
Issuer: j.Options().Namespace,
|
2020-04-29 13:21:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// generate a JWT secret which can be provided to the Token() method
|
|
|
|
// and exchanged for an access token
|
|
|
|
secret, err := j.jwt.Generate(account)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
account.Secret = secret.Token
|
|
|
|
|
|
|
|
// return the account
|
|
|
|
return account, nil
|
2020-04-29 09:21:17 +01:00
|
|
|
}
|
|
|
|
|
2020-05-20 11:59:01 +01:00
|
|
|
func (j *jwt) Grant(rule *auth.Rule) error {
|
2020-04-29 13:21:51 +01:00
|
|
|
j.Lock()
|
|
|
|
defer j.Unlock()
|
2020-05-20 11:59:01 +01:00
|
|
|
j.rules = append(j.rules, rule)
|
2020-04-29 13:21:51 +01:00
|
|
|
return nil
|
2020-04-29 09:21:17 +01:00
|
|
|
}
|
|
|
|
|
2020-05-20 11:59:01 +01:00
|
|
|
func (j *jwt) Revoke(rule *auth.Rule) error {
|
2020-04-29 13:21:51 +01:00
|
|
|
j.Lock()
|
|
|
|
defer j.Unlock()
|
|
|
|
|
2020-05-20 11:59:01 +01:00
|
|
|
rules := []*auth.Rule{}
|
2020-04-29 13:21:51 +01:00
|
|
|
for _, r := range rules {
|
2020-05-20 11:59:01 +01:00
|
|
|
if r.ID != rule.ID {
|
2020-04-29 13:21:51 +01:00
|
|
|
rules = append(rules, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
j.rules = rules
|
|
|
|
return nil
|
2020-04-29 09:21:17 +01:00
|
|
|
}
|
|
|
|
|
2020-05-20 16:49:52 +01:00
|
|
|
func (j *jwt) Verify(acc *auth.Account, res *auth.Resource, opts ...auth.VerifyOption) error {
|
2020-05-19 18:17:17 +01:00
|
|
|
j.Lock()
|
2020-05-20 11:59:01 +01:00
|
|
|
defer j.Unlock()
|
2020-05-20 16:49:52 +01:00
|
|
|
|
2020-05-21 16:41:55 +01:00
|
|
|
var options auth.VerifyOptions
|
2020-05-20 16:49:52 +01:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2020-05-21 16:41:55 +01:00
|
|
|
return rules.Verify(j.rules, acc, res)
|
2020-05-20 11:59:01 +01:00
|
|
|
}
|
2020-04-29 13:21:51 +01:00
|
|
|
|
2020-05-26 15:52:21 +01:00
|
|
|
func (j *jwt) Rules(opts ...auth.RulesOption) ([]*auth.Rule, error) {
|
2020-05-20 11:59:01 +01:00
|
|
|
j.Lock()
|
|
|
|
defer j.Unlock()
|
|
|
|
return j.rules, nil
|
2020-04-29 09:21:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (j *jwt) Inspect(token string) (*auth.Account, error) {
|
|
|
|
return j.jwt.Inspect(token)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *jwt) Token(opts ...auth.TokenOption) (*auth.Token, error) {
|
|
|
|
options := auth.NewTokenOptions(opts...)
|
2020-04-29 13:21:51 +01:00
|
|
|
|
|
|
|
secret := options.RefreshToken
|
|
|
|
if len(options.Secret) > 0 {
|
|
|
|
secret = options.Secret
|
|
|
|
}
|
|
|
|
|
|
|
|
account, err := j.jwt.Inspect(secret)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-04-29 09:21:17 +01:00
|
|
|
}
|
|
|
|
|
2020-05-14 13:56:51 +01:00
|
|
|
access, err := j.jwt.Generate(account, token.WithExpiry(options.Expiry))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
refresh, err := j.jwt.Generate(account, token.WithExpiry(options.Expiry+time.Hour))
|
2020-04-29 09:21:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &auth.Token{
|
2020-05-14 13:56:51 +01:00
|
|
|
Created: access.Created,
|
|
|
|
Expiry: access.Expiry,
|
|
|
|
AccessToken: access.Token,
|
|
|
|
RefreshToken: refresh.Token,
|
2020-04-29 09:21:17 +01:00
|
|
|
}, nil
|
|
|
|
}
|