This repository has been archived by the owner on Apr 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
config.go
159 lines (138 loc) · 4.39 KB
/
config.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package stormpath
import (
"fmt"
"os"
"strings"
"time"
"github.com/spf13/viper"
)
/*
---
stormpath:
client:
apiKey:
file: null
id: null
secret: null
cacheManager:
enabled: true
defaultTtl: 300 # seconds
defaultTti: 300
caches: #Per resource cacehe config
baseUrl: "https://api.stormpath.com/v1"
connectionTimeout: 30 # seconds
authenticationScheme: "SAUTHC1"
proxy:
port: null
host: null
username: null
password: null
*/
//ClientConfiguration representd the overall SDK configuration options
type ClientConfiguration struct {
APIKeyFile string
APIKeyID string
APIKeySecret string
CacheManagerEnabled bool
CacheTTL time.Duration
CacheTTI time.Duration
BaseURL string
ConnectionTimeout int
AuthenticationScheme string
ProxyPort int
ProxyHost string
ProxyUsername string
ProxyPassword string
}
//LoadConfiguration loads the configuration from the default locations
func LoadConfiguration() (ClientConfiguration, error) {
c := newDefaultClientConfiguration()
v := viper.New()
v.SetConfigType("yaml")
v.AutomaticEnv()
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
v.SetConfigName("stormpath")
v.AddConfigPath(os.Getenv("HOME") + "/.stormpath")
v.AddConfigPath(".")
v.ReadInConfig()
c.APIKeyID = v.GetString("stormpath.client.apiKey.id")
c.APIKeySecret = v.GetString("stormpath.client.apiKey.secret")
id, secret, err := loadCredentials(c.APIKeyFile)
if err == nil {
c.APIKeyID = id
c.APIKeySecret = secret
}
if c.APIKeyID == "" && c.APIKeySecret == "" {
return c, fmt.Errorf("API credentials couldn't be loaded")
}
if v.Get("stormpath.client.cacheManager.enabled") != nil {
c.CacheManagerEnabled = v.GetBool("stormpath.client.cacheManager.enabled")
}
if v.Get("stormpath.client.cacheManager.defaultTtl") != nil {
c.CacheTTL = time.Duration(v.GetInt("stormpath.client.cacheManager.defaultTtl")) * time.Second
}
if v.Get("stormpath.client.cacheManager.defaultTti") != nil {
c.CacheTTI = time.Duration(v.GetInt("stormpath.client.cacheManager.defaultTti")) * time.Second
}
if v.GetString("stormpath.client.baseUrl") != "" {
c.BaseURL = v.GetString("stormpath.client.baseUrl")
}
if v.Get("stormpath.client.connectionTimeout") != nil {
c.ConnectionTimeout = v.GetInt("stormpath.client.connectionTimeout")
}
if v.GetString("stormpath.client.authenticationScheme") != "" {
c.AuthenticationScheme = v.GetString("stormpath.client.authenticationScheme")
}
c.ProxyHost = v.GetString("stormpath.client.proxy.host")
c.ProxyPort = v.GetInt("stormpath.client.proxy.port")
c.ProxyUsername = v.GetString("stormpath.client.proxy.username")
c.ProxyPassword = v.GetString("stormpath.client.proxy.password")
return c, nil
}
//LoadConfigurationWithCreds loads the configuration from the default localtions but with custom Stormpath credentials
func LoadConfigurationWithCreds(key string, secret string) ClientConfiguration {
c := newDefaultClientConfiguration()
c.APIKeyID = key
c.APIKeySecret = secret
return c
}
func loadCredentials(extraFileLocation string) (id string, secret string, err error) {
id = os.Getenv("STORMPATH_API_KEY_ID")
secret = os.Getenv("STORMPATH_API_KEY_SECRET")
if id != "" && secret != "" {
return id, secret, nil
}
id, secret, err = loadCredentialsFromFile(os.Getenv("HOME") + "/.stormpath/apiKey.properties")
if err == nil && id != "" && secret != "" {
return
}
id, secret, err = loadCredentialsFromFile("./apiKey.properties")
if err == nil && id != "" && secret != "" {
return
}
if extraFileLocation != "" {
return loadCredentialsFromFile(extraFileLocation)
}
return
}
//GetJWTSigningKey returns the API Key Secret as a []byte to sign JWT tokens
func (config ClientConfiguration) GetJWTSigningKey() []byte {
return []byte(config.APIKeySecret)
}
func newDefaultClientConfiguration() (c ClientConfiguration) {
return ClientConfiguration{
APIKeyFile: "",
APIKeyID: "",
APIKeySecret: "",
CacheManagerEnabled: true,
CacheTTI: 300 * time.Second,
CacheTTL: 300 * time.Second,
BaseURL: "https://api.stormpath.com/v1/",
ConnectionTimeout: 30,
AuthenticationScheme: "SAUTHC1",
ProxyHost: "",
ProxyPort: 0,
ProxyUsername: "",
ProxyPassword: "",
}
}