diff options
| author | mo khan <mo@mokhan.ca> | 2022-04-29 16:24:46 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2022-04-29 16:24:46 -0600 |
| commit | cebb4f22034c5629bf37fc302c849167677bb19d (patch) | |
| tree | be58ab5278095d794ef6795a1f795f9fb653d140 /pkg | |
| parent | 67af58d4e3aa16819ff8c3cc9ccdc2681b2b7397 (diff) | |
extract errors
Diffstat (limited to 'pkg')
| -rw-r--r-- | pkg/dto/client_information_response.go | 22 | ||||
| -rw-r--r-- | pkg/dto/client_information_response_test.go | 28 | ||||
| -rw-r--r-- | pkg/web/register_test.go | 10 |
3 files changed, 56 insertions, 4 deletions
diff --git a/pkg/dto/client_information_response.go b/pkg/dto/client_information_response.go index 61b3ae3..e35c29e 100644 --- a/pkg/dto/client_information_response.go +++ b/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 } diff --git a/pkg/dto/client_information_response_test.go b/pkg/dto/client_information_response_test.go new file mode 100644 index 0000000..d7a11ad --- /dev/null +++ b/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()) + }) + }) +} diff --git a/pkg/web/register_test.go b/pkg/web/register_test.go index bc3eadb..ccb10df 100644 --- a/pkg/web/register_test.go +++ b/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") |
