zitadel/oidc

GitHub: zitadel/oidc

一个通过OpenID基金会认证的Go语言OIDC库,同时提供客户端和服务端的完整实现。

Stars: 1775 | Forks: 201

# OpenID Connect SDK (客户端和服务端) for Go [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release) [![Release](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/3619d1c63b225930.svg)](https://github.com/zitadel/oidc/actions) [![Go Reference](https://pkg.go.dev/badge/github.com/zitadel/oidc/v3.svg)](https://pkg.go.dev/github.com/zitadel/oidc/v3) [![license](https://badgen.net/github/license/zitadel/oidc/)](https://github.com/zitadel/oidc/blob/master/LICENSE) [![release](https://badgen.net/github/release/zitadel/oidc/stable)](https://github.com/zitadel/oidc/releases) [![Go Report Card](https://goreportcard.com/badge/github.com/zitadel/oidc/v3)](https://goreportcard.com/report/github.com/zitadel/oidc/v3) [![codecov](https://codecov.io/gh/zitadel/oidc/branch/main/graph/badge.svg)](https://codecov.io/gh/zitadel/oidc) [![openid_certified](https://cloud.githubusercontent.com/assets/1454075/7611268/4d19de32-f97b-11e4-895b-31b2455a7ca6.png)](https://openid.net/certification/) ## 它是什么 这是一个为 `Go` 编写的、易于使用的 `OIDC` (OpenID Connect) 标准客户端 (RP) 和服务端 (OP) 实现。 该 RP 已通过 [basic](https://www.certification.openid.net/plan-detail.html?public=true&plan=uoprP0OO8Z4Qo) 和 [config](https://www.certification.openid.net/plan-detail.html?public=true&plan=AYSdLbzmWbu9X) 配置文件认证。 在可能的情况下,我们尝试重用/扩展现有的包,例如 `OAuth2 for Go`。 ## 基本概述 该库最重要的包:

/pkg

    /client            使用 OP 进行令牌检索、交换和验证的客户端

        /rp            OIDC Relying Party (client) 的定义和实现

        /rs            OAuth Resource Server (API) 的定义和实现

    /op                OIDC OpenID Provider (server) 的定义和实现

    /oidc              客户端和服务端共享的定义



/example

    /client/api        使用 token introspection 的 api / resource server 实现示例

    /client/app        web app / RP,演示了使用各种认证方法 (code, PKCE, JWT profile) 的授权码流程

    /client/github     扩展 OAuth2 库的示例,提供了一个带有可复用 token source 的 HTTP 客户端

    /client/service    JWT Profile Authorization Grant 演示

    /server            OpenID Provider 实现示例 (包括动态的),带有一些非常基础的登录 UI

### Semver

