Skip to content

Commit 81eddc5

Browse files
authored
chore: Merging v4 branch into dev (#370)
* Renamed WebpushFcmOptions to WebpushFCMOptions (#334) * Added modules support; Added semantic import versioning (#336) * feat: New error handling APIs (#342) * fix: Error handling revamp basic structure * Added more documentation and tests * Updated docs * Exhaustive unit tests; Updated integration test * Fixed some typos in comments * Renamed function in comment * FCM error handling revamp (#343) * FCM error handling revamp * Updated unit tests * RTDB error handling revamp (#345) * Error handling revamp of Auth APIs (#348) * Error handling revamp for Auth APIs * Minor code clean up * Error handling support in the token verification APIs (#349) * New error handling scheme for token verification APIs * Make InvalidToken condition conjunctive * Removing deprecated HTTP and error handling utils (#350) * Removed deprecated APIs from internal * Made HasSuccessStatus the default error checking function * Added unit test for error response handling * Handling timeouts, connection and other network errors (#353) * Handling timeouts, connection and other network errors * Handling wrapped errors * Updated comment * Apply suggestions from code review * Removing old release workflow files * Removing an erroneous import * Fixed bad merge * Fixed failing test due to old dependency * chore: Updated release flow for modules support (#378) * chore: Updated release flow for modules support * Fixed failing unit test * Removed GOPATH from release workflow
1 parent f2eb40b commit 81eddc5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+2175
-979
lines changed

.github/scripts/run_all_tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ gpg --quiet --batch --yes --decrypt --passphrase="${FIREBASE_SERVICE_ACCT_KEY}"
2222

2323
echo "${FIREBASE_API_KEY}" > testdata/integration_apikey.txt
2424

25-
go test -v -race firebase.google.com/go/...
25+
go test -v -race ./...

.github/workflows/ci.yml

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,30 @@ jobs:
55
build:
66
name: Build
77
runs-on: ubuntu-latest
8-
env:
9-
GOPATH: ${{ github.workspace }}/go
108
strategy:
119
matrix:
1210
go: [1.12, 1.13, 1.14]
13-
steps:
1411

12+
steps:
1513
- name: Set up Go ${{ matrix.go }}
1614
uses: actions/setup-go@v1
1715
with:
1816
go-version: ${{ matrix.go }}
19-
id: go
2017

21-
- name: Check out code into GOPATH
18+
- name: Check out code
2219
uses: actions/checkout@v2
23-
with:
24-
path: go/src/firebase.google.com/go
25-
26-
- name: Get dependencies
27-
run: go get -t -v $(go list ./... | grep -v integration)
2820

2921
- name: Run Linter
3022
run: |
31-
go get golang.org/x/lint/golint
32-
$GOPATH/bin/golint -set_exit_status firebase.google.com/go/...
23+
go get -u golang.org/x/lint/golint
24+
GOLINT=`go list -f {{.Target}} golang.org/x/lint/golint`
25+
$GOLINT -set_exit_status ./...
3326
3427
- name: Run Unit Tests
3528
if: success() || failure()
36-
run: go test -v -race -test.short firebase.google.com/go/...
29+
run: go test -v -race -test.short ./...
3730

3831
- name: Run Formatter
39-
working-directory: ./go/src/firebase.google.com/go
4032
run: |
4133
if [[ ! -z "$(gofmt -l -s .)" ]]; then
4234
echo "Go code is not formatted:"
@@ -45,4 +37,4 @@ jobs:
4537
fi
4638
4739
- name: Run Static Analyzer
48-
run: go vet -v firebase.google.com/go/...
40+
run: go vet -v ./...

.github/workflows/release.yml

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,34 +36,26 @@ jobs:
3636

3737
runs-on: ubuntu-latest
3838

39-
env:
40-
GOPATH: ${{ github.workspace }}/go
41-
4239
# When manually triggering the build, the requester can specify a target branch or a tag
4340
# via the 'ref' client parameter.
4441
steps:
45-
- name: Check out code into GOPATH
46-
uses: actions/checkout@v2
47-
with:
48-
path: go/src/firebase.google.com/go
49-
ref: ${{ github.event.client_payload.ref || github.ref }}
50-
5142
- name: Set up Go
5243
uses: actions/setup-go@v1
5344
with:
54-
go-version: 1.11
45+
go-version: 1.12
5546

56-
- name: Get dependencies
57-
run: go get -t -v $(go list ./... | grep -v integration)
47+
- name: Check out code
48+
uses: actions/checkout@v2
49+
with:
50+
ref: ${{ github.event.client_payload.ref || github.ref }}
5851

5952
- name: Run Linter
6053
run: |
61-
echo
62-
go get golang.org/x/lint/golint
63-
$GOPATH/bin/golint -set_exit_status firebase.google.com/go/...
54+
go get -u golang.org/x/lint/golint
55+
GOLINT=`go list -f {{.Target}} golang.org/x/lint/golint`
56+
$GOLINT -set_exit_status ./...
6457
6558
- name: Run Tests
66-
working-directory: ./go/src/firebase.google.com/go
6759
run: ./.github/scripts/run_all_tests.sh
6860
env:
6961
FIREBASE_SERVICE_ACCT_KEY: ${{ secrets.FIREBASE_SERVICE_ACCT_KEY }}

auth/auth.go

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,19 @@ import (
2323
"strings"
2424
"time"
2525

26-
"firebase.google.com/go/internal"
26+
"firebase.google.com/go/v4/internal"
2727
"google.golang.org/api/transport"
2828
)
2929

3030
const (
31+
authErrorCode = "authErrorCode"
3132
firebaseAudience = "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit"
3233
oneHourInSeconds = 3600
34+
35+
// SDK-generated error codes
36+
idTokenRevoked = "ID_TOKEN_REVOKED"
37+
sessionCookieRevoked = "SESSION_COOKIE_REVOKED"
38+
tenantIDMismatch = "TENANT_ID_MISMATCH"
3339
)
3440

3541
var reservedClaims = []string{
@@ -102,7 +108,6 @@ func NewClient(ctx context.Context, conf *internal.AuthConfig) (*Client, error)
102108

103109
hc := internal.WithDefaultRetryConfig(transport)
104110
hc.CreateErrFn = handleHTTPError
105-
hc.SuccessFn = internal.HasSuccessStatus
106111
hc.Opts = []internal.HTTPOption{
107112
internal.WithHeader("X-Client-Version", fmt.Sprintf("Go/Admin/%s", conf.Version)),
108113
}
@@ -261,12 +266,23 @@ func (c *baseClient) withTenantID(tenantID string) *baseClient {
261266
func (c *baseClient) VerifyIDToken(ctx context.Context, idToken string) (*Token, error) {
262267
decoded, err := c.idTokenVerifier.VerifyToken(ctx, idToken)
263268
if err == nil && c.tenantID != "" && c.tenantID != decoded.Firebase.Tenant {
264-
return nil, internal.Errorf(tenantIDMismatch, "invalid tenant id: %q", decoded.Firebase.Tenant)
269+
return nil, &internal.FirebaseError{
270+
ErrorCode: internal.InvalidArgument,
271+
String: fmt.Sprintf("invalid tenant id: %q", decoded.Firebase.Tenant),
272+
Ext: map[string]interface{}{
273+
authErrorCode: tenantIDMismatch,
274+
},
275+
}
265276
}
266277

267278
return decoded, err
268279
}
269280

281+
// IsTenantIDMismatch checks if the given error was due to a mismatched tenant ID in a JWT.
282+
func IsTenantIDMismatch(err error) bool {
283+
return hasAuthErrorCode(err, tenantIDMismatch)
284+
}
285+
270286
// VerifyIDTokenAndCheckRevoked verifies the provided ID token, and additionally checks that the
271287
// token has not been revoked.
272288
//
@@ -284,12 +300,27 @@ func (c *baseClient) VerifyIDTokenAndCheckRevoked(ctx context.Context, idToken s
284300
if err != nil {
285301
return nil, err
286302
}
303+
287304
if revoked {
288-
return nil, internal.Error(idTokenRevoked, "ID token has been revoked")
305+
return nil, &internal.FirebaseError{
306+
ErrorCode: internal.InvalidArgument,
307+
String: "ID token has been revoked",
308+
Ext: map[string]interface{}{
309+
authErrorCode: idTokenRevoked,
310+
},
311+
}
289312
}
313+
290314
return decoded, nil
291315
}
292316

317+
// IsIDTokenRevoked checks if the given error was due to a revoked ID token.
318+
//
319+
// When IsIDTokenRevoked returns true, IsIDTokenInvalid is guranteed to return true.
320+
func IsIDTokenRevoked(err error) bool {
321+
return hasAuthErrorCode(err, idTokenRevoked)
322+
}
323+
293324
// VerifySessionCookie verifies the signature and payload of the provided Firebase session cookie.
294325
//
295326
// VerifySessionCookie accepts a signed JWT token string, and verifies that it is current, issued for the
@@ -324,12 +355,27 @@ func (c *Client) VerifySessionCookieAndCheckRevoked(ctx context.Context, session
324355
if err != nil {
325356
return nil, err
326357
}
358+
327359
if revoked {
328-
return nil, internal.Error(sessionCookieRevoked, "session cookie has been revoked")
360+
return nil, &internal.FirebaseError{
361+
ErrorCode: internal.InvalidArgument,
362+
String: "session cookie has been revoked",
363+
Ext: map[string]interface{}{
364+
authErrorCode: sessionCookieRevoked,
365+
},
366+
}
329367
}
368+
330369
return decoded, nil
331370
}
332371

372+
// IsSessionCookieRevoked checks if the given error was due to a revoked session cookie.
373+
//
374+
// When IsSessionCookieRevoked returns true, IsSessionCookieInvalid is guranteed to return true.
375+
func IsSessionCookieRevoked(err error) bool {
376+
return hasAuthErrorCode(err, sessionCookieRevoked)
377+
}
378+
333379
func (c *baseClient) checkRevoked(ctx context.Context, token *Token) (bool, error) {
334380
user, err := c.GetUser(ctx, token.UID)
335381
if err != nil {
@@ -338,3 +384,13 @@ func (c *baseClient) checkRevoked(ctx context.Context, token *Token) (bool, erro
338384

339385
return token.IssuedAt*1000 < user.TokensValidAfterMillis, nil
340386
}
387+
388+
func hasAuthErrorCode(err error, code string) bool {
389+
fe, ok := err.(*internal.FirebaseError)
390+
if !ok {
391+
return false
392+
}
393+
394+
got, ok := fe.Ext[authErrorCode]
395+
return ok && got == code
396+
}

auth/auth_appengine.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package auth
1919
import (
2020
"context"
2121

22-
"firebase.google.com/go/internal"
22+
"firebase.google.com/go/v4/internal"
2323
"google.golang.org/appengine"
2424
)
2525

auth/auth_std.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package auth // import "firebase.google.com/go/auth"
1919
import (
2020
"context"
2121

22-
"firebase.google.com/go/internal"
22+
"firebase.google.com/go/v4/internal"
2323
)
2424

2525
func newCryptoSigner(ctx context.Context, conf *internal.AuthConfig) (cryptoSigner, error) {

0 commit comments

Comments
 (0)