grokify/goauth
GitHub: grokify/goauth
GoAuth 是一个 Go 语言库,用于简化 API 服务的多种身份验证方法集成,支持 OAuth 2.0、JWT 等。
Stars: 68 | Forks: 4
# GoAuth
[][go-ci-url]
[][go-lint-url]
[][go-sast-url]
[][goreport-url]
[][docs-godoc-url]
[][viz-url]
[][license-url]
GoAuth 是一个全面的 Go 身份验证库,旨在简化 API 服务的 OAuth 2.0、JWT 和其他身份验证方法。它提供了一个统一的配置系统来处理多种身份验证类型和服务,使得从单个 JSON 配置文件创建经过身份验证的 `*http.Client` 实例变得简单。
## 特性
- **统一的凭据管理**:用于多种身份验证类型和服务的单一 JSON 配置格式
- **多种身份验证类型**:OAuth 2.0、JWT、基本身份验证、GCP 服务帐户以及自定义头部/查询参数身份验证
- **40+ OAuth 2.0 提供商**:预配置的流行服务端点
- **多种授权类型**:授权码、客户端凭据、密码、JWT Bearer、SAML2 Bearer 和刷新令牌
- **PKCE 支持**:用于增强安全性的 Proof Key for Code Exchange
- **SCIM 用户模型**:使用 [SCIM](http://www.simplecloud.info/) 模式跨服务检索规范用户信息
- **CLI 工具**:用于令牌生成和 API 请求的命令行实用程序
- **多服务 OAuth**:支持使用多个 OAuth 提供商的应用程序(例如,“使用 Google 登录”和“使用 Facebook 登录”)
## 安装说明
```
go get github.com/grokify/goauth
```
**要求**:Go 1.24+
## 支持的 OAuth 2.0 提供商
GoAuth 包含针对以下服务的预配置 OAuth 2.0 端点:
| 服务 | 服务密钥 | 备注 |
|--------------|--------------------|--------------------|
| Aha | `aha` | 需要子域名 |
| Asana | `asana` | |
| Atlassian | `atlassian` | |
| eBay | `ebay` | 生产环境 |
| eBay Sandbox | `ebaysandbox` | 沙箱环境 |
| Facebook | `facebook` | |
| GitHub | `github` | |
| Google | `google` | |
| HubSpot | `hubspot` | |
| Instagram | `instagram` | |
| Lyft | `lyft` | |
| Mailchimp | `mailchimp` | |
| Monday.com | `monday` | |
| PagerDuty | `pagerduty` | |
| PayPal | `paypal` | 生产环境 |
| PayPal Sandbox | `paypalsandbox` | 沙箱环境 |
| Pipedrive | `pipedrive` | |
| Practicesuite | `practicesuite` | |
| RingCentral | `ringcentral` | 生产环境 |
| RingCentral Sandbox | `ringcentralsandbox` | 沙箱环境 |
| Shippo | `shippo` | |
| Shopify | `shopify` | 需要子域名 |
| Slack | `slack` | |
| Stack Overflow | `stackoverflow` | |
| Stripe | `stripe` | |
| Todoist | `todoist` | |
| Uber | `uber` | |
| WePay | `wepay` | 生产环境 |
| WePay Sandbox | `wepaysandbox` | 沙箱环境 |
| Wrike | `wrike` | |
| Wunderlist | `wunderlist` | |
| Zoom | `zoom` | |
还有针对以下服务的服务特定包:Auth0、Metabase、Salesforce、SuccessFactors、Visa、SparkPost 和 Zendesk。
## 身份验证类型
GoAuth 支持以下身份验证类型:
| 类型 | 类型密钥 | 描述 |
|------------------|--------------------|----------------------------------|
| 基本身份验证 | `basic` | HTTP 基本身份验证 |
| OAuth 2.0 | `oauth2` | 支持多种授权类型的 OAuth 2.0 |
| JWT | `jwt` | JSON Web Token 生成 |
| GCP 服务帐户 | `gcpsa` | Google Cloud Platform 服务帐户 |
| Google OAuth 2.0 | `googleoauth2` | Google 特定的 OAuth 2.0 |
| 头部/查询 | `headerquery` | 自定义头部或查询参数身份验证 |
### OAuth 2.0 授权类型
- `authorization_code` - 授权码流程
- `client_credentials` - 客户端凭据流程
- `password` - 资源所有者密码凭据
- `urn:ietf:params:oauth:grant-type:jwt-bearer` - JWT Bearer
- `urn:ietf:params:oauth:grant-type:saml2-bearer` - SAML2 Bearer
- `refresh_token` - 刷新令牌
- `account_credentials` - 帐户凭据 (Zoom 服务器到服务器)
## 配置
### 凭据集(多个帐户)
GoAuth 使用支持多个凭据的 JSON 配置格式:
```
{
"credentials": {
"my-google-app": {
"service": "google",
"type": "oauth2",
"oauth2": {
"clientID": "your-client-id",
"clientSecret": "your-client-secret",
"redirectURL": "https://example.com/callback",
"scope": ["email", "profile"],
"grantType": "authorization_code"
}
},
"my-ringcentral-app": {
"service": "ringcentral",
"type": "oauth2",
"oauth2": {
"clientID": "your-client-id",
"clientSecret": "your-client-secret",
"grantType": "password",
"username": "your-username",
"password": "your-password"
}
},
"my-api-key": {
"type": "headerquery",
"headerquery": {
"serverURL": "https://api.example.com",
"header": {
"X-API-Key": "your-api-key"
}
}
}
}
}
```
### 凭据类型
#### OAuth 2.0 凭据
```
{
"service": "github",
"type": "oauth2",
"oauth2": {
"serverURL": "https://api.github.com",
"clientID": "your-client-id",
"clientSecret": "your-client-secret",
"redirectURL": "https://example.com/callback",
"scope": ["repo", "user"],
"grantType": "authorization_code",
"pkce": false
}
}
```
#### 基本身份验证凭据
```
{
"type": "basic",
"basic": {
"username": "your-username",
"password": "your-password",
"serverURL": "https://api.example.com",
"allowInsecure": false
}
}
```
#### JWT 凭据
```
{
"type": "jwt",
"jwt": {
"issuer": "your-issuer",
"privateKey": "your-private-key",
"signingMethod": "HS256"
}
}
```
支持的签名方法:`ES256`、`ES384`、`ES512`、`HS256`、`HS384`、`HS512`
#### 头部/查询凭据
```
{
"type": "headerquery",
"headerquery": {
"serverURL": "https://api.example.com",
"header": {
"Authorization": "Bearer your-token",
"X-Custom-Header": "value"
},
"query": {
"api_key": "your-api-key"
}
}
}
```
## 用法
### 创建 HTTP 客户端
```
package main
import (
"context"
"github.com/grokify/goauth"
)
func main() {
ctx := context.Background()
// From credentials file with account key
client, err := goauth.NewClient(ctx, "credentials.json", "my-google-app")
if err != nil {
panic(err)
}
// Use client for API requests
resp, err := client.Get("https://api.example.com/resource")
}
```
### 加载凭据集
```
package main
import (
"context"
"github.com/grokify/goauth"
)
func main() {
ctx := context.Background()
// Load credentials set from file
set, err := goauth.ReadFileCredentialsSet("credentials.json", true)
if err != nil {
panic(err)
}
// Get specific credentials
creds, err := set.Get("my-google-app")
if err != nil {
panic(err)
}
// Create client from credentials
client, err := creds.NewClient(ctx)
if err != nil {
panic(err)
}
// List all account keys
accounts := set.Accounts()
}
```
### 基于 CLI 的令牌检索
适用于没有 Web 服务器的授权码流程:
```
package main
import (
"context"
"github.com/grokify/goauth"
)
func main() {
ctx := context.Background()
creds, _ := goauth.NewCredentialsFromSetFile("credentials.json", "my-app", false)
// This will print the authorization URL and prompt for the code
client, err := creds.NewClientCLI(ctx, "random-state")
if err != nil {
panic(err)
}
}
```
### 规范用户信息 (SCIM)
GoAuth 提供了满足 `OAuth2Util` 接口的 `ClientUtil` 实现,用于检索规范用户信息:
```
type OAuth2Util interface {
SetClient(*http.Client)
GetSCIMUser() (scim.User, error)
}
```
#### Google
```
import "github.com/grokify/goauth/google"
googleClientUtil := google.NewClientUtil(googleOAuth2HTTPClient)
scimUser, err := googleClientUtil.GetSCIMUser()
```
#### Facebook
```
import "github.com/grokify/goauth/facebook"
fbClientUtil := facebook.NewClientUtil(fbOAuth2HTTPClient)
scimUser, err := fbClientUtil.GetSCIMUser()
```
#### RingCentral
```
import "github.com/grokify/goauth/ringcentral"
rcClientUtil := ringcentral.NewClientUtil(rcOAuth2HTTPClient)
scimUser, err := rcClientUtil.GetSCIMUser()
```
也可用于:Aha、Zoom、Metabase、Zendesk 和 Salesforce。
## CLI 工具
GoAuth 包含用于身份验证任务的命令行工具:
### GoAuth
支持所有身份验证类型的主令牌检索工具:
```
go run cmd/goauth/main.go --credentials credentials.json --account my-app
```
### Go API
发出经过身份验证的 API 请求:
```
go run cmd/goapi/main.go --credentials credentials.json --account my-app --url https://api.example.com/resource
```
## 包结构
| 包 | 描述 |
|-------------------|----------------------------------------------|
| `goauth` | 核心凭据管理和客户端创建 |
| `authutil` | 底层身份验证实用程序(BasicAuth、OAuth2、JWT、作用域管理) |
| `endpoints` | 针对 30+ 服务的预配置 OAuth 2.0 端点 |
| `scim` | 用于规范用户表示的 SCIM 模式用户/组模型 |
| `multiservice` | 面向应用程序的多提供商 OAuth2 管理 |
| `google` | Google 特定的 OAuth2 和 GCP 服务帐户处理 |
| `ringcentral` | RingCentral API 集成 |
| `facebook` | Facebook OAuth2 和用户数据检索 |
| `aha`, `zoom`, `metabase`, `zendesk`, `salesforce`, `hubspot` | 服务特定的实现 |
## 测试重定向 URL
此仓库包含一个通用的测试 OAuth 2 重定向页面,适用于无头(无 UI)应用程序。将您的 OAuth 2 重定向 URI 配置为:
**[https://grokify.github.io/goauth/oauth2callback/](https://grokify.github.io/goauth/oauth2callback/)**
此页面将显示授权码,您可以将其复制并粘贴到您的 CLI 应用程序中。
## 示例应用程序
- **多服务 OAuth 演示**:[github.com/grokify/beegoutil](https://github.com/grokify/beegoutil) - 基于 Beego 的演示,展示 Google 和 Facebook 身份验证
- **示例目录**:使用示例请参见 `examples/` 和服务特定的 `cmd/` 目录
## 贡献
欢迎贡献。请提交拉取请求或为错误和功能请求创建问题。
## 许可证
GoAuth 在 [MIT 许可证](LICENSE) 下可用。
标签:API安全, API开发, API认证, DNS解析, EVTX分析, Golang, Go包, Go语言, Homebrew安装, JSON输出, JWT, OAuth2, OAuth提供者, PKCE, SCIM, TLS认证, Web安全, YAML, 代码分析, 凭证管理, 后端安全, 基本认证, 多服务认证, 安全库, 安全编程, 客户端凭证, 开源项目, 授权码, 日志审计, 程序破解, 蓝队分析, 认证库, 认证框架