oauth

package
v1.2.5 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	UserNameKey = "username"
	UserIDKey   = "user_id"
	UserObjKey  = "user_obj"
)
View Source
const (
	OAuthStateCacheKeyFormat     = "oauth:state:%s"
	OAuthStateCacheKeyExpiration = 10 * time.Minute
	UserAllBadges                = "user:badges"
)
View Source
const (
	BaseUserScore = 100
	MaxUserScore  = 100
	MinUserScore  = -100
)
View Source
const (
	UnAuthorized  = "未登录"
	InvalidState  = "非法登录请求"
	BannedAccount = "账号已被封禁"
)

Variables

This section is empty.

Functions

func Callback

func Callback(c *gin.Context)

Callback godoc @Tags oauth @Param request body CallbackRequest true "request body" @Produce json @Success 200 {object} CallbackResponse @Router /api/v1/oauth/callback [post]

func GetLoginURL

func GetLoginURL(c *gin.Context)

GetLoginURL godoc @Tags oauth @Produce json @Success 200 {object} GetLoginURLResponse @Router /api/v1/oauth/login [get]

func GetUserIDFromContext

func GetUserIDFromContext(c *gin.Context) uint64

func GetUserIDFromSession

func GetUserIDFromSession(s sessions.Session) uint64

func HandleUpdateAllBadges

func HandleUpdateAllBadges(ctx context.Context, t *asynq.Task) error

HandleUpdateAllBadges 处理更新所有徽章

func HandleUpdateSingleUserBadgeScore

func HandleUpdateSingleUserBadgeScore(ctx context.Context, t *asynq.Task) error

HandleUpdateSingleUserBadgeScore 处理单个用户徽章分数更新任务

func HandleUpdateUserBadgeScores

func HandleUpdateUserBadgeScores(ctx context.Context, t *asynq.Task) error

HandleUpdateUserBadgeScores 处理所有用户徽章分数更新任务

func LoginRequired

func LoginRequired() gin.HandlerFunc

func Logout

func Logout(c *gin.Context)

Logout godoc @Tags oauth @Produce json @Success 200 {object} LogoutResponse @Router /api/v1/oauth/logout [get]

func SetUserToContext

func SetUserToContext(c *gin.Context, user *User)

SetUserToContext 将User对象存储到Context中

func UserInfo

func UserInfo(c *gin.Context)

UserInfo godoc @Tags oauth @Produce json @Success 200 {object} UserInfoResponse @Router /api/v1/oauth/user-info [get]

Types

type Badge

type Badge struct {
	ID          int    `json:"id"`
	Name        string `json:"name"`
	Description string `json:"description,omitempty"`
	Score       int    `json:"score"`
}

Badge 徽章信息

type BasicUserInfo

type BasicUserInfo struct {
	ID         uint64     `json:"id"`
	Username   string     `json:"username"`
	Nickname   string     `json:"nickname"`
	TrustLevel TrustLevel `json:"trust_level"`
	AvatarUrl  string     `json:"avatar_url"`
	Score      int8       `json:"score"`
}

type CallbackRequest

type CallbackRequest struct {
	State string `json:"state"`
	Code  string `json:"code"`
}

type CallbackResponse

type CallbackResponse struct {
	ErrorMsg string      `json:"error_msg"`
	Data     interface{} `json:"data"`
}

type GetLoginURLResponse

type GetLoginURLResponse struct {
	ErrorMsg string `json:"error_msg"`
	Data     string `json:"data"`
}

type LogoutResponse

type LogoutResponse struct {
	ErrorMsg string      `json:"error_msg"`
	Data     interface{} `json:"data"`
}

type OAuthUserInfo

type OAuthUserInfo struct {
	Id         uint64     `json:"id"`
	Username   string     `json:"username"`
	Name       string     `json:"name"`
	Active     bool       `json:"active"`
	AvatarUrl  string     `json:"avatar_url"`
	TrustLevel TrustLevel `json:"trust_level"`
}

type TrustLevel

type TrustLevel int8
const (
	TrustLevelNewUser TrustLevel = iota
	TrustLevelBasicUser
	TrustLevelUser
	TrustLevelActiveUser
	TrustLevelLeader
)

type User

type User struct {
	ID             uint64     `json:"id" gorm:"primaryKey"`
	Username       string     `json:"username" gorm:"size:255;unique"`
	Nickname       string     `json:"nickname" gorm:"size:255"`
	AvatarUrl      string     `json:"avatar_url" gorm:"size:255"`
	IsActive       bool       `json:"is_active" gorm:"default:true"`
	TrustLevel     TrustLevel `json:"trust_level"`
	Score          int8       `json:"score"`
	ViolationCount uint8      `json:"violation_count" gorm:"default:0"`
	IsAdmin        bool       `json:"is_admin" gorm:"default:false"`
	LastLoginAt    time.Time  `json:"last_login_at" gorm:"index"`
	CreatedAt      time.Time  `json:"created_at" gorm:"autoCreateTime;index"`
	UpdatedAt      time.Time  `json:"updated_at" gorm:"autoUpdateTime;index"`
}

func GetUserFromContext

func GetUserFromContext(c *gin.Context) (*User, bool)

GetUserFromContext 从Context中获取User对象

func (*User) CalculateUserScore

func (u *User) CalculateUserScore(badges []Badge, badgeScores map[int]int) int

func (*User) CheckActive

func (u *User) CheckActive() error

CheckActive 检查用户账户是否激活,未激活则返回错误

func (*User) EnqueueBadgeScoreTask

func (u *User) EnqueueBadgeScoreTask(ctx context.Context)

EnqueueBadgeScoreTask 为用户下发徽章分数计算任务

func (*User) Exact

func (u *User) Exact(tx *gorm.DB, id uint64) error

func (*User) GetUserBadges

func (u *User) GetUserBadges(ctx context.Context) (*UserBadgeResponse, error)

func (*User) RiskLevel

func (u *User) RiskLevel() int8

func (*User) SetScore

func (u *User) SetScore(tx *gorm.DB, newScore int) error

func (*User) UpdateFromOAuthInfo

func (u *User) UpdateFromOAuthInfo(oauthInfo *OAuthUserInfo)

UpdateFromOAuthInfo 根据 OAuth 信息更新用户数据

func (*User) UpdateUserScore

func (u *User) UpdateUserScore(ctx context.Context, newScore int) error

type UserBadgeResponse

type UserBadgeResponse struct {
	Badges []Badge `json:"badges"`
}

UserBadgeResponse API响应

type UserInfoResponse

type UserInfoResponse struct {
	ErrorMsg string        `json:"error_msg"`
	Data     BasicUserInfo `json:"data"`
}

Jump to

Keyboard shortcuts

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