Skip to content

Commit ad5322b

Browse files
avishalomhiranya911
authored andcommitted
Release 2.2.0 (#48)
* Documentation fix md file (#43) * Clarify certificate download comment * Fix Link * Adding User Management utils, including Custom Claims in the auth package. (#42) Adding the user management go SDK. This includes the customClaims and the iterator over all users. (as well as Create, Update, Delete User, and GetUser (by UID, Phone or Email)) Proposal : go/firebase-go-user-mgt Code snippets: https://firebase-dot-devsite.googleplex.com/docs/auth/admin/manage-users https://firebase-dot-devsite.googleplex.com/docs/auth/admin/custom-claims TODO: clean up the case of an http.DefaultClient when there are no options. * Minor improvements to user management code (#44) * some changes to auth * Implemented a reusable HTTP client API * Added test cases * Comment clean up * Simplified the usage by adding HTTPClient * Using the old ctx import * Support for arbitrary entity types in the request * Renamed fields; Added documentation * Removing a redundant else case * initial * more integration tests * set custom - still needs guessing the type for unmarshaling. * tests * server * server * . * json * json * move testdata * get * tests * updated to param struct of pointers to call create and update * Comments * cleanup * cleanup * cleanup * cleanup minor test changes * Changing the iteraator pattern * last page iterator test fix * clean up tests next. * make the fetch tidier * fetch cleanup * cc change * custom claims * newline * adding error propagation to makeExportedUser * remove trivial claims map type * list users test data * rename p ptr, and remove the with... options for the iterator * rename p ptr * some unit tests * adding integration tests for custom claims * NO ERROR * unit tests * comments hkj * addressing comments * delete unneeded json file * phone NUMBER * typo * remove cc from create * refactor param structs * remove package ptr * test refactor * cleanup comments * cleanup debug.test * Adding back the default http client * fix httpClient for tests * creds * creds * fix default http client * cleanupPrintfs * disable * Reanme payload vars * Revert newHTTPKeySource function * add back defaultClient in newClient * reenable testNewHTTPClientNoOpts) * reverting keysource tests) * Take the httpClient from the keysource * Removethe +1 second for the token timestamp. * Adding tests * White spaces * Redesign the error validators * prepare returns an error. * cleanup * dissolve * dissolve * clean tests * split integration tests * addressing comments * addressing comments opt branch/ BEFORE hc change * Removing the defaultClient from the NewClient, and extracting the NewClient creation outside of KeySource * closer from echoServer * cleanup + 500 error unit test * unify error messages * Refactor stop side effect for params preparation * +1 to timestamp for flakiness * removing +1 * disallow defaultClient * whitespaces * http default * add TODO * Code clean up and refactoring * Refactored integration tests (#46) * Refactored integration tests * Minor cleanup * Auth Unit Test Improvements (#45) * Cleaning up new auth tests * More updates to tests; Dissolved commonParams type * More test updates * More argument validation for auth * Fixed a bug in enable/disable user; Added more tests; Cleaned up unit tests * Removed debug file * Create the 5th user in the integration tests for user management. (#47) * Bump version to 2.2.0 (#49)
1 parent edbe442 commit ad5322b

File tree

15 files changed

+1831
-56
lines changed

15 files changed

+1831
-56
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
testdata/integration_*
2+
.vscode/*

CONTRIBUTING.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ We get lots of those and we love helping you, but GitHub is not the best place f
1515
which just ask about usage will be closed. Here are some resources to get help:
1616

1717
- Go through the [guides](https://firebase.google.com/docs/admin/setup/)
18-
- Read the full [API reference](https://firebase.google.com/docs/reference/admin/go/)
18+
- Read the full [API reference](https://godoc.org/firebase.google.com/go)
1919

2020
If the official documentation doesn't help, try asking a question on the
2121
[Firebase Google Group](https://groups.google.com/forum/#!forum/firebase-talk/) or one of our
@@ -122,8 +122,8 @@ do not already have one suitable for running the tests against. Then obtain the
122122
following credentials from the project:
123123

124124
1. *Service account certificate*: This can be downloaded as a JSON file from
125-
the "Settings > Service Accounts" tab of the Firebase console. Copy the
126-
file into your Go workspace as
125+
the "Settings > Service Accounts" tab of the Firebase console. Click
126+
"GENERATE NEW PRIVATE KEY" and copy the file into your Go workspace as
127127
`src/firebase.google.com/go/testdata/integration_cert.json`.
128128
2. *Web API key*: This is displayed in the "Settings > General" tab of the
129129
console. Copy it and save to a new text file. Copy this text file into

auth/auth.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@ import (
2222
"encoding/pem"
2323
"errors"
2424
"fmt"
25+
"net/http"
2526
"strings"
2627

2728
"firebase.google.com/go/internal"
2829
"golang.org/x/net/context"
30+
"google.golang.org/api/transport"
2931
)
3032

3133
const firebaseAudience = "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit"
3234
const googleCertURL = "https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com"
35+
const idToolKitURL = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/"
3336
const issuerPrefix = "https://securetoken.google.com/"
3437
const tokenExpSeconds = 3600
3538

@@ -60,9 +63,11 @@ type Token struct {
6063
// Client facilitates generating custom JWT tokens for Firebase clients, and verifying ID tokens issued
6164
// by Firebase backend services.
6265
type Client struct {
66+
hc *internal.HTTPClient
6367
ks keySource
6468
projectID string
6569
snr signer
70+
url string
6671
}
6772

6873
type signer interface {
@@ -105,19 +110,41 @@ func NewClient(ctx context.Context, c *internal.AuthConfig) (*Client, error) {
105110
return nil, err
106111
}
107112
}
108-
109-
ks, err := newHTTPKeySource(ctx, googleCertURL, c.Opts...)
113+
hc := http.DefaultClient
114+
if len(c.Opts) > 0 { // TODO: fix the default when len = 0
115+
hc, _, err = transport.NewHTTPClient(ctx, c.Opts...)
116+
if err != nil {
117+
return nil, err
118+
}
119+
}
120+
ks, err := newHTTPKeySource(googleCertURL, hc)
110121
if err != nil {
111122
return nil, err
112123
}
113124

114125
return &Client{
126+
hc: &internal.HTTPClient{Client: hc},
115127
ks: ks,
116128
projectID: c.ProjectID,
117129
snr: snr,
130+
url: idToolKitURL,
118131
}, nil
119132
}
120133

134+
// Passes the request struct, returns a byte array of the json
135+
func (c *Client) makeHTTPCall(ctx context.Context, serviceName string, payload interface{}, result interface{}) error {
136+
request := &internal.Request{
137+
Method: "POST",
138+
URL: c.url + serviceName,
139+
Body: internal.NewJSONEntity(payload),
140+
}
141+
resp, err := c.hc.Do(ctx, request)
142+
if err != nil {
143+
return err
144+
}
145+
return resp.Unmarshal(200, result)
146+
}
147+
121148
// CustomToken creates a signed custom authentication token with the specified user ID. The resulting
122149
// JWT can be used in a Firebase client SDK to trigger an authentication flow. See
123150
// https://firebase.google.com/docs/auth/admin/create-custom-tokens#sign_in_using_custom_tokens_on_clients

auth/auth_test.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ import (
3838

3939
var client *Client
4040
var testIDToken string
41+
var testGetUserResponse []byte
42+
var testListUsersResponse []byte
4143

4244
func TestMain(m *testing.M) {
4345
var (
@@ -46,7 +48,6 @@ func TestMain(m *testing.M) {
4648
ctx context.Context
4749
creds *google.DefaultCredentials
4850
)
49-
5051
if appengine.IsDevAppServer() {
5152
aectx, aedone, err := aetest.NewContext()
5253
if err != nil {
@@ -61,23 +62,34 @@ func TestMain(m *testing.M) {
6162
}
6263
} else {
6364
ctx = context.Background()
64-
creds, err = transport.Creds(ctx, option.WithCredentialsFile("../testdata/service_account.json"))
65+
opt := option.WithCredentialsFile("../testdata/service_account.json")
66+
creds, err = transport.Creds(ctx, opt)
6567
if err != nil {
6668
log.Fatalln(err)
6769
}
6870

6971
ks = &fileKeySource{FilePath: "../testdata/public_certs.json"}
7072
}
71-
7273
client, err = NewClient(ctx, &internal.AuthConfig{
7374
Creds: creds,
75+
Opts: []option.ClientOption{option.WithCredentialsFile("../testdata/service_account.json")},
7476
ProjectID: "mock-project-id",
7577
})
7678
if err != nil {
7779
log.Fatalln(err)
7880
}
7981
client.ks = ks
8082

83+
testGetUserResponse, err = ioutil.ReadFile("../testdata/get_user.json")
84+
if err != nil {
85+
log.Fatalln(err)
86+
}
87+
88+
testListUsersResponse, err = ioutil.ReadFile("../testdata/list_users.json")
89+
if err != nil {
90+
log.Fatalln(err)
91+
}
92+
8193
testIDToken = getIDToken(nil)
8294
os.Exit(m.Run())
8395
}
@@ -88,7 +100,7 @@ func TestNewClientInvalidCredentials(t *testing.T) {
88100
}
89101
conf := &internal.AuthConfig{Creds: creds}
90102
if c, err := NewClient(context.Background(), conf); c != nil || err == nil {
91-
t.Errorf("NewCient() = (%v,%v); want = (nil, error)", c, err)
103+
t.Errorf("NewClient() = (%v,%v); want = (nil, error)", c, err)
92104
}
93105
}
94106

@@ -104,7 +116,7 @@ func TestNewClientInvalidPrivateKey(t *testing.T) {
104116
creds := &google.DefaultCredentials{JSON: b}
105117
conf := &internal.AuthConfig{Creds: creds}
106118
if c, err := NewClient(context.Background(), conf); c != nil || err == nil {
107-
t.Errorf("NewCient() = (%v,%v); want = (nil, error)", c, err)
119+
t.Errorf("NewClient() = (%v,%v); want = (nil, error)", c, err)
108120
}
109121
}
110122

@@ -325,8 +337,8 @@ type mockKeySource struct {
325337
err error
326338
}
327339

328-
func (t *mockKeySource) Keys() ([]*publicKey, error) {
329-
return t.keys, t.err
340+
func (k *mockKeySource) Keys() ([]*publicKey, error) {
341+
return k.keys, k.err
330342
}
331343

332344
// fileKeySource loads a set of public keys from the local file system.

auth/crypto.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ import (
3030
"strings"
3131
"sync"
3232
"time"
33-
34-
"golang.org/x/net/context"
35-
36-
"google.golang.org/api/option"
37-
"google.golang.org/api/transport"
3833
)
3934

4035
// publicKey represents a parsed RSA public key along with its unique key ID.
@@ -80,18 +75,7 @@ type httpKeySource struct {
8075
Mutex *sync.Mutex
8176
}
8277

83-
func newHTTPKeySource(ctx context.Context, uri string, opts ...option.ClientOption) (*httpKeySource, error) {
84-
var hc *http.Client
85-
if ctx != nil && len(opts) > 0 {
86-
var err error
87-
hc, _, err = transport.NewHTTPClient(ctx, opts...)
88-
if err != nil {
89-
return nil, err
90-
}
91-
} else {
92-
hc = http.DefaultClient
93-
}
94-
78+
func newHTTPKeySource(uri string, hc *http.Client) (*httpKeySource, error) {
9579
return &httpKeySource{
9680
KeyURI: uri,
9781
HTTPClient: hc,

auth/crypto_test.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ import (
2222
"net/http"
2323
"testing"
2424
"time"
25-
26-
"golang.org/x/net/context"
27-
28-
"google.golang.org/api/option"
2925
)
3026

3127
type mockHTTPResponse struct {
@@ -43,7 +39,7 @@ type mockReadCloser struct {
4339
closeCount int
4440
}
4541

46-
func newHTTPClient(data []byte) (*http.Client, *mockReadCloser) {
42+
func newTestHTTPClient(data []byte) (*http.Client, *mockReadCloser) {
4743
rc := &mockReadCloser{
4844
data: string(data),
4945
closeCount: 0,
@@ -87,16 +83,15 @@ func TestHTTPKeySource(t *testing.T) {
8783
if err != nil {
8884
t.Fatal(err)
8985
}
90-
91-
ks, err := newHTTPKeySource(context.Background(), "http://mock.url")
86+
ks, err := newHTTPKeySource("http://mock.url", http.DefaultClient)
9287
if err != nil {
9388
t.Fatal(err)
9489
}
9590

9691
if ks.HTTPClient == nil {
9792
t.Errorf("HTTPClient = nil; want non-nil")
9893
}
99-
hc, rc := newHTTPClient(data)
94+
hc, rc := newTestHTTPClient(data)
10095
ks.HTTPClient = hc
10196
if err := verifyHTTPKeySource(ks, rc); err != nil {
10297
t.Fatal(err)
@@ -109,8 +104,8 @@ func TestHTTPKeySourceWithClient(t *testing.T) {
109104
t.Fatal(err)
110105
}
111106

112-
hc, rc := newHTTPClient(data)
113-
ks, err := newHTTPKeySource(context.Background(), "http://mock.url", option.WithHTTPClient(hc))
107+
hc, rc := newTestHTTPClient(data)
108+
ks, err := newHTTPKeySource("http://mock.url", hc)
114109
if err != nil {
115110
t.Fatal(err)
116111
}
@@ -124,8 +119,8 @@ func TestHTTPKeySourceWithClient(t *testing.T) {
124119
}
125120

126121
func TestHTTPKeySourceEmptyResponse(t *testing.T) {
127-
hc, _ := newHTTPClient([]byte(""))
128-
ks, err := newHTTPKeySource(context.Background(), "http://mock.url", option.WithHTTPClient(hc))
122+
hc, _ := newTestHTTPClient([]byte(""))
123+
ks, err := newHTTPKeySource("http://mock.url", hc)
129124
if err != nil {
130125
t.Fatal(err)
131126
}
@@ -136,8 +131,8 @@ func TestHTTPKeySourceEmptyResponse(t *testing.T) {
136131
}
137132

138133
func TestHTTPKeySourceIncorrectResponse(t *testing.T) {
139-
hc, _ := newHTTPClient([]byte("{\"foo\": 1}"))
140-
ks, err := newHTTPKeySource(context.Background(), "http://mock.url", option.WithHTTPClient(hc))
134+
hc, _ := newTestHTTPClient([]byte("{\"foo\": 1}"))
135+
ks, err := newHTTPKeySource("http://mock.url", hc)
141136
if err != nil {
142137
t.Fatal(err)
143138
}
@@ -153,7 +148,7 @@ func TestHTTPKeySourceTransportError(t *testing.T) {
153148
Err: errors.New("transport error"),
154149
},
155150
}
156-
ks, err := newHTTPKeySource(context.Background(), "http://mock.url", option.WithHTTPClient(hc))
151+
ks, err := newHTTPKeySource("http://mock.url", hc)
157152
if err != nil {
158153
t.Fatal(err)
159154
}

0 commit comments

Comments
 (0)