🏷️ Added extra claims support for Token domain
This commit is contained in:
parent
6e09c0bd1c
commit
7069f63a72
|
@ -15,19 +15,24 @@ import (
|
||||||
type (
|
type (
|
||||||
// Token describes the data of the token used by the clients.
|
// Token describes the data of the token used by the clients.
|
||||||
Token struct {
|
Token struct {
|
||||||
Scope Scopes
|
CreatedAt time.Time
|
||||||
ClientID *ClientID
|
Expiry time.Time
|
||||||
Me *Me
|
ClientID *ClientID
|
||||||
AccessToken string
|
Me *Me
|
||||||
|
Scope Scopes
|
||||||
|
Extra map[string]interface{}
|
||||||
|
AccessToken string
|
||||||
|
RefreshToken string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTokenOptions contains options for NewToken function.
|
// NewTokenOptions contains options for NewToken function.
|
||||||
NewTokenOptions struct {
|
NewTokenOptions struct {
|
||||||
Expiration time.Duration
|
Expiration time.Duration
|
||||||
Scope Scopes
|
|
||||||
Issuer *ClientID
|
Issuer *ClientID
|
||||||
Subject *Me
|
Subject *Me
|
||||||
|
Scope Scopes
|
||||||
Secret []byte
|
Secret []byte
|
||||||
|
Claims map[string]interface{}
|
||||||
Algorithm string
|
Algorithm string
|
||||||
NonceLength int
|
NonceLength int
|
||||||
}
|
}
|
||||||
|
@ -36,13 +41,14 @@ type (
|
||||||
// DefaultNewTokenOptions describes the default settings for NewToken.
|
// DefaultNewTokenOptions describes the default settings for NewToken.
|
||||||
//nolint: gochecknoglobals, gomnd
|
//nolint: gochecknoglobals, gomnd
|
||||||
var DefaultNewTokenOptions = NewTokenOptions{
|
var DefaultNewTokenOptions = NewTokenOptions{
|
||||||
Algorithm: "HS256",
|
|
||||||
Expiration: 0,
|
Expiration: 0,
|
||||||
Issuer: nil,
|
|
||||||
NonceLength: 32,
|
|
||||||
Scope: nil,
|
Scope: nil,
|
||||||
Secret: nil,
|
Issuer: nil,
|
||||||
Subject: nil,
|
Subject: nil,
|
||||||
|
Secret: nil,
|
||||||
|
Algorithm: "HS256",
|
||||||
|
NonceLength: 32,
|
||||||
|
Claims: nil,
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewToken create a new token by provided options.
|
// NewToken create a new token by provided options.
|
||||||
|
@ -77,6 +83,12 @@ func NewToken(opts NewTokenOptions) (*Token, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for key, val := range opts.Claims {
|
||||||
|
if err = tkn.Set(key, val); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to set JWT token claim: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if opts.Issuer != nil {
|
if opts.Issuer != nil {
|
||||||
if err = tkn.Set(jwt.IssuerKey, opts.Issuer.String()); err != nil {
|
if err = tkn.Set(jwt.IssuerKey, opts.Issuer.String()); err != nil {
|
||||||
return nil, fmt.Errorf("failed to set JWT token field: %w", err)
|
return nil, fmt.Errorf("failed to set JWT token field: %w", err)
|
||||||
|
@ -95,10 +107,14 @@ func NewToken(opts NewTokenOptions) (*Token, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Token{
|
return &Token{
|
||||||
AccessToken: string(accessToken),
|
AccessToken: string(accessToken),
|
||||||
ClientID: opts.Issuer,
|
ClientID: opts.Issuer,
|
||||||
Me: opts.Subject,
|
CreatedAt: now,
|
||||||
Scope: opts.Scope,
|
Expiry: now.Add(opts.Expiration),
|
||||||
|
Extra: opts.Claims,
|
||||||
|
Me: opts.Subject,
|
||||||
|
RefreshToken: "", // TODO(toby3d)
|
||||||
|
Scope: opts.Scope,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,10 +160,14 @@ func TestToken(tb testing.TB) *Token {
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Token{
|
return &Token{
|
||||||
ClientID: cid,
|
CreatedAt: now.Add(-1 * time.Hour),
|
||||||
Me: me,
|
Expiry: now.Add(1 * time.Hour),
|
||||||
Scope: scope,
|
ClientID: cid,
|
||||||
AccessToken: string(accessToken),
|
Me: me,
|
||||||
|
Scope: scope,
|
||||||
|
Extra: nil,
|
||||||
|
AccessToken: string(accessToken),
|
||||||
|
RefreshToken: "", // TODO(toby3d)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue