-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathidp.go
360 lines (288 loc) · 8.66 KB
/
idp.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
package idp
import (
"context"
"crypto/rsa"
"net/http"
"time"
jwt "github.com/dgrijalva/jwt-go"
"github.com/gorilla/sessions"
hclient "github.com/ory/hydra/client"
hjwk "github.com/ory/hydra/jwk"
hoauth2 "github.com/ory/hydra/oauth2"
hydra "github.com/ory/hydra/sdk"
"github.com/patrickmn/go-cache"
)
const (
VerifyPublicKey = "VerifyPublic"
ConsentPrivateKey = "ConsentPrivate"
)
func ClientInfoKey(clientID string) string {
return "ClientInfo:" + clientID
}
var encryptionkey = "something-very-secret"
// Identity Provider's options
type IDPConfig struct {
// Client id issued by Hydra
ClientID string `yaml:"client_id"`
// Client secret issued by Hydra
ClientSecret string `yaml:"client_secret"`
// Hydra's address
ClusterURL string `yaml:"hydra_address"`
// Expiration time of internal key cache
KeyCacheExpiration time.Duration `yaml:"key_cache_expiration"`
// Expiration time of internal clientid cache
ClientCacheExpiration time.Duration `yaml:"client_cache_expiration"`
// Internal cache cleanup interval
CacheCleanupInterval time.Duration `yaml:"cache_cleanup_interval"`
// Expiration time of internal clientid cache
ChallengeExpiration time.Duration `yaml:"challenge_expiration"`
// Gorilla sessions Store for storing the Challenge.
ChallengeStore sessions.Store
}
// Identity Provider helper
type IDP struct {
config *IDPConfig
// Communication with Hydra
hc *hydra.Client
// Http client for communicating with Hydra
client *http.Client
// Cache for all private and public keys
cache *cache.Cache
// Prepared cookie options for creating and deleting cookies
createChallengeCookieOptions *sessions.Options
deleteChallengeCookieOptions *sessions.Options
}
// Create the Identity Provider helper
func NewIDP(config *IDPConfig) *IDP {
var idp = new(IDP)
idp.config = config
idp.cache = cache.New(config.KeyCacheExpiration, config.CacheCleanupInterval)
idp.cache.OnEvicted(func(key string, value interface{}) { idp.refreshCache(key) })
idp.createChallengeCookieOptions = new(sessions.Options)
idp.createChallengeCookieOptions.Path = "/"
idp.createChallengeCookieOptions.MaxAge = int(config.ChallengeExpiration.Seconds())
idp.createChallengeCookieOptions.Secure = true // Send only via https
idp.createChallengeCookieOptions.HttpOnly = false
idp.deleteChallengeCookieOptions = new(sessions.Options)
idp.deleteChallengeCookieOptions.Path = "/"
idp.deleteChallengeCookieOptions.MaxAge = -1 // Mark for deletion
idp.deleteChallengeCookieOptions.Secure = true // Send only via https
idp.deleteChallengeCookieOptions.HttpOnly = false
return idp
}
func (idp *IDP) cacheConsentKey() error {
consentKey, err := idp.downloadConsentKey()
duration := cache.DefaultExpiration
if err != nil {
// re-cache the result even if there's an error, but
// do it with a shorter timeout. This will ensure we
// try to refresh the key once that timeout expires,
// otherwise we'll _never_ refresh the key again.
duration = idp.config.CacheCleanupInterval
}
idp.cache.Set(ConsentPrivateKey, consentKey, duration)
return err
}
func (idp *IDP) cacheVerificationKey() error {
verifyKey, err := idp.downloadVerificationKey()
duration := cache.DefaultExpiration
if err != nil {
// re-cache the result even if there's an error, but
// do it with a shorter timeout. This will ensure we
// try to refresh the key once that timeout expires,
// otherwise we'll _never_ refresh the key again.
duration = idp.config.CacheCleanupInterval
}
idp.cache.Set(VerifyPublicKey, verifyKey, duration)
return err
}
// Called when any key expires
func (idp *IDP) refreshCache(key string) {
switch key {
case VerifyPublicKey:
idp.cacheVerificationKey()
return
case ConsentPrivateKey:
idp.cacheConsentKey()
return
default:
// Will get here for client IDs.
// Fine to just let them expire, the next request from that
// client will trigger a refresh
return
}
}
// Downloads the hydra's public key
func (idp *IDP) downloadVerificationKey() (*rsa.PublicKey, error) {
jwk, err := idp.hc.JSONWebKeys.GetKey(hoauth2.ConsentChallengeKey, "public")
if err != nil {
return nil, err
}
rsaKey, ok := hjwk.First(jwk.Keys).Key.(*rsa.PublicKey)
if !ok {
return nil, ErrorBadPublicKey
}
return rsaKey, nil
}
// Downloads the private key used for signing the consent
func (idp *IDP) downloadConsentKey() (*rsa.PrivateKey, error) {
jwk, err := idp.hc.JSONWebKeys.GetKey(hoauth2.ConsentEndpointKey, "private")
if err != nil {
return nil, err
}
rsaKey, ok := hjwk.First(jwk.Keys).Key.(*rsa.PrivateKey)
if !ok {
return nil, ErrorBadPrivateKey
}
return rsaKey, nil
}
// Connect to Hydra
func (idp *IDP) Connect(verifyTLS bool) error {
var err error
if verifyTLS {
idp.hc, err = hydra.Connect(
hydra.ClientID(idp.config.ClientID),
hydra.ClientSecret(idp.config.ClientSecret),
hydra.ClusterURL(idp.config.ClusterURL),
)
} else {
idp.hc, err = hydra.Connect(
hydra.ClientID(idp.config.ClientID),
hydra.ClientSecret(idp.config.ClientSecret),
hydra.ClusterURL(idp.config.ClusterURL),
hydra.SkipTLSVerify(),
)
}
if err != nil {
return err
}
err = idp.cacheVerificationKey()
if err != nil {
return err
}
err = idp.cacheConsentKey()
if err != nil {
return err
}
return nil
}
// Parse and verify the challenge JWT
func (idp *IDP) getChallengeToken(challengeString string) (*jwt.Token, error) {
token, err := jwt.Parse(challengeString, func(token *jwt.Token) (interface{}, error) {
_, ok := token.Method.(*jwt.SigningMethodRSA)
if !ok {
return nil, ErrorBadSigningMethod
}
return idp.getVerificationKey()
})
if err != nil {
return nil, err
}
if !token.Valid {
return nil, ErrorInvalidToken
}
return token, nil
}
func (idp *IDP) getConsentKey() (*rsa.PrivateKey, error) {
data, ok := idp.cache.Get(ConsentPrivateKey)
if !ok {
return nil, ErrorNotInCache
}
key, ok := data.(*rsa.PrivateKey)
if !ok {
return nil, ErrorBadKey
}
return key, nil
}
func (idp *IDP) getVerificationKey() (*rsa.PublicKey, error) {
data, ok := idp.cache.Get(VerifyPublicKey)
if !ok {
return nil, ErrorNotInCache
}
key, ok := data.(*rsa.PublicKey)
if !ok {
return nil, ErrorBadKey
}
return key, nil
}
func (idp *IDP) getClient(ctx context.Context, clientID string) (*hclient.Client, error) {
clientKey := ClientInfoKey(clientID)
data, ok := idp.cache.Get(clientKey)
if ok {
if data != nil {
client := data.(*hclient.Client)
return client, nil
}
return nil, ErrorNoSuchClient
}
client, err := idp.hc.Clients.GetClient(ctx, clientID)
if err != nil {
// Either the client isn't registered in hydra, or maybe hydra is
// having some problem. Either way, ensure we don't hit hydra again
// for this client if someone (maybe an attacker) retries quickly.
idp.cache.Set(clientKey, nil, idp.config.ClientCacheExpiration)
return nil, err
}
c := client.(*hclient.Client)
idp.cache.Set(clientKey, client, idp.config.ClientCacheExpiration)
return c, nil
}
// Create a new Challenge. The request will contain all the necessary information from Hydra, passed in the URL.
func (idp *IDP) NewChallenge(ctx context.Context, r *http.Request, user string) (challenge *Challenge, err error) {
tokenStr := r.FormValue("challenge")
if tokenStr == "" {
// No challenge token
err = ErrorBadRequest
return
}
token, err := idp.getChallengeToken(tokenStr)
if err != nil {
// Most probably, token can't be verified or parsed
return
}
claims := token.Claims.(jwt.MapClaims)
challenge = new(Challenge)
challenge.Expires = time.Unix(int64(claims["exp"].(float64)), 0)
if challenge.Expires.Before(time.Now()) {
challenge = nil
err = ErrorChallengeExpired
return
}
// Get data from the challenge jwt
challenge.Client, err = idp.getClient(ctx, claims["aud"].(string))
if err != nil {
return nil, err
}
challenge.Redirect = claims["redir"].(string)
challenge.JTI = claims["jti"].(string)
challenge.User = user
challenge.idp = idp
scopes := claims["scp"].([]interface{})
challenge.Scopes = make([]string, len(scopes), len(scopes))
for i, scope := range scopes {
challenge.Scopes[i] = scope.(string)
}
return
}
// Get the Challenge from a cookie, using Gorilla sessions
func (idp *IDP) GetChallenge(r *http.Request) (*Challenge, error) {
session, err := idp.config.ChallengeStore.Get(r, SessionCookieName)
if err != nil {
return nil, err
}
challenge, ok := session.Values[SessionCookieName].(*Challenge)
if !ok {
return nil, ErrorBadChallengeCookie
}
if challenge.Expires.Before(time.Now()) {
return nil, ErrorChallengeExpired
}
challenge.idp = idp
return challenge, nil
}
// Closes connection to Hydra, cleans cache etc.
func (idp *IDP) Close() {
idp.client = nil
// Removes all keys from the cache
idp.cache.Flush()
}