Documentation
¶
Overview ¶
Package oidc 实现 OIDC 单点登录(P2,stdlib-only,无第三方依赖)。
流程(Authorization Code):
- GET /api/v1/user/oidc/login → 302 到 IdP 授权页(state 为 HMAC 签名时间戳,防 CSRF)
- IdP 回调 GET /api/v1/user/oidc/callback?code&state → 验 state → 换 token → 拉 userinfo
- 按 (issuer, sub) 匹配本地用户,其次按 email,未命中且允许注册则建号
- 签发本站 JWT,302 回前端 /#/oidc/callback?token=xxx
Index ¶
- Variables
- func GenerateNonce() string
- type Config
- type Service
- func (s *Service) Enabled() bool
- func (s *Service) ExchangeCallback(ctx context.Context, baseURL, code, state, nonce string) (string, error)
- func (s *Service) FrontendCallback() string
- func (s *Service) LoginURL(ctx context.Context, baseURL, nonce string) (string, error)
- func (s *Service) RedirectURI(baseURL string) string
- func (s *Service) TestDiscovery(ctx context.Context) error
Constants ¶
This section is empty.
Variables ¶
var ErrAccountDisabled = errors.New("OIDC 登录被拒绝:账号已被禁用")
ErrAccountDisabled 匹配命中的本地账号已被禁用(回调不发 JWT,2026-10-08 加固)
var ErrStateInvalid = errors.New("state 校验失败(过期/伪造/缺少浏览器绑定),请重试登录")
ErrStateInvalid state 校验失败(过期/伪造/缺少浏览器绑定)。
独立哨兵(2026-10-26 审计 P2):transport 层要把 OIDC 失败映射成**可公开** 的短文案 + 稳定短码 ⇒ 需要能机器判定「是 state 问题」而不是靠字符串匹配 (字符串匹配会被上游改文案悄悄打断)。
Functions ¶
func GenerateNonce ¶ added in v0.14.9
func GenerateNonce() string
GenerateNonce 生成一次性的浏览器绑定 nonce(login CSRF 防线,2026-10-08 加固): 登录起点下发 HttpOnly Cookie,state 的 HMAC 绑定该 nonce——回调时 Cookie 与 state 必须同源同流,第三方拿"自己的 code+state"诱导受害者浏览器回调时, 受害者没有匹配的 nonce Cookie,登录 CSRF 不成立。
Types ¶
type Config ¶
type Config struct {
Enabled bool `mapstructure:"enabled"`
Issuer string `mapstructure:"issuer"` // 如 https://idp.example.com/realms/pb
ClientID string `mapstructure:"client_id"`
ClientSecret string `mapstructure:"client_secret"`
Scopes string `mapstructure:"scopes"` // 默认 "openid profile email"
FrontendCallback string `mapstructure:"frontend_callback"` // 默认 /#/oidc/callback
}
Config OIDC 配置(conf.SecurityConfig.OIDC)
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service OIDC 登录服务
func (*Service) ExchangeCallback ¶
func (s *Service) ExchangeCallback(ctx context.Context, baseURL, code, state, nonce string) (string, error)
ExchangeCallback 处理回调:code → token → userinfo → 本地用户 → JWT。 nonce 为登录起点下发的浏览器绑定值(Cookie 回读),state 校验必需。
func (*Service) FrontendCallback ¶
FrontendCallback 前端回调地址(带 token= 前缀;hash 路由 query)
func (*Service) RedirectURI ¶
RedirectURI 回调地址(本站)