-
Notifications
You must be signed in to change notification settings - Fork 13
/
static_provider.go
42 lines (37 loc) 路 1.34 KB
/
static_provider.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
package credentials
import (
"fmt"
"net/url"
)
// A StaticProvider is a set of credentials which are set pragmatically,
// and will never expire.
type StaticProvider struct {
Value
}
// NewStaticCredentials returns a pointer to a new Credentials object
// wrapping a static credentials value provider.
func NewStaticCredentials(projectID, privateKeyID, privateKey, clientEmail, clientID string) *Credentials {
_, err := url.ParseQuery(clientEmail)
if err != nil {
return nil
}
return NewCredentials(&StaticProvider{Value: Value{
Type: "service_account",
ProjectID: projectID,
PrivateKeyID: privateKeyID,
PrivateKey: privateKey,
ClientEmail: clientEmail,
ClientID: clientID,
AuthURI: "https://accounts.google.com/o/oauth2/auth",
TokenURI: "https://accounts.google.com/o/oauth2/token",
AuthProviderX509CertURL: "https://www.googleapis.com/oauth2/v1/certs",
ClientX509CertURL: fmt.Sprintf("https://www.googleapis.com/robot/v1/metadata/x509/%s", url.QueryEscape(clientEmail)),
}})
}
// Retrieve returns the credentials or error if the credentials are invalid.
func (s *StaticProvider) Retrieve() (Value, error) {
if s.IsValid() {
return s.Value, nil
}
return Value{}, fmt.Errorf("static credentials are empty")
}