firebase / firebase-admin-go

Firebase Admin Go SDK

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

mattrltrent opened this issue · comments

Describe your environment

  • Operating System version: Macbook Pro Apple M1 Ventura 13.4.1 running Dockerized env from golang:1.20-alpine.
  • Firebase SDK version: 4
  • Library version: v4.12.0
  • Firebase Product: Appcheck

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
}

I found a few problems with this issue:

  • I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
  • This issue does not seem to follow the issue template. Make sure you provide all the required information.

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