Commit 6aab23c

mo khan <mo@mokhan.ca>
2022-04-29 20:02:48
refactor: move structs to dto package
1 parent e77954d
pkg/dto/authorization_request.go
@@ -0,0 +1,10 @@
+package dto
+
+type AuthorizationRequest struct {
+	ResponseType string
+	Scope        string
+	ClientId     string
+	State        string
+	RedirectUri  string
+	Nonce        string
+}
pkg/dto/json_web_key_set.go
@@ -0,0 +1,5 @@
+package dto
+
+type JsonWebKeySet struct {
+	Keys []RsaJsonWebKey `json:"keys"`
+}
pkg/dto/open_id_configuration.go
@@ -0,0 +1,16 @@
+package dto
+
+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"`
+}
pkg/dto/rsa_json_web_key.go
@@ -0,0 +1,9 @@
+package dto
+
+type RsaJsonWebKey struct {
+	E       string `json:"e"`
+	KeyId   string `json:"kid"`
+	KeyType string `json:"kty"`
+	N       string `json:"n"`
+	Use     string `json:"use"`
+}
pkg/web/authorize.go
@@ -5,23 +5,15 @@ import (
 	"net/http"
 
 	"github.com/hashicorp/uuid"
+	"mokhan.ca/xlgmokha/oauth/pkg/dto"
 )
 
-type AuthorizationRequest struct {
-	ResponseType string
-	Scope        string
-	ClientId     string
-	State        string
-	RedirectUri  string
-	Nonce        string
-}
-
 func (h *HttpContext) Authorize(w http.ResponseWriter, r *http.Request) {
 	if r.Method == "GET" {
 		responseType := r.FormValue("response_type")
 		if responseType == "code" {
 			// Authorization Code Flow https://openid.net/specs/openid-connect-core-1_0.html#CodeFlowAuth
-			ar := &AuthorizationRequest{
+			ar := &dto.AuthorizationRequest{
 				ResponseType: r.FormValue("response_type"),
 				Scope:        r.FormValue("scope"),
 				ClientId:     r.FormValue("client_id"),
@@ -34,7 +26,7 @@ func (h *HttpContext) Authorize(w http.ResponseWriter, r *http.Request) {
 			http.Redirect(w, r, url, 302)
 		} else if responseType == "id_token token" || responseType == "id_token" {
 			// Implicit Flow https://openid.net/specs/openid-connect-core-1_0.html#ImplicitFlowAuth
-			ar := &AuthorizationRequest{
+			ar := &dto.AuthorizationRequest{
 				ResponseType: r.FormValue("response_type"),
 				RedirectUri:  r.FormValue("redirect_uri"),
 				Nonce:        r.FormValue("nonce"),
pkg/web/json_web_key_sets.go
@@ -9,18 +9,6 @@ import (
 	"github.com/lestrrat-go/jwx/v2/jwk"
 )
 
-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) JsonWebKeySets(w http.ResponseWriter, r *http.Request) {
 	w.Header().Set("Content-Type", "application/json")
 	privatePem, _ := pem.Decode(h.keyData)
pkg/web/json_web_key_sets_test.go
@@ -11,6 +11,7 @@ import (
 	"testing"
 
 	"github.com/stretchr/testify/assert"
+	"mokhan.ca/xlgmokha/oauth/pkg/dto"
 )
 
 func TestJsonWebKeySets(t *testing.T) {
@@ -31,7 +32,7 @@ func TestJsonWebKeySets(t *testing.T) {
 
 		assert.Equal(t, w.Header().Get("Content-Type"), "application/json")
 
-		var c JsonWebKeySet
+		var c dto.JsonWebKeySet
 		json.NewDecoder(w.Body).Decode(&c)
 
 		assert.Equal(t, 1, len(c.Keys))
pkg/web/open_id_configuration.go
@@ -13,21 +13,6 @@ 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) OpenIdConfiguration(w http.ResponseWriter, r *http.Request) {
 	w.Header().Set("Content-Type", "application/json")
 	tmpl.Execute(w, struct{ Issuer string }{Issuer: h.issuer})
pkg/web/open_id_configuration_test.go
@@ -11,6 +11,7 @@ import (
 	"testing"
 
 	"github.com/stretchr/testify/assert"
+	"mokhan.ca/xlgmokha/oauth/pkg/dto"
 )
 
 func TestOpenIdConfiguration(t *testing.T) {
@@ -31,7 +32,7 @@ func TestOpenIdConfiguration(t *testing.T) {
 
 		assert.Equal(t, w.Header().Get("Content-Type"), "application/json")
 
-		var c OpenIdConfiguration
+		var c dto.OpenIdConfiguration
 		json.NewDecoder(w.Body).Decode(&c)
 
 		assert.Equal(t, c.Issuer, "https://example.org")