add Name to auth.Account as a user friendly alias (#1992)
* add Name to auth.Account as a user friendly alias
This commit is contained in:
@@ -49,7 +49,7 @@ type Auth interface {
|
||||
|
||||
// Account provided by an auth provider
|
||||
type Account struct {
|
||||
// ID of the account e.g. email
|
||||
// ID of the account e.g. UUID. Should not change
|
||||
ID string `json:"id"`
|
||||
// Type of the account, e.g. service
|
||||
Type string `json:"type"`
|
||||
@@ -61,6 +61,8 @@ type Account struct {
|
||||
Scopes []string `json:"scopes"`
|
||||
// Secret for the account, e.g. the password
|
||||
Secret string `json:"secret"`
|
||||
// Name of the account. User friendly name that might change e.g. a username or email
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// Token can be short or long lived
|
||||
|
@@ -54,13 +54,17 @@ func (j *jwtAuth) Generate(id string, opts ...auth.GenerateOption) (*auth.Accoun
|
||||
if len(options.Issuer) == 0 {
|
||||
options.Issuer = j.Options().Issuer
|
||||
}
|
||||
|
||||
name := options.Name
|
||||
if name == "" {
|
||||
name = id
|
||||
}
|
||||
account := &auth.Account{
|
||||
ID: id,
|
||||
Type: options.Type,
|
||||
Scopes: options.Scopes,
|
||||
Metadata: options.Metadata,
|
||||
Issuer: options.Issuer,
|
||||
Name: name,
|
||||
}
|
||||
|
||||
// generate a JWT secret which can be provided to the Token() method
|
||||
|
@@ -40,13 +40,17 @@ func (n *noop) Options() auth.Options {
|
||||
// Generate a new account
|
||||
func (n *noop) Generate(id string, opts ...auth.GenerateOption) (*auth.Account, error) {
|
||||
options := auth.NewGenerateOptions(opts...)
|
||||
|
||||
name := options.Name
|
||||
if name == "" {
|
||||
name = id
|
||||
}
|
||||
return &auth.Account{
|
||||
ID: id,
|
||||
Secret: options.Secret,
|
||||
Metadata: options.Metadata,
|
||||
Scopes: options.Scopes,
|
||||
Issuer: n.Options().Issuer,
|
||||
Name: name,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
@@ -110,6 +110,8 @@ type GenerateOptions struct {
|
||||
Secret string
|
||||
// Issuer of the account, e.g. micro
|
||||
Issuer string
|
||||
// Name of the acouunt e.g. an email or username
|
||||
Name string
|
||||
}
|
||||
|
||||
type GenerateOption func(o *GenerateOptions)
|
||||
@@ -156,6 +158,13 @@ func WithIssuer(i string) GenerateOption {
|
||||
}
|
||||
}
|
||||
|
||||
// WithName for the generated account
|
||||
func WithName(n string) GenerateOption {
|
||||
return func(o *GenerateOptions) {
|
||||
o.Name = n
|
||||
}
|
||||
}
|
||||
|
||||
// NewGenerateOptions from a slice of options
|
||||
func NewGenerateOptions(opts ...GenerateOption) GenerateOptions {
|
||||
var options GenerateOptions
|
||||
|
Reference in New Issue
Block a user