-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcredentials.go
52 lines (45 loc) · 1.14 KB
/
credentials.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
package firebase
import (
"crypto/rsa"
"encoding/json"
"io"
"github.com/SermoDigital/jose/crypto"
)
type (
Credentials struct {
// ProjectID is the project ID.
ProjectID string
// PrivateKey is the RSA256 private key.
PrivateKey *rsa.PrivateKey
// ClientEmail is the client email.
ClientEmail string
}
)
// UnmarshalJSON is the custom unmarshaler for GoogleServiceAccountCredential.
// Private key is parsed from PEM format.
func (c *Credentials) UnmarshalJSON(data []byte) error {
var aux struct {
ProjectID string `json:"project_id"`
PrivateKey string `json:"private_key"`
ClientEmail string `json:"client_email"`
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
privKey, err := crypto.ParseRSAPrivateKeyFromPEM([]byte(aux.PrivateKey))
if err != nil {
return err
}
c.PrivateKey = privKey
c.ProjectID = aux.ProjectID
c.ClientEmail = aux.ClientEmail
return nil
}
// loadCredential loads the Service Account credential from a JSON file.
func loadCredential(r io.Reader) (*Credentials, error) {
var c Credentials
if err := json.NewDecoder(r).Decode(&c); err != nil {
return nil, err
}
return &c, nil
}