thirdauth

package
v0.6.8 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: Apache-2.0 Imports: 20 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 thirdauth 实现自建的第三方登录(GitHub、Google、QQ 与通用 OIDC)。 使用用户自己申请的 OAuth 应用,不经过任何中转服务器;管理界面登录与 Web 服务站点认证共用本包。

Index

Constants

View Source
const (
	TypeGitHub = "github"
	TypeGoogle = "google"
	TypeQQ     = "qq"
	TypeOIDC   = "oidc"
)

提供方类型

View Source
const CallbackPath = "/api/login/oauth/callback"

CallbackPath 是管理界面接收回调的路径。

View Source
const GoogleIssuer = "https://accounts.google.com"

GoogleIssuer 是 Google 类型固定使用的 OIDC 签发者。

View Source
const HTTPTimeout = 20 * time.Second

HTTPTimeout 是一次登录回调中访问身份提供方的总超时。

View Source
const StartPath = "/api/login/oauth/start"

StartPath 是管理界面发起第三方登录的路径。

Variables

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

func Allowed(entries []string, id Identity) bool

Allowed 判断身份是否在允许列表中。列表项的标识可匹配主标识或任一别名(不区分大小写), 提供方key:* 表示该提供方的所有用户。

func AllowedExplicit added in v0.6.6

func AllowedExplicit(entries []string, id Identity) bool

AllowedExplicit 判断身份是否被允许列表逐个列出:不计 提供方key:* 通配。 通配允许的是任何能在该提供方注册账号的人,不能据此认定访客可信(例如跳过 WAF)。

func ParseEntry

func ParseEntry(s string) (provider, value string, ok bool)

ParseEntry 解析允许列表中的一项:提供方key:标识,行尾可用 " #" 追加备注。

func RandomNonce

func RandomNonce() string

RandomNonce 生成随机串,用于把登录流程绑定到发起登录的浏览器。

func Sign

func Sign(secret []byte, kind string, v any, ttl time.Duration) string

Sign 生成带过期时间的签名数据。

func ValidateEntries

func ValidateEntries(entries []string) error

ValidateEntries 校验允许列表的格式。

func Verify

func Verify(secret []byte, kind, token string, out any) error

Verify 校验签名、类型与有效期,并把内容解码到 out。

func VerifyExp added in v0.5.8

func VerifyExp(secret []byte, kind, token string, out any) (int64, error)

VerifyExp 与 Verify 相同,另外返回过期时间(Unix 秒),用于推算签发时间。

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 是登录成功后得到的身份。

func (Identity) String

func (i Identity) String() string

String 返回 提供方:主标识。

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) Normalize

func (c *LoginConfig) Normalize()

Normalize 清理空白项。

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 读取已导入的身份表(不存在时为空)。

func (LuckyIdentities) Resolve

func (ids LuckyIdentities) Resolve(keys []string, all bool) (entries []string, missing int)

Resolve 把 Lucky 的身份 Key 列表(all 为真时为全部启用的身份)解析为允许列表项, missing 返回在身份表里找不到、或不能迁移(见 Entry)的身份数量。

func (LuckyIdentities) Save

func (ids LuckyIdentities) Save(store *config.Store) error

Save 写入身份表。

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 Providers

func Providers(entries []string, configured map[string]Provider) []Provider

Providers 返回允许列表中出现、且已配置的提供方(保持列表顺序、去重)。

func (Provider) AuthorizeURL

func (p Provider) AuthorizeURL(ctx context.Context, redirect, state string) (string, error)

AuthorizeURL 生成跳转到身份提供方的地址。

func (Provider) DisplayName

func (p Provider) DisplayName() string

DisplayName 返回登录按钮上的名称。

func (Provider) Exchange

func (p Provider) Exchange(ctx context.Context, code, redirect string) (Identity, error)

Exchange 用授权码换取访问令牌并查询身份。

func (*Provider) Validate

func (p *Provider) Validate() error

Validate 校验并规范化提供方配置。

Jump to

Keyboard shortcuts

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