@@ -34,9 +34,11 @@ import (
34
34
)
35
35
36
36
const (
37
- credEnvVar = "GOOGLE_APPLICATION_CREDENTIALS"
38
- testProjectID = "mock-project-id"
39
- testVersion = "test-version"
37
+ credEnvVar = "GOOGLE_APPLICATION_CREDENTIALS"
38
+ testProjectID = "mock-project-id"
39
+ testVersion = "test-version"
40
+ defaultIDToolkitV1Endpoint = "https://identitytoolkit.googleapis.com/v1"
41
+ defaultIDToolkitV2Beta1Endpoint = "https://identitytoolkit.googleapis.com/v2beta1"
40
42
)
41
43
42
44
var (
@@ -280,6 +282,38 @@ func TestNewClientExplicitNoAuth(t *testing.T) {
280
282
}
281
283
}
282
284
285
+ func TestNewClientEmulatorHostEnvVar (t * testing.T ) {
286
+ emulatorHost := "localhost:9099"
287
+ idToolkitV1Endpoint := "http://localhost:9099/identitytoolkit.googleapis.com/v1"
288
+ idToolkitV2Beta1Endpoint := "http://localhost:9099/identitytoolkit.googleapis.com/v2beta1"
289
+
290
+ os .Setenv (emulatorHostEnvVar , emulatorHost )
291
+ defer os .Unsetenv (emulatorHostEnvVar )
292
+
293
+ conf := & internal.AuthConfig {
294
+ Opts : []option.ClientOption {
295
+ option .WithoutAuthentication (),
296
+ },
297
+ }
298
+ client , err := NewClient (context .Background (), conf )
299
+ if err != nil {
300
+ t .Fatal (err )
301
+ }
302
+ baseClient := client .baseClient
303
+ if baseClient .userManagementEndpoint != idToolkitV1Endpoint {
304
+ t .Errorf ("baseClient.userManagementEndpoint = %q; want = %q" , baseClient .userManagementEndpoint , idToolkitV1Endpoint )
305
+ }
306
+ if baseClient .providerConfigEndpoint != idToolkitV2Beta1Endpoint {
307
+ t .Errorf ("baseClient.providerConfigEndpoint = %q; want = %q" , baseClient .providerConfigEndpoint , idToolkitV2Beta1Endpoint )
308
+ }
309
+ if baseClient .tenantMgtEndpoint != idToolkitV2Beta1Endpoint {
310
+ t .Errorf ("baseClient.tenantMgtEndpoint = %q; want = %q" , baseClient .tenantMgtEndpoint , idToolkitV2Beta1Endpoint )
311
+ }
312
+ if _ , ok := baseClient .signer .(emulatedSigner ); ! ok {
313
+ t .Errorf ("baseClient.signer = %#v; want = %#v" , baseClient .signer , emulatedSigner {})
314
+ }
315
+ }
316
+
283
317
func TestCustomToken (t * testing.T ) {
284
318
client := & Client {
285
319
baseClient : & baseClient {
@@ -663,6 +697,27 @@ func TestVerifyIDTokenWithNoProjectID(t *testing.T) {
663
697
}
664
698
}
665
699
700
+ func TestVerifyIDTokenInEmulatorMode (t * testing.T ) {
701
+ os .Setenv (emulatorHostEnvVar , "localhost:9099" )
702
+ defer os .Unsetenv (emulatorHostEnvVar )
703
+
704
+ conf := & internal.AuthConfig {
705
+ ProjectID : "" ,
706
+ Opts : optsWithTokenSource ,
707
+ }
708
+ c , err := NewClient (context .Background (), conf )
709
+ if err != nil {
710
+ t .Fatal (err )
711
+ }
712
+
713
+ defer func () {
714
+ if r := recover (); r == nil {
715
+ t .Error ("VeridyIDToken() didn't panic" )
716
+ }
717
+ }()
718
+ c .VerifyIDToken (context .Background (), testIDToken )
719
+ }
720
+
666
721
func TestCustomTokenVerification (t * testing.T ) {
667
722
client := & Client {
668
723
baseClient : & baseClient {
@@ -682,7 +737,7 @@ func TestCustomTokenVerification(t *testing.T) {
682
737
}
683
738
684
739
func TestCertificateRequestError (t * testing.T ) {
685
- tv , err := newIDTokenVerifier (context .Background (), testProjectID )
740
+ tv , err := newIDTokenVerifier (context .Background (), testProjectID , false )
686
741
if err != nil {
687
742
t .Fatal (err )
688
743
}
@@ -1022,7 +1077,7 @@ func signerForTests(ctx context.Context) (cryptoSigner, error) {
1022
1077
}
1023
1078
1024
1079
func idTokenVerifierForTests (ctx context.Context ) (* tokenVerifier , error ) {
1025
- tv , err := newIDTokenVerifier (ctx , testProjectID )
1080
+ tv , err := newIDTokenVerifier (ctx , testProjectID , false )
1026
1081
if err != nil {
1027
1082
return nil , err
1028
1083
}
@@ -1036,7 +1091,7 @@ func idTokenVerifierForTests(ctx context.Context) (*tokenVerifier, error) {
1036
1091
}
1037
1092
1038
1093
func cookieVerifierForTests (ctx context.Context ) (* tokenVerifier , error ) {
1039
- tv , err := newSessionCookieVerifier (ctx , testProjectID )
1094
+ tv , err := newSessionCookieVerifier (ctx , testProjectID , false )
1040
1095
if err != nil {
1041
1096
return nil , err
1042
1097
}
@@ -1163,23 +1218,26 @@ func checkCookieVerifier(tv *tokenVerifier, projectID string) error {
1163
1218
}
1164
1219
1165
1220
func checkBaseClient (client * Client , wantProjectID string ) error {
1166
- umc := client .baseClient
1167
- if umc .userManagementEndpoint != idToolkitV1Endpoint {
1168
- return fmt .Errorf ("userManagementEndpoint = %q; want = %q" , umc .userManagementEndpoint , idToolkitV1Endpoint )
1221
+ baseClient := client .baseClient
1222
+ if baseClient .userManagementEndpoint != defaultIDToolkitV1Endpoint {
1223
+ return fmt .Errorf ("userManagementEndpoint = %q; want = %q" , baseClient .userManagementEndpoint , defaultIDToolkitV1Endpoint )
1224
+ }
1225
+ if baseClient .providerConfigEndpoint != defaultIDToolkitV2Beta1Endpoint {
1226
+ return fmt .Errorf ("providerConfigEndpoint = %q; want = %q" , baseClient .providerConfigEndpoint , defaultIDToolkitV2Beta1Endpoint )
1169
1227
}
1170
- if umc . providerConfigEndpoint != providerConfigEndpoint {
1171
- return fmt .Errorf ("providerConfigEndpoint = %q; want = %q" , umc .providerConfigEndpoint , providerConfigEndpoint )
1228
+ if baseClient . tenantMgtEndpoint != defaultIDToolkitV2Beta1Endpoint {
1229
+ return fmt .Errorf ("providerConfigEndpoint = %q; want = %q" , baseClient .providerConfigEndpoint , defaultIDToolkitV2Beta1Endpoint )
1172
1230
}
1173
- if umc .projectID != wantProjectID {
1174
- return fmt .Errorf ("projectID = %q; want = %q" , umc .projectID , wantProjectID )
1231
+ if baseClient .projectID != wantProjectID {
1232
+ return fmt .Errorf ("projectID = %q; want = %q" , baseClient .projectID , wantProjectID )
1175
1233
}
1176
1234
1177
1235
req , err := http .NewRequest (http .MethodGet , "https://firebase.google.com" , nil )
1178
1236
if err != nil {
1179
1237
return err
1180
1238
}
1181
1239
1182
- for _ , opt := range umc .httpClient .Opts {
1240
+ for _ , opt := range baseClient .httpClient .Opts {
1183
1241
opt (req )
1184
1242
}
1185
1243
version := req .Header .Get ("X-Client-Version" )
0 commit comments