Commit e77954d

mo khan <mo@mokhan.ca>
2022-04-29 19:58:18
refactor: extract a Data Transfer Object package
1 parent a82af83
pkg/dto/client_information_response.go
@@ -0,0 +1,35 @@
+package dto
+
+import (
+	"time"
+
+	"github.com/hashicorp/uuid"
+)
+
+type ClientInformationResponse struct {
+	ClientId                string   `json:"client_id"`
+	ClientSecret            string   `json:"client_secret"`
+	ClientIdIssuedAt        int64    `json:"client_id_issued_at"`
+	ClientSecretExpiresAt   int64    `json:"client_secret_expires_at"`
+	RedirectUris            []string `json:"redirect_uris"`
+	GrantTypes              []string `json:"grant_types"`
+	ClientName              string   `json:"client_name"`
+	TokenEndpointAuthMethod string   `json:"token_endpoint_auth_method"`
+	LogoUri                 string   `json:"logo_uri"`
+	JwksUri                 string   `json:"jwks_uri"`
+}
+
+func NewClientInformationResponse(request *ClientRegistrationRequest) *ClientInformationResponse {
+	expiresAt := time.Now().Add(time.Duration(1) * time.Hour)
+	return &ClientInformationResponse{
+		ClientId:                uuid.GenerateUUID(),
+		ClientIdIssuedAt:        time.Now().Unix(),
+		ClientName:              request.ClientName,
+		ClientSecret:            uuid.GenerateUUID(),
+		ClientSecretExpiresAt:   expiresAt.Unix(),
+		RedirectUris:            request.RedirectUris,
+		TokenEndpointAuthMethod: request.TokenEndpointAuthMethod,
+		LogoUri:                 request.LogoUri,
+		JwksUri:                 request.JwksUri,
+	}
+}
pkg/dto/client_registration_request.go
@@ -0,0 +1,9 @@
+package dto
+
+type ClientRegistrationRequest struct {
+	RedirectUris            []string `json:"redirect_uris"`
+	ClientName              string   `json:"client_name"`
+	TokenEndpointAuthMethod string   `json:"token_endpoint_auth_method"`
+	LogoUri                 string   `json:"logo_uri"`
+	JwksUri                 string   `json:"jwks_uri"`
+}
pkg/web/register.go
@@ -3,52 +3,17 @@ package web
 import (
 	"encoding/json"
 	"net/http"
-	"time"
 
-	"github.com/hashicorp/uuid"
+	"mokhan.ca/xlgmokha/oauth/pkg/dto"
 )
 
-type ClientRegistrationRequest struct {
-	RedirectUris            []string `json:"redirect_uris"`
-	ClientName              string   `json:"client_name"`
-	TokenEndpointAuthMethod string   `json:"token_endpoint_auth_method"`
-	LogoUri                 string   `json:"logo_uri"`
-	JwksUri                 string   `json:"jwks_uri"`
-}
-
-type ClientInformationResponse struct {
-	ClientId                string   `json:"client_id"`
-	ClientSecret            string   `json:"client_secret"`
-	ClientIdIssuedAt        int64    `json:"client_id_issued_at"`
-	ClientSecretExpiresAt   int64    `json:"client_secret_expires_at"`
-	RedirectUris            []string `json:"redirect_uris"`
-	GrantTypes              []string `json:"grant_types"`
-	ClientName              string   `json:"client_name"`
-	TokenEndpointAuthMethod string   `json:"token_endpoint_auth_method"`
-	LogoUri                 string   `json:"logo_uri"`
-	JwksUri                 string   `json:"jwks_uri"`
-}
-
 func (h *HttpContext) Register(w http.ResponseWriter, r *http.Request) {
-	var request ClientRegistrationRequest
+	var request dto.ClientRegistrationRequest
 	json.NewDecoder(r.Body).Decode(&request)
 
-	expiresAt := time.Now().Add(time.Duration(1) * time.Hour)
-	response := ClientInformationResponse{
-		ClientId:                uuid.GenerateUUID(),
-		ClientIdIssuedAt:        time.Now().Unix(),
-		ClientName:              request.ClientName,
-		ClientSecret:            uuid.GenerateUUID(),
-		ClientSecretExpiresAt:   expiresAt.Unix(),
-		RedirectUris:            request.RedirectUris,
-		TokenEndpointAuthMethod: request.TokenEndpointAuthMethod,
-		LogoUri:                 request.LogoUri,
-		JwksUri:                 request.JwksUri,
-	}
-
 	w.WriteHeader(http.StatusCreated)
 	w.Header().Set("Content-Type", "application/json")
 	w.Header().Set("Cache-Control", "no-store")
 	w.Header().Set("Pragma", "no-cache")
-	json.NewEncoder(w).Encode(&response)
+	json.NewEncoder(w).Encode(dto.NewClientInformationResponse(&request))
 }
pkg/web/register_test.go
@@ -8,6 +8,7 @@ import (
 	"testing"
 
 	"github.com/stretchr/testify/assert"
+	"mokhan.ca/xlgmokha/oauth/pkg/dto"
 )
 
 func TestRegister(t *testing.T) {
@@ -28,7 +29,7 @@ func TestRegister(t *testing.T) {
 		assert.Equal(t, "no-store", w.HeaderMap.Get("Cache-Control"))
 		assert.Equal(t, "no-cache", w.HeaderMap.Get("Pragma"))
 
-		var x ClientInformationResponse
+		var x dto.ClientInformationResponse
 		json.NewDecoder(w.Body).Decode(&x)
 
 		assert.Equal(t, "My Client", x.ClientName)