Skip to content

Commit ba0ec40

Browse files
committed
feat: add ent storage support for client_credentials_claims
Add the client_credentials_claims field to the ent ORM schema and update the storage client to persist and retrieve it. Also fix gci import formatting in handlers_test.go. Signed-off-by: Carles Arnal <carnalca@redhat.com> Signed-off-by: Carles Arnal <carlesarnal92@gmail.com>
1 parent 44f15e7 commit ba0ec40

File tree

11 files changed

+203
-41
lines changed

11 files changed

+203
-41
lines changed

server/handlers_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@ func TestHandleClientCredentials(t *testing.T) {
10771077
wantAccessTok bool
10781078
wantIDToken bool
10791079
wantUsername string
1080-
wantGroups []string
1080+
wantGroups []string
10811081
}{
10821082
{
10831083
name: "Basic grant, no scopes",
@@ -1118,17 +1118,17 @@ func TestHandleClientCredentials(t *testing.T) {
11181118
wantUsername: "Test Client",
11191119
},
11201120
{
1121-
name: "With groups scope and clientCredentialsClaims groups populated",
1122-
clientID: "test",
1123-
clientSecret: "barfoo",
1121+
name: "With groups scope and clientCredentialsClaims groups populated",
1122+
clientID: "test",
1123+
clientSecret: "barfoo",
11241124
clientCredentialsClaims: &storage.ClientCredentialsClaims{
11251125
Groups: []string{"admin-group", "dev-group"},
11261126
},
11271127
scopes: "openid groups",
11281128
wantCode: 200,
11291129
wantAccessTok: true,
11301130
wantIDToken: true,
1131-
wantGroups: []string{"admin-group", "dev-group"},
1131+
wantGroups: []string{"admin-group", "dev-group"},
11321132
},
11331133
{
11341134
name: "With groups scope but no clientCredentialsClaims configured",
@@ -1179,10 +1179,10 @@ func TestHandleClientCredentials(t *testing.T) {
11791179

11801180
// Create a confidential client for testing.
11811181
err := s.storage.CreateClient(ctx, storage.Client{
1182-
ID: "test",
1183-
Secret: "barfoo",
1184-
RedirectURIs: []string{"https://example.com/callback"},
1185-
Name: "Test Client",
1182+
ID: "test",
1183+
Secret: "barfoo",
1184+
RedirectURIs: []string{"https://example.com/callback"},
1185+
Name: "Test Client",
11861186
ClientCredentialsClaims: tc.clientCredentialsClaims,
11871187
})
11881188
require.NoError(t, err)
@@ -1241,7 +1241,7 @@ func TestHandleClientCredentials(t *testing.T) {
12411241
var claims struct {
12421242
Name string `json:"name"`
12431243
PreferredUsername string `json:"preferred_username"`
1244-
Groups []string `json:"groups"`
1244+
Groups []string `json:"groups"`
12451245
}
12461246
require.NoError(t, idToken.Claims(&claims))
12471247

storage/ent/client/client.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
// CreateClient saves provided oauth2 client settings into the database.
1010
func (d *Database) CreateClient(ctx context.Context, client storage.Client) error {
11-
_, err := d.client.OAuth2Client.Create().
11+
create := d.client.OAuth2Client.Create().
1212
SetID(client.ID).
1313
SetName(client.Name).
1414
SetSecret(client.Secret).
@@ -17,8 +17,11 @@ func (d *Database) CreateClient(ctx context.Context, client storage.Client) erro
1717
SetRedirectUris(client.RedirectURIs).
1818
SetTrustedPeers(client.TrustedPeers).
1919
SetAllowedConnectors(client.AllowedConnectors).
20-
SetMfaChain(client.MFAChain).
21-
Save(ctx)
20+
SetMfaChain(client.MFAChain)
21+
if client.ClientCredentialsClaims != nil {
22+
create = create.SetClientCredentialsClaims(client.ClientCredentialsClaims)
23+
}
24+
_, err := create.Save(ctx)
2225
if err != nil {
2326
return convertDBError("create oauth2 client: %w", err)
2427
}
@@ -74,16 +77,21 @@ func (d *Database) UpdateClient(ctx context.Context, id string, updater func(old
7477
return rollback(tx, "update client updating: %w", err)
7578
}
7679

77-
_, err = tx.OAuth2Client.UpdateOneID(newClient.ID).
80+
update := tx.OAuth2Client.UpdateOneID(newClient.ID).
7881
SetName(newClient.Name).
7982
SetSecret(newClient.Secret).
8083
SetPublic(newClient.Public).
8184
SetLogoURL(newClient.LogoURL).
8285
SetRedirectUris(newClient.RedirectURIs).
8386
SetTrustedPeers(newClient.TrustedPeers).
8487
SetAllowedConnectors(newClient.AllowedConnectors).
85-
SetMfaChain(newClient.MFAChain).
86-
Save(ctx)
88+
SetMfaChain(newClient.MFAChain)
89+
if newClient.ClientCredentialsClaims != nil {
90+
update = update.SetClientCredentialsClaims(newClient.ClientCredentialsClaims)
91+
} else {
92+
update = update.ClearClientCredentialsClaims()
93+
}
94+
_, err = update.Save(ctx)
8795
if err != nil {
8896
return rollback(tx, "update client uploading: %w", err)
8997
}

storage/ent/client/types.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ func toStorageClient(c *db.OAuth2Client) storage.Client {
8888
Public: c.Public,
8989
Name: c.Name,
9090
LogoURL: c.LogoURL,
91-
AllowedConnectors: c.AllowedConnectors,
92-
MFAChain: c.MfaChain,
91+
AllowedConnectors: c.AllowedConnectors,
92+
MFAChain: c.MfaChain,
93+
ClientCredentialsClaims: c.ClientCredentialsClaims,
9394
}
9495
}
9596

storage/ent/db/migrate/schema.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

storage/ent/db/mutation.go

Lines changed: 93 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

storage/ent/db/oauth2client.go

Lines changed: 17 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

storage/ent/db/oauth2client/oauth2client.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

storage/ent/db/oauth2client/where.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)