Debian Codegraph @

README.md

raw
1 # OpenID Connect SDK (client and server) for Go
2
3 [![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)
4 [![Release](https://github.com/zitadel/oidc/workflows/Release/badge.svg)](https://github.com/zitadel/oidc/actions)
5 [![Go Reference](https://pkg.go.dev/badge/github.com/zitadel/oidc/v3.svg)](https://pkg.go.dev/github.com/zitadel/oidc/v3)
6 [![license](https://badgen.net/github/license/zitadel/oidc/)](https://github.com/zitadel/oidc/blob/master/LICENSE)
7 [![release](https://badgen.net/github/release/zitadel/oidc/stable)](https://github.com/zitadel/oidc/releases)
8 [![Go Report Card](https://goreportcard.com/badge/github.com/zitadel/oidc/v3)](https://goreportcard.com/report/github.com/zitadel/oidc/v3)
9 [![codecov](https://codecov.io/gh/zitadel/oidc/branch/main/graph/badge.svg)](https://codecov.io/gh/zitadel/oidc)
10
11 [![openid_certified](https://cloud.githubusercontent.com/assets/1454075/7611268/4d19de32-f97b-11e4-895b-31b2455a7ca6.png)](https://openid.net/certification/)
12
13 ## What Is It
14
15 This project is an easy-to-use client (RP) and server (OP) implementation for the `OIDC` (OpenID Connect) standard written for `Go`.
16
17 The RP is certified for the [basic](https://www.certification.openid.net/plan-detail.html?public=true&plan=uoprP0OO8Z4Qo) and [config](https://www.certification.openid.net/plan-detail.html?public=true&plan=AYSdLbzmWbu9X) profile.
18
19 Whenever possible we tried to reuse / extend existing packages like `OAuth2 for Go`.
20
21 > [!NOTE]
22 > We currently have limited availability for feature reviews:
23 https://github.com/zitadel/oidc/discussions/785
24
25 ## Basic Overview
26
27 The most important packages of the library:
28
29 <pre>
30 /pkg
31 /client clients using the OP for retrieving, exchanging and verifying tokens
32 /rp definition and implementation of an OIDC Relying Party (client)
33 /rs definition and implementation of an OAuth Resource Server (API)
34 /op definition and implementation of an OIDC OpenID Provider (server)
35 /oidc definitions shared by clients and server
36
37 /example
38 /client/api example of an api / resource server implementation using token introspection
39 /client/app web app / RP demonstrating authorization code flow using various authentication methods (code, PKCE, JWT profile)
40 /client/github example of the extended OAuth2 library, providing an HTTP client with a reuse token source
41 /client/service demonstration of JWT Profile Authorization Grant
42 /server examples of an OpenID Provider implementations (including dynamic) with some very basic login UI
43 </pre>
44
45 ### Semver
46
47 This package uses [semver](https://semver.org/) for [releases](https://github.com/zitadel/oidc/releases). Major releases ship breaking changes. Starting with the `v2` to `v3` increment we provide an [upgrade guide](UPGRADING.md) to ease migration to a newer version.
48
49 ## How To Use It
50
51 Check the `/example` folder where example code for different scenarios is located.
52
53 ```bash
54 # start oidc op server
55 # oidc discovery http://localhost:9998/.well-known/openid-configuration
56 go run github.com/zitadel/oidc/v3/example/server
57 # start oidc web client (in a new terminal)
58 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
59 ```
60
61 - open http://localhost:9999/login in your browser
62 - you will be redirected to op server and the login UI
63 - login with user `test-user@localhost` and password `verysecure`
64 - the OP will redirect you to the client app, which displays the user info
65
66 for the dynamic issuer, just start it with:
67
68 ```bash
69 go run github.com/zitadel/oidc/v3/example/server/dynamic
70 ```
71
72 the oidc web client above will still work, but if you add `oidc.local` (pointing to 127.0.0.1) in your hosts file you can also start it with:
73
74 ```bash
75 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
76 ```
77
78 > Note: Usernames are suffixed with the hostname (`test-user@localhost` or `[email protected]`)
79
80
81 ### Build Tags
82
83 The library uses build tags to enable or disable features. The following build tags are available:
84
85 | Build Tag | Description |
86 |-----------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
87 | `no_otel` | Disables the OTel instrumentation, which is enabled by default. This is useful if you do not want to use OTel or if you want to use a different instrumentation library. |
88
89 ### Logging
90
91 OIDC writes logs through the global [`log/slog`](https://pkg.go.dev/log/slog) functions and therefore uses `slog.Default()`. Configure logging for the whole process before constructing an OIDC provider or relying party:
92
93 ```go
94 slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stderr, nil)))
95 ```
96
97 To disable OIDC logs, set the default logger to a handler that discards output:
98
99 ```go
100 slog.SetDefault(slog.New(slog.NewTextHandler(io.Discard, nil)))
101 ```
102
103 Because the logger is process-wide, this also affects other code that uses the global `slog` functions.
104
105 The existing `WithLogger`, `WithFallbackLogger`, and `Logger` APIs remain available for source compatibility but are deprecated. `op.WithLogger` / `WithFallbackLogger` are no-ops; `rp.WithLogger` still stores a logger for `rp.Logger()` callers. New code should call `slog.SetDefault` directly. Logger parameters on the exported error helpers are also retained for source compatibility, but OIDC logging uses the current global default.
106
107 ### Server configuration
108
109 Example server allows extra configuration using environment variables and could be used for end-to-end testing of your services.
110
111 | Name | Format | Description |
112 | ------------ | -------------------------------- | ------------------------------------- |
113 | PORT | Number between 1 and 65535 | OIDC listen port |
114 | REDIRECT_URI | Comma-separated URIs | List of allowed redirect URIs |
115 | USERS_FILE | Path to json in local filesystem | Users with their data and credentials |
116
117 Here is json equivalent for one of the default users
118
119 ```json
120 {
121 "id2": {
122 "ID": "id2",
123 "Username": "test-user2",
124 "Password": "verysecure",
125 "FirstName": "Test",
126 "LastName": "User2",
127 "Email": "[email protected]",
128 "EmailVerified": true,
129 "Phone": "",
130 "PhoneVerified": false,
131 "PreferredLanguage": "DE",
132 "IsAdmin": false
133 }
134 }
135 ```
136
137 ## Features
138
139 | | Relying party | OpenID Provider | Specification |
140 | -------------------- | ------------- | --------------- | -------------------------------------------- |
141 | Code Flow | yes | yes | OpenID Connect Core 1.0, [Section 3.1][1] |
142 | Implicit Flow | no[^1] | yes | OpenID Connect Core 1.0, [Section 3.2][2] |
143 | Hybrid Flow | no | not yet | OpenID Connect Core 1.0, [Section 3.3][3] |
144 | Client Credentials | yes | yes | OpenID Connect Core 1.0, [Section 9][4] |
145 | Refresh Token | yes | yes | OpenID Connect Core 1.0, [Section 12][5] |
146 | Discovery | yes | yes | OpenID Connect [Discovery][6] 1.0 |
147 | JWT Profile | yes | yes | [RFC 7523][7] |
148 | PKCE | yes | yes | [RFC 7636][8] |
149 | Token Exchange | yes | yes | [RFC 8693][9] |
150 | Device Authorization | yes | yes | [RFC 8628][10] |
151 | mTLS | not yet | not yet | [RFC 8705][11] |
152 | Back-Channel Logout | not yet | yes | OpenID Connect [Back-Channel Logout][12] 1.0 |
153
154 [1]: https://openid.net/specs/openid-connect-core-1_0.html#CodeFlowAuth "3.1. Authentication using the Authorization Code Flow"
155 [2]: https://openid.net/specs/openid-connect-core-1_0.html#ImplicitFlowAuth "3.2. Authentication using the Implicit Flow"
156 [3]: https://openid.net/specs/openid-connect-core-1_0.html#HybridFlowAuth "3.3. Authentication using the Hybrid Flow"
157 [4]: https://openid.net/specs/openid-connect-core-1_0.html#ClientAuthentication "9. Client Authentication"
158 [5]: https://openid.net/specs/openid-connect-core-1_0.html#RefreshTokens "12. Using Refresh Tokens"
159 [6]: https://openid.net/specs/openid-connect-discovery-1_0.html "OpenID Connect Discovery 1.0 incorporating errata set 1"
160 [7]: https://www.rfc-editor.org/rfc/rfc7523.html "JSON Web Token (JWT) Profile for OAuth 2.0 Client Authentication and Authorization Grants"
161 [8]: https://www.rfc-editor.org/rfc/rfc7636.html "Proof Key for Code Exchange by OAuth Public Clients"
162 [9]: https://www.rfc-editor.org/rfc/rfc8693.html "OAuth 2.0 Token Exchange"
163 [10]: https://www.rfc-editor.org/rfc/rfc8628.html "OAuth 2.0 Device Authorization Grant"
164 [11]: https://www.rfc-editor.org/rfc/rfc8705.html "OAuth 2.0 Mutual-TLS Client Authentication and Certificate-Bound Access Tokens"
165 [12]: https://openid.net/specs/openid-connect-backchannel-1_0.html "OpenID Connect Back-Channel Logout 1.0 incorporating errata set 1"
166
167 ## Contributors
168
169 <a href="https://github.com/zitadel/oidc/graphs/contributors">
170 <img src="https://contrib.rocks/image?repo=zitadel/oidc" alt="Screen with contributors' avatars from contrib.rocks" />
171 </a>
172
173 Made with [contrib.rocks](https://contrib.rocks).
174
175 ### Resources
176
177 For your convenience you can find the relevant guides linked below.
178
179 - [OpenID Connect Core 1.0 incorporating errata set 2](https://openid.net/specs/openid-connect-core-1_0.html)
180 - [OIDC/OAuth Flow in Zitadel (using this library)](https://zitadel.com/docs/guides/integrate/login/oidc/login-users)
181
182 ## Supported Go Versions
183
184 For security reasons, we only support and recommend the use of one of the latest two Go versions (:white_check_mark:).
185 Versions that also build are marked with :warning:.
186
187 | Version | Supported |
188 | ------- | ------------------ |
189 | <1.25 | :x: |
190 | 1.25 | :white_check_mark: |
191 | 1.26 | :white_check_mark: |
192
193 ## Why another library
194
195 As of 2020 there are not a lot of `OIDC` library's in `Go` which can handle server and client implementations. ZITADEL is strongly committed to the general field of IAM (Identity and Access Management) and as such, we need solid frameworks to implement services.
196
197 ### Goals
198
199 - [Certify this library as OP](https://openid.net/certification/#OPs)
200
201 ### Other Go OpenID Connect libraries
202
203 [https://github.com/coreos/go-oidc](https://github.com/coreos/go-oidc)
204
205 The `go-oidc` does only support `RP` and is not feasible to use as `OP` that's why we could not rely on `go-oidc`
206
207 [https://github.com/ory/fosite](https://github.com/ory/fosite)
208
209 We did not choose `fosite` because it implements `OAuth 2.0` on its own and does not rely on the golang provided package. Nonetheless, this is a great project.
210
211 ## License
212
213 The full functionality of this library is and stays open source and free to use for everyone. Visit
214 our [website](https://zitadel.com) and get in touch.
215
216 See the exact licensing terms [here](LICENSE)
217
218 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "
219 AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
220 language governing permissions and limitations under the License.
221
222 [^1]: https://github.com/zitadel/oidc/issues/135#issuecomment-950563892