Skip to content

Commit 0ac4300

Browse files
authored
use google.golang.org/api/option to init app (#47)
* use google's option to init service account * change fetch key to use app's http client
1 parent 5526738 commit 0ac4300

File tree

748 files changed

+593740
-51
lines changed

Some content is hidden

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

748 files changed

+593740
-51
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ before_install:
77
- go get github.com/mattn/goveralls
88

99
script:
10+
- mkdir -p private
11+
- echo $SERVICE_ACCOUNT > private/service_account.json
1012
- $HOME/gopath/bin/goveralls -service=travis-ci

Gopkg.lock

Lines changed: 23 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
SERVICE_ACCOUNT=$(shell cat private/service_account.json)
21
PROJECT_ID=$(shell cat private/project_id)
32
DATABASE_URL=$(shell cat private/database_url)
43
API_KEY=$(shell cat private/api_key)
54

65
test:
76
env \
8-
SERVICE_ACCOUNT='$(SERVICE_ACCOUNT)' \
97
PROJECT_ID='$(PROJECT_ID)' \
108
DATABASE_URL='$(DATABASE_URL)' \
119
API_KEY='$(API_KEY)' \

README.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,15 @@ package main
107107
import (
108108
"io/ioutil"
109109

110+
"google.golang.org/api/option"
110111
"github.com/acoshift/go-firebase-admin"
111112
)
112113

113114
func main() {
114115
// Init App with service_account
115-
serviceAccount, _ := ioutil.ReadFile("service_account.json")
116116
firApp, err := firebase.InitializeApp(context.Background(), firebase.AppOptions{
117-
ServiceAccount: serviceAccount,
118117
ProjectID: "YOUR_PROJECT_ID",
119-
})
118+
}, option.WithCredentialsFile("service_account.json"))
120119

121120
if err != nil {
122121
panic(err)
@@ -132,16 +131,15 @@ package main
132131
import (
133132
"io/ioutil"
134133

134+
"google.golang.org/api/option"
135135
"github.com/acoshift/go-firebase-admin"
136136
)
137137

138138
func main() {
139139
// Init App with service_account
140-
serviceAccount, _ := ioutil.ReadFile("service_account.json")
141140
firApp, err := firebase.InitializeApp(context.Background(), firebase.AppOptions{
142-
ServiceAccount: serviceAccount,
143141
ProjectID: "YOUR_PROJECT_ID",
144-
})
142+
}, option.WithCredentialsFile("service_account.json"))
145143

146144
if err != nil {
147145
panic(err)
@@ -171,16 +169,15 @@ package main
171169
import (
172170
"io/ioutil"
173171

172+
"google.golang.org/api/option"
174173
"github.com/acoshift/go-firebase-admin"
175174
)
176175

177176
func main() {
178177
// Init App with service_account
179-
serviceAccount, _ := ioutil.ReadFile("service_account.json")
180178
firApp, err := firebase.InitializeApp(context.Background(), firebase.AppOptions{
181-
ServiceAccount: serviceAccount,
182179
ProjectID: "YOUR_PROJECT_ID",
183-
})
180+
}, option.WithCredentialsFile("service_account.json"))
184181

185182
if err != nil {
186183
panic(err)
@@ -227,18 +224,17 @@ package main
227224
import (
228225
"io/ioutil"
229226

227+
"google.golang.org/api/option"
230228
"github.com/acoshift/go-firebase-admin"
231229
)
232230

233231
func main() {
234232
// Init App with service_account
235-
serviceAccount, _ := ioutil.ReadFile("service_account.json")
236233
firApp, err := firebase.InitializeApp(context.Background(), firebase.AppOptions{
237-
ServiceAccount: serviceAccount,
238234
ProjectID: "YOUR_PROJECT_ID",
239235
DatabaseURL: "YOUR_DATABASE_URL",
240236
APIKey: "YOUR_API_KEY",
241-
})
237+
}, option.WithCredentialsFile("service_account.json"))
242238

