Commit 7a956c5

mo khan <mo@mokhan.ca>
2022-04-27 23:20:52
test jwks handler
1 parent 8ed9213
Changed files (2)
pkg/web/well_known.go
@@ -33,6 +33,18 @@ type OpenIdConfiguration struct {
 	ClaimsSupported                  []string `json:"claims_supported"`
 }
 
+type JsonWebKeySet struct {
+	Keys []RsaJsonWebKey `json:"keys"`
+}
+
+type RsaJsonWebKey struct {
+	E       string `json:"e"`
+	KeyId   string `json:"kid"`
+	KeyType string `json:"kty"`
+	N       string `json:"n"`
+	Use     string `json:"use"`
+}
+
 func (h *HttpContext) WellKnown(w http.ResponseWriter, r *http.Request) {
 	if r.URL.Path == "/.well-known/openid-configuration" {
 		w.Header().Set("Content-Type", "application/json")
pkg/web/well_known_test.go
@@ -1,7 +1,12 @@
 package web
 
 import (
+	"bytes"
+	"crypto/rand"
+	"crypto/rsa"
+	"crypto/x509"
 	"encoding/json"
+	"encoding/pem"
 	"net/http/httptest"
 	"testing"
 
@@ -9,7 +14,14 @@ import (
 )
 
 func TestWellKnown(t *testing.T) {
-	h := NewHttpContext("https://example.org", []byte{})
+	key, _ := rsa.GenerateKey(rand.Reader, 1024)
+	b := new(bytes.Buffer)
+	pem.Encode(b, &pem.Block{
+		Type:  "RSA PRIVATE KEY",
+		Bytes: x509.MarshalPKCS1PrivateKey(key),
+	})
+
+	h := NewHttpContext("https://example.org", b.Bytes())
 
 	t.Run(".well-known/openid-configuration", func(t *testing.T) {
 		w := httptest.NewRecorder()
@@ -52,4 +64,22 @@ func TestWellKnown(t *testing.T) {
 			"sub",
 		})
 	})
+
+	t.Run(".well-known/jwks.json", func(t *testing.T) {
+		w := httptest.NewRecorder()
+		r := httptest.NewRequest("GET", "/.well-known/jwks.json", nil)
+
+		h.WellKnown(w, r)
+
+		assert.Equal(t, w.Header().Get("Content-Type"), "application/json")
+
+		var c JsonWebKeySet
+		json.NewDecoder(w.Body).Decode(&c)
+
+		assert.Equal(t, 1, len(c.Keys))
+		assert.Equal(t, "X", c.Keys[0].KeyId)
+		assert.Equal(t, "RSA", c.Keys[0].KeyType)
+		assert.NotEmpty(t, c.Keys[0].E)
+		assert.NotEmpty(t, c.Keys[0].N)
+	})
 }