/
middlewares.go
391 lines (329 loc) · 11.6 KB
/
middlewares.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package app
import (
//"fmt"
"net/http"
"net/url"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
oidc "github.com/coreos/go-oidc"
"golang.org/x/oauth2"
"golang.org/x/net/context"
"github.com/opensentry/idpui/config"
idp "github.com/opensentry/idp/client"
bulky "github.com/charmixer/bulky/client"
)
func RequireScopes(env *Environment, requiredScopes ...string) gin.HandlerFunc {
fn := func(c *gin.Context) {
log := c.MustGet(env.Constants.LogKey).(*logrus.Entry)
log = log.WithFields(logrus.Fields{
"func": "RequireScopes",
})
if len(requiredScopes) <= 0 {
log.Debug("'Missing required scopes'")
c.AbortWithStatus(http.StatusInternalServerError)
return
}
_requiredScopes := FetchRequiredScopes(env, c)
_requiredScopes = append(_requiredScopes, requiredScopes...)
c.Set(env.Constants.ContextRequiredScopesKey, _requiredScopes)
c.Next()
return
}
return gin.HandlerFunc(fn)
}
func FetchRequiredScopes(env *Environment, c *gin.Context) (requiredScopes []string) {
t, exists := c.Get(env.Constants.ContextRequiredScopesKey)
if exists == true {
return t.([]string)
}
return nil
}
func UsePrecalculatedStateFromQuery(env *Environment, queryParamKey string) gin.HandlerFunc {
fn := func(c *gin.Context) {
log := c.MustGet(env.Constants.LogKey).(*logrus.Entry)
log = log.WithFields(logrus.Fields{
"func": "UsePrecalculatedState",
})
u, err := url.Parse(c.Request.RequestURI)
if err != nil {
log.Debug(err.Error())
c.AbortWithStatus(http.StatusInternalServerError)
return
}
q := u.Query()
state := q.Get(queryParamKey)
if state != "" {
c.Set(env.Constants.ContextPrecalculatedStateKey, state)
}
c.Next()
return
}
return gin.HandlerFunc(fn)
}
func FetchPrecalculatedState(env *Environment, c *gin.Context) (precaluclatedState string) {
t, exists := c.Get(env.Constants.ContextPrecalculatedStateKey)
if exists == true {
return t.(string)
}
return ""
}
func ConfigureOauth2(env *Environment) gin.HandlerFunc {
fn := func(c *gin.Context) {
log := c.MustGet(env.Constants.LogKey).(*logrus.Entry)
log = log.WithFields(logrus.Fields{
"func": "ConfigureOauth2",
})
requiredScopes := FetchRequiredScopes(env, c)
if requiredScopes == nil {
log.Debug("Missing required scopes")
c.AbortWithStatus(http.StatusInternalServerError)
return
}
// Setup the OAuth2 config for the request.
endpoint := env.Provider.Endpoint()
endpoint.AuthStyle = 2 // Force basic secret, so token exchange does not auto to post which we did not allow.
exchangeRedirectUrl, err := createAuthorizationCodeExchangeUrl(c.Request)
if err != nil {
log.Debug(err.Error())
c.AbortWithStatus(http.StatusInternalServerError)
return
}
var oauth2Config *oauth2.Config = &oauth2.Config{
ClientID: env.ClientId,
ClientSecret: env.ClientSecret,
Endpoint: endpoint,
RedirectURL: exchangeRedirectUrl,
Scopes: requiredScopes,
}
if oauth2Config == nil {
log.Debug("Create oauth2 config failed")
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.Set(env.Constants.ContextOAuth2ConfigKey, oauth2Config) // Save as pointer.
c.Next()
return
}
return gin.HandlerFunc(fn)
}
func FetchOAuth2Config(env *Environment, c *gin.Context) (*oauth2.Config) {
t, exists := c.Get(env.Constants.ContextOAuth2ConfigKey) // Read as pointer
if exists == true {
return t.(*oauth2.Config)
}
return nil
}
// This implements authorization code flow exchange controller functionality to prevent session storage on normal callback endpoints.
func RequestTokenUsingAuthorizationCode(env *Environment) gin.HandlerFunc {
fn := func(c *gin.Context) {
log := c.MustGet(env.Constants.LogKey).(*logrus.Entry)
log = log.WithFields(logrus.Fields{
"func": "RequestTokenUsingAuthorizationCode",
})
error := c.Query("error"); // Hydra specific error handling
if error != "" {
c.AbortWithStatus(http.StatusBadRequest)
return
}
oauth2Config := FetchOAuth2Config(env, c)
if oauth2Config == nil {
log.Debug("Missing oauth2 config. Hint: Oauth2 config is missing from context. Did you call ConfigureOauth2 before calling RequestTokenUsingAuthorizationCode?")
c.AbortWithStatus(http.StatusInternalServerError)
return
}
code := c.Query("code")
if code == "" {
// Unauthorized, request an access token for required scopes only using authorization code flow.
// TODO: Add pkce
idTokenHint := IdTokenHint(env, c)
// Use precalculated state iff present
state := FetchPrecalculatedState(env, c)
initUrl, err := StartAuthenticationSession(env, c, oauth2Config, idTokenHint, state)
if err != nil {
log.Debug(err.Error())
c.AbortWithStatus(http.StatusInternalServerError)
return
}
// This is not allowed by ORY Hydra even though the url (without params) is registered to the client
/*emailChallenge := c.Query("email_challenge")
if emailChallenge != "" {
q := initUrl.Query()
redirectUri := q.Get("redirect_uri")
if redirectUri != "" {
_redirect, err := url.Parse(redirectUri)
if err != nil {
log.Debug(err.Error())
c.AbortWithStatus(http.StatusInternalServerError)
return
}
rq := _redirect.Query()
rq.Add("email_challenge", emailChallenge)
_redirect.RawQuery = rq.Encode()
q.Set("redirect_uri", _redirect.String())
initUrl.RawQuery = q.Encode()
}
}*/
c.Redirect(http.StatusFound, initUrl.String())
c.Abort()
return
}
// We recived a code request, try and exchange it for a token.
requestState := c.Query("state")
if requestState == "" {
log.Debug("Missing state in query")
c.AbortWithStatus(http.StatusBadRequest)
return
}
log = log.WithFields(logrus.Fields{ "state":requestState })
valid := ValidateSessionState(env, c, requestState)
if valid == false {
log.Debug("Request state invalid")
c.AbortWithStatus(http.StatusBadRequest)
return
}
// Require redirect_to registered to session exchange state
redirectTo, exists := FetchSessionRedirect(env, c, requestState)
if exists == false {
log.Debug("Session redirect not found")
c.AbortWithStatus(http.StatusBadRequest)
return
}
log = log.WithFields(logrus.Fields{ "session.redirect_to":redirectTo })
token, err := oauth2Config.Exchange(context.Background(), code)
if err != nil {
log.Debug(err.Error())
c.AbortWithStatus(http.StatusBadRequest) // FIXME: Maybe we should redirect back reboot the process. Since the access token was not aquired.
return
}
if token.Valid() == false {
c.AbortWithStatus(http.StatusForbidden)
return
}
// Optional extract IdToken iff present.
idToken, idTokenHint, err := fetchIdTokenFromAccessToken(env, oauth2Config, token)
if err != nil {
log.Debug(err.Error())
c.AbortWithStatus(http.StatusBadRequest)
return
}
if idToken != nil {
c.Set(env.Constants.ContextIdTokenKey, idToken)
}
if idTokenHint != "" {
c.Set(env.Constants.ContextIdTokenHintKey, idTokenHint)
}
c.Set(env.Constants.ContextAccessTokenKey, token)
c.Next()
return
}
return gin.HandlerFunc(fn)
}
func fetchIdTokenFromAccessToken(env *Environment, oauth2Config *oauth2.Config, token *oauth2.Token) (idToken *oidc.IDToken, idTokenHint string, err error) {
idTokenHint, ok := token.Extra("id_token").(string)
if ok != true {
return nil, "", nil
}
// Found id_token, verify it.
oidcConfig := &oidc.Config{ ClientID:oauth2Config.ClientID }
verifier := env.Provider.Verifier(oidcConfig)
idToken, err = verifier.Verify(context.Background(), idTokenHint)
if err != nil {
return nil, "", err
}
return idToken, idTokenHint, nil
}
func AccessToken(env *Environment, c *gin.Context) (*oauth2.Token) {
t, exists := c.Get(env.Constants.ContextAccessTokenKey)
if exists == true {
return t.(*oauth2.Token)
}
return nil
}
func IdToken(env *Environment, c *gin.Context) (*oidc.IDToken) {
t, exists := c.Get(env.Constants.ContextIdTokenKey)
if exists == true {
return t.(*oidc.IDToken)
}
return nil
}
func IdTokenHint(env *Environment, c *gin.Context) (string) {
t, exists := c.Get(env.Constants.ContextIdTokenHintKey)
if exists == true {
return t.(string)
}
return ""
}
func RequireIdentity(env *Environment) gin.HandlerFunc {
fn := func(c *gin.Context) {
log := c.MustGet(env.Constants.LogKey).(*logrus.Entry)
log = log.WithFields(logrus.Fields{
"func": "RequireIdentity",
})
token := AccessToken(env, c)
if token == nil {
log.Debug("Missing access token. Hint: Access token is missing from context. Did you call RequestTokenUsingAuthorizationCode called before calling RequireIdentity?")
c.AbortWithStatus(http.StatusInternalServerError)
return
}
idToken := IdToken(env, c)
if idToken == nil {
log.Debug("Missing id token. Hint: Id token is missing from context. Did you call RequireScopes(openid) and RequestTokenUsingAuthorizationCode called before calling RequireIdentity?")
c.AbortWithStatus(http.StatusInternalServerError)
return
}
oauth2Config := FetchOAuth2Config(env, c)
if oauth2Config == nil {
log.Debug("Missing oauth2 config. Hint: Oauth2 config is missing from context. Did you call ConfigureOauth2 before calling RequireIdentity?")
c.AbortWithStatus(http.StatusInternalServerError)
return
}
// FIXME: This should be defined on application start up and verified and stored in a map to use here.
idpHumansUrl := config.GetString("idp.public.url") + config.GetString("idp.public.endpoints.humans.collection")
// Id token found lookup identity.
idpClient := idp.NewIdpClientWithUserAccessToken(oauth2Config, token)
humanRequest := idp.ReadHumansRequest{ Id:idToken.Subject }
status, responses, err := idp.ReadHumans(idpClient, idpHumansUrl, []idp.ReadHumansRequest{ humanRequest })
if err != nil {
log.Debug(err.Error())
c.AbortWithStatus(http.StatusInternalServerError)
return
}
if status != http.StatusOK {
log.WithFields(logrus.Fields{ "id":humanRequest.Id, "status":status }).Debug("Humans read failed")
c.AbortWithStatus(http.StatusInternalServerError)
return
}
var resp idp.ReadHumansResponse
reqStatus, reqErrors := bulky.Unmarshal(0, responses, &resp)
if len(reqErrors) > 0 {
log.Debug(reqErrors)
log.Debug("Humans unmarshal failed")
c.AbortWithStatus(http.StatusInternalServerError)
return
}
if reqStatus == 200 {
human := resp[0]
if human.Id == "" {
log.WithFields(logrus.Fields{ "id":humanRequest.Id }).Debug("Human not found. Hint: Idp did not return an id for the human")
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.Set(env.Constants.ContextIdentityKey, resp[0])
c.Next()
return
}
// Deny
log.WithFields(logrus.Fields{ "id":humanRequest.Id }).Debug("Human not found")
c.AbortWithStatus(http.StatusNotFound)
return
}
return gin.HandlerFunc(fn)
}
func GetIdentity(env *Environment, c *gin.Context) *idp.Human {
identity, exists := c.Get(env.Constants.ContextIdentityKey)
if exists == true {
human := identity.(idp.Human)
return &human
}
return nil
}