Repository navigation
Expand file tree
/
Copy pathconfig_test.go
More file actions
189 lines (159 loc) · 7.9 KB
/
Copy pathconfig_test.go
File metadata and controls
189 lines (159 loc) · 7.9 KB
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
179
180
181
182
183
184
185
186
187
188
189
package authbytecore
import (
"reflect"
"strings"
"testing"
"github.com/go-quicktest/qt"
"github.com/spf13/viper"
)
// TestACRForMethodDistinct proves a sign-in requests a method-specific acr_values,
// and the permitted methods (eParaksts Mobile vs eID Scan) resolve to DIFFERENT
// values, so Entrust can actually force the requested method (the login-method ↔
// signing-flow binding), the card through the provider's plugin has its own, and
// the unrecognized "eid" sentinel has none.
func TestACRForMethodDistinct(t *testing.T) {
c := &Configuration{
EparakstsACRMobile: "urn:test:acr:mobile",
EparakstsACREIDScan: "urn:test:acr:eidscan",
EparakstsACRLegacyEID: "urn:test:acr:plugin",
}
qt.Check(t, qt.Equals(c.ACRForMethod("eparakstsMobile"), "urn:test:acr:mobile"))
qt.Check(t, qt.Equals(c.ACRForMethod("eidScan"), "urn:test:acr:eidscan"))
qt.Check(t, qt.Equals(c.ACRForMethod("legacyEid"), "urn:test:acr:plugin"))
qt.Check(t, qt.Not(qt.Equals(c.ACRForMethod("eparakstsMobile"), c.ACRForMethod("eidScan"))))
// The unrecognized-method sentinel is no provider's method → no acr.
qt.Check(t, qt.Equals(c.ACRForMethod("eid"), ""))
// Web eID card login is not an Entrust method → no acr (the card is read on
// this service's own sign-in page).
qt.Check(t, qt.Equals(c.ACRForMethod("webEid"), ""))
qt.Check(t, qt.Equals(c.ACRForMethod("unknown"), ""))
}
// TestDefaultsGiveDistinctACRs proves the shipped defaults are also distinct (a
// regression guard against re-introducing identical placeholder ACRs), the card
// through the provider's plugin asks for eParaksts' own flow name, and no acr is
// shipped for the "eid" sentinel.
func TestDefaultsGiveDistinctACRs(t *testing.T) {
v := viper.New()
NewConfiguration().Bind("", v)
mobile := v.GetString("eparaksts_acr_mobile")
eidScan := v.GetString("eparaksts_acr_eidscan")
plugin := v.GetString("eparaksts_acr_legacy_eid")
qt.Check(t, qt.IsTrue(mobile != ""))
qt.Check(t, qt.IsTrue(eidScan != ""))
qt.Check(t, qt.Not(qt.Equals(mobile, eidScan)))
qt.Check(t, qt.Equals(plugin, "urn:eparaksts:authentication:flow:sc_plugin"))
qt.Check(t, qt.Equals(v.GetString("eparaksts_acr_eid"), ""))
}
// TestUpstreamConfigKeysBindToEnvironment proves that every upstream-connector
// setting is actually REACHABLE from the environment — the property the type
// system does not check and a reader cannot see.
//
// It is derived, not listed: the keys come from the struct's own mapstructure
// tags, so a field added tomorrow is covered the day it is added rather than
// the day someone remembers to extend a list. That is the whole point. The
// defect it was written for was a field that existed at every layer — declared,
// validated, applied to the connector, documented in the README — and had no
// BindEnv call, so setting the documented variable did nothing and said
// nothing. Nothing in the build could see it, because nothing in the build
// reads an environment variable that is never bound.
//
// The convention this relies on, which holds for every key here: the
// environment variable is the mapstructure key, upper-cased.
// One upstream per deployment: the generic connector or the eParaksts profile,
// never both — a second configured family used to be picked over silently,
// which is how a misconfiguration goes unnoticed until somebody logs in through
// the wrong provider. And never none.
func TestValidateUpstreamRefusesTwoFamiliesAndNone(t *testing.T) {
both := Configuration{
OIDCUpstreamAuthorityURL: "https://login.example/tenant/v2.0", OIDCUpstreamClientID: "cid",
EparakstsAuthorityURL: "https://eparaksts.example", EparakstsClientID: "e",
}
err := both.validateUpstream()
qt.Assert(t, qt.IsNotNil(err))
qt.Assert(t, qt.StringContains(err.Error(), "two upstream identity providers"))
none := Configuration{}
qt.Assert(t, qt.IsNotNil(none.validateUpstream()))
generic := Configuration{OIDCUpstreamAuthorityURL: "https://login.example/tenant/v2.0", OIDCUpstreamClientID: "cid"}
qt.Assert(t, qt.IsNil(generic.validateUpstream()))
eparaksts := Configuration{EparakstsAuthorityURL: "https://eparaksts.example", EparakstsClientID: "e"}
qt.Assert(t, qt.IsNil(eparaksts.validateUpstream()))
// The explicit-endpoint form of the generic connector counts as configured too.
explicit := Configuration{
OIDCUpstreamAuthorizeURL: "https://idp.example/a", OIDCUpstreamTokenURL: "https://idp.example/t",
OIDCUpstreamUserInfoURL: "https://idp.example/u", OIDCUpstreamClientID: "cid",
EparakstsAuthorityURL: "https://eparaksts.example",
}
qt.Assert(t, qt.StringContains(explicit.validateUpstream().Error(), "two upstream identity providers"))
}
func TestUpstreamConfigKeysBindToEnvironment(t *testing.T) {
prefixes := []string{"oidc_upstream_", "eparaksts_"}
typ := reflect.TypeOf(Configuration{})
keys := make([]string, 0, typ.NumField())
for i := range typ.NumField() {
key := typ.Field(i).Tag.Get("mapstructure")
for _, prefix := range prefixes {
if strings.HasPrefix(key, prefix) {
keys = append(keys, key)
break
}
}
}
// A guard on the guard: a refactor that renamed the fields away from these
// prefixes would otherwise leave this test passing over nothing at all.
qt.Assert(t, qt.IsTrue(len(keys) >= 10))
for _, key := range keys {
t.Run(key, func(t *testing.T) {
want := "probe-" + key
t.Setenv(strings.ToUpper(key), want)
v := viper.New()
NewConfiguration().Bind("", v)
qt.Check(t, qt.Equals(v.GetString(key), want),
qt.Commentf("%s is not bound to %s — add it to the BindEnv block; "+
"a field with no binding is invisible until an operator sets it and nothing happens",
key, strings.ToUpper(key)))
})
}
}
// The generic upstream is trusted with identity codes only when
// the deployment says so, the eParaksts profile always; and a generic upstream
// with a code claim named but no trust is refused at the start, naming both
// settings, so an upgrade never quietly stops matching people by the code.
func TestTheGenericUpstreamIsTrustedWithCodesOnlyWhenSaid(t *testing.T) {
generic := Configuration{OIDCUpstreamAuthorityURL: "https://login.example/tenant/v2.0", OIDCUpstreamClientID: "cid"}
qt.Check(t, qt.IsFalse(generic.UpstreamConfig().TrustIdentityCodes))
trusted := generic
trusted.OIDCUpstreamTrustIdentityCodes = true
qt.Check(t, qt.IsTrue(trusted.UpstreamConfig().TrustIdentityCodes))
eparaksts := Configuration{EparakstsAuthorityURL: "https://eparaksts.example", EparakstsClientID: "e"}
qt.Check(t, qt.IsTrue(eparaksts.UpstreamConfig().TrustIdentityCodes))
named := generic
named.OIDCUpstreamClaimSerial = "serial_number"
err := named.validateUpstream()
qt.Assert(t, qt.IsNotNil(err))
qt.Check(t, qt.StringContains(err.Error(), "OIDC_UPSTREAM_CLAIM_SERIAL"))
qt.Check(t, qt.StringContains(err.Error(), "OIDC_UPSTREAM_TRUST_IDENTITY_CODES"))
named.OIDCUpstreamTrustIdentityCodes = true
qt.Check(t, qt.IsNil(named.validateUpstream()))
// A claim name changes nothing for the eParaksts profile, which is trusted.
eparaksts.OIDCUpstreamClaimSerial = "serial_number"
qt.Check(t, qt.IsNil(eparaksts.validateUpstream()))
}
// The email claim name reaches the connector under either profile, and an unset
// name is left for the connector's own default.
func TestUpstreamConfigCarriesTheEmailClaim(t *testing.T) {
named := &Configuration{OIDCUpstreamClaimEmail: "mail"}
qt.Check(t, qt.Equals(named.UpstreamConfig().ClaimEmail, "mail"))
generic := &Configuration{
OIDCUpstreamAuthorityURL: "https://idp.example",
OIDCUpstreamClaimEmail: "mail",
}
qt.Check(t, qt.Equals(generic.UpstreamConfig().ClaimEmail, "mail"))
qt.Check(t, qt.Equals((&Configuration{}).UpstreamConfig().ClaimEmail, ""))
}
// Discovery, the browser's return and every token name one issuer: a trailing
// slash in the configuration is not part of it.
func TestTheIssuerIsOneValue(t *testing.T) {
t.Setenv("AUTH_ISSUER_URL", "http://localhost:8080/")
app := TestApp(t)
qt.Check(t, qt.Equals(app.Config().IssuerURL, "http://localhost:8080"))
}