Commit 234dba5

mo khan <mo@mokhan.ca>
2022-05-16 02:02:19
extract configuration type
1 parent 18c35cd
pkg/web/configuration.go
@@ -1,6 +1,6 @@
 package web
 
 type Configuration struct {
-	issuer  string
-	keyData []byte
+	Issuer  string
+	KeyData []byte
 }
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,
 	}
 }
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)
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()
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})
 }
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()
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) {
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()
 }
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
 }