2020-10-12 14:52:53 +02:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
2020-10-31 14:47:28 +01:00
|
|
|
"encoding/json"
|
2020-10-12 14:52:53 +02:00
|
|
|
"errors"
|
2020-10-17 05:40:17 +02:00
|
|
|
"strings"
|
2020-10-12 14:52:53 +02:00
|
|
|
"time"
|
2020-10-17 05:40:17 +02:00
|
|
|
"unicode"
|
2020-10-12 14:52:53 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// ErrNotSupported is fired when a specific method is not implemented
|
|
|
|
// or not supported entirely.
|
|
|
|
// Can be used by User implementations when
|
|
|
|
// an authentication system does not implement a specific, but required,
|
|
|
|
// method of the User interface.
|
|
|
|
var ErrNotSupported = errors.New("not supported")
|
|
|
|
|
|
|
|
// User is a generic view of an authorized client.
|
|
|
|
// See `Context.User` and `SetUser` methods for more.
|
|
|
|
//
|
|
|
|
// The informational methods starts with a "Get" prefix
|
|
|
|
// in order to allow the implementation to contain exported
|
|
|
|
// fields such as `Username` so they can be JSON encoded when necessary.
|
|
|
|
//
|
|
|
|
// The caller is free to cast this with the implementation directly
|
|
|
|
// when special features are offered by the authorization system.
|
2020-10-17 05:40:17 +02:00
|
|
|
//
|
|
|
|
// To make optional some of the fields you can just embed the User interface
|
|
|
|
// and implement whatever methods you want to support.
|
|
|
|
//
|
2020-10-31 14:47:28 +01:00
|
|
|
// There are three builtin implementations of the User interface:
|
|
|
|
// - SimpleUser
|
|
|
|
// - UserMap (a wrapper by SetUser)
|
|
|
|
// - UserPartial (a wrapper by SetUser)
|
2020-10-12 14:52:53 +02:00
|
|
|
type User interface {
|
2020-11-21 11:04:37 +01:00
|
|
|
// GetRaw should return the raw instance of the user, if supported.
|
|
|
|
GetRaw() (interface{}, error)
|
2020-10-12 14:52:53 +02:00
|
|
|
// GetAuthorization should return the authorization method,
|
|
|
|
// e.g. Basic Authentication.
|
2020-10-31 14:47:28 +01:00
|
|
|
GetAuthorization() (string, error)
|
2020-10-12 14:52:53 +02:00
|
|
|
// GetAuthorizedAt should return the exact time the
|
|
|
|
// client has been authorized for the "first" time.
|
2020-10-31 14:47:28 +01:00
|
|
|
GetAuthorizedAt() (time.Time, error)
|
|
|
|
// GetID should return the ID of the User.
|
|
|
|
GetID() (string, error)
|
2020-10-12 14:52:53 +02:00
|
|
|
// GetUsername should return the name of the User.
|
2020-10-31 14:47:28 +01:00
|
|
|
GetUsername() (string, error)
|
2020-10-12 14:52:53 +02:00
|
|
|
// GetPassword should return the encoded or raw password
|
|
|
|
// (depends on the implementation) of the User.
|
2020-10-31 14:47:28 +01:00
|
|
|
GetPassword() (string, error)
|
2020-10-12 14:52:53 +02:00
|
|
|
// GetEmail should return the e-mail of the User.
|
2020-10-31 14:47:28 +01:00
|
|
|
GetEmail() (string, error)
|
2020-10-17 05:40:17 +02:00
|
|
|
// GetRoles should optionally return the specific user's roles.
|
|
|
|
// Returns `ErrNotSupported` if this method is not
|
|
|
|
// implemented by the User implementation.
|
|
|
|
GetRoles() ([]string, error)
|
|
|
|
// GetToken should optionally return a token used
|
|
|
|
// to authorize this User.
|
2020-10-31 14:47:28 +01:00
|
|
|
GetToken() ([]byte, error)
|
2020-10-17 05:40:17 +02:00
|
|
|
// GetField should optionally return a dynamic field
|
|
|
|
// based on its key. Useful for custom user fields.
|
|
|
|
// Keep in mind that these fields are encoded as a separate JSON key.
|
|
|
|
GetField(key string) (interface{}, error)
|
|
|
|
} /* Notes:
|
|
|
|
We could use a structure of User wrapper and separate interfaces for each of the methods
|
|
|
|
so they return ErrNotSupported if the implementation is missing it, so the `Features`
|
|
|
|
field and HasUserFeature can be omitted and
|
|
|
|
add a Raw() interface{} to return the underline User implementation too.
|
|
|
|
The advandages of the above idea is that we don't have to add new methods
|
|
|
|
for each of the builtin features and we can keep the (assumed) struct small.
|
|
|
|
But we dont as it has many disadvantages, unless is requested.
|
2020-10-31 14:47:28 +01:00
|
|
|
^ UPDATE: this is done through UserPartial.
|
2020-10-17 05:40:17 +02:00
|
|
|
|
|
|
|
The disadvantage of the current implementation is that the developer MUST
|
|
|
|
complete the whole interface in order to be a valid User and if we add
|
|
|
|
new methods in the future their implementation will break
|
|
|
|
(unless they have a static interface implementation check as we have on SimpleUser).
|
|
|
|
We kind of by-pass this disadvantage by providing a SimpleUser which can be embedded (as pointer)
|
|
|
|
to the end-developer's custom implementations.
|
|
|
|
*/
|
2020-10-12 14:52:53 +02:00
|
|
|
|
|
|
|
// SimpleUser is a simple implementation of the User interface.
|
|
|
|
type SimpleUser struct {
|
2021-01-06 00:52:39 +01:00
|
|
|
Authorization string `json:"authorization,omitempty" db:"authorization"`
|
|
|
|
AuthorizedAt time.Time `json:"authorized_at,omitempty" db:"authorized_at"`
|
|
|
|
ID string `json:"id,omitempty" db:"id"`
|
|
|
|
Username string `json:"username,omitempty" db:"username"`
|
|
|
|
Password string `json:"password,omitempty" db:"password"`
|
|
|
|
Email string `json:"email,omitempty" db:"email"`
|
|
|
|
Roles []string `json:"roles,omitempty" db:"roles"`
|
|
|
|
Token json.RawMessage `json:"token,omitempty" db:"token"`
|
|
|
|
Fields Map `json:"fields,omitempty" db:"fields"`
|
2020-10-12 14:52:53 +02:00
|
|
|
}
|
|
|
|
|
2020-10-31 14:47:28 +01:00
|
|
|
var _ User = (*SimpleUser)(nil)
|
2020-10-12 14:52:53 +02:00
|
|
|
|
2020-11-21 11:04:37 +01:00
|
|
|
// GetRaw returns itself.
|
|
|
|
func (u *SimpleUser) GetRaw() (interface{}, error) {
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:52:53 +02:00
|
|
|
// GetAuthorization returns the authorization method,
|
|
|
|
// e.g. Basic Authentication.
|
2020-10-31 14:47:28 +01:00
|
|
|
func (u *SimpleUser) GetAuthorization() (string, error) {
|
|
|
|
return u.Authorization, nil
|
2020-10-12 14:52:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetAuthorizedAt returns the exact time the
|
|
|
|
// client has been authorized for the "first" time.
|
2020-10-31 14:47:28 +01:00
|
|
|
func (u *SimpleUser) GetAuthorizedAt() (time.Time, error) {
|
|
|
|
return u.AuthorizedAt, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetID returns the ID of the User.
|
|
|
|
func (u *SimpleUser) GetID() (string, error) {
|
|
|
|
return u.ID, nil
|
2020-10-12 14:52:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetUsername returns the name of the User.
|
2020-10-31 14:47:28 +01:00
|
|
|
func (u *SimpleUser) GetUsername() (string, error) {
|
|
|
|
return u.Username, nil
|
2020-10-12 14:52:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPassword returns the raw password of the User.
|
2020-10-31 14:47:28 +01:00
|
|
|
func (u *SimpleUser) GetPassword() (string, error) {
|
|
|
|
return u.Password, nil
|
2020-10-12 14:52:53 +02:00
|
|
|
}
|
|
|
|
|
2020-10-31 14:47:28 +01:00
|
|
|
// GetEmail returns the e-mail of (string,error) User.
|
|
|
|
func (u *SimpleUser) GetEmail() (string, error) {
|
|
|
|
return u.Email, nil
|
2020-10-12 14:52:53 +02:00
|
|
|
}
|
|
|
|
|
2020-10-17 05:40:17 +02:00
|
|
|
// GetRoles returns the specific user's roles.
|
|
|
|
// Returns with `ErrNotSupported` if the Roles field is not initialized.
|
|
|
|
func (u *SimpleUser) GetRoles() ([]string, error) {
|
|
|
|
if u.Roles == nil {
|
|
|
|
return nil, ErrNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
return u.Roles, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetToken returns the token associated with this User.
|
|
|
|
// It may return empty if the User is not featured with a Token.
|
|
|
|
//
|
|
|
|
// The implementation can change that behavior.
|
|
|
|
// Returns with `ErrNotSupported` if the Token field is empty.
|
2020-10-31 14:47:28 +01:00
|
|
|
func (u *SimpleUser) GetToken() ([]byte, error) {
|
|
|
|
if len(u.Token) == 0 {
|
|
|
|
return nil, ErrNotSupported
|
2020-10-17 05:40:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return u.Token, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetField optionally returns a dynamic field from the `Fields` field
|
|
|
|
// based on its key.
|
|
|
|
func (u *SimpleUser) GetField(key string) (interface{}, error) {
|
|
|
|
if u.Fields == nil {
|
|
|
|
return nil, ErrNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
return u.Fields[key], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserMap can be used to convert a common map[string]interface{} to a User.
|
|
|
|
// Usage:
|
2022-06-17 21:03:18 +02:00
|
|
|
//
|
|
|
|
// user := map[string]interface{}{
|
|
|
|
// "username": "kataras",
|
|
|
|
// "age" : 27,
|
|
|
|
// }
|
|
|
|
//
|
2020-10-31 14:47:28 +01:00
|
|
|
// ctx.SetUser(user)
|
2020-10-17 05:40:17 +02:00
|
|
|
// OR
|
2020-10-31 14:47:28 +01:00
|
|
|
// user := UserStruct{....}
|
2020-10-17 05:40:17 +02:00
|
|
|
// ctx.SetUser(user)
|
|
|
|
// [...]
|
2020-10-31 14:47:28 +01:00
|
|
|
// username, err := ctx.User().GetUsername()
|
|
|
|
// field,err := ctx.User().GetField("age")
|
|
|
|
// age := field.(int)
|
2020-10-17 05:40:17 +02:00
|
|
|
// OR cast it:
|
|
|
|
// user := ctx.User().(UserMap)
|
|
|
|
// username := user["username"].(string)
|
|
|
|
// age := user["age"].(int)
|
|
|
|
type UserMap Map
|
|
|
|
|
2020-10-31 14:47:28 +01:00
|
|
|
var _ User = UserMap{}
|
2020-10-17 05:40:17 +02:00
|
|
|
|
2020-11-21 11:04:37 +01:00
|
|
|
// GetRaw returns the underline map.
|
|
|
|
func (u UserMap) GetRaw() (interface{}, error) {
|
|
|
|
return Map(u), nil
|
|
|
|
}
|
|
|
|
|
2020-10-17 05:40:17 +02:00
|
|
|
// GetAuthorization returns the authorization or Authorization value of the map.
|
2020-10-31 14:47:28 +01:00
|
|
|
func (u UserMap) GetAuthorization() (string, error) {
|
2020-10-17 05:40:17 +02:00
|
|
|
return u.str("authorization")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAuthorizedAt returns the authorized_at or Authorized_At value of the map.
|
2020-10-31 14:47:28 +01:00
|
|
|
func (u UserMap) GetAuthorizedAt() (time.Time, error) {
|
2020-10-17 05:40:17 +02:00
|
|
|
return u.time("authorized_at")
|
|
|
|
}
|
|
|
|
|
2020-10-31 14:47:28 +01:00
|
|
|
// GetID returns the id or Id or ID value of the map.
|
|
|
|
func (u UserMap) GetID() (string, error) {
|
|
|
|
return u.str("id")
|
|
|
|
}
|
|
|
|
|
2020-10-17 05:40:17 +02:00
|
|
|
// GetUsername returns the username or Username value of the map.
|
2020-10-31 14:47:28 +01:00
|
|
|
func (u UserMap) GetUsername() (string, error) {
|
2020-10-17 05:40:17 +02:00
|
|
|
return u.str("username")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPassword returns the password or Password value of the map.
|
2020-10-31 14:47:28 +01:00
|
|
|
func (u UserMap) GetPassword() (string, error) {
|
2020-10-17 05:40:17 +02:00
|
|
|
return u.str("password")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetEmail returns the email or Email value of the map.
|
2020-10-31 14:47:28 +01:00
|
|
|
func (u UserMap) GetEmail() (string, error) {
|
2020-10-17 05:40:17 +02:00
|
|
|
return u.str("email")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRoles returns the roles or Roles value of the map.
|
|
|
|
func (u UserMap) GetRoles() ([]string, error) {
|
2020-10-31 14:47:28 +01:00
|
|
|
return u.strSlice("roles")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetToken returns the roles or Roles value of the map.
|
|
|
|
func (u UserMap) GetToken() ([]byte, error) {
|
|
|
|
return u.bytes("token")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetField returns the raw map's value based on its "key".
|
|
|
|
// It's not kind of useful here as you can just use the map.
|
|
|
|
func (u UserMap) GetField(key string) (interface{}, error) {
|
|
|
|
return u[key], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u UserMap) val(key string) interface{} {
|
|
|
|
isTitle := unicode.IsTitle(rune(key[0])) // if starts with uppercase.
|
|
|
|
if isTitle {
|
|
|
|
key = strings.ToLower(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return u[key]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u UserMap) bytes(key string) ([]byte, error) {
|
|
|
|
if v := u.val(key); v != nil {
|
|
|
|
switch s := v.(type) {
|
|
|
|
case []byte:
|
|
|
|
return s, nil
|
|
|
|
case string:
|
|
|
|
return []byte(s), nil
|
|
|
|
}
|
2020-10-17 05:40:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ErrNotSupported
|
|
|
|
}
|
|
|
|
|
2020-10-31 14:47:28 +01:00
|
|
|
func (u UserMap) str(key string) (string, error) {
|
|
|
|
if v := u.val(key); v != nil {
|
|
|
|
if s, ok := v.(string); ok {
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// exists or not we don't care, if it's invalid type we don't fill it.
|
2020-10-17 05:40:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return "", ErrNotSupported
|
|
|
|
}
|
|
|
|
|
2020-10-31 14:47:28 +01:00
|
|
|
func (u UserMap) strSlice(key string) ([]string, error) {
|
|
|
|
if v := u.val(key); v != nil {
|
|
|
|
if s, ok := v.([]string); ok {
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ErrNotSupported
|
2020-10-17 05:40:17 +02:00
|
|
|
}
|
|
|
|
|
2020-10-31 14:47:28 +01:00
|
|
|
func (u UserMap) time(key string) (time.Time, error) {
|
|
|
|
if v := u.val(key); v != nil {
|
|
|
|
if t, ok := v.(time.Time); ok {
|
|
|
|
return t, nil
|
2020-10-17 05:40:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-31 14:47:28 +01:00
|
|
|
return time.Time{}, ErrNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
type (
|
|
|
|
userGetAuthorization interface {
|
|
|
|
GetAuthorization() string
|
|
|
|
}
|
|
|
|
|
|
|
|
userGetAuthorizedAt interface {
|
|
|
|
GetAuthorizedAt() time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
userGetID interface {
|
|
|
|
GetID() string
|
|
|
|
}
|
|
|
|
|
2020-11-21 11:04:37 +01:00
|
|
|
// UserGetUsername interface which
|
|
|
|
// requires a single method to complete
|
|
|
|
// a User on Context.SetUser.
|
|
|
|
UserGetUsername interface {
|
2020-10-31 14:47:28 +01:00
|
|
|
GetUsername() string
|
|
|
|
}
|
|
|
|
|
2020-11-21 11:04:37 +01:00
|
|
|
// UserGetPassword interface which
|
|
|
|
// requires a single method to complete
|
|
|
|
// a User on Context.SetUser.
|
|
|
|
UserGetPassword interface {
|
2020-10-31 14:47:28 +01:00
|
|
|
GetPassword() string
|
|
|
|
}
|
|
|
|
|
|
|
|
userGetEmail interface {
|
|
|
|
GetEmail() string
|
|
|
|
}
|
|
|
|
|
|
|
|
userGetRoles interface {
|
|
|
|
GetRoles() []string
|
|
|
|
}
|
|
|
|
|
|
|
|
userGetToken interface {
|
|
|
|
GetToken() []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
userGetField interface {
|
|
|
|
GetField(string) interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserPartial is a User.
|
|
|
|
// It's a helper which wraps a struct value that
|
|
|
|
// may or may not complete the whole User interface.
|
2020-11-21 11:04:37 +01:00
|
|
|
// See Context.SetUser.
|
2020-10-31 14:47:28 +01:00
|
|
|
UserPartial struct {
|
2020-11-24 13:58:02 +01:00
|
|
|
Raw interface{} `json:"raw"`
|
|
|
|
userGetAuthorization `json:",omitempty"`
|
|
|
|
userGetAuthorizedAt `json:",omitempty"`
|
|
|
|
userGetID `json:",omitempty"`
|
|
|
|
UserGetUsername `json:",omitempty"`
|
|
|
|
UserGetPassword `json:",omitempty"`
|
|
|
|
userGetEmail `json:",omitempty"`
|
|
|
|
userGetRoles `json:",omitempty"`
|
|
|
|
userGetToken `json:",omitempty"`
|
|
|
|
userGetField `json:",omitempty"`
|
2020-10-31 14:47:28 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ User = (*UserPartial)(nil)
|
|
|
|
|
|
|
|
func newUserPartial(i interface{}) *UserPartial {
|
2020-11-21 11:04:37 +01:00
|
|
|
if i == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-31 14:47:28 +01:00
|
|
|
p := &UserPartial{Raw: i}
|
|
|
|
|
|
|
|
if u, ok := i.(userGetAuthorization); ok {
|
|
|
|
p.userGetAuthorization = u
|
|
|
|
}
|
|
|
|
|
|
|
|
if u, ok := i.(userGetAuthorizedAt); ok {
|
|
|
|
p.userGetAuthorizedAt = u
|
|
|
|
}
|
|
|
|
|
|
|
|
if u, ok := i.(userGetID); ok {
|
|
|
|
p.userGetID = u
|
|
|
|
}
|
|
|
|
|
2020-11-21 11:04:37 +01:00
|
|
|
if u, ok := i.(UserGetUsername); ok {
|
|
|
|
p.UserGetUsername = u
|
2020-10-31 14:47:28 +01:00
|
|
|
}
|
2020-10-17 05:40:17 +02:00
|
|
|
|
2020-11-21 11:04:37 +01:00
|
|
|
if u, ok := i.(UserGetPassword); ok {
|
|
|
|
p.UserGetPassword = u
|
2020-10-17 05:40:17 +02:00
|
|
|
}
|
|
|
|
|
2020-10-31 14:47:28 +01:00
|
|
|
if u, ok := i.(userGetEmail); ok {
|
|
|
|
p.userGetEmail = u
|
2020-10-17 05:40:17 +02:00
|
|
|
}
|
|
|
|
|
2020-10-31 14:47:28 +01:00
|
|
|
if u, ok := i.(userGetRoles); ok {
|
|
|
|
p.userGetRoles = u
|
2020-10-17 05:40:17 +02:00
|
|
|
}
|
|
|
|
|
2020-10-31 14:47:28 +01:00
|
|
|
if u, ok := i.(userGetToken); ok {
|
|
|
|
p.userGetToken = u
|
2020-10-17 05:40:17 +02:00
|
|
|
}
|
|
|
|
|
2020-10-31 14:47:28 +01:00
|
|
|
if u, ok := i.(userGetField); ok {
|
|
|
|
p.userGetField = u
|
2020-10-17 05:40:17 +02:00
|
|
|
}
|
|
|
|
|
2020-11-21 11:04:37 +01:00
|
|
|
// if !containsAtLeastOneMethod {
|
|
|
|
// return nil
|
|
|
|
// }
|
2020-10-31 14:47:28 +01:00
|
|
|
|
|
|
|
return p
|
2020-10-17 05:40:17 +02:00
|
|
|
}
|
|
|
|
|
2020-11-21 11:04:37 +01:00
|
|
|
// GetRaw returns the original raw instance of the user.
|
|
|
|
func (u *UserPartial) GetRaw() (interface{}, error) {
|
|
|
|
if u == nil {
|
|
|
|
return nil, ErrNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
return u.Raw, nil
|
|
|
|
}
|
|
|
|
|
2020-10-31 14:47:28 +01:00
|
|
|
// GetAuthorization should return the authorization method,
|
|
|
|
// e.g. Basic Authentication.
|
|
|
|
func (u *UserPartial) GetAuthorization() (string, error) {
|
|
|
|
if v := u.userGetAuthorization; v != nil {
|
|
|
|
return v.GetAuthorization(), nil
|
2020-10-17 05:40:17 +02:00
|
|
|
}
|
|
|
|
|
2020-10-31 14:47:28 +01:00
|
|
|
return "", ErrNotSupported
|
2020-10-17 05:40:17 +02:00
|
|
|
}
|
|
|
|
|
2020-10-31 14:47:28 +01:00
|
|
|
// GetAuthorizedAt should return the exact time the
|
|
|
|
// client has been authorized for the "first" time.
|
|
|
|
func (u *UserPartial) GetAuthorizedAt() (time.Time, error) {
|
|
|
|
if v := u.userGetAuthorizedAt; v != nil {
|
|
|
|
return v.GetAuthorizedAt(), nil
|
|
|
|
}
|
2020-10-17 05:40:17 +02:00
|
|
|
|
2020-10-31 14:47:28 +01:00
|
|
|
return time.Time{}, ErrNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetID should return the ID of the User.
|
|
|
|
func (u *UserPartial) GetID() (string, error) {
|
|
|
|
if v := u.userGetID; v != nil {
|
|
|
|
return v.GetID(), nil
|
2020-10-17 05:40:17 +02:00
|
|
|
}
|
|
|
|
|
2020-10-31 14:47:28 +01:00
|
|
|
return "", ErrNotSupported
|
2020-10-17 05:40:17 +02:00
|
|
|
}
|
|
|
|
|
2020-10-31 14:47:28 +01:00
|
|
|
// GetUsername should return the name of the User.
|
|
|
|
func (u *UserPartial) GetUsername() (string, error) {
|
2020-11-21 11:04:37 +01:00
|
|
|
if v := u.UserGetUsername; v != nil {
|
2020-10-31 14:47:28 +01:00
|
|
|
return v.GetUsername(), nil
|
2020-10-17 05:40:17 +02:00
|
|
|
}
|
|
|
|
|
2020-10-31 14:47:28 +01:00
|
|
|
return "", ErrNotSupported
|
2020-10-17 05:40:17 +02:00
|
|
|
}
|
|
|
|
|
2020-10-31 14:47:28 +01:00
|
|
|
// GetPassword should return the encoded or raw password
|
|
|
|
// (depends on the implementation) of the User.
|
|
|
|
func (u *UserPartial) GetPassword() (string, error) {
|
2020-11-21 11:04:37 +01:00
|
|
|
if v := u.UserGetPassword; v != nil {
|
2020-10-31 14:47:28 +01:00
|
|
|
return v.GetPassword(), nil
|
2020-10-17 05:40:17 +02:00
|
|
|
}
|
|
|
|
|
2020-10-31 14:47:28 +01:00
|
|
|
return "", ErrNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetEmail should return the e-mail of the User.
|
|
|
|
func (u *UserPartial) GetEmail() (string, error) {
|
|
|
|
if v := u.userGetEmail; v != nil {
|
|
|
|
return v.GetEmail(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", ErrNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRoles should optionally return the specific user's roles.
|
|
|
|
// Returns `ErrNotSupported` if this method is not
|
|
|
|
// implemented by the User implementation.
|
|
|
|
func (u *UserPartial) GetRoles() ([]string, error) {
|
|
|
|
if v := u.userGetRoles; v != nil {
|
|
|
|
return v.GetRoles(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ErrNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetToken should optionally return a token used
|
|
|
|
// to authorize this User.
|
|
|
|
func (u *UserPartial) GetToken() ([]byte, error) {
|
|
|
|
if v := u.userGetToken; v != nil {
|
|
|
|
return v.GetToken(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ErrNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetField should optionally return a dynamic field
|
|
|
|
// based on its key. Useful for custom user fields.
|
|
|
|
// Keep in mind that these fields are encoded as a separate JSON key.
|
|
|
|
func (u *UserPartial) GetField(key string) (interface{}, error) {
|
|
|
|
if v := u.userGetField; v != nil {
|
|
|
|
return v.GetField(key), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ErrNotSupported
|
2020-10-17 05:40:17 +02:00
|
|
|
}
|