-
Notifications
You must be signed in to change notification settings - Fork 13
/
authconfig.go
178 lines (155 loc) · 4.65 KB
/
authconfig.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package config
import (
"net/url"
"os"
"strings"
"time"
"github.com/Axway/agent-sdk/pkg/util/exception"
"github.com/Axway/agent-sdk/pkg/util/log"
)
const tokenEndpoint = "/protocol/openid-connect/token"
// AuthConfig - Interface for service account config
type AuthConfig interface {
GetTokenURL() string
GetRealm() string
GetAudience() string
GetClientID() string
GetPrivateKey() string
GetPublicKey() string
GetKeyPassword() string
GetTimeout() time.Duration
validate()
}
// AuthConfiguration -
type AuthConfiguration struct {
AuthConfig
URL string `config:"url"`
Realm string `config:"realm"`
ClientID string `config:"clientId"`
PrivateKey string `config:"privateKey"`
PublicKey string `config:"publicKey"`
PrivateKeyData string `config:"privateKeyData"`
PublicKeyData string `config:"publicKeyData"`
KeyPwd string `config:"keyPassword"`
ClientIDReuse bool `config:"clientIdReuse"`
Timeout time.Duration `config:"timeout"`
}
func newAuthConfig() AuthConfig {
return &AuthConfiguration{
Timeout: 30 * time.Second,
}
}
func newTestAuthConfig() AuthConfig {
os.Setenv("CENTRAL_AUTH_PRIVATEKEY_DATA", "1")
os.Setenv("CENTRAL_AUTH_PUBLICKEY_DATA", "1")
return &AuthConfiguration{
Timeout: 30 * time.Second,
URL: "https://auth.com",
Realm: "realm",
ClientID: "clientid",
PrivateKey: "file",
PublicKey: "file",
}
}
func (a *AuthConfiguration) validate() {
if a.URL == "" {
exception.Throw(ErrBadConfig.FormatError(pathAuthURL))
} else if _, err := url.ParseRequestURI(a.URL); err != nil {
exception.Throw(ErrBadConfig.FormatError(pathAuthURL))
}
if a.GetRealm() == "" {
exception.Throw(ErrBadConfig.FormatError(pathAuthRealm))
}
if a.GetClientID() == "" {
// raise deprecation warning for IDs prefixed DOSA_
if strings.HasPrefix(a.GetClientID(), "DOSA_") {
log.Warn("DOSA_* service accounts are deprecated, please migrate to an Amplify Platform Service account")
}
exception.Throw(ErrBadConfig.FormatError(pathAuthClientID))
}
a.validatePrivateKey()
a.validatePublicKey()
}
func (a *AuthConfiguration) validatePrivateKey() {
if a.GetPrivateKey() == "" {
exception.Throw(ErrBadConfig.FormatError(pathAuthPrivateKey))
} else {
if !fileExists(a.GetPrivateKey()) {
privateKeyData := os.Getenv("CENTRAL_AUTH_PRIVATEKEY_DATA")
if privateKeyData == "" {
exception.Throw(ErrBadConfig.FormatError(pathAuthPrivateKey))
}
saveKeyData(a.GetPrivateKey(), privateKeyData)
}
// Validate that the file is readable
if _, err := os.Open(a.GetPrivateKey()); err != nil {
exception.Throw(ErrReadingKeyFile.FormatError("private key", a.GetPrivateKey()))
}
}
}
func (a *AuthConfiguration) validatePublicKey() {
if a.GetPublicKey() == "" {
exception.Throw(ErrBadConfig.FormatError(pathAuthPublicKey))
} else {
if !fileExists(a.GetPublicKey()) {
publicKeyData := os.Getenv("CENTRAL_AUTH_PUBLICKEY_DATA")
if publicKeyData == "" {
exception.Throw(ErrBadConfig.FormatError(pathAuthPublicKey))
}
saveKeyData(a.GetPublicKey(), publicKeyData)
}
// Validate that the file is readable
if _, err := os.Open(a.GetPublicKey()); err != nil {
exception.Throw(ErrReadingKeyFile.FormatError("public key", a.GetPublicKey()))
}
}
}
// GetTokenURL - Returns the token URL
func (a *AuthConfiguration) GetTokenURL() string {
if a.URL == "" || a.Realm == "" {
return ""
}
return a.URL + "/realms/" + a.Realm + tokenEndpoint
}
// GetRealm - Returns the token audience URL
func (a *AuthConfiguration) GetRealm() string {
return a.Realm
}
// GetAudience - Returns the token audience URL
func (a *AuthConfiguration) GetAudience() string {
if a.URL == "" || a.Realm == "" {
return ""
}
return a.URL + "/realms/" + a.Realm
}
// GetClientID - Returns the token audience URL
func (a *AuthConfiguration) GetClientID() string {
return a.ClientID
}
// GetPrivateKey - Returns the private key file path
func (a *AuthConfiguration) GetPrivateKey() string {
return a.PrivateKey
}
// GetPublicKey - Returns the public key file path
func (a *AuthConfiguration) GetPublicKey() string {
return a.PublicKey
}
// GetKeyPassword - Returns the token audience URL
func (a *AuthConfiguration) GetKeyPassword() string {
return a.KeyPwd
}
// GetTimeout - Returns the token audience URL
func (a *AuthConfiguration) GetTimeout() time.Duration {
return a.Timeout
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func saveKeyData(filename string, data string) {
dataBytes := []byte(data)
os.WriteFile(filename, dataBytes, 0600)
}