Skip to content

Commit 27a53e5

Browse files
authored
feat(auth): IdP Configuration Management API (#286)
* Added IdP management functions to the public API surface * Added integration tests * Minor test update * Added snippets
1 parent 722f904 commit 27a53e5

File tree

9 files changed

+504
-61
lines changed

9 files changed

+504
-61
lines changed

auth/auth.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"strings"
2424

2525
"firebase.google.com/go/internal"
26+
"google.golang.org/api/transport"
2627
)
2728

2829
const (
@@ -40,8 +41,8 @@ var reservedClaims = []string{
4041
// Client facilitates generating custom JWT tokens for Firebase clients, and verifying ID tokens issued
4142
// by Firebase backend services.
4243
type Client struct {
43-
userManagementClient
44-
pcc *providerConfigClient // TODO: Embed this to add the methods to the public API
44+
*userManagementClient
45+
*providerConfigClient
4546
idTokenVerifier *tokenVerifier
4647
cookieVerifier *tokenVerifier
4748
signer cryptoSigner
@@ -83,24 +84,24 @@ func NewClient(ctx context.Context, conf *internal.AuthConfig) (*Client, error)
8384
}
8485
}
8586

86-
userMgt, err := newUserManagementClient(ctx, conf)
87+
idTokenVerifier, err := newIDTokenVerifier(ctx, conf.ProjectID)
8788
if err != nil {
8889
return nil, err
8990
}
9091

91-
idTokenVerifier, err := newIDTokenVerifier(ctx, conf.ProjectID)
92+
cookieVerifier, err := newSessionCookieVerifier(ctx, conf.ProjectID)
9293
if err != nil {
9394
return nil, err
9495
}
9596

96-
cookieVerifier, err := newSessionCookieVerifier(ctx, conf.ProjectID)
97+
hc, _, err := transport.NewHTTPClient(ctx, conf.Opts...)
9798
if err != nil {
9899
return nil, err
99100
}
100101

101102
return &Client{
102-
userManagementClient: *userMgt,
103-
pcc: newProviderConfigClient(userMgt.httpClient.Client, conf),
103+
userManagementClient: newUserManagementClient(hc, conf),
104+
providerConfigClient: newProviderConfigClient(hc, conf),
104105
idTokenVerifier: idTokenVerifier,
105106
cookieVerifier: cookieVerifier,
106107
signer: signer,

auth/auth_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -941,19 +941,20 @@ func checkCookieVerifier(tv *tokenVerifier, projectID string) error {
941941
}
942942

943943
func checkUserManagementClient(client *Client, wantProjectID string) error {
944-
if client.baseURL != idToolkitV1Endpoint {
945-
return fmt.Errorf("baseURL = %q; want = %q", client.baseURL, idToolkitV1Endpoint)
944+
umc := client.userManagementClient
945+
if umc.baseURL != idToolkitV1Endpoint {
946+
return fmt.Errorf("baseURL = %q; want = %q", umc.baseURL, idToolkitV1Endpoint)
946947
}
947-
if client.projectID != wantProjectID {
948-
return fmt.Errorf("projectID = %q; want = %q", client.projectID, wantProjectID)
948+
if umc.projectID != wantProjectID {
949+
return fmt.Errorf("projectID = %q; want = %q", umc.projectID, wantProjectID)
949950
}
950951

951952
req, err := http.NewRequest(http.MethodGet, "https://firebase.google.com", nil)
952953
if err != nil {
953954
return err
954955
}
955956

956-
for _, opt := range client.httpClient.Opts {
957+
for _, opt := range umc.httpClient.Opts {
957958
opt(req)
958959
}
959960
version := req.Header.Get("X-Client-Version")

auth/email_action_links_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ func TestEmailVerificationLinkError(t *testing.T) {
265265
}
266266
s := echoServer(testActionLinkResponse, t)
267267
defer s.Close()
268-
s.Client.httpClient.RetryConfig = nil
268+
s.Client.userManagementClient.httpClient.RetryConfig = nil
269269
s.Status = http.StatusInternalServerError
270270

271271
for code, check := range cases {

auth/provider_config.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -601,19 +601,18 @@ type providerConfigClient struct {
601601
httpClient *internal.HTTPClient
602602
}
603603

604-
func newProviderConfigClient(hc *http.Client, conf *internal.AuthConfig) *providerConfigClient {
605-
client := &internal.HTTPClient{
606-
Client: hc,
607-
SuccessFn: internal.HasSuccessStatus,
608-
CreateErrFn: handleHTTPError,
609-
Opts: []internal.HTTPOption{
610-
internal.WithHeader("X-Client-Version", fmt.Sprintf("Go/Admin/%s", conf.Version)),
611-
},
604+
func newProviderConfigClient(client *http.Client, conf *internal.AuthConfig) *providerConfigClient {
605+
hc := internal.WithDefaultRetryConfig(client)
606+
hc.CreateErrFn = handleHTTPError
607+
hc.SuccessFn = internal.HasSuccessStatus
608+
hc.Opts = []internal.HTTPOption{
609+
internal.WithHeader("X-Client-Version", fmt.Sprintf("Go/Admin/%s", conf.Version)),
612610
}
611+
613612
return &providerConfigClient{
614613
endpoint: providerConfigEndpoint,
615614
projectID: conf.ProjectID,
616-
httpClient: client,
615+
httpClient: hc,
617616
}
618617
}
619618

auth/provider_config_test.go

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func TestOIDCProviderConfig(t *testing.T) {
101101
s := echoServer([]byte(oidcConfigResponse), t)
102102
defer s.Close()
103103

104-
client := s.Client.pcc
104+
client := s.Client
105105
oidc, err := client.OIDCProviderConfig(context.Background(), "oidc.provider")
106106
if err != nil {
107107
t.Fatal(err)
@@ -139,7 +139,7 @@ func TestOIDCProviderConfigError(t *testing.T) {
139139
defer s.Close()
140140
s.Status = http.StatusNotFound
141141

142-
client := s.Client.pcc
142+
client := s.Client
143143
saml, err := client.OIDCProviderConfig(context.Background(), "oidc.provider")
144144
if saml != nil || err == nil || !IsConfigurationNotFound(err) {
145145
t.Errorf("OIDCProviderConfig() = (%v, %v); want = (nil, ConfigurationNotFound)", saml, err)
@@ -150,7 +150,7 @@ func TestCreateOIDCProviderConfig(t *testing.T) {
150150
s := echoServer([]byte(oidcConfigResponse), t)
151151
defer s.Close()
152152

153-
client := s.Client.pcc
153+
client := s.Client
154154
options := (&OIDCProviderConfigToCreate{}).
155155
ID(oidcProviderConfig.ID).
156156
DisplayName(oidcProviderConfig.DisplayName).
@@ -181,7 +181,7 @@ func TestCreateOIDCProviderConfigMinimal(t *testing.T) {
181181
s := echoServer([]byte(oidcConfigResponse), t)
182182
defer s.Close()
183183

184-
client := s.Client.pcc
184+
client := s.Client
185185
options := (&OIDCProviderConfigToCreate{}).
186186
ID(oidcProviderConfig.ID).
187187
ClientID(oidcProviderConfig.ClientID).
@@ -207,7 +207,7 @@ func TestCreateOIDCProviderConfigMinimal(t *testing.T) {
207207
func TestCreateOIDCProviderConfigZeroValues(t *testing.T) {
208208
s := echoServer([]byte(oidcConfigResponse), t)
209209
defer s.Close()
210-
client := s.Client.pcc
210+
client := s.Client
211211

212212
options := (&OIDCProviderConfigToCreate{}).
213213
ID(oidcProviderConfig.ID).
@@ -240,7 +240,8 @@ func TestCreateOIDCProviderConfigError(t *testing.T) {
240240
s.Status = http.StatusInternalServerError
241241
defer s.Close()
242242

243-
client := s.Client.pcc
243+
client := s.Client
244+
client.providerConfigClient.httpClient.RetryConfig = nil
244245
options := (&OIDCProviderConfigToCreate{}).
245246
ID(oidcProviderConfig.ID).
246247
ClientID(oidcProviderConfig.ClientID).
@@ -316,7 +317,7 @@ func TestUpdateOIDCProviderConfig(t *testing.T) {
316317
s := echoServer([]byte(oidcConfigResponse), t)
317318
defer s.Close()
318319

319-
client := s.Client.pcc
320+
client := s.Client
320321
options := (&OIDCProviderConfigToUpdate{}).
321322
DisplayName(oidcProviderConfig.DisplayName).
322323
Enabled(oidcProviderConfig.Enabled).
@@ -352,7 +353,7 @@ func TestUpdateOIDCProviderConfigMinimal(t *testing.T) {
352353
s := echoServer([]byte(oidcConfigResponse), t)
353354
defer s.Close()
354355

355-
client := s.Client.pcc
356+
client := s.Client
356357
options := (&OIDCProviderConfigToUpdate{}).
357358
DisplayName("Other name")
358359
oidc, err := client.UpdateOIDCProviderConfig(context.Background(), "oidc.provider", options)
@@ -379,7 +380,7 @@ func TestUpdateOIDCProviderConfigZeroValues(t *testing.T) {
379380
s := echoServer([]byte(oidcConfigResponse), t)
380381
defer s.Close()
381382

382-
client := s.Client.pcc
383+
client := s.Client
383384
options := (&OIDCProviderConfigToUpdate{}).
384385
DisplayName("").
385386
Enabled(false)
@@ -468,7 +469,7 @@ func TestDeleteOIDCProviderConfig(t *testing.T) {
468469
s := echoServer([]byte("{}"), t)
469470
defer s.Close()
470471

471-
client := s.Client.pcc
472+
client := s.Client
472473
if err := client.DeleteOIDCProviderConfig(context.Background(), "oidc.provider"); err != nil {
473474
t.Fatal(err)
474475
}
@@ -501,7 +502,7 @@ func TestDeleteOIDCProviderConfigError(t *testing.T) {
501502
defer s.Close()
502503
s.Status = http.StatusNotFound
503504

504-
client := s.Client.pcc
505+
client := s.Client
505506
err := client.DeleteOIDCProviderConfig(context.Background(), "oidc.provider")
506507
if err == nil || !IsConfigurationNotFound(err) {
507508
t.Errorf("DeleteOIDCProviderConfig() = %v; want = ConfigurationNotFound", err)
@@ -562,7 +563,7 @@ func TestOIDCProviderConfigs(t *testing.T) {
562563
}
563564
}
564565

565-
client := s.Client.pcc
566+
client := s.Client
566567
testIterator(
567568
client.OIDCProviderConfigs(context.Background(), ""),
568569
"",
@@ -578,7 +579,8 @@ func TestOIDCProviderConfigsError(t *testing.T) {
578579
defer s.Close()
579580
s.Status = http.StatusInternalServerError
580581

581-
client := s.Client.pcc
582+
client := s.Client
583+
client.providerConfigClient.httpClient.RetryConfig = nil
582584
it := client.OIDCProviderConfigs(context.Background(), "")
583585
config, err := it.Next()
584586
if config != nil || err == nil || !IsUnknown(err) {
@@ -590,7 +592,7 @@ func TestSAMLProviderConfig(t *testing.T) {
590592
s := echoServer([]byte(samlConfigResponse), t)
591593
defer s.Close()
592594

593-
client := s.Client.pcc
595+
client := s.Client
594596
saml, err := client.SAMLProviderConfig(context.Background(), "saml.provider")
595597
if err != nil {
596598
t.Fatal(err)
@@ -628,7 +630,7 @@ func TestSAMLProviderConfigError(t *testing.T) {
628630
defer s.Close()
629631
s.Status = http.StatusNotFound
630632

631-
client := s.Client.pcc
633+
client := s.Client
632634
saml, err := client.SAMLProviderConfig(context.Background(), "saml.provider")
633635
if saml != nil || err == nil || !IsConfigurationNotFound(err) {
634636
t.Errorf("SAMLProviderConfig() = (%v, %v); want = (nil, ConfigurationNotFound)", saml, err)
@@ -639,7 +641,7 @@ func TestCreateSAMLProviderConfig(t *testing.T) {
639641
s := echoServer([]byte(samlConfigResponse), t)
640642
defer s.Close()
641643

642-
client := s.Client.pcc
644+
client := s.Client
643645
options := (&SAMLProviderConfigToCreate{}).
644646
ID(samlProviderConfig.ID).
645647
DisplayName(samlProviderConfig.DisplayName).
@@ -682,7 +684,7 @@ func TestCreateSAMLProviderConfigMinimal(t *testing.T) {
682684
s := echoServer([]byte(samlConfigResponse), t)
683685
defer s.Close()
684686

685-
client := s.Client.pcc
687+
client := s.Client
686688
options := (&SAMLProviderConfigToCreate{}).
687689
ID(samlProviderConfig.ID).
688690
IDPEntityID(samlProviderConfig.IDPEntityID).
@@ -718,7 +720,7 @@ func TestCreateSAMLProviderConfigMinimal(t *testing.T) {
718720
func TestCreateSAMLProviderConfigZeroValues(t *testing.T) {
719721
s := echoServer([]byte(samlConfigResponse), t)
720722
defer s.Close()
721-
client := s.Client.pcc
723+
client := s.Client
722724

723725
options := (&SAMLProviderConfigToCreate{}).
724726
ID(samlProviderConfig.ID).
@@ -763,7 +765,8 @@ func TestCreateSAMLProviderConfigError(t *testing.T) {
763765
s.Status = http.StatusInternalServerError
764766
defer s.Close()
765767

766-
client := s.Client.pcc
768+
client := s.Client
769+
client.providerConfigClient.httpClient.RetryConfig = nil
767770
options := (&SAMLProviderConfigToCreate{}).
768771
ID(samlProviderConfig.ID).
769772
IDPEntityID(samlProviderConfig.IDPEntityID).
@@ -889,7 +892,7 @@ func TestUpdateSAMLProviderConfig(t *testing.T) {
889892
s := echoServer([]byte(samlConfigResponse), t)
890893
defer s.Close()
891894

892-
client := s.Client.pcc
895+
client := s.Client
893896
options := (&SAMLProviderConfigToUpdate{}).
894897
DisplayName(samlProviderConfig.DisplayName).
895898
Enabled(samlProviderConfig.Enabled).
@@ -941,7 +944,7 @@ func TestUpdateSAMLProviderConfigMinimal(t *testing.T) {
941944
s := echoServer([]byte(samlConfigResponse), t)
942945
defer s.Close()
943946

944-
client := s.Client.pcc
947+
client := s.Client
945948
options := (&SAMLProviderConfigToUpdate{}).
946949
DisplayName("Other name")
947950
saml, err := client.UpdateSAMLProviderConfig(context.Background(), "saml.provider", options)
@@ -968,7 +971,7 @@ func TestUpdateSAMLProviderConfigZeroValues(t *testing.T) {
968971
s := echoServer([]byte(samlConfigResponse), t)
969972
defer s.Close()
970973

971-
client := s.Client.pcc
974+
client := s.Client
972975
options := (&SAMLProviderConfigToUpdate{}).
973976
DisplayName("").
974977
Enabled(false).
@@ -1094,7 +1097,7 @@ func TestDeleteSAMLProviderConfig(t *testing.T) {
10941097
s := echoServer([]byte("{}"), t)
10951098
defer s.Close()
10961099

1097-
client := s.Client.pcc
1100+
client := s.Client
10981101
if err := client.DeleteSAMLProviderConfig(context.Background(), "saml.provider"); err != nil {
10991102
t.Fatal(err)
11001103
}
@@ -1127,7 +1130,7 @@ func TestDeleteSAMLProviderConfigError(t *testing.T) {
11271130
defer s.Close()
11281131
s.Status = http.StatusNotFound
11291132

1130-
client := s.Client.pcc
1133+
client := s.Client
11311134
err := client.DeleteSAMLProviderConfig(context.Background(), "saml.provider")
11321135
if err == nil || !IsConfigurationNotFound(err) {
11331136
t.Errorf("DeleteSAMLProviderConfig() = %v; want = ConfigurationNotFound", err)
@@ -1188,7 +1191,7 @@ func TestSAMLProviderConfigs(t *testing.T) {
11881191
}
11891192
}
11901193

1191-
client := s.Client.pcc
1194+
client := s.Client
11921195
testIterator(
11931196
client.SAMLProviderConfigs(context.Background(), ""),
11941197
"",
@@ -1204,7 +1207,8 @@ func TestSAMLProviderConfigsError(t *testing.T) {
12041207
defer s.Close()
12051208
s.Status = http.StatusInternalServerError
12061209

1207-
client := s.Client.pcc
1210+
client := s.Client
1211+
client.providerConfigClient.httpClient.RetryConfig = nil
12081212
it := client.SAMLProviderConfigs(context.Background(), "")
12091213
config, err := it.Next()
12101214
if config != nil || err == nil || !IsUnknown(err) {

auth/user_mgt.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -479,12 +479,8 @@ type userManagementClient struct {
479479
httpClient *internal.HTTPClient
480480
}
481481

482-
func newUserManagementClient(ctx context.Context, conf *internal.AuthConfig) (*userManagementClient, error) {
483-
hc, _, err := internal.NewHTTPClient(ctx, conf.Opts...)
484-
if err != nil {
485-
return nil, err
486-
}
487-
482+
func newUserManagementClient(client *http.Client, conf *internal.AuthConfig) *userManagementClient {
483+
hc := internal.WithDefaultRetryConfig(client)
488484
hc.CreateErrFn = handleHTTPError
489485
hc.SuccessFn = internal.HasSuccessStatus
490486
hc.Opts = []internal.HTTPOption{
@@ -495,7 +491,7 @@ func newUserManagementClient(ctx context.Context, conf *internal.AuthConfig) (*u
495491
baseURL: idToolkitV1Endpoint,
496492
projectID: conf.ProjectID,
497493
httpClient: hc,
498-
}, nil
494+
}
499495
}
500496

501497
// GetUser gets the user data corresponding to the specified user ID.

0 commit comments

Comments
 (0)