@@ -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+
215219var 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 ))
0 commit comments