Skip to content

Commit

Permalink
Replaces key discovery with well-known feature
Browse files Browse the repository at this point in the history
Closes #43
  • Loading branch information
arekkas committed Dec 14, 2017
1 parent 6519846 commit 40c6d73
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
7 changes: 5 additions & 2 deletions director/director.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ func (d *Director) Director(r *http.Request) {
return
}

token, err := jwt.NewWithClaims(jwt.SigningMethodRS256, access.ToClaims()).SignedString(privateKey)
token := jwt.NewWithClaims(jwt.SigningMethodRS256, access.ToClaims())
token.Header["kid"] = d.KeyManager.PublicKeyID()

signed, err := token.SignedString(privateKey)
if err != nil {
d.Logger.
WithError(errors.WithStack(err)).
Expand All @@ -125,5 +128,5 @@ func (d *Director) Director(r *http.Request) {

r.URL.Scheme = d.TargetURL.Scheme
r.URL.Host = d.TargetURL.Host
*r = *r.WithContext(context.WithValue(r.Context(), requestAllowed, token))
*r = *r.WithContext(context.WithValue(r.Context(), requestAllowed, signed))
}
18 changes: 9 additions & 9 deletions rsakey/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ type Handler struct {
}

func (h *Handler) SetRoutes(r *httprouter.Router) {
r.GET("/keys/id-token.public", h.GetPublicKey)
r.GET("/.well-known/jwks.json", h.WellKnown)
}

func (h *Handler) GetPublicKey(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
func (h *Handler) WellKnown(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
key, err := h.M.PublicKey()
if err != nil {
h.H.WriteError(w, r, err)
return
}

jwk := &jose.JSONWebKey{
Key: key,
KeyID: "id-token.public",
Algorithm: h.M.Algorithm(),
}

h.H.Write(w, r, jwk)
h.H.Write(w, r, &jose.JSONWebKeySet{
Keys: []jose.JSONWebKey{{
Key: key,
KeyID: h.M.PublicKeyID(),
Algorithm: h.M.Algorithm(),
}},
})
}
1 change: 1 addition & 0 deletions rsakey/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ type Manager interface {
Refresh() error
PrivateKey() (*rsa.PrivateKey, error)
PublicKey() (*rsa.PublicKey, error)
PublicKeyID() string
Algorithm() string
}
4 changes: 4 additions & 0 deletions rsakey/manager_hydra.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ func (m *HydraManager) PrivateKey() (*rsa.PrivateKey, error) {
return m.key, nil
}

func (m *HydraManager) PublicKeyID() string {
return m.Set + ":public"
}

func (m *HydraManager) Algorithm() string {
return "RS256"
}
4 changes: 4 additions & 0 deletions rsakey/manager_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func (m *LocalManager) PrivateKey() (*rsa.PrivateKey, error) {
return m.key, nil
}

func (m *LocalManager) PublicKeyID() string {
return "id-token:public"
}

func (m *LocalManager) Algorithm() string {
return "RS256"
}

0 comments on commit 40c6d73

Please sign in to comment.