Improve JWT Package Errors (#1206)
Co-authored-by: Asim Aslam <asim@aslam.me>
This commit is contained in:
parent
f4118dc357
commit
36bcd3bd82
@ -17,6 +17,9 @@ var ErrEncodingToken = errors.New("An error occured while encoding the JWT")
|
|||||||
// ErrInvalidToken is returned when the token provided is not valid
|
// ErrInvalidToken is returned when the token provided is not valid
|
||||||
var ErrInvalidToken = errors.New("An invalid token was provided")
|
var ErrInvalidToken = errors.New("An invalid token was provided")
|
||||||
|
|
||||||
|
// ErrMissingToken is returned when no token is provided
|
||||||
|
var ErrMissingToken = errors.New("A valid JWT is required")
|
||||||
|
|
||||||
// NewAuth returns a new instance of the Auth service
|
// NewAuth returns a new instance of the Auth service
|
||||||
func NewAuth(opts ...auth.Option) auth.Auth {
|
func NewAuth(opts ...auth.Option) auth.Auth {
|
||||||
svc := new(svc)
|
svc := new(svc)
|
||||||
@ -64,7 +67,7 @@ func (s *svc) Generate(id string, ops ...auth.GenerateOption) (*auth.Account, er
|
|||||||
options := auth.NewGenerateOptions(ops...)
|
options := auth.NewGenerateOptions(ops...)
|
||||||
account := jwt.NewWithClaims(jwt.SigningMethodRS256, AuthClaims{
|
account := jwt.NewWithClaims(jwt.SigningMethodRS256, AuthClaims{
|
||||||
id, options.Roles, options.Metadata, jwt.StandardClaims{
|
id, options.Roles, options.Metadata, jwt.StandardClaims{
|
||||||
Subject: "TODO",
|
Subject: id,
|
||||||
ExpiresAt: time.Now().Add(time.Hour * 24).Unix(),
|
ExpiresAt: time.Now().Add(time.Hour * 24).Unix(),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@ -89,6 +92,10 @@ func (s *svc) Revoke(token string) error {
|
|||||||
|
|
||||||
// Validate a JWT
|
// Validate a JWT
|
||||||
func (s *svc) Validate(token string) (*auth.Account, error) {
|
func (s *svc) Validate(token string) (*auth.Account, error) {
|
||||||
|
if token == "" {
|
||||||
|
return nil, ErrMissingToken
|
||||||
|
}
|
||||||
|
|
||||||
res, err := jwt.ParseWithClaims(token, &AuthClaims{}, func(token *jwt.Token) (interface{}, error) {
|
res, err := jwt.ParseWithClaims(token, &AuthClaims{}, func(token *jwt.Token) (interface{}, error) {
|
||||||
return jwt.ParseRSAPublicKeyFromPEM(s.options.PublicKey)
|
return jwt.ParseRSAPublicKeyFromPEM(s.options.PublicKey)
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user