Commit cebb4f2

mo khan <mo@mokhan.ca>
2022-04-29 22:24:46
extract errors
1 parent 67af58d
pkg/dto/client_information_response.go
@@ -2,6 +2,7 @@ package dto
 
 import (
 	"errors"
+	"net/url"
 	"time"
 
 	"github.com/hashicorp/uuid"
@@ -35,9 +36,26 @@ func NewClientInformationResponse(request *ClientRegistrationRequest) *ClientInf
 	}
 }
 
-func (x *ClientInformationResponse) Valid() error {
+type ClientRegistrationError error
+
+var (
+	InvalidClientMetadata       ClientRegistrationError = errors.New("invalid_client_metadata")
+	InvalidRedirectUri                                  = errors.New("invalid_redirect_uri")
+	InvalidSoftwareStatement                            = errors.New("invalid_software_statement")
+	UnapprovedSoftwareStatement                         = errors.New("unapproved_software_statement")
+)
+
+func (x *ClientInformationResponse) Valid() ClientRegistrationError {
 	if x.ClientName == "" {
-		return errors.New("invalid_client_metadata")
+		return InvalidClientMetadata
+	}
+	if len(x.RedirectUris) == 0 {
+		return InvalidRedirectUri
+	}
+	for _, item := range x.RedirectUris {
+		if _, err := url.ParseRequestURI(item); err != nil {
+			return InvalidRedirectUri
+		}
 	}
 	return nil
 }
pkg/dto/client_information_response_test.go
@@ -0,0 +1,28 @@
+package dto
+
+import (
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestClientInformationResponse(t *testing.T) {
+	t.Run("Valid", func(t *testing.T) {
+		t.Run("blank client_name", func(t *testing.T) {
+			response := NewClientInformationResponse(&ClientRegistrationRequest{ClientName: ""})
+			assert.Equal(t, InvalidClientMetadata, response.Valid())
+		})
+
+		t.Run("empty redirect_uris", func(t *testing.T) {
+			response := NewClientInformationResponse(&ClientRegistrationRequest{ClientName: "Example", RedirectUris: []string{}})
+			assert.Equal(t, InvalidRedirectUri, response.Valid())
+		})
+
+		t.Run("invalid redirect_uri", func(t *testing.T) {
+			assert.Equal(t, InvalidRedirectUri, NewClientInformationResponse(&ClientRegistrationRequest{
+				ClientName:   "Example",
+				RedirectUris: []string{"invalid"},
+			}).Valid())
+		})
+	})
+}
pkg/web/register_test.go
@@ -18,8 +18,14 @@ func TestRegister(t *testing.T) {
 		t.Run("with a valid request body", func(t *testing.T) {
 			w := httptest.NewRecorder()
 
-			body := `{"redirect_uris": ["https://client.example.org/callback"], "client_name": "My Client", "token_endpoint_auth_method": "client_secret_basic", "logo_uri": "https://client.example.org/logo.png", "jwks_uri": "https://client.example.org/my_public_keys.jwks"}`
-			r := httptest.NewRequest("POST", "/register", strings.NewReader(body))
+			r := httptest.NewRequest("POST", "/register", strings.NewReader(`{
+					"redirect_uris": ["https://client.example.org/callback"],
+					"client_name": "My Client",
+					"token_endpoint_auth_method": "client_secret_basic",
+					"logo_uri": "https://client.example.org/logo.png",
+					"jwks_uri": "https://client.example.org/my_public_keys.jwks"
+				}`,
+			))
 			r.Header.Set("Content-Type", "application/json")
 			r.Header.Set("Accept", "application/json")