How to verify if Google Firebase admin SDK credentials are correct

Viewed 318

Is there any way to verify if Firebase Admin SDK Credentials is correct when initializing app with below code?

ctx := context.Background()
opt := option.WithCredentialsFile("path/to/firebase-admin-sdk-cred.json")
app, err := firebase.NewApp(ctx, nil, opt)
if err != nil {
    return nil, err
}

Because I seem to not get any error when I tried to intentionally put the wrong credentials. I check the implementation of the firebase.NewApp() but it seems it only throw error when there is no config. Below is the code of firebase.NewApp()

func NewApp(ctx context.Context, config *Config, opts ...option.ClientOption) (*App, error) {
o := []option.ClientOption{option.WithScopes(internal.FirebaseScopes...)}
o = append(o, opts...)
if config == nil {
    var err error
    if config, err = getConfigDefaults(); err != nil {
        return nil, err
    }
}

pid := getProjectID(ctx, config, o...)
ao := defaultAuthOverrides
if config.AuthOverride != nil {
    ao = *config.AuthOverride
}

return &App{
    authOverride:     ao,
    dbURL:            config.DatabaseURL,
    projectID:        pid,
    serviceAccountID: config.ServiceAccountID,
    storageBucket:    config.StorageBucket,
    opts:             o,
}, nil 
}

so Is there any way to check if the credentials is valid during the initialization of Firebase Admin(app) instance because it seems catching error isn't the solution here?

2 Answers

I think the answer is in the documentation:

Some use cases require you to create multiple apps at the same time. For example, you might want to read data from the Realtime Database of one Firebase project and mint custom tokens for another project. Or you might want to authenticate two apps with separate credentials. The Firebase SDK allows you create multiple apps at the same time, each with their own configuration information.

Source

I assume the only way to check credentials is invoke an Auth method for example:

client, err := app.Auth(context.Background())

I ended up using google.golang.org/api/transport to force the validation and fail fast

// Check if credential is correct
_, err = transport.Creds(ctx, opt)
if err != nil {
    return nil, err
}
Related