Skip to content

Commit

Permalink
fix: Skipping credential lookup in emulator mode (#459)
Browse files Browse the repository at this point in the history
  • Loading branch information
hiranya911 committed Sep 2, 2021
1 parent ea909b1 commit 241c293
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
16 changes: 15 additions & 1 deletion auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"time"

"firebase.google.com/go/v4/internal"
"golang.org/x/oauth2"
"google.golang.org/api/option"
"google.golang.org/api/transport"
)

Expand All @@ -47,6 +49,10 @@ var reservedClaims = []string{
"exp", "firebase", "iat", "iss", "jti", "nbf", "nonce", "sub",
}

var emulatorToken = &oauth2.Token{
AccessToken: "owner",
}

// Client is the interface for the Firebase auth service.
//
// Client facilitates generating custom JWT tokens for Firebase clients, and verifying ID tokens issued
Expand Down Expand Up @@ -114,7 +120,15 @@ func NewClient(ctx context.Context, conf *internal.AuthConfig) (*Client, error)
return nil, err
}

transport, _, err := transport.NewHTTPClient(ctx, conf.Opts...)
var opts []option.ClientOption
if isEmulator {
ts := oauth2.StaticTokenSource(emulatorToken)
opts = append(opts, option.WithTokenSource(ts))
} else {
opts = append(opts, conf.Opts...)
}

transport, _, err := transport.NewHTTPClient(ctx, opts...)
if err != nil {
return nil, err
}
Expand Down
10 changes: 4 additions & 6 deletions auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,11 @@ func TestNewClientEmulatorHostEnvVar(t *testing.T) {
os.Setenv(emulatorHostEnvVar, emulatorHost)
defer os.Unsetenv(emulatorHostEnvVar)

conf := &internal.AuthConfig{
Opts: []option.ClientOption{
option.WithoutAuthentication(),
},
}
client, err := NewClient(context.Background(), conf)
client, err := NewClient(context.Background(), &internal.AuthConfig{})
if err != nil {
t.Fatal(err)
}

baseClient := client.baseClient
if baseClient.userManagementEndpoint != idToolkitV1Endpoint {
t.Errorf("baseClient.userManagementEndpoint = %q; want = %q", baseClient.userManagementEndpoint, idToolkitV1Endpoint)
Expand Down Expand Up @@ -778,6 +774,7 @@ func TestEmulatorVerifyIDTokenUnreachableEmulator(t *testing.T) {
t.Fatal(err)
}
client.httpClient.Client.Transport = eConnRefusedTransport{}
client.httpClient.RetryConfig = nil
client.isEmulator = true

token := getEmulatedIDToken(nil)
Expand Down Expand Up @@ -1239,6 +1236,7 @@ func TestEmulatorVerifySessionCookieUnreachableEmulator(t *testing.T) {
t.Fatal(err)
}
client.httpClient.Client.Transport = eConnRefusedTransport{}
client.httpClient.RetryConfig = nil
client.isEmulator = true

token := getEmulatedSessionCookie(nil)
Expand Down

0 comments on commit 241c293

Please sign in to comment.