firebase / firebase-admin-go

Firebase Admin Go SDK
Apache License 2.0
1.13k stars 242 forks source link

Authentication emulator fails on clean environment to required default credentials #458

Closed samlof closed 2 years ago

samlof commented 3 years ago

[REQUIRED] Step 2: Describe your environment

[REQUIRED] Step 3: Describe the problem

Running auth emulator with the wanted env variable FIREBASE_AUTH_EMULATOR_HOST fails.

Steps to reproduce:

Start a project in a clean machine. Add env variable FIREBASE_AUTH_EMULATOR_HOST and make firebase auth app. It gives error google: could not find default credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.

Relevant Code:

conf := &firebase.Config{ProjectID: "demo-project"}
app, err := firebase.NewApp(ctx, conf)
auth, err := app.Auth(ctx)
if err != nil {
   log.Fatalf("making firebase auth failed: %v", err) // this line is hit
}

The problem was missing the option firebase.NewApp(ctx, conf, option.WithoutAuthentication()). This isn't documented anywhere. Took me an hour of searching google and trying all kinds of things.

I came across this as Github Action failed to it. Tested on a brand new EC2 ubuntu instance in Amazon and fails too. With WithoutAuthentication() option both work as expected.

samlof commented 3 years ago

What you actually want is

authOpt := option.WithTokenSource(
            oauth2.StaticTokenSource(
                &oauth2.Token{
                    AccessToken: "owner",
                    TokenType:   "Bearer",
                }))
app, err = firebase.NewApp(ctx, conf, authOpt)

This one works with creating users too. WithoutAuthentication() doesn't work with creating user

hiranya911 commented 2 years ago

Fixed with the above PR. Will be included in the next release.