oidc

package
v0.33.4 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package oidc 实现 OIDC 单点登录(P2,stdlib-only,无第三方依赖)。

流程(Authorization Code):

  1. GET /api/v1/user/oidc/login → 302 到 IdP 授权页(state 为 HMAC 签名时间戳,防 CSRF)
  2. IdP 回调 GET /api/v1/user/oidc/callback?code&state → 验 state → 换 token → 拉 userinfo
  3. 按 (issuer, sub) 匹配本地用户,其次按 email,未命中且允许注册则建号
  4. 签发本站 JWT,302 回前端 /#/oidc/callback?token=xxx

Index

Constants

This section is empty.

Variables

View Source
var ErrAccountDisabled = errors.New("OIDC 登录被拒绝:账号已被禁用")

ErrAccountDisabled 匹配命中的本地账号已被禁用(回调不发 JWT,2026-10-08 加固)

View Source
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 NewService

func NewService(gormDB *gorm.DB, cfg Config) *Service

NewService 构建服务

func (*Service) Enabled

func (s *Service) Enabled() bool

Enabled 是否启用

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

func (s *Service) FrontendCallback() string

FrontendCallback 前端回调地址(带 token= 前缀;hash 路由 query)

func (*Service) LoginURL

func (s *Service) LoginURL(ctx context.Context, baseURL, nonce string) (string, error)

LoginURL 构建授权跳转地址(nonce 由调用方生成并下发 Cookie,见 GenerateNonce)

func (*Service) RedirectURI

func (s *Service) RedirectURI(baseURL string) string

RedirectURI 回调地址(本站)

func (*Service) TestDiscovery

func (s *Service) TestDiscovery(ctx context.Context) error

TestDiscovery 管理端「测试连接」:验证 issuer 的 discovery 端点可达且合法。

Jump to

Keyboard shortcuts

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