Documentation
¶
Overview ¶
Package users contains the domain concept definitions needed to support SuperMQ users service functionality.
This package defines the core domain concepts and types necessary to handle users in the context of a SuperMQ users service. It abstracts the underlying complexities of user management and provides a structured approach to working with users.
Index ¶
- Constants
- func NewDeleteHandler(ctx context.Context, users Repository, policyService policies.Service, ...)
- type Credentials
- type Emailer
- type Hasher
- type MembersPage
- type Metadata
- type Page
- type Repository
- type Role
- type Service
- type Status
- type User
- type UserRepository
- type UserReq
- type UserVerification
- type UsersPage
Constants ¶
const ( Disabled = "disabled" Enabled = "enabled" Deleted = "deleted" All = "all" Unknown = "unknown" )
String representation of the possible status values.
const (
Admin = "admin"
)
String representation of the possible role values.
const VerificationExpiryDuration = 24 * time.Hour
Variables ¶
This section is empty.
Functions ¶
func NewDeleteHandler ¶
func NewDeleteHandler(ctx context.Context, users Repository, policyService policies.Service, domainsClient grpcDomainsV1.DomainsServiceClient, defCheckInterval, deleteAfter time.Duration, logger *slog.Logger)
Types ¶
type Credentials ¶
type Emailer ¶
type Emailer interface {
// SendPasswordReset sends an email to the user with a link to reset the password.
SendPasswordReset(To []string, user, token string) error
// SendVerification sends an email to the user with a verification token.
SendVerification(To []string, user, verificationToken string) error
}
Emailer wrapper around the email.
type Hasher ¶
type Hasher interface {
// Hash generates the hashed string from plain-text.
Hash(string) (string, error)
// Compare compares plain-text version to the hashed one. An error should
// indicate failed comparison.
Compare(string, string) error
}
Hasher specifies an API for generating hashes of an arbitrary textual content.
type MembersPage ¶
MembersPage contains page related metadata as well as list of members that belong to this page.
type Page ¶
type Page struct {
Total uint64 `json:"total"`
Offset uint64 `json:"offset"`
Limit uint64 `json:"limit"`
OnlyTotal bool `json:"only_total"`
Id string `json:"id,omitempty"`
Order string `json:"order,omitempty"`
Dir string `json:"dir,omitempty"`
Metadata Metadata `json:"metadata,omitempty"`
Domain string `json:"domain,omitempty"`
Tag string `json:"tag,omitempty"`
Permission string `json:"permission,omitempty"`
Status Status `json:"status,omitempty"`
IDs []string `json:"ids,omitempty"`
Role Role `json:"-"`
ListPerms bool `json:"-"`
Username string `json:"username,omitempty"`
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
Email string `json:"email,omitempty"`
Verified bool `json:"verified,omitempty"`
}
Page contains page metadata that helps navigation.
type Repository ¶
type Repository interface {
// RetrieveByID retrieves user by their unique ID.
RetrieveByID(ctx context.Context, id string) (User, error)
// RetrieveAll retrieves all users.
RetrieveAll(ctx context.Context, pm Page) (UsersPage, error)
// RetrieveByEmail retrieves user by its unique credentials.
RetrieveByEmail(ctx context.Context, email string) (User, error)
// RetrieveByUsername retrieves user by its unique credentials.
RetrieveByUsername(ctx context.Context, username string) (User, error)
// Update updates the user name and metadata.
Update(ctx context.Context, id string, user UserReq) (User, error)
// UpdateUsername updates the User's names.
UpdateUsername(ctx context.Context, user User) (User, error)
// UpdateSecret updates secret for user with given email.
UpdateSecret(ctx context.Context, user User) (User, error)
// UpdateEmail updates email for user with given id.
UpdateEmail(ctx context.Context, user User) (User, error)
// UpdateRole updates role for user with given id.
UpdateRole(ctx context.Context, user User) (User, error)
// UpdateVerifiedAt updates the verified time for user with given id.
UpdateVerifiedAt(ctx context.Context, user User) (User, error)
// ChangeStatus changes user status to enabled or disabled
ChangeStatus(ctx context.Context, user User) (User, error)
// Delete deletes user with given id
Delete(ctx context.Context, id string) error
// Searchusers retrieves users based on search criteria.
SearchUsers(ctx context.Context, pm Page) (UsersPage, error)
// RetrieveAllByIDs retrieves for given user IDs .
RetrieveAllByIDs(ctx context.Context, pm Page) (UsersPage, error)
CheckSuperAdmin(ctx context.Context, adminID string) error
// Save persists the user account. A non-nil error is returned to indicate
// operation failure.
Save(ctx context.Context, user User) (User, error)
// AddUserVerification adds new verification for given user id and email
AddUserVerification(ctx context.Context, uv UserVerification) error
// RetrieveVerificationToken retrieves verification token of given user id and email.
RetrieveUserVerification(ctx context.Context, userID, email string) (UserVerification, error)
// UpdateUserVerificationDetails update verification details for the given user id and email.
UpdateUserVerification(ctx context.Context, uv UserVerification) error
}
type Role ¶
type Role uint8
Role represents User role.
const ( UserRole Role = iota AdminRole // AllRole is used for querying purposes to list users irrespective // of their role - both admin and user. It is never stored in the // database as the actual user role and should always be the largest // value in this enumeration. AllRole )
Possible User role values.
func (Role) MarshalJSON ¶
func (*Role) UnmarshalJSON ¶
type Service ¶
type Service interface {
// Register creates new user. In case of the failed registration, a
// non-nil error value is returned.
Register(ctx context.Context, session authn.Session, user User, selfRegister bool) (User, error)
// SendVerification sends a verification email to the user.
SendVerification(ctx context.Context, session authn.Session) error
// VerifyEmail verifies user's email using the verification token.
VerifyEmail(ctx context.Context, verificationToken string) (User, error)
// View retrieves user info for a given user ID and an authorized token.
View(ctx context.Context, session authn.Session, id string) (User, error)
// ViewProfile retrieves user info for a given token.
ViewProfile(ctx context.Context, session authn.Session) (User, error)
// ListUsers retrieves users list for a valid auth token.
ListUsers(ctx context.Context, session authn.Session, pm Page) (UsersPage, error)
// SearchUsers searches for users with provided filters for a valid auth token.
SearchUsers(ctx context.Context, pm Page) (UsersPage, error)
// Update updates the user's name and metadata.
Update(ctx context.Context, session authn.Session, id string, user UserReq) (User, error)
// UpdateTags updates the user's tags.
UpdateTags(ctx context.Context, session authn.Session, id string, user UserReq) (User, error)
// UpdateEmail updates the user's email.
UpdateEmail(ctx context.Context, session authn.Session, id, email string) (User, error)
// UpdateUsername updates the user's username.
UpdateUsername(ctx context.Context, session authn.Session, id, username string) (User, error)
// UpdateProfilePicture updates the user's profile picture.
UpdateProfilePicture(ctx context.Context, session authn.Session, id string, usr UserReq) (User, error)
// SendPasswordReset generates reset password link and sends it to the user via email.
SendPasswordReset(ctx context.Context, email string) error
// UpdateSecret updates the user's secret.
UpdateSecret(ctx context.Context, session authn.Session, oldSecret, newSecret string) (User, error)
// ResetSecret change users secret in reset flow.
// token can be authentication token or secret reset token.
ResetSecret(ctx context.Context, session authn.Session, secret string) error
// UpdateRole updates the user's Role.
UpdateRole(ctx context.Context, session authn.Session, user User) (User, error)
// Enable logically enables the user identified with the provided ID.
Enable(ctx context.Context, session authn.Session, id string) (User, error)
// Disable logically disables the user identified with the provided ID.
Disable(ctx context.Context, session authn.Session, id string) (User, error)
// Delete deletes user with given ID.
Delete(ctx context.Context, session authn.Session, id string) error
// Identify returns the user id from the given token.
Identify(ctx context.Context, session authn.Session) (string, error)
// IssueToken issues a new access and refresh token when provided with either a username or email.
IssueToken(ctx context.Context, identity, secret string) (*grpcTokenV1.Token, error)
// RefreshToken refreshes expired access tokens.
// After an access token expires, the refresh token is used to get
// a new pair of access and refresh tokens.
RefreshToken(ctx context.Context, session authn.Session, refreshToken string) (*grpcTokenV1.Token, error)
// OAuthCallback handles the callback from any supported OAuth provider.
// It processes the OAuth tokens and either signs in or signs up the user based on the provided state.
OAuthCallback(ctx context.Context, user User) (User, error)
// OAuthAddUserPolicy adds a policy to the user for an OAuth request.
OAuthAddUserPolicy(ctx context.Context, user User) error
}
Service specifies an API that must be fullfiled by the domain service implementation, and all of its decorators (e.g. logging & metrics).
func NewService ¶
func NewService(token grpcTokenV1.TokenServiceClient, urepo Repository, policyService policies.Service, emailer Emailer, hasher Hasher, idp supermq.IDProvider) Service
NewService returns a new Users service implementation.
type Status ¶
type Status uint8
Status represents User status.
const ( // EnabledStatus represents enabled User. EnabledStatus Status = iota // DisabledStatus represents disabled User. DisabledStatus // DeletedStatus represents a user that will be deleted. DeletedStatus // AllStatus is used for querying purposes to list users irrespective // of their status - both enabled and disabled. It is never stored in the // database as the actual User status and should always be the largest // value in this enumeration. AllStatus )
Possible User status values.
func (Status) MarshalJSON ¶
Custom Marshaller for Uesr/Groups.
func (*Status) UnmarshalJSON ¶
Custom Unmarshaler for User/Groups.
type User ¶
type User struct {
ID string `json:"id"`
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
Tags []string `json:"tags,omitempty"`
Metadata Metadata `json:"metadata,omitempty"`
Status Status `json:"status"` // 0 for enabled, 1 for disabled
Role Role `json:"role"` // 0 for normal user, 1 for admin
ProfilePicture string `json:"profile_picture,omitempty"` // profile picture URL
Credentials Credentials `json:"credentials,omitempty"`
Permissions []string `json:"permissions,omitempty"`
Email string `json:"email,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
UpdatedBy string `json:"updated_by,omitempty"`
VerifiedAt time.Time `json:"verified_at,omitempty"`
AuthProvider string `json:"auth_provider,omitempty"`
}
type UserRepository ¶
UserRepository struct implements the Repository interface.
type UserReq ¶ added in v0.17.0
type UserReq struct {
FirstName *string `json:"first_name,omitempty"`
LastName *string `json:"last_name,omitempty"`
Metadata *Metadata `json:"metadata,omitempty"`
Tags *[]string `json:"tags,omitempty"`
ProfilePicture *string `json:"profile_picture,omitempty"`
UpdatedBy *string `json:"updated_by,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
}
type UserVerification ¶ added in v0.18.1
type UserVerification struct {
UserID string `json:"user_id"`
Email string `json:"email"`
OTP string `json:"otp"`
CreatedAt time.Time `json:"-"`
ExpiresAt time.Time `json:"-"`
UsedAt time.Time `json:"-"`
}
UserVerification OTP is sent to the user's email as base64 encoded with UserID, Email and OTP. It should not be exposed via API.
func NewUserVerification ¶ added in v0.18.1
func NewUserVerification(userID, email string) (UserVerification, error)
func (*UserVerification) Decode ¶ added in v0.18.1
func (u *UserVerification) Decode(data string) error
func (UserVerification) Encode ¶ added in v0.18.1
func (u UserVerification) Encode() (string, error)
func (UserVerification) Match ¶ added in v0.18.1
func (u UserVerification) Match(ruv UserVerification) error
func (UserVerification) Valid ¶ added in v0.18.1
func (u UserVerification) Valid() error
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package api contains API-related concerns: endpoint definitions, middlewares and all resource representations.
|
Package api contains API-related concerns: endpoint definitions, middlewares and all resource representations. |
|
grpc
Package grpc contains implementation of Users service gRPC API.
|
Package grpc contains implementation of Users service gRPC API. |
|
Package emailer contains the domain concept definitions needed to support SuperMQ users email service functionality.
|
Package emailer contains the domain concept definitions needed to support SuperMQ users email service functionality. |
|
Package events provides the domain concept definitions needed to support SuperMQ users service functionality.
|
Package events provides the domain concept definitions needed to support SuperMQ users service functionality. |
|
Package hasher contains the domain concept definitions needed to support SuperMQ users password hasher sub-service functionality.
|
Package hasher contains the domain concept definitions needed to support SuperMQ users password hasher sub-service functionality. |
|
Package middleware provides authorization, logging, metrics and tracing middleware for SuperMQ Users Service.
|
Package middleware provides authorization, logging, metrics and tracing middleware for SuperMQ Users Service. |
|
Package mocks contains mocks for testing purposes.
|
Package mocks contains mocks for testing purposes. |
|
Package postgres contains the database implementation of users repository layer.
|
Package postgres contains the database implementation of users repository layer. |