Skip to content

Commit 137ea92

Browse files
authored
feat(oauthserver): update oauth grant list & authorization details response structure (#2247)
update oauth grant list response structure for api consistency
1 parent 2906b24 commit 137ea92

4 files changed

Lines changed: 52 additions & 61 deletions

File tree

internal/api/oauthserver/authorize.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ type AuthorizationDetailsResponse struct {
4343

4444
// ClientDetailsResponse represents client details in authorization response
4545
type ClientDetailsResponse struct {
46-
ClientID string `json:"client_id"`
47-
ClientName string `json:"client_name,omitempty"`
48-
ClientURI string `json:"client_uri,omitempty"`
49-
LogoURI string `json:"logo_uri,omitempty"`
46+
ID string `json:"id"`
47+
Name string `json:"name,omitempty"`
48+
URI string `json:"uri,omitempty"`
49+
LogoURI string `json:"logo_uri,omitempty"`
5050
}
5151

5252
// UserDetailsResponse represents user details in authorization response
@@ -237,10 +237,10 @@ func (s *Server) OAuthServerGetAuthorization(w http.ResponseWriter, r *http.Requ
237237
AuthorizationID: authorization.AuthorizationID,
238238
RedirectURI: authorization.RedirectURI,
239239
Client: ClientDetailsResponse{
240-
ClientID: authorization.Client.ID.String(),
241-
ClientName: utilities.StringValue(authorization.Client.ClientName),
242-
ClientURI: utilities.StringValue(authorization.Client.ClientURI),
243-
LogoURI: utilities.StringValue(authorization.Client.LogoURI),
240+
ID: authorization.Client.ID.String(),
241+
Name: utilities.StringValue(authorization.Client.ClientName),
242+
URI: utilities.StringValue(authorization.Client.ClientURI),
243+
LogoURI: utilities.StringValue(authorization.Client.LogoURI),
244244
},
245245
User: UserDetailsResponse{
246246
ID: user.ID.String(),

internal/api/oauthserver/handlers.go

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -541,17 +541,9 @@ func (s *Server) getTokenService() *tokens.Service {
541541

542542
// UserOAuthGrantResponse represents an OAuth grant that a user has authorized
543543
type UserOAuthGrantResponse struct {
544-
ClientID string `json:"client_id"`
545-
ClientName string `json:"client_name,omitempty"`
546-
ClientURI string `json:"client_uri,omitempty"`
547-
LogoURI string `json:"logo_uri,omitempty"`
548-
Scopes []string `json:"scopes"`
549-
GrantedAt time.Time `json:"granted_at"`
550-
}
551-
552-
// UserOAuthGrantsListResponse represents the response for listing user's OAuth grants
553-
type UserOAuthGrantsListResponse struct {
554-
Grants []UserOAuthGrantResponse `json:"grants"`
544+
Client ClientDetailsResponse `json:"client"`
545+
Scopes []string `json:"scopes"`
546+
GrantedAt time.Time `json:"granted_at"`
555547
}
556548

557549
// UserListOAuthGrants handles GET /user/oauth/grants
@@ -587,22 +579,20 @@ func (s *Server) UserListOAuthGrants(w http.ResponseWriter, r *http.Request) err
587579
}
588580

589581
response := UserOAuthGrantResponse{
590-
ClientID: client.ID.String(),
591-
ClientName: utilities.StringValue(client.ClientName),
592-
ClientURI: utilities.StringValue(client.ClientURI),
593-
LogoURI: utilities.StringValue(client.LogoURI),
594-
Scopes: consent.GetScopeList(),
595-
GrantedAt: consent.GrantedAt,
582+
Client: ClientDetailsResponse{
583+
ID: client.ID.String(),
584+
Name: utilities.StringValue(client.ClientName),
585+
URI: utilities.StringValue(client.ClientURI),
586+
LogoURI: utilities.StringValue(client.LogoURI),
587+
},
588+
Scopes: consent.GetScopeList(),
589+
GrantedAt: consent.GrantedAt,
596590
}
597591

598592
grants = append(grants, response)
599593
}
600594

601-
response := UserOAuthGrantsListResponse{
602-
Grants: grants,
603-
}
604-
605-
return shared.SendJSON(w, http.StatusOK, response)
595+
return shared.SendJSON(w, http.StatusOK, grants)
606596
}
607597

608598
// UserRevokeOAuthGrant handles DELETE /user/oauth/grants?client_id=...

internal/api/oauthserver/handlers_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -511,25 +511,25 @@ func (ts *OAuthClientTestSuite) TestUserListOAuthGrants() {
511511
// Check response
512512
assert.Equal(ts.T(), http.StatusOK, w.Code)
513513

514-
var response UserOAuthGrantsListResponse
515-
err = json.Unmarshal(w.Body.Bytes(), &response)
514+
var grants []UserOAuthGrantResponse
515+
err = json.Unmarshal(w.Body.Bytes(), &grants)
516516
require.NoError(ts.T(), err)
517517

518518
// Should have 2 grants
519-
assert.Len(ts.T(), response.Grants, 2)
519+
assert.Len(ts.T(), grants, 2)
520520

521521
// Verify client details are included
522-
for _, grant := range response.Grants {
523-
assert.NotEmpty(ts.T(), grant.ClientID)
524-
assert.Equal(ts.T(), "Test Client", grant.ClientName)
522+
for _, grant := range grants {
523+
assert.NotEmpty(ts.T(), grant.Client.ID)
524+
assert.Equal(ts.T(), "Test Client", grant.Client.Name)
525525
assert.NotEmpty(ts.T(), grant.Scopes)
526526
assert.NotEmpty(ts.T(), grant.GrantedAt)
527527
}
528528

529529
// Check that client1 (with read and write scopes) is in the response
530530
found := false
531-
for _, grant := range response.Grants {
532-
if grant.ClientID == client1.ID.String() {
531+
for _, grant := range grants {
532+
if grant.Client.ID == client1.ID.String() {
533533
found = true
534534
assert.Contains(ts.T(), grant.Scopes, "read")
535535
assert.Contains(ts.T(), grant.Scopes, "write")
@@ -553,12 +553,12 @@ func (ts *OAuthClientTestSuite) TestUserListOAuthGrantsEmpty() {
553553

554554
assert.Equal(ts.T(), http.StatusOK, w.Code)
555555

556-
var response UserOAuthGrantsListResponse
557-
err = json.Unmarshal(w.Body.Bytes(), &response)
556+
var grants []UserOAuthGrantResponse
557+
err = json.Unmarshal(w.Body.Bytes(), &grants)
558558
require.NoError(ts.T(), err)
559559

560560
// Should have 0 grants
561-
assert.Len(ts.T(), response.Grants, 0)
561+
assert.Len(ts.T(), grants, 0)
562562
}
563563

564564
func (ts *OAuthClientTestSuite) TestUserListOAuthGrantsNoAuth() {

openapi.yaml

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -837,37 +837,38 @@ paths:
837837
content:
838838
application/json:
839839
schema:
840-
type: object
841-
properties:
842-
grants:
843-
type: array
844-
items:
840+
type: array
841+
items:
842+
type: object
843+
properties:
844+
client:
845845
type: object
846+
description: OAuth client details
846847
properties:
847-
client_id:
848+
id:
848849
type: string
849850
format: uuid
850851
description: Unique client identifier
851-
client_name:
852+
name:
852853
type: string
853854
description: Human-readable name of the client application
854-
client_uri:
855+
uri:
855856
type: string
856857
format: uri
857858
description: URL of the client application's homepage
858859
logo_uri:
859860
type: string
860861
format: uri
861862
description: URL of the client application's logo
862-
scopes:
863-
type: array
864-
items:
865-
type: string
866-
description: List of scopes granted to this client
867-
granted_at:
868-
type: string
869-
format: date-time
870-
description: Timestamp when grant was authorized
863+
scopes:
864+
type: array
865+
items:
866+
type: string
867+
description: List of scopes granted to this client
868+
granted_at:
869+
type: string
870+
format: date-time
871+
description: Timestamp when grant was authorized
871872
401:
872873
$ref: "#/components/responses/UnauthorizedResponse"
873874
403:
@@ -2479,12 +2480,12 @@ paths:
24792480
client:
24802481
type: object
24812482
properties:
2482-
client_id:
2483+
id:
24832484
type: string
24842485
format: uuid
2485-
client_name:
2486+
name:
24862487
type: string
2487-
client_uri:
2488+
uri:
24882489
type: string
24892490
format: uri
24902491
logo_uri:

0 commit comments

Comments
 (0)