Documentation
¶
Index ¶
- Constants
- Variables
- func IsLoginInCtx(ctx scene.Context) (string, bool)
- func SetAuthContext(ctx scene.Context, userID string)
- type AccessToken
- type AuthContext
- type ExternalAccount
- type HTTPLoginStatusVerifier
- type IAccessTokenRepository
- type IAccessTokenService
- type IAuthenticationRepository
- type IAuthenticationService
- type LoginStatus
- type User
Constants ¶
View Source
const Lens scene.ModuleName = "authentication"
Variables ¶
View Source
var ( ErrAuthenticationFailed = _eg.CreateError(0, "authentication failed") ErrUserAlreadyExists = _eg.CreateError(1, "user already exists") ErrUserNotFound = _eg.CreateError(2, "user not found") ErrNotLogin = _eg.CreateError(3, "not login") ErrTokenNotFound = _eg.CreateError(4, "token not found") ErrFailToAddUser = _eg.CreateError(5, "fail to add user") ErrInternalError = _eg.CreateError(6, "internal error") ErrTokenExpired = _eg.CreateError(7, "token expired") ErrFailToGetToken = _eg.CreateError(8, "fail to get token") )
View Source
var ( PermAdmin = permission.Create("authentication:admin") PermTokenCreate = permission.Create("authentication:token:create") PermTokenList = permission.Create("authentication:token:list") PermTokenDelete = permission.Create("authentication:token:delete") PermUserManage = permission.Create("authentication:user:manage") )
Functions ¶
func IsLoginInCtx ¶ added in v0.3.0
IsLoginInCtx return userId and if user has logged in
func SetAuthContext ¶ added in v0.2.8
Types ¶
type AccessToken ¶ added in v0.2.10
type AccessToken struct {
// Token 是唯一的令牌字符串,建议作为数据库主键
Token string `json:"token" bson:"_id" gorm:"primaryKey"`
// UserID 是此令牌所属用户的ID
UserID string `json:"user_id" bson:"user_id"`
// Name 是用户为令牌设置的友好名称,方便管理(例如 "My CLI Token")
Name string `json:"name" bson:"name"`
// CreatedAt 是令牌的创建时间
CreatedAt int64 `json:"created_at" bson:"created_at"`
// ExpiredAt 是过期时间 -1 代表永不过期
ExpireAt int64 `json:"expire_at" bson:"expire_at"`
}
type AuthContext ¶
type AuthContext struct {
UserID string
}
func GetAuthContext ¶ added in v0.2.8
func GetAuthContext(ctx scene.Context) (AuthContext, bool)
func NewAuthContext ¶
func NewAuthContext(userID string) AuthContext
func (*AuthContext) IsLogin ¶
func (c *AuthContext) IsLogin() bool
type ExternalAccount ¶ added in v0.2.10
type ExternalAccount struct {
// UserID is the unique identifier for external user_id
UserID string `json:"user_id" bson:"user_id"`
// Token is the access token, external service should find a way to
// marshal their token into a single string.
Token string `json:"token" bson:"token"`
// ExternalID is the platform name for external platform
ExternalID string `json:"external_id" bson:"external_id"`
// SceneID is the user id used in authentication module
SceneID string `json:"scene_id" bson:"scene_id"`
}
type HTTPLoginStatusVerifier ¶
type HTTPLoginStatusVerifier interface {
scene.Service
Verify(request *http.Request) (status LoginStatus, err error)
Login(userId string, resp http.ResponseWriter) (status LoginStatus, err error)
Logout(resp http.ResponseWriter) (err error)
}
type IAccessTokenRepository ¶ added in v0.2.10
type IAccessTokenRepository interface {
scene.Named
// CreateToken 创建并存储一个新的 AccessToken
CreateToken(token AccessToken) (AccessToken, error)
// GetTokenByValue 通过令牌字符串查找 AccessToken
GetTokenByValue(token string) (AccessToken, error)
// ListTokensByUser 分页列出某个用户的所有 AccessToken
ListTokensByUser(userId string, offset, limit int64) (model.PaginationResult[AccessToken], error)
// ListTokens 分页列出系统中的所有 AccessToken
ListTokens(offset, limit int64) (model.PaginationResult[AccessToken], error)
// DeleteToken 删除一个 AccessToken
DeleteToken(token string) error
}
IAccessTokenRepository 定义了 AccessToken 的持久化存储接口
type IAccessTokenService ¶ added in v0.2.10
type IAccessTokenService interface {
scene.Service
// Create a new token for user
Create(userId, name string, expireAt int64) (AccessToken, error)
// ListByUser 分页列出某个用户的所有 AccessToken
ListByUser(userId string, offset, limit int64) (model.PaginationResult[AccessToken], error)
// List 分页列出系统中的所有 AccessToken
List(offset, limit int64) (model.PaginationResult[AccessToken], error)
// Validate token and return user ID
Validate(token string) (userId string, valid bool, err error)
Delete(token string) error
}
type IAuthenticationRepository ¶ added in v0.2.10
type IAuthenticationRepository interface {
scene.Named
Authenticate(username string, password string) (userID string, err error)
UserById(userId string) (User, error)
UserByName(username string) (User, error)
UserByEmail(email string) (User, error)
AddUser(user User) (User, error)
DeleteUser(userId string) error
UpdateUser(user User) error
ListUsers(offset, limit int64) (model.PaginationResult[User], error)
}
type IAuthenticationService ¶ added in v0.2.10
type IAuthenticationService interface {
scene.Service
AddUser(username, password string) (User, error)
DeleteUser(userId string) error
UpdateUser(user User) error
Authenticate(username string, password string) (userID string, err error)
AuthenticateByToken(token string) (userID string, err error)
HasUser(userId string) (bool, error)
UserById(userId string) (User, error)
UserByName(username string) (User, error)
UserByEmail(email string) (User, error)
ListUsers(offset, limit int64) (model.PaginationResult[User], error)
}
type LoginStatus ¶
type User ¶
type User struct {
// UserID unique id. should be generated by server.
UserID string `json:"user_id" bson:"user_id" gorm:"primaryKey"`
// Username unique name. should be unique. should be input by user. but can be changed
Username string `json:"username" bson:"username" gorm:"uniqueIndex"`
// Password just password. for now, store in plain text
Password string `json:"password" bson:"password"`
// Email user's email address
Email string `json:"email" bson:"email"`
// DisplayName extra information, can be empty
DisplayName string `json:"display_name" bson:"display_name"`
// Avatar extra information, can be empty
Avatar string `json:"avatar" bson:"avatar"`
// Timezone extra information, can be empty
Timezone string `json:"timezone" bson:"timezone"`
}
Source Files
¶
Click to show internal directories.
Click to hide internal directories.