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 2 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
2 changes: 2 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 @@ -137,6 +138,7 @@ func NewClient(ctx context.Context, conf *internal.AuthConfig) (*Client, error)
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", runtime.Version(), conf.Version)),
lahirumaramba marked this conversation as resolved.
Show resolved Hide resolved
}

baseURL := defaultAuthURL
Expand Down
7 changes: 7 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,12 @@ func checkBaseClient(client *Client, wantProjectID string) error {
return fmt.Errorf("version = %q; want = %q", version, wantVersion)
}

xGoogAPIClientHeader := req.Header.Get("x-goog-api-client")
wantXGoogAPIClientHeader := fmt.Sprintf("gl-go/%s fire-admin/%s", runtime.Version(), 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", runtime.Version(), 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
2 changes: 2 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 @@ -897,6 +898,7 @@ func newFCMClient(hc *http.Client, conf *internal.MessagingConfig, messagingEndp
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", runtime.Version(), conf.Version)),
}

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

Expand Down Expand Up @@ -1394,6 +1395,10 @@ 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)
}
xGoogAPIClientHeader := "gl-go/" + runtime.Version() + " 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