Documentation
¶
Overview ¶
Package thirdauth 实现自建的第三方登录(GitHub、Google、QQ 与通用 OIDC)。 使用用户自己申请的 OAuth 应用,不经过任何中转服务器;管理界面登录与 Web 服务站点认证共用本包。
Index ¶
- Constants
- Variables
- func Allowed(entries []string, id Identity) bool
- func AllowedExplicit(entries []string, id Identity) bool
- func ParseEntry(s string) (provider, value string, ok bool)
- func RandomNonce() string
- func Sign(secret []byte, kind string, v any, ttl time.Duration) string
- func ValidateEntries(entries []string) error
- func Verify(secret []byte, kind, token string, out any) error
- func VerifyExp(secret []byte, kind, token string, out any) (int64, error)
- type Identity
- type LoginConfig
- type LuckyIdentities
- type LuckyIdentity
- type Provider
Constants ¶
const ( TypeGitHub = "github" TypeGoogle = "google" TypeQQ = "qq" TypeOIDC = "oidc" )
提供方类型
const CallbackPath = "/api/login/oauth/callback"
CallbackPath 是管理界面接收回调的路径。
const GoogleIssuer = "https://accounts.google.com"
GoogleIssuer 是 Google 类型固定使用的 OIDC 签发者。
const HTTPTimeout = 20 * time.Second
HTTPTimeout 是一次登录回调中访问身份提供方的总超时。
const StartPath = "/api/login/oauth/start"
StartPath 是管理界面发起第三方登录的路径。
Variables ¶
var Endpoints = struct { GitHub struct{ Authorize, Token, User string } QQ struct{ Authorize, Token, Me string } }{ GitHub: struct{ Authorize, Token, User string }{ Authorize: "https://github.com/login/oauth/authorize", Token: "https://github.com/login/oauth/access_token", User: "https://api.github.com/user", }, QQ: struct{ Authorize, Token, Me string }{ Authorize: "https://graph.qq.com/oauth2.0/authorize", Token: "https://graph.qq.com/oauth2.0/token", Me: "https://graph.qq.com/oauth2.0/me", }, }
Endpoints 是各提供方的固定端点,测试时可替换。
Functions ¶
func AllowedExplicit ¶ added in v0.6.6
AllowedExplicit 判断身份是否被允许列表逐个列出:不计 提供方key:* 通配。 通配允许的是任何能在该提供方注册账号的人,不能据此认定访客可信(例如跳过 WAF)。
func ParseEntry ¶
ParseEntry 解析允许列表中的一项:提供方key:标识,行尾可用 " #" 追加备注。
Types ¶
type Identity ¶
type Identity struct {
// Provider 为提供方 Key
Provider string
// Primary 为展示与提示用的主标识:GitHub 为登录名,OIDC 为已验证邮箱(缺失时为 sub),QQ 为 openid
Primary string
// Aliases 为同一账号的其他可匹配标识(GitHub 数字 ID、OIDC 的 sub 等)。
// 按稳定 ID 授权比按用户名更安全,用户名可被改名后由他人重新注册
Aliases []string
}
Identity 是登录成功后得到的身份。
type LoginConfig ¶
type LoginConfig struct {
Enable bool `json:"enable"`
// CallbackBase 为管理界面的外部访问地址(须与浏览器打开管理界面使用的地址一致),例如 https://gately.example.com
CallbackBase string `json:"callbackBase"`
Providers []Provider `json:"providers"`
// Allowed 为允许登录管理界面的身份,格式见 ParseEntry
Allowed []string `json:"allowed"`
// SkipTOTP 为真时第三方登录跳过两步验证(对应 Lucky 的“第三方登录跳过两步验证”)
SkipTOTP bool `json:"skipTotp"`
}
LoginConfig 是管理界面的第三方登录设置。
func (LoginConfig) CallbackURL ¶
func (c LoginConfig) CallbackURL() string
CallbackURL 返回应在提供方登记的回调地址。
func (LoginConfig) Masked ¶
func (c LoginConfig) Masked() LoginConfig
Masked 返回 Client Secret 脱敏后的副本,用于 API 输出。
func (LoginConfig) ProviderMap ¶
func (c LoginConfig) ProviderMap() map[string]Provider
ProviderMap 以 Key 索引提供方。
func (LoginConfig) Secrets ¶ added in v0.6.0
func (c LoginConfig) Secrets() map[string]string
Secrets 返回各登录方式的 Client Secret 明文,按登录方式标识(Key)索引,供管理界面“显示密码”使用。
func (*LoginConfig) Validate ¶
func (c *LoginConfig) Validate(old LoginConfig) error
Validate 校验配置,old 为已保存的配置,用于还原被脱敏的 Client Secret。
type LuckyIdentities ¶
type LuckyIdentities struct {
List []LuckyIdentity `json:"list"`
}
LuckyIdentities 是持久化的 Lucky 身份表。
func LoadLuckyIdentities ¶
func LoadLuckyIdentities(store *config.Store) (LuckyIdentities, error)
LoadLuckyIdentities 读取已导入的身份表(不存在时为空)。
type LuckyIdentity ¶
type LuckyIdentity struct {
Key string `json:"Key"`
Enable bool `json:"Enable"`
Type string `json:"Type"`
ID string `json:"ID"`
Name string `json:"Name"`
EMail string `json:"EMail"`
}
LuckyIdentity 是 Lucky 第三方登录管理中登记的一个身份。
func (LuckyIdentity) Entry ¶
func (i LuckyIdentity) Entry() (entry string, ok bool)
Entry 转换为 Gately 允许列表项。优先使用稳定 ID(GitHub 数字 ID、Google sub、QQ openid), 用户名与邮箱写在备注里便于辨认。
身份表来自导入的文件,内容不可信:类型或 ID 为空、ID 为 *(允许列表里表示该提供方的所有用户)、 类型或 ID 含空白、控制字符、冒号(类型)或 #(会被当作备注的开头)时 ok 为 false,这个身份不能迁移。 否则一份身份表就能让“该登录方式的任何账号”进入允许列表,或在界面按行编辑时拆出新的列表项。
type Provider ¶
type Provider struct {
// Key 同时作为身份前缀,例如 key 为 github 时身份写作 github:用户名
Key string `json:"key"`
Name string `json:"name"`
Type string `json:"type"`
ClientID string `json:"clientId"`
ClientSecret string `json:"clientSecret"`
// Issuer 为 OIDC 签发者地址,例如 https://accounts.google.com(Google 类型自动填充)
Issuer string `json:"issuer"`
// Proxy 为服务器访问该提供方(换取令牌、查询用户信息、读取发现文档)时使用的代理,
// 支持 http://、https://、socks5://,留空直连。只作用于本登录方式,不影响 Gately 其他功能
Proxy string `json:"proxy"`
}
Provider 是一个第三方登录配置。
func (Provider) AuthorizeURL ¶
AuthorizeURL 生成跳转到身份提供方的地址。