Debian Codegraph @

UPGRADING.md

raw
1 # Upgrading
2
3 All commands are executed from the root of the project that imports oidc packages.
4 `sed` commands are created with **GNU sed** in mind and might need alternate syntax
5 on non-GNU systems, such as MacOS.
6 Alternatively, GNU sed can be installed on such systems. (`coreutils` package?).
7
8 ## Global `slog` logger migration
9
10 OIDC now logs through the global `log/slog` functions. Configure the process-wide default before constructing an OP or RP:
11
12 ```go
13 slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stderr, nil)))
14 ```
15
16 To disable OIDC logs, install a discard handler instead:
17
18 ```go
19 slog.SetDefault(slog.New(slog.NewTextHandler(io.Discard, nil)))
20 ```
21
22 OIDC package logging always uses the process-wide `slog` default. The exported `WithLogger`, `WithFallbackLogger`, and `Logger` APIs remain for source compatibility but are deprecated. `op.WithLogger` / `op.WithFallbackLogger` are no-ops; `op.Provider.Logger()` returns `slog.Default()`. `rp.WithLogger` still stores a logger returned by `rp.Logger()` (falling back to `slog.Default()`), but the RP package itself no longer writes through that logger. Logger arguments on `RequestError`, `WriteError`, and `TryErrorRedirect` are also retained but ignored.
23
24 Remove uses of `logging.ToContext` and `github.com/zitadel/logging` middleware. Applications that need request logging or context enrichment should provide their own middleware and `slog.Handler`. Discovery debug records are now sent to the global logger without first checking for a context logger; use the global handler's level configuration to suppress them. RP records no longer receive the `rp` attribute group or its function-name enrichment.
25
26 ## V2 to V3
27
28 **TL;DR** at the [bottom](#full-script) of this chapter is a full `sed` script
29 containing all automatic steps at once.
30
31
32 As first steps we will:
33 1. Download the latest v3 module;
34 2. Replace imports in all Go files;
35 3. Tidy the module file;
36
37 ```bash
38 go get -u github.com/zitadel/oidc/v3
39 find . -type f -name '*.go' | xargs sed -i \
40 -e 's/github\.com\/zitadel\/oidc\/v2/github.com\/zitadel\/oidc\/v3/g'
41 go mod tidy
42 ```
43
44 ### global
45
46 #### go-jose package
47
48 `gopkg.in/square/go-jose.v2` import has been changed to `github.com/go-jose/go-jose/v3`.
49 That means that the imported types are also changed and imports need to be adapted.
50
51 ```bash
52 find . -type f -name '*.go' | xargs sed -i \
53 -e 's/gopkg.in\/square\/go-jose\.v2/github.com\/go-jose\/go-jose\/v3/g'
54 go mod tidy
55 ```
56
57 ### op
58
59 ```go
60 import "github.com/zitadel/oidc/v3/pkg/op"
61 ```
62
63 #### Logger
64
65 > [!NOTE]
66 > Current versions use the process-wide logger described in [Global `slog` logger migration](#global-slog-logger-migration). The text below describes the historical V2 to V3 change.
67
68 This version of OIDC adds logging to the framework. For this we use the new Go standard library `log/slog`. (Until v3.12.0 we used `x/exp/slog`).
69 Mostly OIDC will use error level logs where it's returning an error through an HTTP handler. OIDC errors that are user facing don't carry much context, also for security reasons. With logging we are now able to print the error context, so that developers can more easily find the source of their issues. Previously we just discarded such context.
70
71 Most users of the OP package with the storage interface will not experience breaking changes. However, if you use `RequestError()` directly in your code, you now need to give it a `Logger` as final argument.
72
73 The `OpenIDProvider` and sub-interfaces like `Authorizer` and `Exchanger` got a `Logger()` method to return the configured logger. This logger is in turn used by `AuthRequestError()`. You configure the logger with the `WithLogger()` for the `Provider`. By default, the `slog.Default()` is used.
74
75 We also provide a new optional interface: [`LogAuthRequest`](https://pkg.go.dev/github.com/zitadel/oidc/v3/pkg/op#LogAuthRequest). If an `AuthRequest` implements this interface, it is completely passed into the logger after an error. Its `LogValue()` will be used by `slog` to print desired fields. This allows omitting sensitive fields you wish not no print. If the interface is not implemented, no `AuthRequest` details will ever be printed.
76
77 #### Server interface
78
79 We've added a new [`Server`](https://pkg.go.dev/github.com/zitadel/oidc/v3/pkg/op#Server) interface. This interface is experimental and subject to change. See [issue 440](https://github.com/zitadel/oidc/issues/440) for the motivation and discussion around this new interface.
80 Usage of the new interface is not required, but may be used for advanced scenarios when working with the `Storage` interface isn't the optimal solution for your app (like we experienced in [Zitadel](https://github.com/zitadel/zitadel)).
81
82 #### AuthRequestError
83
84 `AuthRequestError` now takes the complete `Authorizer` as final argument, instead of only the encoder.
85 This is to facilitate the use of the `Logger` as described above.
86
87 ```bash
88 find . -type f -name '*.go' | xargs sed -i \
89 -e 's/\bAuthRequestError(w, r, authReq, err, authorizer.Encoder())/AuthRequestError(w, r, authReq, err, authorizer)/g'
90 ```
91
92 Note: the sed regex might not find all uses if the local variables of the passed arguments use different names.
93
94 #### AccessTokenVerifier
95
96 `AccessTokenVerifier` interface has become a struct type. `NewAccessTokenVerifier` now returns a pointer to `AccessTokenVerifier`.
97 Variable and struct fields declarations need to be changed from `op.AccessTokenVerifier` to `*op.AccessTokenVerifier`.
98
99 ```bash
100 find . -type f -name '*.go' | xargs sed -i \
101 -e 's/\bop\.AccessTokenVerifier\b/*op.AccessTokenVerifier/g'
102 ```
103
104 #### JWTProfileVerifier
105
106 `JWTProfileVerifier` interface has become a struct type. `NewJWTProfileVerifier` now returns a pointer to `JWTProfileVerifier`.
107 Variable and struct fields declarations need to be changed from `op.JWTProfileVerifier` to `*op.JWTProfileVerifier`.
108
109 ```bash
110 find . -type f -name '*.go' | xargs sed -i \
111 -e 's/\bop\.JWTProfileVerifier\b/*op.JWTProfileVerifier/g'
112 ```
113
114 #### IDTokenHintVerifier
115
116 `IDTokenHintVerifier` interface has become a struct type. `NewIDTokenHintVerifier` now returns a pointer to `IDTokenHintVerifier`.
117 Variable and struct fields declarations need to be changed from `op.IDTokenHintVerifier` to `*op.IDTokenHintVerifier`.
118
119 ```bash
120 find . -type f -name '*.go' | xargs sed -i \
121 -e 's/\bop\.IDTokenHintVerifier\b/*op.IDTokenHintVerifier/g'
122 ```
123
124 #### ParseRequestObject
125
126 `ParseRequestObject` no longer returns `*oidc.AuthRequest` as it already operates on the pointer for the passed `authReq` argument. As such the argument and the return value were the same pointer. Callers can just use the original `*oidc.AuthRequest` now.
127
128 #### Endpoint Configuration
129
130 `Endpoint`s returned from `Configuration` interface methods are now pointers. Usually, `op.Provider` is the main implementation of the `Configuration` interface. However, if a custom implementation is used, you should be able to update it using the following:
131
132 ```bash
133 find . -type f -name '*.go' | xargs sed -i \
134 -e 's/AuthorizationEndpoint() Endpoint/AuthorizationEndpoint() *Endpoint/g' \
135 -e 's/TokenEndpoint() Endpoint/TokenEndpoint() *Endpoint/g' \
136 -e 's/IntrospectionEndpoint() Endpoint/IntrospectionEndpoint() *Endpoint/g' \
137 -e 's/UserinfoEndpoint() Endpoint/UserinfoEndpoint() *Endpoint/g' \
138 -e 's/RevocationEndpoint() Endpoint/RevocationEndpoint() *Endpoint/g' \
139 -e 's/EndSessionEndpoint() Endpoint/EndSessionEndpoint() *Endpoint/g' \
140 -e 's/KeysEndpoint() Endpoint/KeysEndpoint() *Endpoint/g' \
141 -e 's/DeviceAuthorizationEndpoint() Endpoint/DeviceAuthorizationEndpoint() *Endpoint/g'
142 ```
143
144 #### CreateDiscoveryConfig
145
146 `CreateDiscoveryConfig` now takes a context as first argument. The following adds `context.TODO()` to the function:
147
148 ```bash
149 find . -type f -name '*.go' | xargs sed -i \
150 -e 's/op\.CreateDiscoveryConfig(/op.CreateDiscoveryConfig(context.TODO(), /g'
151 ```
152
153 It now takes the issuer out of the context using the [`IssuerFromContext`](https://pkg.go.dev/github.com/zitadel/oidc/v3/pkg/op#IssuerFromContext) functionality,
154 instead of the `config.IssuerFromRequest()` method.
155
156 #### CreateRouter
157
158 `CreateRouter` now returns a `chi.Router` instead of `*mux.Router`.
159 Usually this function is called when the Provider is constructed and not by package consumers.
160 However, if your project does call this function directly, manual update of the code is required.
161
162 #### DeviceAuthorizationStorage
163
164 `DeviceAuthorizationStorage` dropped the following methods:
165
166 - `GetDeviceAuthorizationByUserCode`
167 - `CompleteDeviceAuthorization`
168 - `DenyDeviceAuthorization`
169
170 These methods proved not to be required from a library point of view.
171 Implementations of a device authorization flow may take care of these calls in a way they see fit.
172
173 #### AuthorizeCodeChallenge
174
175 The `AuthorizeCodeChallenge` function now only takes the `CodeVerifier` argument, instead of the complete `*oidc.AccessTokenRequest`.
176
177 ```bash
178 find . -type f -name '*.go' | xargs sed -i \
179 -e 's/op\.AuthorizeCodeChallenge(tokenReq/op.AuthorizeCodeChallenge(tokenReq.CodeVerifier/g'
180 ```
181
182 ### client
183
184 ```go
185 import "github.com/zitadel/oidc/v3/pkg/client"
186 ```
187
188 #### Context
189
190 All client calls now take a context as first argument. The following adds `context.TODO()` to all the affected functions:
191
192 ```bash
193 find . -type f -name '*.go' | xargs sed -i \
194 -e 's/client\.Discover(/client.Discover(context.TODO(), /g' \
195 -e 's/client\.CallTokenEndpoint(/client.CallTokenEndpoint(context.TODO(), /g' \
196 -e 's/client\.CallEndSessionEndpoint(/client.CallEndSessionEndpoint(context.TODO(), /g' \
197 -e 's/client\.CallRevokeEndpoint(/client.CallRevokeEndpoint(context.TODO(), /g' \
198 -e 's/client\.CallTokenExchangeEndpoint(/client.CallTokenExchangeEndpoint(context.TODO(), /g' \
199 -e 's/client\.CallDeviceAuthorizationEndpoint(/client.CallDeviceAuthorizationEndpoint(context.TODO(), /g' \
200 -e 's/client\.JWTProfileExchange(/client.JWTProfileExchange(context.TODO(), /g'
201 ```
202
203 #### keyFile type
204
205 The `keyFile` struct type is now exported a `KeyFile` and returned by the `ConfigFromKeyFile` and `ConfigFromKeyFileData`. No changes are needed on the caller's side.
206
207 ### client/profile
208
209 The package now defines a new interface `TokenSource` which compliments the `oauth2.TokenSource` with a `TokenCtx` method, so that a context can be explicitly added on each call. Users can migrate to the new method when they wish.
210
211 `NewJWTProfileTokenSource` now takes a context as first argument, so do the related `NewJWTProfileTokenSourceFromKeyFile` and `NewJWTProfileTokenSourceFromKeyFileData`. The context is used for the Discovery request.
212
213 ```bash
214 find . -type f -name '*.go' | xargs sed -i \
215 -e 's/profile\.NewJWTProfileTokenSource(/profile.NewJWTProfileTokenSource(context.TODO(), /g' \
216 -e 's/profile\.NewJWTProfileTokenSourceFromKeyFileData(/profile.NewJWTProfileTokenSourceFromKeyFileData(context.TODO(), /g' \
217 -e 's/profile\.NewJWTProfileTokenSourceFromKeyFile(/profile.NewJWTProfileTokenSourceFromKeyFile(context.TODO(), /g'
218 ```
219
220
221 ### client/rp
222
223 ```go
224 import "github.com/zitadel/oidc/v3/pkg/client/rs"
225 ```
226
227 #### Discover
228
229 The `Discover` function has been removed. Use `client.Discover` instead.
230
231 #### Context
232
233 Most `rp` functions now require a context as first argument. The following adds `context.TODO()` to the function that have no additional changes. Functions with more complex changes are documented below.
234
235 ```bash
236 find . -type f -name '*.go' | xargs sed -i \
237 -e 's/rp\.NewRelyingPartyOIDC(/rp.NewRelyingPartyOIDC(context.TODO(), /g' \
238 -e 's/rp\.EndSession(/rp.EndSession(context.TODO(), /g' \
239 -e 's/rp\.RevokeToken(/rp.RevokeToken(context.TODO(), /g' \
240 -e 's/rp\.DeviceAuthorization(/rp.DeviceAuthorization(context.TODO(), /g'
241 ```
242
243 Remember to replace `context.TODO()` with a context that is applicable for your app, where possible.
244
245 #### RefreshAccessToken
246
247 1. Renamed to `RefreshTokens`;
248 2. A context must be passed;
249 3. An `*oidc.Tokens` object is now returned, which included an ID Token if it was returned by the server;
250 4. The function is now generic and requires a type argument for the `IDTokenClaims` implementation inside the returned `oidc.Tokens` object;
251
252 For most use cases `*oidc.IDTokenClaims` can be used as type argument. A custom implementation of `oidc.IDClaims` can be used if type-safe access to custom claims is required.
253
254 ```bash
255 find . -type f -name '*.go' | xargs sed -i \
256 -e 's/rp\.RefreshAccessToken(/rp.RefreshTokens[*oidc.IDTokenClaims](context.TODO(), /g'
257 ```
258
259 Users that called `tokens.Extra("id_token").(string)` and a subsequent `VerifyTokens` to get the claims, no longer need to do this. The ID token is verified (when present) by `RefreshTokens` already.
260
261
262 #### Userinfo
263
264 1. A context must be passed as first argument;
265 2. The function is now generic and requires a type argument for the returned user info object;
266
267 For most use cases `*oidc.UserInfo` can be used a type argument. A [custom implementation](https://pkg.go.dev/github.com/zitadel/oidc/v3/pkg/client/rp#example-Userinfo-Custom) of `rp.SubjectGetter` can be used if type-safe access to custom claims is required.
268
269 ```bash
270 find . -type f -name '*.go' | xargs sed -i \
271 -e 's/rp\.Userinfo(/rp.Userinfo[*oidc.UserInfo](context.TODO(), /g'
272 ```
273
274 #### UserinfoCallback
275
276 `UserinfoCallback` has an additional type argument for the `UserInfo` object. Typically, the type argument can be inferred by the compiler, by the function that is passed. The actual code update cannot be done by a simple `sed` script and depends on how the caller implemented the function.
277
278
279 #### IDTokenVerifier
280
281 `IDTokenVerifier` interface has become a struct type. `NewIDTokenVerifier` now returns a pointer to `IDTokenVerifier`.
282 Variable and struct fields declarations need to be changed from `rp.IDTokenVerifier` to `*rp.AccessTokenVerifier`.
283
284 ```bash
285 find . -type f -name '*.go' | xargs sed -i \
286 -e 's/\brp\.IDTokenVerifier\b/*rp.IDTokenVerifier/g'
287 ```
288
289 ### client/rs
290
291 ```go
292 import "github.com/zitadel/oidc/v3/pkg/client/rs"
293 ```
294
295 #### NewResourceServer
296
297 The `NewResourceServerClientCredentials` and `NewResourceServerJWTProfile` constructor functions now take a context as first argument.
298
299 ```bash
300 find . -type f -name '*.go' | xargs sed -i \
301 -e 's/rs\.NewResourceServerClientCredentials(/rs.NewResourceServerClientCredentials(context.TODO(), /g' \
302 -e 's/rs\.NewResourceServerJWTProfile(/rs.NewResourceServerJWTProfile(context.TODO(), /g'
303 ```
304
305 #### Introspect
306
307 `Introspect` is now generic and requires a type argument for the returned introspection response. For most use cases `*oidc.IntrospectionResponse` can be used as type argument. Any other response type if type-safe access to [custom claims](https://pkg.go.dev/github.com/zitadel/oidc/v3/pkg/client/rs#example-Introspect-Custom) is required.
308
309 ```bash
310 find . -type f -name '*.go' | xargs sed -i \
311 -e 's/rs\.Introspect(/rs.Introspect[*oidc.IntrospectionResponse](/g'
312 ```
313
314 ### client/tokenexchange
315
316 The `TokenExchanger` constructor functions `NewTokenExchanger` and `NewTokenExchangerClientCredentials` now take a context as first argument.
317 As well as the `ExchangeToken` function.
318
319 ```bash
320 find . -type f -name '*.go' | xargs sed -i \
321 -e 's/tokenexchange\.NewTokenExchanger(/tokenexchange.NewTokenExchanger(context.TODO(), /g' \
322 -e 's/tokenexchange\.NewTokenExchangerClientCredentials(/tokenexchange.NewTokenExchangerClientCredentials(context.TODO(), /g' \
323 -e 's/tokenexchange\.ExchangeToken(/tokenexchange.ExchangeToken(context.TODO(), /g'
324 ```
325
326 ### oidc
327
328 #### SpaceDelimitedArray
329
330 The `SpaceDelimitedArray` type's `Encode()` function has been renamed to `String()` so it implements the `fmt.Stringer` interface. If the `Encode` method was called by a package consumer, it should be changed manually.
331
332 #### Verifier
333
334 The `Verifier` interface as been changed into a struct type. The struct type is aliased in the `op` and `rp` packages for the specific token use cases. See the relevant section above.
335
336 ### Full script
337
338 For the courageous this is the full `sed` script which combines all the steps described above.
339 It should migrate most of the code in a repository to a more-or-less compilable state,
340 using defaults such as `context.TODO()` where possible.
341
342 Warnings:
343 - Again, this is written for **GNU sed** not the posix variant.
344 - Assumes imports that use the package names, not aliases.
345 - Do this on a project with version control (e.g. Git), that allows you to rollback if things went wrong.
346 - The script has been tested on the [ZITADEL](https://github.com/zitadel/zitadel) project, but we do not use all affected symbols. Parts of the script are mere guesswork.
347
348 ```bash
349 go get -u github.com/zitadel/oidc/v3
350 find . -type f -name '*.go' | xargs sed -i \
351 -e 's/github\.com\/zitadel\/oidc\/v2/github.com\/zitadel\/oidc\/v3/g' \
352 -e 's/gopkg.in\/square\/go-jose\.v2/github.com\/go-jose\/go-jose\/v3/g' \
353 -e 's/\bAuthRequestError(w, r, authReq, err, authorizer.Encoder())/AuthRequestError(w, r, authReq, err, authorizer)/g' \
354 -e 's/\bop\.AccessTokenVerifier\b/*op.AccessTokenVerifier/g' \
355 -e 's/\bop\.JWTProfileVerifier\b/*op.JWTProfileVerifier/g' \
356 -e 's/\bop\.IDTokenHintVerifier\b/*op.IDTokenHintVerifier/g' \
357 -e 's/AuthorizationEndpoint() Endpoint/AuthorizationEndpoint() *Endpoint/g' \
358 -e 's/TokenEndpoint() Endpoint/TokenEndpoint() *Endpoint/g' \
359 -e 's/IntrospectionEndpoint() Endpoint/IntrospectionEndpoint() *Endpoint/g' \
360 -e 's/UserinfoEndpoint() Endpoint/UserinfoEndpoint() *Endpoint/g' \
361 -e 's/RevocationEndpoint() Endpoint/RevocationEndpoint() *Endpoint/g' \
362 -e 's/EndSessionEndpoint() Endpoint/EndSessionEndpoint() *Endpoint/g' \
363 -e 's/KeysEndpoint() Endpoint/KeysEndpoint() *Endpoint/g' \
364 -e 's/DeviceAuthorizationEndpoint() Endpoint/DeviceAuthorizationEndpoint() *Endpoint/g' \
365 -e 's/op\.CreateDiscoveryConfig(/op.CreateDiscoveryConfig(context.TODO(), /g' \
366 -e 's/op\.AuthorizeCodeChallenge(tokenReq/op.AuthorizeCodeChallenge(tokenReq.CodeVerifier/g' \
367 -e 's/client\.Discover(/client.Discover(context.TODO(), /g' \
368 -e 's/client\.CallTokenEndpoint(/client.CallTokenEndpoint(context.TODO(), /g' \
369 -e 's/client\.CallEndSessionEndpoint(/client.CallEndSessionEndpoint(context.TODO(), /g' \
370 -e 's/client\.CallRevokeEndpoint(/client.CallRevokeEndpoint(context.TODO(), /g' \
371 -e 's/client\.CallTokenExchangeEndpoint(/client.CallTokenExchangeEndpoint(context.TODO(), /g' \
372 -e 's/client\.CallDeviceAuthorizationEndpoint(/client.CallDeviceAuthorizationEndpoint(context.TODO(), /g' \
373 -e 's/client\.JWTProfileExchange(/client.JWTProfileExchange(context.TODO(), /g' \
374 -e 's/profile\.NewJWTProfileTokenSource(/profile.NewJWTProfileTokenSource(context.TODO(), /g' \
375 -e 's/profile\.NewJWTProfileTokenSourceFromKeyFileData(/profile.NewJWTProfileTokenSourceFromKeyFileData(context.TODO(), /g' \
376 -e 's/profile\.NewJWTProfileTokenSourceFromKeyFile(/profile.NewJWTProfileTokenSourceFromKeyFile(context.TODO(), /g' \
377 -e 's/rp\.NewRelyingPartyOIDC(/rp.NewRelyingPartyOIDC(context.TODO(), /g' \
378 -e 's/rp\.EndSession(/rp.EndSession(context.TODO(), /g' \
379 -e 's/rp\.RevokeToken(/rp.RevokeToken(context.TODO(), /g' \
380 -e 's/rp\.DeviceAuthorization(/rp.DeviceAuthorization(context.TODO(), /g' \
381 -e 's/rp\.RefreshAccessToken(/rp.RefreshTokens[*oidc.IDTokenClaims](context.TODO(), /g' \
382 -e 's/rp\.Userinfo(/rp.Userinfo[*oidc.UserInfo](context.TODO(), /g' \
383 -e 's/\brp\.IDTokenVerifier\b/*rp.IDTokenVerifier/g' \
384 -e 's/rs\.NewResourceServerClientCredentials(/rs.NewResourceServerClientCredentials(context.TODO(), /g' \
385 -e 's/rs\.NewResourceServerJWTProfile(/rs.NewResourceServerJWTProfile(context.TODO(), /g' \
386 -e 's/rs\.Introspect(/rs.Introspect[*oidc.IntrospectionResponse](/g' \
387 -e 's/tokenexchange\.NewTokenExchanger(/tokenexchange.NewTokenExchanger(context.TODO(), /g' \
388 -e 's/tokenexchange\.NewTokenExchangerClientCredentials(/tokenexchange.NewTokenExchangerClientCredentials(context.TODO(), /g' \
389 -e 's/tokenexchange\.ExchangeToken(/tokenexchange.ExchangeToken(context.TODO(), /g'
390 go mod tidy
391 ```