-
Notifications
You must be signed in to change notification settings - Fork 14
/
config.go
137 lines (114 loc) · 3.69 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
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package config
import (
"errors"
"fmt"
"github.com/allcloud-io/clisso/log"
"github.com/icza/gog"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
// OneLoginProviderConfig represents a OneLogin provider configuration.
type OneLoginProviderConfig struct {
ClientID string
ClientSecret string
Subdomain string
Type string
Username string
Region string
}
// GetOneLoginProvider returns a OneLoginProviderConfig struct containing the configuration for
// provider p.
func GetOneLoginProvider(p string) (*OneLoginProviderConfig, error) {
log.Log.WithField("provider", p).Trace("Reading OneLogin provider config")
clientSecret := viper.GetString(fmt.Sprintf("providers.%s.client-secret", p))
clientID := viper.GetString(fmt.Sprintf("providers.%s.client-id", p))
subdomain := viper.GetString(fmt.Sprintf("providers.%s.subdomain", p))
username := viper.GetString(fmt.Sprintf("providers.%s.username", p))
region := viper.GetString(fmt.Sprintf("providers.%s.region", p))
log.Log.WithFields(logrus.Fields{
"clientSecret": gog.If(log.Log.GetLevel() == logrus.TraceLevel, clientSecret, "<redacted>"),
"clientID": clientID,
"subdomain": subdomain,
"username": username,
"region": region,
}).Debug("Read OneLogin provider config")
if clientSecret == "" {
return nil, errors.New("client-secret config value must bet set")
}
if clientID == "" {
return nil, errors.New("client-id config value must bet set")
}
if subdomain == "" {
return nil, errors.New("subdomain config value must bet set")
}
if region == "" {
region = "US"
}
c := OneLoginProviderConfig{
ClientID: clientID,
ClientSecret: clientSecret,
Subdomain: subdomain,
Username: username,
Region: region,
}
return &c, nil
}
// OneLoginAppConfig represents a OneLogin app configuration.
type OneLoginAppConfig struct {
ID string
Provider string
}
// GetOneLoginApp returns a OneLoginAppConfig struct containing the configuration for app.
func GetOneLoginApp(app string) (*OneLoginAppConfig, error) {
config := viper.GetStringMapString("apps." + app)
appID := config["app-id"]
provider := config["provider"]
if appID == "" {
return nil, errors.New("app-id config value must be set")
}
c := OneLoginAppConfig{
ID: appID,
Provider: provider,
}
return &c, nil
}
// OktaProviderConfig represents an Okta provider configuration.
type OktaProviderConfig struct {
BaseURL string
Username string
}
// GetOktaProvider returns a OktaProviderConfig struct containing the configuration for provider p.
func GetOktaProvider(p string) (*OktaProviderConfig, error) {
baseURL := viper.GetString(fmt.Sprintf("providers.%s.base-url", p))
username := viper.GetString(fmt.Sprintf("providers.%s.username", p))
if baseURL == "" {
return nil, errors.New("base-url config value must bet set")
}
return &OktaProviderConfig{BaseURL: baseURL, Username: username}, nil
}
// OktaAppConfig represents an Okta app configuration.
type OktaAppConfig struct {
Provider string
URL string
}
// GetOktaApp returns an OktaAppConfig struct containing the configuration for app.
func GetOktaApp(app string) (*OktaAppConfig, error) {
config := viper.GetStringMapString("apps." + app)
provider := config["provider"]
url := config["url"]
if provider == "" {
return nil, errors.New("provider config value must be set")
}
if url == "" {
return nil, errors.New("url config value must be set")
}
return &OktaAppConfig{
Provider: provider,
URL: url,
}, nil
}