该包使用 [semver](https://semver.org/) 进行版本[发布](https://github.com/zitadel/oidc/releases)。主要版本发布包含破坏性变更。从 `v2` 到 `v3` 的升级开始,我们提供了一份[升级指南](UPGRADING.md),以简化向新版本的迁移。

## 如何使用

请查看 `/example` 文件夹,其中包含不同场景的示例代码。


```
# 启动 oidc op server

# oidc discovery http://localhost:9998/.well-known/openid-configuration

go run github.com/zitadel/oidc/v3/example/server

# 启动 oidc web client(在新终端中)

CLIENT_ID=web CLIENT_SECRET=secret ISSUER=http://localhost:9998/ SCOPES="openid profile" PORT=9999 go run github.com/zitadel/oidc/v3/example/client/app
```


- 在浏览器中打开 http://localhost:9999/login
- 你将被重定向到 OP 服务器和登录 UI
- 使用用户 `test-user@localhost` 和密码 `verysecure` 登录
- OP 会将你重定向回客户端应用,该应用会显示用户信息

对于动态 issuer,只需使用以下命令启动:


```
go run github.com/zitadel/oidc/v3/example/server/dynamic
```


上面的 oidc web 客户端仍然可以使用,但如果你在 hosts 文件中添加 `oidc.local` (指向 127.0.0.1),你也可以通过以下方式启动:


```
CLIENT_ID=web CLIENT_SECRET=secret ISSUER=http://oidc.local:9998/ SCOPES="openid profile" PORT=9999 go run github.com/zitadel/oidc/v3/example/client/app
```


### Build Tags

该库使用 build tags 来启用或禁用功能。可用的 build tags 如下:

| Build Tag | 描述                                                                                                                                                              |

|-----------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

| `no_otel`  | 禁用 OTel instrumentation,该功能默认启用。如果你不想使用 OTel 或者想使用不同的 instrumentation 库,这会很有用。 |

### 服务器配置

示例服务器允许使用环境变量进行额外配置,并可用于服务的端到端测试。

| 名称         | 格式                           | 描述                           |

| ------------ | -------------------------------- | ------------------------------------- |

| PORT         | 1 到 65535 之间的数字       | OIDC 监听端口                      |

| REDIRECT_URI | 逗号分隔的 URI             | 允许的 redirect URI 列表         |

| USERS_FILE   | 本地文件系统中 json 文件的路径 | 包含用户数据及其凭证的用户文件 |

这是一个默认用户的 json 示例


```
{

  "id2": {

    "ID": "id2",

    "Username": "test-user2",

    "Password": "verysecure",

    "FirstName": "Test",

    "LastName": "User2",

    "Email": "test-user2@zitadel.ch",

    "EmailVerified": true,

    "Phone": "",

    "PhoneVerified": false,

    "PreferredLanguage": "DE",

    "IsAdmin": false

  }

}
```


## 功能

|                      | Relying party | OpenID Provider | Specification                                |

| -------------------- | ------------- | --------------- | -------------------------------------------- |

| Code Flow            | yes           | yes             | OpenID Connect Core 1.0, [Section 3.1][1]    |

| Implicit Flow        | no[^1]        | yes             | OpenID Connect Core 1.0, [Section 3.2][2]    |

| Hybrid Flow          | no            | not yet         | OpenID Connect Core 1.0, [Section 3.3][3]    |

| Client Credentials   | yes           | yes             | OpenID Connect Core 1.0, [Section 9][4]      |

| Refresh Token        | yes           | yes             | OpenID Connect Core 1.0, [Section 12][5]     |

| Discovery            | yes           | yes             | OpenID Connect [Discovery][6] 1.0            |

| JWT Profile          | yes           | yes             | [RFC 7523][7]                                |

| PKCE                 | yes           | yes             | [RFC 7636][8]                                |

| Token Exchange       | yes           | yes             | [RFC 8693][9]                                |

| Device Authorization | yes           | yes             | [RFC 8628][10]                               |

| mTLS                 | not yet       | not yet         | [RFC 8705][11]                               |

| Back-Channel Logout  | not yet       | yes             | OpenID Connect [Back-Channel Logout][12] 1.0 |



### 资源

为了方便起见,你在下方可以找到相关的指南链接。

- [OpenID Connect Core 1. incorporating errata set 1](https://openid.net/specs/openid-connect-core-1_0.html)
- [OIDC/OAuth Flow in Zitadel (using this library)](https://zitadel.com/docs/guides/integrate/login-users)

## 支持的 Go 版本

出于安全原因,我们只支持并推荐使用最近两个 Go 版本之一 (:white_check_mark:)。

同样可以构建的版本用 :warning: 标记。

| Version | Supported          |

| ------- | ------------------ |

| <1.24   | :x:                |

| 1.24    | :white_check_mark: |

| 1.25    | :white_check_mark: |

## 为什么要有另一个库

截至 2020 年,`Go` 中能同时处理服务端和客户端实现的 `OIDC` 库并不多。ZITADEL 强致力于 IAM (身份与访问管理) 的广泛领域,因此,我们需要稳固的框架来实现服务。

### 目标

- [认证此库为 OP](https://openid.net/certification/#OPs)

### 其他 Go OpenID Connect 库

[https://github.com/coreos/go-oidc](https://github.com/coreos/go-oidc)

`go-oidc` 仅支持 `RP`,无法作为 `OP` 使用,这就是我们不能依赖 `go-oidc` 的原因。

[https://github.com/ory/fosite](https://github.com/ory/fosite)

我们没有选择 `fosite`,因为它自行实现了 `OAuth 2.0`,而没有依赖 Golang 提供的包。尽管如此,这仍是一个很棒的项目。

## 许可证

该库的全部功能是且始终保持开源,并免费供所有人使用。访问我们的[网站](https://zitadel.com)并取得联系。

请参阅[此处](LICENSE)了解确切的许可条款。

除非适用法律要求或书面同意,否则根据许可证分发的软件是按“原样”分发的,没有任何形式的明示或暗示的担保或条件。请参阅许可证以了解许可证下的特定语言管理权限和限制。
标签:EVTX分析, EVTX分析, Go, Golang, IdP, Modbus, OAuth2, OIDC, OP, OpenID Connect, OpenID Foundation认证, PE 加载器, RP, Ruby工具, SSO, Syscall, Web开发, 中间件, 信赖方, 单点登录, 安全, 安全编程, 开源, 日志审计, 用户代理, 认证库, 超时处理, 身份提供商