243239
if err != nil {
244240
panic(err)

app.go

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@ package firebase
33
import (
44
"context"
55
"crypto/rsa"
6+
"encoding/json"
67
"net/http"
78

89
jwtgo "github.com/dgrijalva/jwt-go"
9-
"golang.org/x/oauth2/google"
1010
"golang.org/x/oauth2/jwt"
11+
"google.golang.org/api/option"
12+
"google.golang.org/api/transport"
1113
)
1214

1315
// App holds information about application configuration
1416
type App struct {
1517
projectID string
1618
jwtConfig *jwt.Config
1719
privateKey *rsa.PrivateKey
20+
clientEmail string
1821
databaseURL string
1922
databaseAuthVariable interface{}
2023
client *http.Client
@@ -24,14 +27,15 @@ type App struct {
2427
// AppOptions is the firebase app options for initialize app
2528
type AppOptions struct {
2629
ProjectID string
27-
ServiceAccount []byte
2830
DatabaseURL string
2931
DatabaseAuthVariableOverride interface{}
3032
APIKey string
3133
}
3234

3335
// InitializeApp initializes firebase application with options
34-
func InitializeApp(ctx context.Context, options AppOptions) (*App, error) {
36+
func InitializeApp(ctx context.Context, options AppOptions, opts ...option.ClientOption) (*App, error) {
37+
opts = append([]option.ClientOption{option.WithScopes(scopes...)}, opts...)
38+
3539
var err error
3640

3741
app := App{
@@ -41,21 +45,32 @@ func InitializeApp(ctx context.Context, options AppOptions) (*App, error) {
4145
apiKey: options.APIKey,
4246
}
4347

44-
if options.ServiceAccount != nil {
45-
app.jwtConfig, err = google.JWTConfigFromJSON(options.ServiceAccount, scopes...)
46-
if err != nil {
47-
return nil, err
48-
}
49-
app.privateKey, err = jwtgo.ParseRSAPrivateKeyFromPEM(app.jwtConfig.PrivateKey)
50-
if err != nil {
51-
return nil, err
52-
}
53-
app.client = app.jwtConfig.Client(ctx)
54-
} else {
55-
app.client, err = google.DefaultClient(ctx, scopes...)
48+
app.client, _, err = transport.NewHTTPClient(ctx, opts...)
49+
if err != nil {
50+
app.client = http.DefaultClient
51+
}
52+
53+
cred, err := transport.Creds(ctx, opts...)
54+
if err != nil {
55+
return nil, err
56+
}
57+
58+
if len(app.projectID) == 0 {
59+
app.projectID = cred.ProjectID
60+
}
61+
62+
// load private key from google credential
63+
var serviceAccount struct {
64+
PrivateKey string `json:"private_key"`
65+
ClientEmail string `json:"client_email"`
66+
}
67+
json.Unmarshal(cred.JSON, &serviceAccount)
68+
if len(serviceAccount.PrivateKey) > 0 {
69+
app.privateKey, err = jwtgo.ParseRSAPrivateKeyFromPEM([]byte(serviceAccount.PrivateKey))
5670
if err != nil {
5771
return nil, err
5872
}
73+
app.clientEmail = serviceAccount.ClientEmail
5974
}
6075

6176
return &app, nil

app_test.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@ package firebase_test
22

33
import (
44
"context"
5-
"io/ioutil"
65
"os"
76
"testing"
87

98
"github.com/acoshift/go-firebase-admin"
109
"github.com/stretchr/testify/assert"
10+
"google.golang.org/api/option"
1111
)
1212

1313
type config struct {
1414
ProjectID string `yaml:"projectId"`
15-
ServiceAccount []byte `yaml:"serviceAccount"`
1615
DatabaseURL string `yaml:"databaseURL"`
1716
DatabaseAuthVariableOverride interface{} `yaml:"DatabaseAuthVariableOverride"`
1817
APIKey string `yaml:"apiKey"`
@@ -23,19 +22,17 @@ func initApp(t *testing.T) *firebase.App {
2322

2423
// load config from env
2524
c := config{
26-
ProjectID: os.Getenv("PROJECT_ID"),
27-
ServiceAccount: []byte(os.Getenv("SERVICE_ACCOUNT")),
28-
DatabaseURL: os.Getenv("DATABASE_URL"),
29-
APIKey: os.Getenv("API_KEY"),
25+
ProjectID: os.Getenv("PROJECT_ID"),
26+
DatabaseURL: os.Getenv("DATABASE_URL"),
27+
APIKey: os.Getenv("API_KEY"),
3028
}
3129

32-
// if service account is in separate file service_account.json
33-
if len(c.ServiceAccount) <= 0 {
34-
serviceAccount, _ := ioutil.ReadFile("private/service_account.json")
35-
c.ServiceAccount = serviceAccount
36-
}
37-
38-
app, _ := firebase.InitializeApp(context.Background(), firebase.AppOptions(c))
30+
app, _ := firebase.InitializeApp(context.Background(), firebase.AppOptions{
31+
ProjectID: c.ProjectID,
32+
DatabaseURL: c.DatabaseURL,
33+
DatabaseAuthVariableOverride: c.DatabaseAuthVariableOverride,
34+
APIKey: c.APIKey,
35+
}, option.WithCredentialsFile("private/service_account.json"))
3936
return app
4037
}
4138

auth.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9-
"net/http"
109
"sync"
1110
"time"
1211

@@ -49,8 +48,8 @@ func (auth *Auth) CreateCustomToken(userID string, claims interface{}) (string,
4948
}
5049
now := time.Now()
5150
payload := &customClaims{
52-
Issuer: auth.app.jwtConfig.Email,
53-
Subject: auth.app.jwtConfig.Email,
51+
Issuer: auth.app.clientEmail,
52+
Subject: auth.app.clientEmail,
5453
Audience: customTokenAudience,
5554
IssuedAt: now.Unix(),
5655
ExpiresAt: now.Add(time.Hour).Unix(),
@@ -115,7 +114,7 @@ func (auth *Auth) VerifyIDToken(idToken string) (*Token, error) {
115114
func (auth *Auth) fetchKeys() error {
116115
auth.keysMutex.Lock()
117116
defer auth.keysMutex.Unlock()
118-
resp, err := http.Get(keysEndpoint)
117+
resp, err := auth.app.client.Get(keysEndpoint)
119118
if err != nil {
120119
return err
121120
}

vendor/golang.org/x/text/.gitattributes

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/golang.org/x/text/.gitignore

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/golang.org/x/text/AUTHORS

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)