diff options
| author | mo khan <mo@mokhan.ca> | 2022-05-15 20:02:19 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2022-05-15 20:02:19 -0600 |
| commit | 234dba52532d2b39c0dde4f3852912dede1d761f (patch) | |
| tree | 5d83ca394dc05fcab58e83f31d4132748770a947 | |
| parent | 18c35cdcccc475d685804b0958486b4f71b1c73e (diff) | |
extract configuration type
| -rw-r--r-- | pkg/web/configuration.go | 4 | ||||
| -rw-r--r-- | pkg/web/http_context.go | 8 | ||||
| -rw-r--r-- | pkg/web/json_web_key_sets.go | 2 | ||||
| -rw-r--r-- | pkg/web/json_web_key_sets_test.go | 7 | ||||
| -rw-r--r-- | pkg/web/open_id_configuration.go | 2 | ||||
| -rw-r--r-- | pkg/web/open_id_configuration_test.go | 5 | ||||
| -rw-r--r-- | pkg/web/register_test.go | 2 | ||||
| -rw-r--r-- | pkg/web/routes.go | 5 | ||||
| -rw-r--r-- | pkg/web/token.go | 4 |
9 files changed, 24 insertions, 15 deletions
diff --git a/pkg/web/configuration.go b/pkg/web/configuration.go index 3fcd000..0275c58 100644 --- a/pkg/web/configuration.go +++ b/pkg/web/configuration.go @@ -1,6 +1,6 @@ package web type Configuration struct { - issuer string - keyData []byte + Issuer string + KeyData []byte } diff --git a/pkg/web/http_context.go b/pkg/web/http_context.go index ccbc7c9..4c0c516 100644 --- a/pkg/web/http_context.go +++ b/pkg/web/http_context.go @@ -11,9 +11,10 @@ import ( type HttpContext struct { cfg *Configuration log *logrus.Logger + mux *http.ServeMux } -func NewHttpContext(issuer string, keyData []byte) *HttpContext { +func NewHttpContext(cfg *Configuration) *HttpContext { logger := logrus.New() logger.SetFormatter(&logrus.TextFormatter{ DisableColors: true, @@ -28,10 +29,7 @@ func NewHttpContext(issuer string, keyData []byte) *HttpContext { }) return &HttpContext{ - cfg: &Configuration{ - issuer: issuer, - keyData: keyData, - }, + cfg: cfg, log: logger, } } diff --git a/pkg/web/json_web_key_sets.go b/pkg/web/json_web_key_sets.go index c3dafcb..4a29d1b 100644 --- a/pkg/web/json_web_key_sets.go +++ b/pkg/web/json_web_key_sets.go @@ -11,7 +11,7 @@ import ( func (h *HttpContext) JsonWebKeySets(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") - privatePem, _ := pem.Decode(h.cfg.keyData) + privatePem, _ := pem.Decode(h.cfg.KeyData) parsedKey, _ := x509.ParsePKCS1PrivateKey(privatePem.Bytes) key, _ := jwk.FromRaw(parsedKey) pubKey, _ := jwk.PublicKeyOf(key) diff --git a/pkg/web/json_web_key_sets_test.go b/pkg/web/json_web_key_sets_test.go index b059481..3425b2c 100644 --- a/pkg/web/json_web_key_sets_test.go +++ b/pkg/web/json_web_key_sets_test.go @@ -22,7 +22,12 @@ func TestJsonWebKeySets(t *testing.T) { Bytes: x509.MarshalPKCS1PrivateKey(key), }) - h := NewHttpContext("https://example.org", b.Bytes()) + cfg := &Configuration{ + Issuer: "https://example.org", + KeyData: b.Bytes(), + } + // h := NewHttpContext("https://example.org", b.Bytes()) + h := NewHttpContext(cfg) t.Run(".well-known/jwks.json", func(t *testing.T) { w := httptest.NewRecorder() diff --git a/pkg/web/open_id_configuration.go b/pkg/web/open_id_configuration.go index 2c71c32..8c1f49e 100644 --- a/pkg/web/open_id_configuration.go +++ b/pkg/web/open_id_configuration.go @@ -15,5 +15,5 @@ var ( func (h *HttpContext) OpenIdConfiguration(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") - tmpl.Execute(w, struct{ Issuer string }{Issuer: h.cfg.issuer}) + tmpl.Execute(w, struct{ Issuer string }{Issuer: h.cfg.Issuer}) } diff --git a/pkg/web/open_id_configuration_test.go b/pkg/web/open_id_configuration_test.go index 4c86a35..bfbe90f 100644 --- a/pkg/web/open_id_configuration_test.go +++ b/pkg/web/open_id_configuration_test.go @@ -22,7 +22,10 @@ func TestOpenIdConfiguration(t *testing.T) { Bytes: x509.MarshalPKCS1PrivateKey(key), }) - h := NewHttpContext("https://example.org", b.Bytes()) + h := NewHttpContext(&Configuration{ + Issuer: "https://example.org", + KeyData: b.Bytes(), + }) t.Run(".well-known/openid-configuration", func(t *testing.T) { w := httptest.NewRecorder() diff --git a/pkg/web/register_test.go b/pkg/web/register_test.go index ea02748..6d13588 100644 --- a/pkg/web/register_test.go +++ b/pkg/web/register_test.go @@ -12,7 +12,7 @@ import ( ) func TestRegister(t *testing.T) { - srv := NewHttpContext("https://example.com", []byte{}) + srv := NewHttpContext(&Configuration{Issuer: "https://example.com", KeyData: []byte{}}) t.Run("POST /register", func(t *testing.T) { t.Run("with a valid request body", func(t *testing.T) { diff --git a/pkg/web/routes.go b/pkg/web/routes.go index 69c2333..b2a7643 100644 --- a/pkg/web/routes.go +++ b/pkg/web/routes.go @@ -5,5 +5,8 @@ import ( ) func NewRoutes(issuer string, keyData []byte) http.Handler { - return NewHttpContext(issuer, keyData).Router() + return NewHttpContext(&Configuration{ + Issuer: issuer, + KeyData: keyData, + }).Router() } diff --git a/pkg/web/token.go b/pkg/web/token.go index 1604246..c56628d 100644 --- a/pkg/web/token.go +++ b/pkg/web/token.go @@ -62,7 +62,7 @@ func (h *HttpContext) createIdToken(clientId string) string { } expiresAt := now.Add(time.Hour * time.Duration(1)) idToken := jwt.NewWithClaims(jwt.SigningMethodRS256, &jwt.StandardClaims{ - Issuer: h.cfg.issuer, + Issuer: h.cfg.Issuer, Subject: "1", Audience: clientId, ExpiresAt: expiresAt.Unix(), @@ -71,7 +71,7 @@ func (h *HttpContext) createIdToken(clientId string) string { Id: uuid.GenerateUUID(), }) - key, _ := jwt.ParseRSAPrivateKeyFromPEM(h.cfg.keyData) + key, _ := jwt.ParseRSAPrivateKeyFromPEM(h.cfg.KeyData) signedIdToken, _ := idToken.SignedString(key) return signedIdToken } |
