auth

package
v0.9.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 30, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	APIKeyStatusActive  = APIKeyStatus("active")
	APIKeyStatusRevoked = APIKeyStatus("revoked")

	APIKeyScopeAll = APIKeyScope("all")
)
View Source
const (
	ApplicationStatusActive   = ApplicationStatus("active")
	ApplicationStatusInactive = ApplicationStatus("inactive")
)
View Source
const (
	UserStatusActive   = UserStatus("active")
	UserStatusInactive = UserStatus("inactive")
)

Variables

This section is empty.

Functions

func ValidateActivateApplicationRequest

func ValidateActivateApplicationRequest(req ActivateApplicationRequest) error

func ValidateActivateUserRequest

func ValidateActivateUserRequest(req ActivateUserRequest) error

func ValidateAuthenticateUserRequest

func ValidateAuthenticateUserRequest(req AuthenticateUserRequest) error

func ValidateChangePasswordRequest

func ValidateChangePasswordRequest(req ChangePasswordRequest) error

func ValidateCreateAPIKeyRequest

func ValidateCreateAPIKeyRequest(req CreateAPIKeyRequest) error

func ValidateCreateApplicationRequest

func ValidateCreateApplicationRequest(req CreateApplicationRequest) error

func ValidateCreateUserRequest

func ValidateCreateUserRequest(req CreateUserRequest) error

func ValidateListUserRequest

func ValidateListUserRequest(req ListUserRequest) error

func ValidateResetPasswordRequest

func ValidateResetPasswordRequest(req ResetPasswordRequest) error

func ValidateRevokeAPIKeyRequest

func ValidateRevokeAPIKeyRequest(req RevokeAPIKeyRequest) error

func ValidateUpdateApplicationRequest

func ValidateUpdateApplicationRequest(req UpdateApplicationRequest) error

func ValidateUpdateUserRequest

func ValidateUpdateUserRequest(req UpdateUserRequest) error

func VerifyAPIKeyString

func VerifyAPIKeyString(ks APIKeyString, hashedKs APIKeyHashedString) error

func VerifyUserPassword

func VerifyUserPassword(password RawPassword, hashedPassword HashedPassword) error

Types

type APIKey

type APIKey struct {
	ID            string             `json:"id"`
	HashString    APIKeyHashedString `json:"hash_string"`
	Version       int                `json:"version"`
	ApplicationID string             `json:"application_id"`
	Scopes        []APIKeyScope      `json:"scopes"`
	Status        APIKeyStatus       `json:"status"`

	CreatedAt int64  `json:"created_at"` // Unix Time (in second)
	CreatedBy string `json:"created_by"`
	UpdatedAt int64  `json:"updated_at"` // Unix Time (in second)
	UpdatedBy string `json:"updated_by"`
}

type APIKeyAuthenticator

type APIKeyAuthenticator interface {
	CreateAPIKey(
		ctx context.Context,
		ts int64,
		request CreateAPIKeyRequest,
	) (APIKey, APIKeyString, error)

	// RevokeAPIKey revokes the API key with the given ID.
	// The error can be ErrAPIKeyNotFound and others.
	RevokeAPIKey(
		ctx context.Context,
		ts int64,
		request RevokeAPIKeyRequest,
	) error

	// Authenticate authenticates the given API key string. It returns the API key if the authentication is successful.
	// The error can be ErrAPIKeyNotFound, ErrMismatchAPIKey, ErrRevokedAPIKey and others.
	Authenticate(ctx context.Context, key APIKeyString) (APIKey, error)

	ListAPIKeys(ctx context.Context, req ListAPIKeysRequest) (ListAPIKeysResult, error)
}

APIKeyAuthenticator is the interface that wraps the basic API key authentication methods and other management methods.

func NewAPIKeyAuthenticator

func NewAPIKeyAuthenticator(storage APIKeyStorage) APIKeyAuthenticator

type APIKeyHashedString

type APIKeyHashedString string

APIKeyHashedString is the hashed string representation of an API key. It is stored in the database. BU server is not able to recover the original APIKeyString from this.

type APIKeyScope

type APIKeyScope string

type APIKeyStatus

type APIKeyStatus string

type APIKeyStorage

type APIKeyStorage interface {
	CreateTx(ctx context.Context, options ...storage.CreateTxOption) (storage.Tx, context.Context, error)
	StoreAPIKey(ctx context.Context, tx storage.Tx, key APIKey) error
	GetAPIKey(ctx context.Context, tx storage.Tx, id string) (ListAPIKeyRecord, error)
	ListAPIKeys(ctx context.Context, tx storage.Tx, req ListAPIKeysRequest) (ListAPIKeysResult, error)
}

APIKeyStorage is the interface that APIKeyAuthenticator relies on to persist the API key data.

