firebase / firebase-admin-go

Firebase Admin Go SDK
Apache License 2.0
1.14k stars 247 forks source link

AppCheck Admin SDK requires use of internal package externally in its API #572

Open mattrltrent opened 1 year ago

mattrltrent commented 1 year ago

Describe your environment

Describe the problem

Original Stack Overflow source here.

First, I believe the Firebase documentation for how to use AppCheck with the Golang SDK is wrong here.

It says you need to create a new appcheck.Client directly via method off of your firebase.App, but this method doesn't exist (anymore?):

func main() {
    app, err := firebaseAdmin.NewApp(context.Background(), nil)
    if err != nil {
        log.Fatalf("error initializing app: %v\n", err)
    }

    appCheck, err = app.AppCheck(context.Background()) // <-- here, does not exist
    if err != nil {
        log.Fatalf("error initializing app: %v\n", err)
    }

    http.HandleFunc("/yourApiEndpoint", requireAppCheck(yourApiEndpointHandler))
    log.Fatal(http.ListenAndServe(":8080", nil))
}

So, instead, I've referred to Firebase's appcheck package here which says you now need to create a new instance via this method instead:

func NewClient(ctx context.Context, conf *internal.AppCheckConfig) (*Client, error)

This, in theory, should work. However, note the use of an internal package.

We can try using NewClient(...), and will be able to provide its first arg of context.Context, but won't be able to provide a value for its second arg of type *internal.AppCheckConfig because it's internal.

Steps to reproduce:

import firebase "firebase.google.com/go"

// get the admin app of type *firebase.App
app, err := firebase.NewApp(context.Background(), nil, opt)
if err != nil {
    return err
}

// then try to get the type of *appcheck.Client
appCheck, err := appcheck.NewClient(context.Background(), &internal.AppCheckConfig{ // <-- won't work because using internal package
    ProjectID: "your_proj_id",
})
if err != nil {
    return err
}
google-oss-bot commented 1 year ago

I found a few problems with this issue:

fis commented 1 year ago

The "steps to reproduce" example imports the v1 major version of the module, which did not have an AppCheck method. The v4 major version ("firebase.google.com/go/v4") used by the example in the documentation does (docs).

The example from the documentation builds without errors for me. In an empty directory:

$ cat > main.go
package main

import (
    "context"
    "log"
    "net/http"

    firebaseAdmin "firebase.google.com/go/v4"
    "firebase.google.com/go/v4/appcheck"
)
// ...
// rest of the code from https://firebase.google.com/docs/app-check/custom-resource-backend#go
// ...
$ go mod init example.com/test
go: creating new go.mod: module example.com/test
go: to add module requirements and sums:
$ go mod tidy
go: finding module for package firebase.google.com/go/v4/appcheck
go: finding module for package firebase.google.com/go/v4
go: found firebase.google.com/go/v4 in firebase.google.com/go/v4 v4.12.0
go: found firebase.google.com/go/v4/appcheck in firebase.google.com/go/v4 v4.12.0
$ go build .
// no errors