-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathoidc.go
347 lines (286 loc) · 15 KB
/
oidc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package traefik_oidc_auth
import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"path"
"strings"
"github.com/golang-jwt/jwt/v5"
)
type OidcEndpoints struct {
AuthorizationEndpoint string `json:"authorization_endpoint"`
BackchannelAuthenticationEndpoint string `json:"backchannel_authentication_endpoint"`
DeviceAuthorizationEndpoint string `json:"device_authorization_endpoint"`
EndSessionEndpoint string `json:"end_session_endpoint"`
IntrospectionEndpoint string `json:"introspection_endpoint"`
KerberosEndpoint string `json:"kerberos_endpoint"`
PushedAuthorizationRequestEndpoint string `json:"pushed_authorization_request_endpoint"`
RegistrationEndpoint string `json:"registration_endpoint"`
RevocationEndpoint string `json:"revocation_endpoint"`
TokenEndpoint string `json:"token_endpoint"`
TokenRevocationEndpoint string `json:"token_revocation_endpoint"`
UserinfoEndpoint string `json:"userinfo_endpoint"`
}
// OidcDiscovery represents the discovered OIDC endpoints
type OidcDiscovery struct {
AcrValuesSupported []string `json:"acr_values_supported"`
AuthorizationEncryptionAlgValuesSupported []string `json:"authorization_encryption_alg_values_supported"`
AuthorizationEncryptionEncValuesSupported []string `json:"authorization_encryption_enc_values_supported"`
AuthorizationEndpoint string `json:"authorization_endpoint"`
AuthorizationSigningAlgValuesSupported []string `json:"authorization_signing_alg_values_supported"`
BackchannelAuthenticationEndpoint string `json:"backchannel_authentication_endpoint"`
BackchannelAuthenticationRequestSigningAlgValuesSupported []string `json:"backchannel_authentication_request_signing_alg_values_supported"`
BackchannelLogoutSessionSupported bool `json:"backchannel_logout_session_supported"`
BackchannelLogoutSupported bool `json:"backchannel_logout_supported"`
BackchannelTokenDeliveryModesSupported []string `json:"backchannel_token_delivery_modes_supported"`
CheckSessionIframe string `json:"check_session_iframe"`
ClaimsParameterSupported bool `json:"claims_parameter_supported"`
ClaimsSupported []string `json:"claims_supported"`
ClaimTypesSupported []string `json:"claim_types_supported"`
CloudGraphHostName string `json:"cloud_graph_host_name"`
CloudInstanceName string `json:"cloud_instance_name"`
CodeChallengeMethodsSupported []string `json:"code_challenge_methods_supported"`
DeviceAuthorizationEndpoint string `json:"device_authorization_endpoint"`
DisplayValuesSupported []string `json:"display_values_supported"`
EndSessionEndpoint string `json:"end_session_endpoint"`
FrontchannelLogoutSessionSupported bool `json:"frontchannel_logout_session_supported"`
FrontchannelLogoutSupported bool `json:"frontchannel_logout_supported"`
GrantTypesSupported []string `json:"grant_types_supported"`
HttpLogoutSupported bool `json:"http_logout_supported"`
IdTokenEncryptionAlgValuesSupported []string `json:"id_token_encryption_alg_values_supported"`
IdTokenEncryptionEncValuesSupported []string `json:"id_token_encryption_enc_values_supported"`
IdTokenSigningAlgValuesSupported []string `json:"id_token_signing_alg_values_supported"`
IntrospectionEndpoint string `json:"introspection_endpoint"`
IntrospectionEndpointAuthMethodsSupported []string `json:"introspection_endpoint_auth_methods_supported"`
IntrospectionEndpointAuthSigningAlgValuesSupported []string `json:"introspection_endpoint_auth_signing_alg_values_supported"`
Issuer string `json:"issuer"`
JWKSURI string `json:"jwks_uri"`
KerberosEndpoint string `json:"kerberos_endpoint"`
MicrosoftGraphHost string `json:"msgraph_host"`
MtlsEndpointAliases *OidcEndpoints `json:"mtls_endpoint_aliases"`
PushedAuthorizationRequestEndpoint string `json:"pushed_authorization_request_endpoint"`
RbacURL string `json:"rbac_url"`
RegistrationEndpoint string `json:"registration_endpoint"`
RequestObjectEncryptionAlgValuesSupported []string `json:"request_object_encryption_alg_values_supported"`
RequestObjectEncryptionEncValuesSupported []string `json:"request_object_encryption_enc_values_supported"`
RequestObjectSigningAlgValuesSupported []string `json:"request_object_signing_alg_values_supported"`
RequestParameterSupported bool `json:"request_parameter_supported"`
RequestURIParameterSupported bool `json:"request_uri_parameter_supported"`
RequirePushedAuthorizationRequests bool `json:"require_pushed_authorization_requests"`
RequireRequestUriRegistration bool `json:"require_request_uri_registration"`
ResponseModesSupported []string `json:"response_modes_supported"`
ResponseTypesSupported []string `json:"response_types_supported"`
RevocationEndpoint string `json:"revocation_endpoint"`
RevocationEndpointAuthMethodsSupported []string `json:"revocation_endpoint_auth_methods_supported"`
RevocationEndpointAuthSigningAlgValuesSupported []string `json:"revocation_endpoint_auth_signing_alg_values_supported"`
ScopesSupported []string `json:"scopes_supported"`
SubjectTypesSupported []string `json:"subject_types_supported"`
TenantRegionScope string `json:"tenant_region_scope"`
TlsClientCertificateBoundAccessTokens bool `json:"tls_client_certificate_bound_access_tokens"`
TokenEndpoint string `json:"token_endpoint"`
TokenEndpointAuthMethodsSupported []string `json:"token_endpoint_auth_methods_supported"`
TokenEndpointAuthSigningAlgValuesSupported []string `json:"token_endpoint_auth_signing_alg_values_supported"`
TokenRevocationEndpoint string `json:"token_revocation_endpoint"`
UserinfoEncryptionAlgValuesSupported []string `json:"userinfo_encryption_alg_values_supported"`
UserinfoEncryptionEncValuesSupported []string `json:"userinfo_encryption_enc_values_supported"`
UserinfoEndpoint string `json:"userinfo_endpoint"`
UserinfoSigningAlgValuesSupported []string `json:"userinfo_signing_alg_values_supported"`
}
type OidcTokenResponse struct {
AccessToken string `json:"access_token"`
IdToken string `json:"id_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
}
type OidcIntrospectionResponse struct {
Active bool `json:"active"`
}
type OidcState struct {
Action string `json:"action"`
RedirectUrl string `json:"redirect_url"`
}
func GetOidcDiscovery(logLevel string, providerUrl *url.URL) (*OidcDiscovery, error) {
wellKnownUrl := *providerUrl
wellKnownUrl.Path = path.Join(wellKnownUrl.Path, ".well-known/openid-configuration")
// // create a http client with configurable options
// // needed to skip certificate verification
// tr := &http.Transport{
// MaxIdleConns: 10,
// IdleConnTimeout: 30 * time.Second,
// TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
// }
// client := &http.Client{Transport: tr}
// Make HTTP GET request to the OpenID provider's discovery endpoint
resp, err := http.Get(wellKnownUrl.String())
if err != nil {
log(logLevel, LogLevelError, "http-get discovery endpoints - Err: %s", err.Error())
return nil, errors.New("HTTP GET error")
}
defer resp.Body.Close()
// Check if the response status code is successful
if resp.StatusCode >= 300 {
log(logLevel, LogLevelError, "http-get OIDC discovery endpoints - http status code: %s", resp.Status)
return nil, errors.New("HTTP error - Status code: " + resp.Status)
}
// Decode the JSON response
document := OidcDiscovery{}
err = json.NewDecoder(resp.Body).Decode(&document)
if err != nil {
log(logLevel, LogLevelError, "Failed to decode OIDC discovery document. Status code: %s", err.Error())
return &document, errors.New("Failed to decode OIDC discovery document. Status code: " + err.Error())
}
return &document, nil
}
func randomBytesInHex(count int) (string, error) {
buf := make([]byte, count)
_, err := io.ReadFull(rand.Reader, buf)
if err != nil {
return "", fmt.Errorf("could not generate %d random bytes: %v", count, err)
}
return hex.EncodeToString(buf), nil
}
func exchangeAuthCode(oidcAuth *TraefikOidcAuth, req *http.Request, authCode string) (*OidcTokenResponse, error) {
redirectUrl := oidcAuth.GetAbsoluteCallbackURL(req).String()
urlValues := url.Values{
"grant_type": {"authorization_code"},
"client_id": {oidcAuth.Config.Provider.ClientId},
"code": {authCode},
"redirect_uri": {redirectUrl},
}
if oidcAuth.Config.Provider.ClientSecret != "" {
urlValues.Add("client_secret", oidcAuth.Config.Provider.ClientSecret)
}
if oidcAuth.Config.Provider.UsePkce {
codeVerifierCookie, err := req.Cookie("CodeVerifier")
if err != nil {
return nil, err
}
codeVerifier, err := decrypt(codeVerifierCookie.Value, oidcAuth.Config.Secret)
if err != nil {
return nil, err
}
urlValues.Add("code_verifier", codeVerifier)
}
resp, err := http.PostForm(oidcAuth.DiscoveryDocument.TokenEndpoint, urlValues)
if err != nil {
log(oidcAuth.Config.LogLevel, LogLevelError, "Sending AuthorizationCode in POST: %s", err.Error())
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
log(oidcAuth.Config.LogLevel, LogLevelError, "Received bad HTTP response from Provider: %s", string(body))
return nil, err
}
tokenResponse := &OidcTokenResponse{}
err = json.NewDecoder(resp.Body).Decode(tokenResponse)
if err != nil {
log(oidcAuth.Config.LogLevel, LogLevelError, "Decoding OidcTokenResponse: %s", err.Error())
return nil, err
}
return tokenResponse, nil
}
func (toa *TraefikOidcAuth) validateTokenLocally(tokenString string) (bool, map[string]interface{}, error) {
claims := jwt.MapClaims{}
err := toa.Jwks.EnsureLoaded(toa, false)
if err != nil {
return false, nil, err
}
options := []jwt.ParserOption{
jwt.WithExpirationRequired(),
}
if toa.Config.Provider.ValidateIssuer {
options = append(options, jwt.WithIssuer(toa.Config.Provider.ValidIssuer))
}
if toa.Config.Provider.ValidateAudience {
options = append(options, jwt.WithAudience(toa.Config.Provider.ValidAudience))
}
parser := jwt.NewParser(options...)
_, err = parser.ParseWithClaims(tokenString, claims, toa.Jwks.Keyfunc)
if err != nil {
err := toa.Jwks.EnsureLoaded(toa, true)
if err != nil {
return false, nil, err
}
_, err = parser.ParseWithClaims(tokenString, claims, toa.Jwks.Keyfunc)
if err != nil {
return false, nil, err
}
}
return true, claims, nil
}
func (toa *TraefikOidcAuth) introspectToken(token string) (bool, map[string]interface{}, error) {
client := &http.Client{}
data := url.Values{
"token": {token},
}
//log(toa.Config.LogLevel, LogLevelDebug, "Token: %s", token)
endpoint := toa.DiscoveryDocument.IntrospectionEndpoint
//if endpoint == "" {
// endpoint = toa.DiscoveryDocument.UserinfoEndpoint
//}
req, err := http.NewRequest(
http.MethodPost,
endpoint,
strings.NewReader(data.Encode()),
)
if err != nil {
return false, nil, err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.SetBasicAuth(toa.Config.Provider.ClientId, toa.Config.Provider.ClientSecret)
resp, err := client.Do(req)
if err != nil {
log(toa.Config.LogLevel, LogLevelError, "Error on introspection request: %s", err.Error())
return false, nil, err
}
defer resp.Body.Close()
var introspectResponse map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&introspectResponse)
if err != nil {
log(toa.Config.LogLevel, LogLevelError, "Failed to decode introspection response: %s", err.Error())
return false, nil, err
}
// TODO: Remove
//toa.logAvailableClaims(introspectResponse)
if introspectResponse["active"] != nil {
return introspectResponse["active"].(bool), introspectResponse, nil
} else {
return false, nil, errors.New("received invalid introspection response")
}
}
func (toa *TraefikOidcAuth) renewToken(refreshToken string) (*OidcTokenResponse, error) {
urlValues := url.Values{
"grant_type": {"refresh_token"},
"client_id": {toa.Config.Provider.ClientId},
"scope": {strings.Join(toa.Config.Scopes, " ")},
"refresh_token": {refreshToken},
}
if toa.Config.Provider.ClientSecret != "" {
urlValues.Add("client_secret", toa.Config.Provider.ClientSecret)
}
resp, err := http.PostForm(toa.DiscoveryDocument.TokenEndpoint, urlValues)
if err != nil {
log(toa.Config.LogLevel, LogLevelError, "Sending token renewal request in POST: %s", err.Error())
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
log(toa.Config.LogLevel, LogLevelError, "Received bad HTTP response from Provider: %s", string(body))
return nil, err
}
tokenResponse := &OidcTokenResponse{}
err = json.NewDecoder(resp.Body).Decode(tokenResponse)
if err != nil {
log(toa.Config.LogLevel, LogLevelError, "Decoding OidcTokenResponse: %s", err.Error())
return nil, err
}
return tokenResponse, nil
}