Commit 8ed9213

mo khan <mo@mokhan.ca>
2022-04-27 22:30:36
add tests for .well-known/openid-configuration
1 parent 6098e9a
pkg/web/http_context.go
@@ -0,0 +1,17 @@
+package web
+
+import "log"
+
+type HttpContext struct {
+	issuer  string
+	keyData []byte
+	log     *log.Logger
+}
+
+func NewHttpContext(issuer string, keyData []byte) *HttpContext {
+	return &HttpContext{
+		issuer:  issuer,
+		keyData: keyData,
+		log:     log.Default(),
+	}
+}
pkg/web/mux.go
@@ -1,22 +1,11 @@
 package web
 
 import (
-	"log"
 	"net/http"
 )
 
-type HttpContext struct {
-	issuer  string
-	keyData []byte
-	log     *log.Logger
-}
-
 func NewMux(issuer string, keyData []byte) http.Handler {
-	h := &HttpContext{
-		issuer:  issuer,
-		keyData: keyData,
-		log:     log.Default(),
-	}
+	h := NewHttpContext(issuer, keyData)
 	mux := http.NewServeMux()
 	mux.Handle("/", http.HandlerFunc(h.Default))
 	mux.Handle("/.well-known/jwks.json", http.HandlerFunc(h.WellKnown))
pkg/web/well_known.go
@@ -18,6 +18,21 @@ var (
 	tmpl = template.Must(template.New("").Parse(string(oidcConfig)))
 )
 
+type OpenIdConfiguration struct {
+	Issuer                           string   `json:"issuer"`
+	AuthorizationEndpoint            string   `json:"authorization_endpoint"`
+	TokenEndpoint                    string   `json:"token_endpoint"`
+	UserInfoEndpoint                 string   `json:"userinfo_endpoint"`
+	JwksUri                          string   `json:"jwks_uri"`
+	RevocationEndpoint               string   `json:"revocation_endpoint"`
+	ScopesSupported                  []string `json:"scopes_supported"`
+	ResponseTypesSupported           []string `json:"response_types_supported"`
+	ResponseModesSupported           []string `json:"response_modes_supported"`
+	SubjectTypesSupported            []string `json:"subject_types_supported"`
+	IdTokenSigningAlgValuesSupported []string `json:"id_token_signing_alg_values_supported"`
+	ClaimsSupported                  []string `json:"claims_supported"`
+}
+
 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
@@ -0,0 +1,55 @@
+package web
+
+import (
+	"encoding/json"
+	"net/http/httptest"
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestWellKnown(t *testing.T) {
+	h := NewHttpContext("https://example.org", []byte{})
+
+	t.Run(".well-known/openid-configuration", func(t *testing.T) {
+		w := httptest.NewRecorder()
+		r := httptest.NewRequest("GET", "/.well-known/openid-configuration", nil)
+
+		h.WellKnown(w, r)
+
+		assert.Equal(t, w.Header().Get("Content-Type"), "application/json")
+
+		var c OpenIdConfiguration
+		json.NewDecoder(w.Body).Decode(&c)
+
+		assert.Equal(t, c.Issuer, "https://example.org")
+		assert.Equal(t, c.AuthorizationEndpoint, "https://example.org/authorize")
+		assert.Equal(t, c.TokenEndpoint, "https://example.org/token")
+		assert.Equal(t, c.UserInfoEndpoint, "https://example.org/userinfo")
+		assert.Equal(t, c.JwksUri, "https://example.org/.well-known/jwks.json")
+		assert.Equal(t, c.RevocationEndpoint, "https://example.org/revoke")
+		assert.EqualValues(t, c.ScopesSupported, []string{"openid"})
+		assert.EqualValues(t, c.ResponseTypesSupported, []string{
+			"code id_token token",
+			"code id_token",
+			"code token",
+			"code",
+			"id_token token",
+			"id_token",
+		})
+		assert.EqualValues(t, c.ResponseModesSupported, []string{
+			"query",
+			"fragment",
+			"form_post",
+		})
+		assert.EqualValues(t, c.SubjectTypesSupported, []string{"public"})
+		assert.EqualValues(t, c.IdTokenSigningAlgValuesSupported, []string{"RS256"})
+		assert.EqualValues(t, c.ClaimsSupported, []string{
+			"aud",
+			"exp",
+			"iat",
+			"iss",
+			"sub",
+		})
+	})
+}
go.mod
@@ -6,9 +6,11 @@ require (
 	github.com/golang-jwt/jwt v3.2.2+incompatible
 	github.com/hashicorp/uuid v0.0.0-20160311170451-ebb0a03e909c
 	github.com/lestrrat-go/jwx/v2 v2.0.0-beta1
+	github.com/stretchr/testify v1.7.1
 )
 
 require (
+	github.com/davecgh/go-spew v1.1.0 // indirect
 	github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
 	github.com/goccy/go-json v0.9.6 // indirect
 	github.com/lestrrat-go/blackmagic v1.0.1 // indirect
@@ -16,5 +18,7 @@ require (
 	github.com/lestrrat-go/httprc v1.0.1 // indirect
 	github.com/lestrrat-go/iter v1.0.2 // indirect
 	github.com/lestrrat-go/option v1.0.0 // indirect
+	github.com/pmezard/go-difflib v1.0.0 // indirect
 	golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
+	gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
 )
go.sum
@@ -36,6 +36,7 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
 golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
 golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
 golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=