Debian Codegraph @

example/server/storage/client.go

raw
1 package storage
2
3 import (
4 "time"
5
6 "github.com/zitadel/oidc/v3/pkg/oidc"
7 "github.com/zitadel/oidc/v3/pkg/op"
8 )
9
10 var (
11 // we use the default login UI and pass the (auth request) id
12 defaultLoginURL = func(id string) string {
13 return "/login/username?authRequestID=" + id
14 }
15
16 // clients to be used by the storage interface
17 clients = map[string]*Client{}
18 )
19
20 // Client represents the storage model of an OAuth/OIDC client
21 // this could also be your database model
22 type Client struct {
23 id string
24 secret string
25 redirectURIs []string
26 applicationType op.ApplicationType
27 authMethod oidc.AuthMethod
28 loginURL func(string) string
29 responseTypes []oidc.ResponseType
30 grantTypes []oidc.GrantType
31 accessTokenType op.AccessTokenType
32 devMode bool
33 idTokenUserinfoClaimsAssertion bool
34 clockSkew time.Duration
35 postLogoutRedirectURIGlobs []string
36 redirectURIGlobs []string
37 }
38
39 // GetID must return the client_id
40 func (c *Client) GetID() string {
41 return c.id
42 }
43
44 // RedirectURIs must return the registered redirect_uris for Code and Implicit Flow
45 func (c *Client) RedirectURIs() []string {
46 return c.redirectURIs
47 }
48
49 // PostLogoutRedirectURIs must return the registered post_logout_redirect_uris for sign-outs
50 func (c *Client) PostLogoutRedirectURIs() []string {
51 return []string{}
52 }
53
54 // ApplicationType must return the type of the client (app, native, user agent)
55 func (c *Client) ApplicationType() op.ApplicationType {
56 return c.applicationType
57 }
58
59 // AuthMethod must return the authentication method (client_secret_basic, client_secret_post, none, private_key_jwt)
60 func (c *Client) AuthMethod() oidc.AuthMethod {
61 return c.authMethod
62 }
63
64 // ResponseTypes must return all allowed response types (code, id_token token, id_token)
65 // these must match with the allowed grant types
66 func (c *Client) ResponseTypes() []oidc.ResponseType {
67 return c.responseTypes
68 }
69
70 // GrantTypes must return all allowed grant types (authorization_code, refresh_token, urn:ietf:params:oauth:grant-type:jwt-bearer)
71 func (c *Client) GrantTypes() []oidc.GrantType {
72 return c.grantTypes
73 }
74
75 // LoginURL will be called to redirect the user (agent) to the login UI
76 // you could implement some logic here to redirect the users to different login UIs depending on the client
77 func (c *Client) LoginURL(id string) string {
78 return c.loginURL(id)
79 }
80
81 // AccessTokenType must return the type of access token the client uses (Bearer (opaque) or JWT)
82 func (c *Client) AccessTokenType() op.AccessTokenType {
83 return c.accessTokenType
84 }
85
86 // IDTokenLifetime must return the lifetime of the client's id_tokens
87 func (c *Client) IDTokenLifetime() time.Duration {
88 return 1 * time.Hour
89 }
90
91 // DevMode enables the use of non-compliant configs such as redirect_uris (e.g. http schema for user agent client)
92 func (c *Client) DevMode() bool {
93 return c.devMode
94 }
95
96 // RestrictAdditionalIdTokenScopes allows specifying which custom scopes shall be asserted into the id_token
97 func (c *Client) RestrictAdditionalIdTokenScopes() func(scopes []string) []string {
98 return func(scopes []string) []string {
99 return scopes
100 }
101 }
102
103 // RestrictAdditionalAccessTokenScopes allows specifying which custom scopes shall be asserted into the JWT access_token
104 func (c *Client) RestrictAdditionalAccessTokenScopes() func(scopes []string) []string {
105 return func(scopes []string) []string {
106 return scopes
107 }
108 }
109
110 // IsScopeAllowed enables Client specific custom scopes validation
111 // in this example we allow the CustomScope for all clients
112 func (c *Client) IsScopeAllowed(scope string) bool {
113 return scope == CustomScope
114 }
115
116 // IDTokenUserinfoClaimsAssertion allows specifying if claims of scope profile, email, phone and address are asserted into the id_token
117 // even if an access token if issued which violates the OIDC Core spec
118 // (5.4. Requesting Claims using Scope Values: https://openid.net/specs/openid-connect-core-1_0.html#ScopeClaims)
119 // some clients though require that e.g. email is always in the id_token when requested even if an access_token is issued
120 func (c *Client) IDTokenUserinfoClaimsAssertion() bool {
121 return c.idTokenUserinfoClaimsAssertion
122 }
123
124 // ClockSkew enables clients to instruct the OP to apply a clock skew on the various times and expirations
125 // (subtract from issued_at, add to expiration, ...)
126 func (c *Client) ClockSkew() time.Duration {
127 return c.clockSkew
128 }
129
130 // RegisterClients enables you to register clients for the example implementation
131 // there are some clients (web and native) to try out different cases
132 // add more if necessary.
133 //
134 // RegisterClients should be called before the Storage is used so that there are
135 // no race conditions.
136 func RegisterClients(registerClients ...*Client) {
137 for _, client := range registerClients {
138 clients[client.id] = client
139 }
140 }
141
142 // NativeClient will create a client of type native, which will always use PKCE and allow the use of refresh tokens
143 // user-defined redirectURIs may include:
144 // - http://localhost without port specification (e.g. http://localhost/auth/callback)
145 // - custom protocol (e.g. custom://auth/callback)
146 // (the examples will be used as default, if none is provided)
147 func NativeClient(id string, redirectURIs ...string) *Client {
148 if len(redirectURIs) == 0 {
149 redirectURIs = []string{
150 "http://localhost/auth/callback",
151 "custom://auth/callback",
152 }
153 }
154 return &Client{
155 id: id,
156 secret: "", // no secret needed (due to PKCE)
157 redirectURIs: redirectURIs,
158 applicationType: op.ApplicationTypeNative,
159 authMethod: oidc.AuthMethodNone,
160 loginURL: defaultLoginURL,
161 responseTypes: []oidc.ResponseType{oidc.ResponseTypeCode},
162 grantTypes: []oidc.GrantType{oidc.GrantTypeCode, oidc.GrantTypeRefreshToken},
163 accessTokenType: op.AccessTokenTypeBearer,
164 devMode: false,
165 idTokenUserinfoClaimsAssertion: false,
166 clockSkew: 0,
167 }
168 }
169
170 // WebClient will create a client of type web, which will always use Basic Auth and allow the use of refresh tokens
171 // user-defined redirectURIs may include:
172 // - http://localhost with port specification (e.g. http://localhost:9999/auth/callback)
173 // (the example will be used as default, if none is provided)
174 func WebClient(id, secret string, redirectURIs ...string) *Client {
175 if len(redirectURIs) == 0 {
176 redirectURIs = []string{
177 "http://localhost:9999/auth/callback",
178 }
179 }
180 return &Client{
181 id: id,
182 secret: secret,
183 redirectURIs: redirectURIs,
184 applicationType: op.ApplicationTypeWeb,
185 authMethod: oidc.AuthMethodBasic,
186 loginURL: defaultLoginURL,
187 responseTypes: []oidc.ResponseType{oidc.ResponseTypeCode, oidc.ResponseTypeIDTokenOnly, oidc.ResponseTypeIDToken},
188 grantTypes: []oidc.GrantType{oidc.GrantTypeCode, oidc.GrantTypeRefreshToken, oidc.GrantTypeTokenExchange},
189 accessTokenType: op.AccessTokenTypeBearer,
190 devMode: true,
191 idTokenUserinfoClaimsAssertion: false,
192 clockSkew: 0,
193 }
194 }
195
196 // DeviceClient creates a device client with Basic authentication.
197 func DeviceClient(id, secret string) *Client {
198 return &Client{
199 id: id,
200 secret: secret,
201 redirectURIs: nil,
202 applicationType: op.ApplicationTypeWeb,
203 authMethod: oidc.AuthMethodBasic,
204 loginURL: defaultLoginURL,
205 responseTypes: []oidc.ResponseType{oidc.ResponseTypeCode},
206 grantTypes: []oidc.GrantType{oidc.GrantTypeDeviceCode},
207 accessTokenType: op.AccessTokenTypeBearer,
208 devMode: false,
209 idTokenUserinfoClaimsAssertion: false,
210 clockSkew: 0,
211 }
212 }
213
214 type hasRedirectGlobs struct {
215 *Client
216 }
217
218 // RedirectURIGlobs provide wildcarding for additional valid redirects
219 func (c hasRedirectGlobs) RedirectURIGlobs() []string {
220 return c.redirectURIGlobs
221 }
222
223 // PostLogoutRedirectURIGlobs provide extra wildcarding for additional valid redirects
224 func (c hasRedirectGlobs) PostLogoutRedirectURIGlobs() []string {
225 return c.postLogoutRedirectURIGlobs
226 }
227
228 // RedirectGlobsClient wraps the client in an op.HasRedirectGlobs
229 // only if DevMode is enabled.
230 func RedirectGlobsClient(client *Client) op.Client {
231 if client.devMode {
232 return hasRedirectGlobs{client}
233 }
234 return client
235 }