auth

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2026 License: Apache-2.0 Imports: 19 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package auth 实现管理员账号、会话令牌与登录限流。

令牌格式:base64url(payload) + "." + base64url(HMAC-SHA256(payload))。 修改密码时轮换签名密钥,使所有旧会话立即失效。

Index

Constants

View Source
const CookieName = "gately_token"

CookieName 是会话 Cookie 名称。

Variables

View Source
var (
	// ErrInvalidCredentials 表示用户名或密码错误。
	ErrInvalidCredentials = errors.New("用户名或密码错误")
	// ErrLocked 表示该 IP 因多次失败被临时锁定。
	ErrLocked = errors.New("登录失败次数过多,请稍后再试")
	// ErrTOTPRequired 表示需要输入两步验证码。
	ErrTOTPRequired = errors.New("请输入两步验证码")
)

Functions

func NewTOTPSecret

func NewTOTPSecret() string

NewTOTPSecret 生成 160 位随机密钥(Base32 编码)。

func TOTPURL

func TOTPURL(secret, account string) string

TOTPURL 生成验证器 App 可扫描的 otpauth 地址。

func TokenFromRequest

func TokenFromRequest(r *http.Request) string

TokenFromRequest 从 Authorization 头或 Cookie 中取令牌。

func UserFromContext

func UserFromContext(ctx context.Context) string

UserFromContext 返回中间件写入的用户名。

func ValidateTOTP

func ValidateTOTP(secret, code string, now time.Time) bool

ValidateTOTP 校验验证码(不防重放,用于绑定时确认验证器)。

func WithUser added in v0.2.0

func WithUser(ctx context.Context, user string) context.Context

WithUser 把用户名写入上下文(API 令牌等其他认证方式使用)。

Types

type Account

type Account struct {
	Username      string `json:"username"`
	PasswordHash  string `json:"passwordHash"`
	SessionSecret string `json:"sessionSecret"`
	TokenTTLHours int    `json:"tokenTTLHours"`
	// TOTPSecret 非空表示已启用两步验证
	TOTPSecret string `json:"totpSecret,omitempty"`
}

Account 是持久化的管理员账号信息。

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager 管理账号校验和会话。

func NewManager

func NewManager(acc Account, save func(Account) error) *Manager

NewManager 用已加载的账号创建管理器,save 用于持久化账号变更。

func (*Manager) ChangeCredentials

func (m *Manager) ChangeCredentials(oldPassword, newUsername, newPassword string) error

ChangeCredentials 修改用户名和密码,需要提供旧密码;成功后旧令牌全部失效。

func (*Manager) EnsureInitialized

func (m *Manager) EnsureInitialized() (generated string, err error)

EnsureInitialized 在首次启动时创建 admin 账号与随机密码。 返回值 generated 非空表示新生成的初始密码,调用方需要展示给用户。

func (*Manager) EntrySecret added in v0.2.0

func (m *Manager) EntrySecret() string

EntrySecret 返回用于签名安全入口 Cookie 的密钥。

func (*Manager) Issue added in v0.3.0

func (m *Manager) Issue() (string, time.Time)

Issue 为已通过其他方式(如第三方登录)验证的管理员直接签发会话令牌。

func (*Manager) Login

func (m *Manager) Login(ip, username, password string, code ...string) (string, time.Time, error)

Login 校验凭据并签发令牌。启用两步验证时 code 必填。

func (*Manager) Require

func (m *Manager) Require(next http.Handler) http.Handler

Require 是要求登录的中间件。

func (*Manager) SetLimits added in v0.2.0

func (m *Manager) SetLimits(maxFail int, lock time.Duration)

SetLimits 设置连续失败上限与锁定时长,非正数表示使用默认值。

func (*Manager) SetTOTP

func (m *Manager) SetTOTP(password, code, secret string) error

SetTOTP 启用或关闭两步验证。secret 为空表示关闭,关闭时还需提供当前验证码。 成功后轮换会话密钥,使此前签发的令牌全部失效。

func (*Manager) SetTokenTTL added in v0.2.0

func (m *Manager) SetTokenTTL(hours int) error

SetTokenTTL 修改会话有效期并持久化,已签发的会话不受影响。

func (*Manager) TOTPEnabled

func (m *Manager) TOTPEnabled() bool

TOTPEnabled 返回是否已启用两步验证。

func (*Manager) TokenTTLHours added in v0.2.0

func (m *Manager) TokenTTLHours() int

TokenTTLHours 返回会话有效期(小时)。

func (*Manager) Username

func (m *Manager) Username() string

Username 返回当前管理员用户名。

func (*Manager) Verify

func (m *Manager) Verify(token string) (string, bool)

Verify 校验令牌,返回用户名。

Jump to

Keyboard shortcuts

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