SuperAPI AI 工具集

goth-fundamentals

编程与开发

查看项目

更新于 10-15 21:47

技能介绍

用途:为 Go 项目接入多提供商 OAuth 认证提供指导,围绕 github.com/markbates/goth 库展开,覆盖从安装到完整认证流程的用法。

主要能力:goth 提供简洁、符合 Go 习惯的多提供商 OAuth 认证。核心概念包括 Provider 接口(Name、BeginAuth、UnmarshalSession、FetchUser、Debug、RefreshToken、RefreshTokenAvailable)、Session 接口(GetAuthURL、Authorize、Marshal)以及认证成功后返回的 User 结构(Email、Name、FirstName、LastName、NickName、UserID、AvatarURL、AccessToken、RefreshToken、ExpiresAt、IDToken 等字段)。gothic 辅助包提供 BeginAuthHandler、CompleteUserAuth、GetFromSession、Logout 等便捷函数,并可通过 GetProviderName 覆盖提供商识别方式(查询参数或路径段)。库内置 70+ 提供商,涵盖 Google、Microsoft、Apple、Amazon、Okta、Auth0、GitHub、GitLab、Bitbucket、Gitea、Facebook、Twitter、Instagram、LinkedIn、Discord、Slack、Salesforce、Shopify、Zoom、Spotify、Twitch、PayPal、Stripe、Uber 等,需按需单独导入对应 provider 包。

使用方式:安装命令为 go get github.com/markbates/goth。典型流程为:在 init 中调用 goth.UseProviders 注册提供商并传入客户端 ID、密钥与回调地址;创建 /auth/ 与 /auth/callback/ 路由,分别调用 gothic.BeginAuthHandler 与 gothic.CompleteUserAuth;用 gothic.Logout 处理登出。会话默认使用 gorilla/sessions,可通过 gothic.Store 配置 CookieStore 的 MaxAge、Path、HttpOnly、Secure 等选项。凭据建议通过环境变量管理,可配合 godotenv 加载。错误处理可针对 access_denied、invalid_grant 等做分支;长会话可用 RefreshTokenAvailable 与 RefreshToken 在过期前刷新令牌。

适用场景与限制:适用于 Go Web 应用接入第三方登录。资料未提供具体版本要求、性能数据、收费信息及完整安全加固细节,相关技能 goth-providers、goth-echo-security 仅作关联提及。