forked from jarias/stormpath-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcredentials.go
40 lines (31 loc) · 1.01 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
package stormpath
import (
"os"
"github.com/dmotylev/goproperties"
)
//Credentials represents a set of Stormpath credentials
type Credentials struct {
ID string
Secret string
}
//NewCredentialsFromFile creates a new credentials from a Stormpath key files
func NewCredentialsFromFile(file string) (Credentials, error) {
c := Credentials{}
p, err := properties.Load(file)
if err != nil {
return Credentials{}, err
}
c.ID = p.String("apiKey.id", "")
c.Secret = p.String("apiKey.secret", "")
return c, err
}
//NewDefaultCredentials would create a new credentials based on env variables first then the default file location
//at ~/.config/stormpath/apiKey.properties
func NewDefaultCredentials() (Credentials, error) {
apiKeyID := os.Getenv("STORMPATH_API_KEY_ID")
apiKeySecret := os.Getenv("STORMPATH_API_KEY_SECRET")
if apiKeyID != "" && apiKeySecret != "" {
return Credentials{apiKeyID, apiKeySecret}, nil
}
return NewCredentialsFromFile(os.Getenv("HOME") + "/.config/stormpath/apiKey.properties")
}