type APIKeyString

type APIKeyString string

APIKeyString is the string representation of an API key. The BU server client has to provide this string to the BU server to authenticate itself. The format of APIKeyString is [ID]:[SECRET].

func NewAPIKeyString

func NewAPIKeyString() (APIKeyString, error)

func (APIKeyString) Hash

func (ks APIKeyString) Hash() (APIKeyHashedString, error)

func (APIKeyString) ID

func (ks APIKeyString) ID() (string, error)

type ActivateApplicationRequest

type ActivateApplicationRequest struct {
	RequestUser
	ApplicationID string `json:"application_id"` // Unique identifier of the application.
}

type ActivateUserRequest

type ActivateUserRequest struct {
	RequestUser string `json:"request_user"`
	UserID      string `json:"user_id"`
	Username    string `json:"username"`
}

type Application

type Application struct {
	ID      string            `json:"id"`      // Unique identifier of the application.
	Version int64             `json:"version"` // Version number of the application.
	Status  ApplicationStatus `json:"status"`  // Status of the application.

	CreatedAt int64  `json:"created_at"` // Unix Time (in second) when the application was created.
	CreatedBy string `json:"created_by"` // User who created the application.
	UpdatedAt int64  `json:"updated_at"` // Unix Time (in second) when the application was last updated.
	UpdatedBy string `json:"updated_by"` // User who last updated the application.

	Name         string   `json:"name"`          // Name of the application.
	CompanyName  string   `json:"company_name"`  // Name of the company associated with the application.
	Addresses    []string `json:"addresses"`     // List of addresses associated with the application.
	Emails       []string `json:"emails"`        // List of emails associated with the application.
	PhoneNumbers []string `json:"phone_numbers"` // List of phone numbers associated with the application.
}

Application represents an application. It's the client of BU server.

type ApplicationManager

type ApplicationManager interface {
	CreateApplication(ctx context.Context, ts int64, req CreateApplicationRequest) (Application, error)
	ListApplications(ctx context.Context, req ListApplicationRequest) (ListApplicationResult, error)
	UpdateApplication(ctx context.Context, ts int64, req UpdateApplicationRequest) (Application, error)
	ActivateApplication(ctx context.Context, ts int64, req ActivateApplicationRequest) (Application, error)
	DeactivateApplication(ctx context.Context, ts int64, req DeactivateApplicationRequest) (Application, error)
}

func NewApplicationManager

func NewApplicationManager(s ApplicationStorage) ApplicationManager

type ApplicationStatus

type ApplicationStatus string

ApplicationStatus represents the status of an application.

type ApplicationStorage

type ApplicationStorage interface {
	CreateTx(ctx context.Context, options ...storage.CreateTxOption) (storage.Tx, context.Context, error)
	StoreApplication(ctx context.Context, tx storage.Tx, app Application) error
	ListApplication(ctx context.Context, tx storage.Tx, req ListApplicationRequest) (ListApplicationResult, error)
}

ApplicationStorage represents a storage interface for managing applications.

type AuthenticateUserRequest

type AuthenticateUserRequest struct {
	Username string      `json:"username"`
	Password RawPassword `json:"password"`
}

type ChangePasswordRequest

type ChangePasswordRequest struct {
	UserID      string      `json:"user_id"`
	Username    string      `json:"username"`
	OldPassword RawPassword `json:"old_password"`
	Password    RawPassword `json:"password"`
}

type CreateAPIKeyRequest

type CreateAPIKeyRequest struct {
	RequestUser
	ApplicationID string        `json:"application_id"`
	Scopes        []APIKeyScope `json:"scopes"`
}

type CreateApplicationRequest

type CreateApplicationRequest struct {
	RequestUser

	Name         string   `json:"name"`          // Name of the application.
	CompanyName  string   `json:"company_name"`  // Name of the company associated with the application.
	Addresses    []string `json:"addresses"`     // List of addresses associated with the application.
	Emails       []string `json:"emails"`        // List of emails associated with the application.
	PhoneNumbers []string `json:"phone_numbers"` // List of phone numbers associated with the application.
}

type CreateUserRequest

type CreateUserRequest struct {
	RequestUser string      `json:"request_user"`
	Username    string      `json:"username"`
	Password    RawPassword `json:"password"`
	Name        string      `json:"name"`
	Emails      []string    `json:"emails"`
	Note        string      `json:"note"`
}

type DeactivateApplicationRequest

type DeactivateApplicationRequest ActivateApplicationRequest

type HashedPassword

type HashedPassword string

type ListAPIKeyRecord

type ListAPIKeyRecord struct {
	APIKey      APIKey      `json:"api_key"`
	Application Application `json:"application"`
}

