Skip to content

Commit

Permalink
jwk: add use parameter to generated JWKs - closes ory#279 (ory#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
arekkas authored Oct 4, 2016
1 parent e33df89 commit 05b5f84
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
4 changes: 2 additions & 2 deletions cmd/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ func (h *Handler) registerRoutes(router *httprouter.Router) {
h.Warden = warden.NewHandler(c, router)

// Create root account if new install
h.createRS256KeysIfNotExist(c, oauth2.ConsentEndpointKey, "private")
h.createRS256KeysIfNotExist(c, oauth2.ConsentChallengeKey, "private")
createRS256KeysIfNotExist(c, oauth2.ConsentEndpointKey, "private", "sig")
createRS256KeysIfNotExist(c, oauth2.ConsentChallengeKey, "private", "sig")

h.createRootIfNewInstall(c)
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/server/handler_oauth2_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,12 @@ func newOAuth2Provider(c *config.Config, km jwk.Manager) fosite.OAuth2Provider {
var ctx = c.Context()
var store = ctx.FositeStore

createRS256KeysIfNotExist(c, oauth2.OpenIDConnectKeyName, "private", "sig")
keys, err := km.GetKey(oauth2.OpenIDConnectKeyName, "private")
if errors.Cause(err) == pkg.ErrNotFound {
logrus.Warnln("Could not find OpenID Connect signing keys. Generating a new keypair...")
keys, err = new(jwk.RS256Generator).Generate("")

pkg.Must(err, "Could not generate signing key for OpenID Connect")
km.AddKeySet(oauth2.OpenIDConnectKeyName, keys)
logrus.Infoln("Keypair generated.")
Expand Down
8 changes: 6 additions & 2 deletions cmd/server/helper_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@ import (
"github.com/ory-am/hydra/pkg"
)

func (h *Handler) createRS256KeysIfNotExist(c *config.Config, set, lookup string) {
func createRS256KeysIfNotExist(c *config.Config, set, kid, use string) {
ctx := c.Context()
generator := jwk.RS256Generator{}

if _, err := ctx.KeyManager.GetKey(set, lookup); errors.Cause(err) == pkg.ErrNotFound {
if _, err := ctx.KeyManager.GetKey(set, kid); errors.Cause(err) == pkg.ErrNotFound {
logrus.Infof("Key pair for signing %s is missing. Creating new one.", set)

keys, err := generator.Generate("")
pkg.Must(err, "Could not generate %s key: %s", set, err)

for i, k := range keys.Keys {
k.Use = use
keys.Keys[i] = k
}
err = ctx.KeyManager.AddKeySet(set, keys)
pkg.Must(err, "Could not persist %s key: %s", set, err)
}
Expand Down

0 comments on commit 05b5f84

Please sign in to comment.