Skip to content

Commit ef5aeee

Browse files
committed
rename structs
1 parent 73c7b52 commit ef5aeee

File tree

2 files changed

+39
-37
lines changed

2 files changed

+39
-37
lines changed

internal/oauthex/auth_meta.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ type AuthServerMeta struct {
112112
CodeChallengeMethodsSupported []string `json:"code_challenge_methods_supported,omitempty"`
113113
}
114114

115-
// AuthClientMeta represents the client metadata fields for the DCR POST request (RFC 7591).
116-
type AuthClientMeta struct {
115+
// ClientRegistrationMetadata represents the client metadata fields for the DCR POST request (RFC 7591).
116+
type ClientRegistrationMetadata struct {
117117
// RedirectURIs is a REQUIRED JSON array of redirection URI strings for use in
118118
// redirect-based flows (such as the authorization code grant).
119119
RedirectURIs []string `json:"redirect_uris"`
@@ -180,12 +180,12 @@ type AuthClientMeta struct {
180180
SoftwareStatement string `json:"software_statement,omitempty"`
181181
}
182182

183-
// AuthClientInformation represents the fields returned by the Authorization Server
183+
// ClientRegistrationResponse represents the fields returned by the Authorization Server
184184
// (RFC 7591, Section 3.2.1 and 3.2.2).
185-
type AuthClientInformation struct {
186-
// AuthClientMeta contains all registered client metadata, returned by the
185+
type ClientRegistrationResponse struct {
186+
// ClientRegistrationMetadata contains all registered client metadata, returned by the
187187
// server on success, potentially with modified or defaulted values.
188-
AuthClientMeta
188+
ClientRegistrationMetadata
189189

190190
// ClientID is the REQUIRED newly issued OAuth 2.0 client identifier.
191191
ClientID string `json:"client_id"`
@@ -202,16 +202,20 @@ type AuthClientInformation struct {
202202
ClientSecretExpiresAt int64 `json:"client_secret_expires_at,omitempty"`
203203
}
204204

205-
// AuthClientRegistrationError is the error response from the Authorization Server
205+
// ClientRegistrationError is the error response from the Authorization Server
206206
// for a failed registration attempt (RFC 7591, Section 3.2.2).
207-
type AuthClientRegistrationError struct {
208-
// Error is the REQUIRED error code if registration failed (RFC 7591, 3.2.2).
209-
Error string `json:"error"`
207+
type ClientRegistrationError struct {
208+
// ErrorCode is the REQUIRED error code if registration failed (RFC 7591, 3.2.2).
209+
ErrorCode string `json:"error"`
210210

211211
// ErrorDescription is an OPTIONAL human-readable error message.
212212
ErrorDescription string `json:"error_description,omitempty"`
213213
}
214214

215+
func (e *ClientRegistrationError) Error() string {
216+
return fmt.Sprintf("registration failed: %s (%s)", e.ErrorCode, e.ErrorDescription)
217+
}
218+
215219
var wellKnownPaths = []string{
216220
"/.well-known/oauth-authorization-server",
217221
"/.well-known/openid-configuration",
@@ -248,9 +252,9 @@ func GetAuthServerMeta(ctx context.Context, issuerURL string, c *http.Client) (*
248252
}
249253

250254
// RegisterClient performs Dynamic Client Registration according to RFC 7591.
251-
func RegisterClient(ctx context.Context, serverMeta *AuthServerMeta, clientMeta *AuthClientMeta, c *http.Client) (*AuthClientInformation, error) {
252-
if serverMeta == nil || serverMeta.RegistrationEndpoint == "" {
253-
return nil, fmt.Errorf("server metadata does not contain a registration_endpoint")
255+
func RegisterClient(ctx context.Context, registrationEndpoint string, clientMeta *ClientRegistrationMetadata, c *http.Client) (*ClientRegistrationResponse, error) {
256+
if registrationEndpoint == "" {
257+
return nil, fmt.Errorf("registration_endpoint is required")
254258
}
255259

256260
if c == nil {
@@ -262,7 +266,7 @@ func RegisterClient(ctx context.Context, serverMeta *AuthServerMeta, clientMeta
262266
return nil, fmt.Errorf("failed to marshal client metadata: %w", err)
263267
}
264268

265-
req, err := http.NewRequestWithContext(ctx, "POST", serverMeta.RegistrationEndpoint, bytes.NewBuffer(payload))
269+
req, err := http.NewRequestWithContext(ctx, "POST", registrationEndpoint, bytes.NewBuffer(payload))
266270
if err != nil {
267271
return nil, fmt.Errorf("failed to create registration request: %w", err)
268272
}
@@ -282,22 +286,22 @@ func RegisterClient(ctx context.Context, serverMeta *AuthServerMeta, clientMeta
282286
}
283287

284288
if resp.StatusCode == http.StatusCreated {
285-
var authClientInfo AuthClientInformation
286-
if err := json.Unmarshal(body, &authClientInfo); err != nil {
289+
var regResponse ClientRegistrationResponse
290+
if err := json.Unmarshal(body, &regResponse); err != nil {
287291
return nil, fmt.Errorf("failed to decode successful registration response: %w (%s)", err, string(body))
288292
}
289-
if authClientInfo.ClientID == "" {
293+
if regResponse.ClientID == "" {
290294
return nil, fmt.Errorf("registration response is missing required 'client_id' field")
291295
}
292-
return &authClientInfo, nil
296+
return &regResponse, nil
293297
}
294298

295299
if resp.StatusCode == http.StatusBadRequest {
296-
var regError AuthClientRegistrationError
300+
var regError ClientRegistrationError
297301
if err := json.Unmarshal(body, &regError); err != nil {
298302
return nil, fmt.Errorf("failed to decode registration error response: %w (%s)", err, string(body))
299303
}
300-
return nil, fmt.Errorf("registration failed: %s (%s)", regError.Error, regError.ErrorDescription)
304+
return nil, &regError
301305
}
302306

303307
return nil, fmt.Errorf("registration failed with status %s: %s", resp.Status, string(body))

internal/oauthex/auth_meta_test.go

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ func TestAuthServerMetaParse(t *testing.T) {
3232
}
3333
}
3434

35-
func TestAuthClientMetaParse(t *testing.T) {
35+
func TestClientRegistrationMetadataParse(t *testing.T) {
3636
// Verify that we can parse a typical client metadata JSON.
3737
data, err := os.ReadFile(filepath.FromSlash("testdata/client-auth-meta.json"))
3838
if err != nil {
3939
t.Fatal(err)
4040
}
41-
var a AuthClientMeta
41+
var a ClientRegistrationMetadata
4242
if err := json.Unmarshal(data, &a); err != nil {
4343
t.Fatal(err)
4444
}
@@ -55,7 +55,7 @@ func TestRegisterClient(t *testing.T) {
5555
testCases := []struct {
5656
name string
5757
handler http.HandlerFunc
58-
clientMeta *AuthClientMeta
58+
clientMeta *ClientRegistrationMetadata
5959
wantClientID string
6060
wantErr string
6161
}{
@@ -69,7 +69,7 @@ func TestRegisterClient(t *testing.T) {
6969
if err != nil {
7070
t.Fatal(err)
7171
}
72-
var receivedMeta AuthClientMeta
72+
var receivedMeta ClientRegistrationMetadata
7373
if err := json.Unmarshal(body, &receivedMeta); err != nil {
7474
t.Fatalf("Failed to unmarshal request body: %v", err)
7575
}
@@ -80,36 +80,36 @@ func TestRegisterClient(t *testing.T) {
8080
w.WriteHeader(http.StatusCreated)
8181
w.Write([]byte(`{"client_id":"test-client-id","client_secret":"test-client-secret","client_name":"Test App"}`))
8282
},
83-
clientMeta: &AuthClientMeta{ClientName: "Test App", RedirectURIs: []string{"http://localhost/cb"}},
83+
clientMeta: &ClientRegistrationMetadata{ClientName: "Test App", RedirectURIs: []string{"http://localhost/cb"}},
8484
wantClientID: "test-client-id",
8585
},
8686
{
87-
name: "Error - Missing ClientID in Response",
87+
name: "Missing ClientID in Response",
8888
handler: func(w http.ResponseWriter, r *http.Request) {
8989
w.Header().Set("Content-Type", "application/json")
9090
w.WriteHeader(http.StatusCreated)
9191
w.Write([]byte(`{"client_secret":"test-client-secret"}`)) // No client_id
9292
},
93-
clientMeta: &AuthClientMeta{RedirectURIs: []string{"http://localhost/cb"}},
93+
clientMeta: &ClientRegistrationMetadata{RedirectURIs: []string{"http://localhost/cb"}},
9494
wantErr: "registration response is missing required 'client_id' field",
9595
},
9696
{
97-
name: "Error - Standard OAuth Error",
97+
name: "Standard OAuth Error",
9898
handler: func(w http.ResponseWriter, r *http.Request) {
9999
w.Header().Set("Content-Type", "application/json")
100100
w.WriteHeader(http.StatusBadRequest)
101101
w.Write([]byte(`{"error":"invalid_redirect_uri","error_description":"Redirect URI is not valid."}`))
102102
},
103-
clientMeta: &AuthClientMeta{RedirectURIs: []string{"http://invalid/cb"}},
103+
clientMeta: &ClientRegistrationMetadata{RedirectURIs: []string{"http://invalid/cb"}},
104104
wantErr: "registration failed: invalid_redirect_uri (Redirect URI is not valid.)",
105105
},
106106
{
107-
name: "Error - Non-JSON Server Error",
107+
name: "Non-JSON Server Error",
108108
handler: func(w http.ResponseWriter, r *http.Request) {
109109
w.WriteHeader(http.StatusInternalServerError)
110110
w.Write([]byte("Internal Server Error"))
111111
},
112-
clientMeta: &AuthClientMeta{RedirectURIs: []string{"http://localhost/cb"}},
112+
clientMeta: &ClientRegistrationMetadata{RedirectURIs: []string{"http://localhost/cb"}},
113113
wantErr: "registration failed with status 500 Internal Server Error",
114114
},
115115
}
@@ -119,8 +119,7 @@ func TestRegisterClient(t *testing.T) {
119119
server := httptest.NewServer(tc.handler)
120120
defer server.Close()
121121

122-
serverMeta := &AuthServerMeta{RegistrationEndpoint: server.URL}
123-
info, err := RegisterClient(context.Background(), serverMeta, tc.clientMeta, server.Client())
122+
info, err := RegisterClient(context.Background(), server.URL, tc.clientMeta, server.Client())
124123

125124
if tc.wantErr != "" {
126125
if err == nil {
@@ -141,13 +140,12 @@ func TestRegisterClient(t *testing.T) {
141140
})
142141
}
143142

144-
t.Run("Error - No Endpoint in Metadata", func(t *testing.T) {
145-
serverMeta := &AuthServerMeta{Issuer: "http://localhost"} // No RegistrationEndpoint
146-
_, err := RegisterClient(context.Background(), serverMeta, &AuthClientMeta{}, nil)
143+
t.Run("No Endpoint", func(t *testing.T) {
144+
_, err := RegisterClient(context.Background(), "", &ClientRegistrationMetadata{}, nil)
147145
if err == nil {
148146
t.Fatal("Expected an error for missing registration endpoint, got nil")
149147
}
150-
expectedErr := "server metadata does not contain a registration_endpoint"
148+
expectedErr := "registration_endpoint is required"
151149
if err.Error() != expectedErr {
152150
t.Errorf("Expected error '%s', got '%v'", expectedErr, err)
153151
}

0 commit comments

Comments
 (0)