auth

package
v1.1.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: MIT Imports: 36 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RoleAdmin  = "admin"
	RoleClient = "client"
	RoleGuest  = "guest"
)

Variables

View Source
var (
	TwoFactorIssuer = "Sonar"
)

Functions

func AdminDeleteAllSessions

func AdminDeleteAllSessions(ctx context.Context, _ *rpc.JsonRpcRequest) (any, *rpc.JsonRpcError)

func AdminDeleteSession

func AdminDeleteSession(ctx context.Context, req *rpc.JsonRpcRequest) (any, *rpc.JsonRpcError)

func AdminGetOidc

func AdminGetOidc(_ context.Context, req *rpc.JsonRpcRequest) (any, *rpc.JsonRpcError)

func AdminGetSessions

func AdminGetSessions(ctx context.Context, _ *rpc.JsonRpcRequest) (any, *rpc.JsonRpcError)

func AdminSetOidc

func AdminSetOidc(_ context.Context, req *rpc.JsonRpcRequest) (any, *rpc.JsonRpcError)

func BindingExternalAccount

func BindingExternalAccount(c *gin.Context)

func CheckPassword

func CheckPassword(username, passwd string) (uuid string, success bool)

CheckPassword 检查密码是否正确

如果密码正确,返回用户的 UUID 和 true;否则返回空字符串和 false。 兼容迁移前的固定盐 SHA-256 哈希:验证通过后会静默把该账号的哈希升级为 bcrypt,之后的校验都走 bcrypt,不需要用户重新设置密码。

func CreateAccount

func CreateAccount(username, passwd string) (user models.User, err error)

func CreateAccountWithDB

func CreateAccountWithDB(db *gorm.DB, username, passwd string) (user models.User, err error)

func CreateSession

func CreateSession(uuid string, expires int, userAgent, ip, loginMethod string) (string, error)

CreateSession 创建新会话

func DeleteAccountByUsername

func DeleteAccountByUsername(username string) (err error)

func DeleteAccountByUsernameWithDB

func DeleteAccountByUsernameWithDB(db *gorm.DB, username string) (err error)

func DeleteAllSessions

func DeleteAllSessions() error

func DeleteSession

func DeleteSession(session string) (err error)

DeleteSession 删除指定会话

func Disable2FA

func Disable2FA(c *gin.Context)

func Enable2FA

func Enable2FA(c *gin.Context)

func ForceDisableAllTwoFactor

func ForceDisableAllTwoFactor() error

ForceDisableAllTwoFactor 强制关闭所有用户的 2FA

func ForceResetPassword

func ForceResetPassword(username, passwd string) (err error)

ForceResetPassword 强制重置用户密码

func Generate2FA

func Generate2FA(c *gin.Context)

func GetAllSessions

func GetAllSessions() (sessions []models.Session, err error)

GetAllSessions 获取所有会话

func GetCallbackURL

func GetCallbackURL(c *gin.Context) string

GetCallbackURL 构造 OIDC provider 回调地址(固定路径 /api/oauth_callback)。

func GetFirstUser

func GetFirstUser() (user models.User, err error)

GetFirstUser 获取库中第一个用户账号(本项目目前仅支持单一管理员账号)

func GetPrincipal

func GetPrincipal(c *gin.Context) *rpc.Principal

GetPrincipal 读取已识别的主体;若中间件未运行则返回 nil。

func GetRole

func GetRole(c *gin.Context) string

GetRole 获取当前请求的角色

func GetSession

func GetSession(session string) (uuid string, err error)

GetSession 根据会话 ID 获取 UUID

func GetUserBySSO

func GetUserBySSO(ssoID string) (user models.User, err error)

通过 SSO 信息获取用户

func GetUserBySession

func GetUserBySession(session string) (models.User, error)

func GetUserByUUID

func GetUserByUUID(uuid string) (user models.User, err error)

func IdentifyPrincipal

func IdentifyPrincipal(c *gin.Context) *rpc.Principal

IdentifyPrincipal 识别当前请求的调用主体。不写入任何状态,可安全多次调用。 识别优先级与历史 IdentityMiddleware 一致:API Key > Session > Client Token > 匿名。

func IdentityMiddleware

func IdentityMiddleware() gin.HandlerFunc

IdentityMiddleware 统一身份识别中间件,在路由栈最外层运行。 负责识别当前请求者身份(Admin / Client / Guest),并写入 Context。 身份识别统一委托给 IdentifyPrincipal;同时保留旧的 c.Set 键(role/uuid/ api_key/session/client_uuid)以兼容现有 handler 与中间件。

func Login

func Login(c *gin.Context)

func Logout

func Logout(c *gin.Context)

func OAuth

func OAuth(c *gin.Context)

/api/oauth

func OAuthCallback

func OAuthCallback(c *gin.Context)

/api/oauth_callback

func PrivateSiteMiddleware

func PrivateSiteMiddleware() gin.HandlerFunc

PrivateSiteMiddleware 私有站点访问控制。 依赖 IdentityMiddleware 已设置的 role,对未认证的访客在私有站点模式下进行拦截。

func RemoveExpiredSessions

func RemoveExpiredSessions() error

func RequireRole

func RequireRole(allowedRoles ...string) gin.HandlerFunc

RequireRole 声明式权限校验中间件,仅允许指定角色通过。

func RequireSensitive2FA

func RequireSensitive2FA() gin.HandlerFunc

func SetPrincipal

func SetPrincipal(c *gin.Context, p *rpc.Principal)

SetPrincipal 将已识别的主体写入 gin.Context,供下游(RPC 边界等)复用。

func UnbindExternalAccount

func UnbindExternalAccount(c *gin.Context)

func UpdateLatest

func UpdateLatest(session, useragent, ip string) error

func UpdateUser

func UpdateUser(c *gin.Context)

func VerifySensitive2FA

func VerifySensitive2FA(c *gin.Context) error

VerifySensitive2FA gin 适配层:从 gin.Context 提取参数后委托核心校验。

func VerifySensitive2FACore

func VerifySensitive2FACore(userUUID, code string, isAPIKey bool) error

VerifySensitive2FACore 传输无关的 2FA 校验核心。 输入原始值:userUUID、2FA code、是否为 API Key。 API Key 豁免;未启用 2FA 的用户放行;其余需要有效 code。

Types

type LoginRequest

type LoginRequest struct {
	Username string `json:"username"`
	Password string `json:"password"`
	TwoFa    string `json:"2fa_code"`
}

Directories

Path Synopsis
qq

Jump to

Keyboard shortcuts

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