-
Notifications
You must be signed in to change notification settings - Fork 15
/
authconfig.go
170 lines (150 loc) · 5.33 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
package config
import (
"errors"
"os"
"strings"
"github.com/ashirt-ops/ashirt-server/backend/helpers"
"github.com/go-webauthn/webauthn/protocol"
"github.com/kelseyhightower/envconfig"
)
// AuthConfig provides configuration details for all authentication services
type AuthConfig struct {
Services []string
AuthConfigs map[string]AuthInstanceConfig
}
// AuthInstanceConfig provides all of the _possible_ configuration values for an auth instance.
// Note: it is expected that not all fields will be populated. It is up to the user to verify
// that these fields exist and have correct values
type AuthInstanceConfig struct {
Type string
Name string
RegistrationEnabled bool `ignored:"true"`
OIDCConfig
WebauthnConfig
}
type OIDCConfig struct {
FriendlyName string `split_words:"true"`
ProviderURL string `split_words:"true"`
ClientID string `split_words:"true"`
ClientSecret string `split_words:"true"`
Scopes string
ProfileFirstNameField string `split_words:"true"`
ProfileLastNameField string `split_words:"true"`
ProfileEmailField string `split_words:"true"`
ProfileSlugField string `split_words:"true"`
}
type WebauthnConfig struct {
DisplayName string `split_words:"true"`
// All of the below have innate defaults, and so are effectively optional
Timeout int
RPOrigin string `envconfig:"RP_ORIGIN"`
AttestationPreference string `split_words:"true"`
Debug bool
AuthenticatorAttachment string `split_words:"true"`
AuthenticatorResidentKey string `split_words:"true"`
AuthenticatorRequireResidentKey *bool `split_words:"true"`
AuthenticatorUserVerification string `split_words:"true"`
}
func (w WebauthnConfig) Conveyance() protocol.ConveyancePreference {
val := strings.TrimSpace(strings.ToLower(w.AttestationPreference))
if val == "indirect" {
return protocol.PreferIndirectAttestation
}
if val == "direct" {
return protocol.PreferDirectAttestation
}
// standard default
return protocol.PreferNoAttestation
}
func (w WebauthnConfig) AuthenticatorAttachmentPreference() protocol.AuthenticatorAttachment {
val := strings.TrimSpace((strings.ToLower((w.AuthenticatorAttachment))))
if val == "platform" {
return protocol.Platform
}
if val == "cross-platform" {
return protocol.Platform
}
if val == "" {
return "" // letting the library decide what the default is
}
// Warn here?
return ""
}
func (w WebauthnConfig) AuthenticatorResidentKeyPreference() protocol.ResidentKeyRequirement {
val := strings.TrimSpace(strings.ToLower(w.AuthenticatorResidentKey))
if val == "preferred" {
return protocol.ResidentKeyRequirementPreferred
}
if val == "required" {
return protocol.ResidentKeyRequirementRequired
}
return protocol.ResidentKeyRequirementDiscouraged
}
func (w WebauthnConfig) AuthenticatorUserVerificationPreference() protocol.UserVerificationRequirement {
val := strings.TrimSpace(strings.ToLower(w.AttestationPreference))
if val == "required" {
return protocol.VerificationRequired
}
if val == "discouraged" {
return protocol.VerificationDiscouraged
}
return protocol.VerificationPreferred
}
func (w WebauthnConfig) BuildAuthenticatorSelection() protocol.AuthenticatorSelection {
return protocol.AuthenticatorSelection{
RequireResidentKey: w.AuthenticatorRequireResidentKey,
UserVerification: w.AuthenticatorUserVerificationPreference(),
ResidentKey: w.AuthenticatorResidentKeyPreference(),
AuthenticatorAttachment: w.AuthenticatorAttachmentPreference(),
}
}
func splitNoSpaces(s, delimiter string) []string {
arr := strings.Split(s, delimiter)
for i, v := range arr {
arr[i] = strings.TrimSpace(v)
}
return arr
}
func loadAuthConfig() error {
config := AuthConfig{}
servicesStr := os.Getenv("AUTH_SERVICES")
if servicesStr == "" {
return errors.New("Auth services not defined")
}
servicesArr := splitNoSpaces(servicesStr, ",")
serviceRegistrationStr := os.Getenv("AUTH_SERVICES_ALLOW_REGISTRATION")
serviceRegistrationArr := splitNoSpaces(serviceRegistrationStr, ",")
config.Services = servicesArr
config.AuthConfigs = make(map[string]AuthInstanceConfig)
for _, service := range servicesArr {
innerConfig := AuthInstanceConfig{}
if helpers.ContainsMatch(serviceRegistrationArr, service) {
innerConfig.RegistrationEnabled = true
}
err := envconfig.Process("auth_"+service, &innerConfig)
if err != nil {
return err
}
config.AuthConfigs[service] = innerConfig
}
auth = config
return nil
}
// AuthConfigInstance attempts to retrieve a particular auth configuration set from the environment.
// Note this looks for environment variables prefixed with AUTH_${SERVICE_NAME}, and will only
// retrieve these values for services named in the AUTH_SERVICES environment
func AuthConfigInstance(name string) AuthInstanceConfig {
if name == "ashirt" { // special case -- local auth doesn't have any normal environment variables
return AuthInstanceConfig{
Name: "ashirt",
Type: "local",
RegistrationEnabled: auth.AuthConfigs[name].RegistrationEnabled,
}
}
v := auth.AuthConfigs[name]
return v
}
// SupportedAuthServices retrieves the parsed AUTH_SERVICES value from the environment
func SupportedAuthServices() []string {
return auth.Services
}