diff options
| author | mo khan <mo@mokhan.ca> | 2022-05-08 20:45:07 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2022-05-08 20:45:07 -0600 |
| commit | 430322196d4aca30d359dfb0d8e8b02d6ad590ba (patch) | |
| tree | bcc2c1ad1b853357b2601c2d54863d812e22270d | |
| parent | cebb4f22034c5629bf37fc302c849167677bb19d (diff) | |
refactor: extract client metadata type
| -rw-r--r-- | pkg/dto/client_information_response.go | 13 | ||||
| -rw-r--r-- | pkg/dto/client_information_response_test.go | 19 | ||||
| -rw-r--r-- | pkg/dto/client_metadata.go | 19 | ||||
| -rw-r--r-- | pkg/dto/client_registration_request.go | 8 | ||||
| -rw-r--r-- | pkg/dto/grant_type.go | 13 | ||||
| -rw-r--r-- | pkg/dto/response_type.go | 8 | ||||
| -rw-r--r-- | pkg/dto/token_endpoint_auth_method.go | 46 |
7 files changed, 109 insertions, 17 deletions
diff --git a/pkg/dto/client_information_response.go b/pkg/dto/client_information_response.go index e35c29e..be7099d 100644 --- a/pkg/dto/client_information_response.go +++ b/pkg/dto/client_information_response.go @@ -9,10 +9,11 @@ import ( ) 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"` + 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"` @@ -30,9 +31,9 @@ func NewClientInformationResponse(request *ClientRegistrationRequest) *ClientInf ClientSecret: uuid.GenerateUUID(), ClientSecretExpiresAt: expiresAt.Unix(), RedirectUris: request.RedirectUris, - TokenEndpointAuthMethod: request.TokenEndpointAuthMethod, + TokenEndpointAuthMethod: request.TokenEndpointAuthMethod.String(), LogoUri: request.LogoUri, - JwksUri: request.JwksUri, + JwksUri: request.JWKSUri, } } diff --git a/pkg/dto/client_information_response_test.go b/pkg/dto/client_information_response_test.go index d7a11ad..de1364a 100644 --- a/pkg/dto/client_information_response_test.go +++ b/pkg/dto/client_information_response_test.go @@ -9,19 +9,30 @@ import ( 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: ""}) + response := NewClientInformationResponse(&ClientRegistrationRequest{ + ClientMetadata: ClientMetadata{ + ClientName: "", + }, + }) assert.Equal(t, InvalidClientMetadata, response.Valid()) }) t.Run("empty redirect_uris", func(t *testing.T) { - response := NewClientInformationResponse(&ClientRegistrationRequest{ClientName: "Example", RedirectUris: []string{}}) + response := NewClientInformationResponse(&ClientRegistrationRequest{ + ClientMetadata: ClientMetadata{ + 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"}, + ClientMetadata: ClientMetadata{ + ClientName: "Example", + RedirectUris: []string{"invalid"}, + }, }).Valid()) }) }) diff --git a/pkg/dto/client_metadata.go b/pkg/dto/client_metadata.go new file mode 100644 index 0000000..154c5fb --- /dev/null +++ b/pkg/dto/client_metadata.go @@ -0,0 +1,19 @@ +package dto + +type ClientMetadata struct { + RedirectUris []string `json:"redirect_uris"` + TokenEndpointAuthMethod TokenEndpointAuthMethod `json:"token_endpoint_auth_method"` + GrantTypes []GrantType + ResponseTypes []ResponseType + ClientName string `json:"client_name"` + ClientUri string + LogoUri string `json:"logo_uri"` + Scope string + Contacts []string + TermsOfServiceUri string + PolicyUri string `json:"policy_uri"` + JWKSUri string `json:"jwks_uri"` + JWKS JsonWebKeySet `json:"jwks"` + SoftwareId string + SoftwareVersion string +} diff --git a/pkg/dto/client_registration_request.go b/pkg/dto/client_registration_request.go index 9c03db6..8dbdada 100644 --- a/pkg/dto/client_registration_request.go +++ b/pkg/dto/client_registration_request.go @@ -1,11 +1,5 @@ package dto type ClientRegistrationRequest struct { - RedirectUris []string `json:"redirect_uris"` - ClientName string `json:"client_name"` - TokenEndpointAuthMethod string `json:"token_endpoint_auth_method"` - PolicyUri string `json:"policy_uri"` - Jwks JsonWebKeySet `json:"jwks"` - LogoUri string `json:"logo_uri"` - JwksUri string `json:"jwks_uri"` + ClientMetadata } diff --git a/pkg/dto/grant_type.go b/pkg/dto/grant_type.go new file mode 100644 index 0000000..fd2cb43 --- /dev/null +++ b/pkg/dto/grant_type.go @@ -0,0 +1,13 @@ +package dto + +type GrantType int + +const ( + AuthorizationCode GrantType = iota + Implicit + Password + ClientCredentials + RefreshToken + JWTBearer + SAML2Bearer +) diff --git a/pkg/dto/response_type.go b/pkg/dto/response_type.go new file mode 100644 index 0000000..adb44dd --- /dev/null +++ b/pkg/dto/response_type.go @@ -0,0 +1,8 @@ +package dto + +type ResponseType int + +const ( + Code ResponseType = iota + Token +) diff --git a/pkg/dto/token_endpoint_auth_method.go b/pkg/dto/token_endpoint_auth_method.go new file mode 100644 index 0000000..65df460 --- /dev/null +++ b/pkg/dto/token_endpoint_auth_method.go @@ -0,0 +1,46 @@ +package dto + +import ( + "bytes" + "encoding/json" +) + +type TokenEndpointAuthMethod int + +const ( + None TokenEndpointAuthMethod = iota + ClientSecretPost + ClientSecretBasic +) + +var toString = map[TokenEndpointAuthMethod]string{ + None: "none", + ClientSecretPost: "client_secret_post", + ClientSecretBasic: "client_secret_basic", +} + +var toID = map[string]TokenEndpointAuthMethod{ + "none": None, + "client_secret_post": ClientSecretPost, + "client_secret_basic": ClientSecretBasic, +} + +func (x TokenEndpointAuthMethod) MarshalJSON() ([]byte, error) { + buffer := bytes.NewBufferString(`"`) + buffer.WriteString(toString[x]) + buffer.WriteString(`"`) + return buffer.Bytes(), nil +} + +func (x *TokenEndpointAuthMethod) UnmarshalJSON(b []byte) error { + var val string + if err := json.Unmarshal(b, &val); err != nil { + return err + } + *x = toID[val] + return nil +} + +func (x TokenEndpointAuthMethod) String() string { + return toString[x] +} |