type ListAPIKeysRequest

type ListAPIKeysRequest struct {
	Offset int
	Limit  int

	ApplicationIDs []string       // Filter by application ID.
	Statuses       []APIKeyStatus // Filter by status.
}

type ListAPIKeysResult

type ListAPIKeysResult struct {
	Total int                `json:"total"`
	Keys  []ListAPIKeyRecord `json:"keys"`
}

type ListApplicationRequest

type ListApplicationRequest struct {
	Offset int // Offset for pagination.
	Limit  int // Limit for pagination.

	IDs      []string            // Filter by application ID.
	Statuses []ApplicationStatus // Filter by status.
}

type ListApplicationResult

type ListApplicationResult struct {
	Total        int           `json:"total"` // Total number of applications.
	Applications []Application `json:"apps"`  // List of Applications.
}

type ListUserRequest

type ListUserRequest struct {
	Offset int `json:"offset"` // Offset for pagination.
	Limit  int `json:"limit"`  // Limit for pagination.

	IDs       []string `json:"ids"`       // Filter by user ID.
	Usernames []string `json:"usernames"` // Filter by username.
}

type ListUserResult

type ListUserResult struct {
	Total int64  `json:"total"`
	Users []User `json:"users"`
}

type RawPassword

type RawPassword string

type RequestUser

type RequestUser struct {
	User string `json:"user"` // User who makes the request.
}

type ResetPasswordRequest

type ResetPasswordRequest struct {
	RequestUser string      `json:"request_user"`
	UserID      string      `json:"user_id"`
	Username    string      `json:"username"`
	Password    RawPassword `json:"password"`
}

type RevokeAPIKeyRequest

type RevokeAPIKeyRequest struct {
	RequestUser
	ApplicationID string `json:"application_id"`
	ID            string `json:"id"`
}

type UpdateApplicationRequest

type UpdateApplicationRequest struct {
	CreateApplicationRequest

	ID string `json:"id"` // Unique identifier of the application.
}

type UpdateUserRequest

type UpdateUserRequest struct {
	RequestUser string   `json:"request_user"`
	UserID      string   `json:"user_id"`
	Username    string   `json:"username"`
	Name        string   `json:"name"`
	Emails      []string `json:"emails"`
	Note        string   `json:"note"`
}

type User

type User struct {
	ID       string         `json:"id"`
	Username string         `json:"username"`
	Status   UserStatus     `json:"status"`
	Version  int64          `json:"version"`
	Password HashedPassword `json:"password"`
	Name     string         `json:"name"`
	Emails   []string       `json:"emails"`
	Note     string         `json:"note"`

	CreatedAt int64  `json:"created_at"`
	CreatedBy string `json:"created_by"`
	UpdatedAt int64  `json:"updated_at"`
	UpdatedBy string `json:"updated_by"`
}

type UserManager

type UserManager interface {
	CreateUser(ctx context.Context, ts int64, req CreateUserRequest) (User, error)
	ChangePassword(ctx context.Context, ts int64, req ChangePasswordRequest) (User, error)
	ResetPassword(ctx context.Context, ts int64, req ResetPasswordRequest) (User, error)
	UpdateUser(ctx context.Context, ts int64, req UpdateUserRequest) (User, error)
	ActivateUser(ctx context.Context, ts int64, req ActivateUserRequest) (User, error)
	DeactivateUser(ctx context.Context, ts int64, req ActivateUserRequest) (User, error)
	Authenticate(ctx context.Context, ts int64, req AuthenticateUserRequest) (UserToken, error)
	ListUsers(ctx context.Context, req ListUserRequest) (ListUserResult, error)

	TokenAuthorization(ctx context.Context, ts int64, token string) (UserToken, error)
}

func NewUserManager

func NewUserManager(s UserStorage) UserManager

type UserStatus

type UserStatus string

type UserStorage

type UserStorage interface {
	CreateTx(ctx context.Context, options ...storage.CreateTxOption) (storage.Tx, context.Context, error)
	StoreUser(ctx context.Context, tx storage.Tx, user User) error
	ListUsers(ctx context.Context, tx storage.Tx, req ListUserRequest) (ListUserResult, error)
	StoreUserToken(ctx context.Context, tx storage.Tx, token UserToken) error
	GetUserToken(ctx context.Context, tx storage.Tx, token string) (UserToken, error)

	// This function should be called periodically to prevent the database from growing too large due to expired tokens.
	RemoveUserTokenByExpiredAt(ctx context.Context, tx storage.Tx, expiredAt int64) error
}

type UserToken

type UserToken struct {
	Token     string `json:"token"`
	UserID    string `json:"user_id"`
	CreatedAt int64  `json:"created_at"`
	ExpiredAt int64  `json:"expired_at"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL