jwt: add updateKeys method and call in constructor

Fixes #225.
This commit is contained in:
Jimmy Zelinskie 2016-09-25 15:51:58 -04:00
parent 3d8fc63df3
commit 7f7f2726b4

View file

@ -57,16 +57,26 @@ func NewHook(cfg Config) middleware.Hook {
closing: make(chan struct{}),
}
h.updateKeys()
go func() {
for {
select {
case <-h.closing:
return
case <-time.After(cfg.JWKUpdateInterval):
resp, err := http.Get(cfg.JWKSetURL)
h.updateKeys()
}
}
}()
return h
}
func (h *hook) updateKeys() {
resp, err := http.Get(h.cfg.JWKSetURL)
if err != nil {
log.Errorln("failed to fetch JWK Set: " + err.Error())
continue
return
}
parsedJWKs := map[string]gojwk.Key{}
@ -74,7 +84,7 @@ func NewHook(cfg Config) middleware.Hook {
if err != nil {
resp.Body.Close()
log.Errorln("failed to decode JWK JSON: " + err.Error())
continue
return
}
resp.Body.Close()
@ -83,16 +93,11 @@ func NewHook(cfg Config) middleware.Hook {
publicKey, err := parsedJWK.DecodePublicKey()
if err != nil {
log.Errorln("failed to decode JWK into public key: " + err.Error())
continue
return
}
keys[kid] = publicKey
}
h.publicKeys = keys
}
}
}()
return h
}
func (h *hook) Stop() <-chan error {