Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add X-Goog-Api-Client header to Auth and FCM requests #648

Merged
merged 3 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"errors"
"fmt"
"os"
"runtime"
"strings"
"time"

Expand Down Expand Up @@ -133,10 +134,12 @@ func NewClient(ctx context.Context, conf *internal.AuthConfig) (*Client, error)
return nil, err
}

goVersion := strings.TrimPrefix(runtime.Version(), "go")
hc := internal.WithDefaultRetryConfig(transport)
hc.CreateErrFn = handleHTTPError
hc.Opts = []internal.HTTPOption{
internal.WithHeader("X-Client-Version", fmt.Sprintf("Go/Admin/%s", conf.Version)),
internal.WithHeader("x-goog-api-client", fmt.Sprintf("gl-go/%s fire-admin/%s", goVersion, conf.Version)),
}

baseURL := defaultAuthURL
Expand Down
8 changes: 8 additions & 0 deletions auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"log"
"net/http"
"os"
"runtime"
"strings"
"syscall"
"testing"
Expand Down Expand Up @@ -1451,6 +1452,13 @@ func checkBaseClient(client *Client, wantProjectID string) error {
return fmt.Errorf("version = %q; want = %q", version, wantVersion)
}

goVersion := strings.TrimPrefix(runtime.Version(), "go")
xGoogAPIClientHeader := req.Header.Get("x-goog-api-client")
wantXGoogAPIClientHeader := fmt.Sprintf("gl-go/%s fire-admin/%s", goVersion, testVersion)
if xGoogAPIClientHeader != wantXGoogAPIClientHeader {
return fmt.Errorf("x-goog-api-client header = %q; want = %q", xGoogAPIClientHeader, wantXGoogAPIClientHeader)
}

return nil
}

Expand Down
7 changes: 7 additions & 0 deletions auth/user_mgt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"net/http"
"net/http/httptest"
"reflect"
"runtime"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -2315,6 +2316,12 @@ func echoServer(resp interface{}, t *testing.T) *mockAuthServer {
t.Errorf("X-Client-Version header = %q; want: %q", gh, wh)
}

gh = r.Header.Get("x-goog-api-client")
wh = fmt.Sprintf("gl-go/%s fire-admin/%s", strings.TrimPrefix(runtime.Version(), "go"), testVersion)
if gh != wh {
t.Errorf("x-goog-api-client header = %q; want: %q", gh, wh)
}

for k, v := range s.Header {
w.Header().Set(k, v)
}
Expand Down
3 changes: 3 additions & 0 deletions messaging/messaging.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"net/http"
"regexp"
"runtime"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -893,10 +894,12 @@ func newFCMClient(hc *http.Client, conf *internal.MessagingConfig, messagingEndp
client := internal.WithDefaultRetryConfig(hc)
client.CreateErrFn = handleFCMError

goVersion := strings.TrimPrefix(runtime.Version(), "go")
version := fmt.Sprintf("fire-admin-go/%s", conf.Version)
client.Opts = []internal.HTTPOption{
internal.WithHeader(apiFormatVersionHeader, apiFormatVersion),
internal.WithHeader(firebaseClientHeader, version),
internal.WithHeader("x-goog-api-client", fmt.Sprintf("gl-go/%s fire-admin/%s", goVersion, conf.Version)),
}

return &fcmClient{
Expand Down
7 changes: 7 additions & 0 deletions messaging/messaging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"net/http"
"net/http/httptest"
"reflect"
"runtime"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -1394,6 +1396,11 @@ func checkFCMRequest(t *testing.T, b []byte, tr *http.Request, want map[string]i
if h := tr.Header.Get("X-FIREBASE-CLIENT"); h != clientVersion {
t.Errorf("X-FIREBASE-CLIENT = %q; want = %q", h, clientVersion)
}
goVersion := strings.TrimPrefix(runtime.Version(), "go")
xGoogAPIClientHeader := "gl-go/" + goVersion + " fire-admin/" + testMessagingConfig.Version
if h := tr.Header.Get("x-goog-api-client"); h != xGoogAPIClientHeader {
t.Errorf("x-goog-api-client header = %q; want = %q", h, xGoogAPIClientHeader)
}
}

var httpErrors = []struct {
Expand Down