-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathapp.go
117 lines (101 loc) · 2.98 KB
/
app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package firebase
import (
"context"
"crypto/rsa"
"encoding/json"
"net/http"
jwtgo "github.com/dgrijalva/jwt-go"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
"google.golang.org/api/transport"
)
// App holds information about application configuration
type App struct {
projectID string
privateKey *rsa.PrivateKey
clientEmail string
databaseURL string
databaseAuthVariable interface{}
client *http.Client
apiKey string
}
// AppOptions is the firebase app options for initialize app
type AppOptions struct {
ProjectID string
ServiceAccount []byte
DatabaseURL string
DatabaseAuthVariableOverride interface{}
APIKey string
}
// InitializeApp initializes firebase application with options
func InitializeApp(ctx context.Context, options AppOptions, opts ...option.ClientOption) (*App, error) {
opts = append([]option.ClientOption{option.WithScopes(scopes...)}, opts...)
var err error
app := App{
projectID: options.ProjectID,
databaseURL: options.DatabaseURL,
databaseAuthVariable: options.DatabaseAuthVariableOverride,
apiKey: options.APIKey,
}
// check service account first
if len(options.ServiceAccount) > 0 {
jwtConfig, err := google.JWTConfigFromJSON(options.ServiceAccount, scopes...)
if err != nil {
return nil, err
}
app.privateKey, err = jwtgo.ParseRSAPrivateKeyFromPEM(jwtConfig.PrivateKey)
if err != nil {
return nil, err
}
app.client = jwtConfig.Client(ctx)
app.clientEmail = jwtConfig.Email
// load project id
var serviceAccount struct {
ProjectID string `json:"project_id"`
}
json.Unmarshal(options.ServiceAccount, &serviceAccount)
app.projectID = serviceAccount.ProjectID
} else {
// load service account using google's option
app.client, _, err = transport.NewHTTPClient(ctx, opts...)
if err != nil {
app.client = http.DefaultClient
}
cred, err := transport.Creds(ctx, opts...)
if err != nil {
return nil, err
}
if len(app.projectID) == 0 {
app.projectID = cred.ProjectID
}
// load private key from google credential
var serviceAccount struct {
PrivateKey string `json:"private_key"`
ClientEmail string `json:"client_email"`
}
json.Unmarshal(cred.JSON, &serviceAccount)
if len(serviceAccount.PrivateKey) > 0 {
app.privateKey, err = jwtgo.ParseRSAPrivateKeyFromPEM([]byte(serviceAccount.PrivateKey))
if err != nil {
return nil, err
}
app.clientEmail = serviceAccount.ClientEmail
}
}
return &app, nil
}
// Auth creates new Auth instance
// each instance has the same firebase app instance
// but difference public keys instance
// better create only one instance
func (app *App) Auth() *Auth {
return newAuth(app)
}
// Database creates new Database instance
func (app *App) Database() *Database {
return newDatabase(app)
}
// FCM creates new FCM instance
func (app *App) FCM() *FCM {
return newFCM(app)
}