| 1 |
package oauth2_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"encoding/base64" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"net/http" |
| 9 |
"net/url" |
| 10 |
"regexp" |
| 11 |
"strings" |
| 12 |
"testing" |
| 13 |
"testing/fstest" |
| 14 |
"time" |
| 15 |
|
| 16 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/config" |
| 17 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/config/types" |
| 18 |
oauth2types "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/types" |
| 19 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/state" |
| 20 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/test/testsuite" |
| 21 |
"github.com/stretchr/testify/require" |
| 22 |
) |
| 23 |
|
| 24 |
const invalid = "invalid" |
| 25 |
|
| 26 |
func TestHandler(t *testing.T) { |
| 27 |
t.Parallel() |
| 28 |
|
| 29 |
for _, tc := range []struct { |
| 30 |
name string |
| 31 |
conf config.Config |
| 32 |
state state.State |
| 33 |
invalidState bool |
| 34 |
xForwardedFor string |
| 35 |
preAllow bool |
| 36 |
postAllow bool |
| 37 |
}{ |
| 38 |
{ |
| 39 |
"default", |
| 40 |
func() config.Config { |
| 41 |
conf := config.Defaults |
| 42 |
conf.HTTP.Secret = testsuite.Secret |
| 43 |
conf.HTTP.Check.IPAddr = false |
| 44 |
conf.OAuth2.Endpoints = config.OAuth2Endpoints{} |
| 45 |
conf.OAuth2.Scopes = []string{oauth2types.ScopeOpenID, oauth2types.ScopeProfile} |
| 46 |
conf.OpenVPN.Bypass.CommonNames = make(types.RegexpSlice, 0) |
| 47 |
conf.OpenVPN.AuthTokenUser = true |
| 48 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 49 |
conf.OAuth2.Validate.Expression = "user.username == string(token.claims.sub)" |
| 50 |
|
| 51 |
return conf |
| 52 |
}(), |
| 53 |
state.State{Client: state.ClientIdentifier{CID: 0, KID: 1, CommonName: "name"}, IPAddr: "127.0.0.1", IPPort: "12345"}, |
| 54 |
false, |
| 55 |
"", |
| 56 |
true, |
| 57 |
true, |
| 58 |
}, |
| 59 |
{ |
| 60 |
"with username defined", |
| 61 |
func() config.Config { |
| 62 |
conf := config.Defaults |
| 63 |
conf.HTTP.Secret = testsuite.Secret |
| 64 |
conf.HTTP.Check.IPAddr = false |
| 65 |
conf.OAuth2.Endpoints = config.OAuth2Endpoints{} |
| 66 |
conf.OAuth2.Scopes = []string{oauth2types.ScopeOpenID, oauth2types.ScopeProfile} |
| 67 |
conf.OpenVPN.Bypass.CommonNames = make(types.RegexpSlice, 0) |
| 68 |
conf.OpenVPN.AuthTokenUser = true |
| 69 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 70 |
|
| 71 |
return conf |
| 72 |
}(), |
| 73 |
state.State{Client: state.ClientIdentifier{CID: 0, KID: 1, UsernameIsDefined: 1, CommonName: "name"}, IPAddr: "127.0.0.1", IPPort: "12345"}, |
| 74 |
false, |
| 75 |
"", |
| 76 |
true, |
| 77 |
true, |
| 78 |
}, |
| 79 |
{ |
| 80 |
"with template", |
| 81 |
func() config.Config { |
| 82 |
tmpl, err := types.NewTemplate("../../LICENSE.txt") |
| 83 |
require.NoError(t, err) |
| 84 |
|
| 85 |
conf := config.Defaults |
| 86 |
conf.HTTP.Secret = testsuite.Secret |
| 87 |
conf.HTTP.Check.IPAddr = false |
| 88 |
conf.HTTP.Template = tmpl |
| 89 |
conf.OAuth2.Endpoints = config.OAuth2Endpoints{} |
| 90 |
conf.OAuth2.Scopes = []string{oauth2types.ScopeOpenID, oauth2types.ScopeProfile} |
| 91 |
conf.OpenVPN.Bypass.CommonNames = make(types.RegexpSlice, 0) |
| 92 |
conf.OpenVPN.AuthTokenUser = true |
| 93 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 94 |
|
| 95 |
return conf |
| 96 |
}(), |
| 97 |
state.State{Client: state.ClientIdentifier{CID: 0, KID: 1, CommonName: "name"}, IPAddr: "127.0.0.1", IPPort: "12345"}, |
| 98 |
false, |
| 99 |
"", |
| 100 |
true, |
| 101 |
true, |
| 102 |
}, |
| 103 |
{ |
| 104 |
"with userinfo enabled", |
| 105 |
func() config.Config { |
| 106 |
conf := config.Defaults |
| 107 |
conf.HTTP.Secret = testsuite.Secret |
| 108 |
conf.HTTP.Check.IPAddr = false |
| 109 |
conf.OAuth2.Endpoints = config.OAuth2Endpoints{} |
| 110 |
conf.OAuth2.Scopes = []string{oauth2types.ScopeOpenID, oauth2types.ScopeProfile} |
| 111 |
conf.OAuth2.Validate.Groups = []string{"group1"} |
| 112 |
conf.OAuth2.UserInfo = true |
| 113 |
conf.OpenVPN.Bypass.CommonNames = make(types.RegexpSlice, 0) |
| 114 |
conf.OpenVPN.AuthTokenUser = true |
| 115 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 116 |
|
| 117 |
return conf |
| 118 |
}(), |
| 119 |
state.State{Client: state.ClientIdentifier{CID: 0, KID: 1, CommonName: "name"}, IPAddr: "127.0.0.1", IPPort: "12345"}, |
| 120 |
false, |
| 121 |
"", |
| 122 |
true, |
| 123 |
true, |
| 124 |
}, |
| 125 |
{ |
| 126 |
"with userinfo enabled + validate groups", |
| 127 |
func() config.Config { |
| 128 |
conf := config.Defaults |
| 129 |
conf.HTTP.Secret = testsuite.Secret |
| 130 |
conf.HTTP.Check.IPAddr = false |
| 131 |
conf.OAuth2.Endpoints = config.OAuth2Endpoints{} |
| 132 |
conf.OAuth2.Scopes = []string{oauth2types.ScopeOpenID, oauth2types.ScopeProfile} |
| 133 |
conf.OAuth2.Validate.Groups = []string{"group0", "group1"} |
| 134 |
conf.OAuth2.UserInfo = true |
| 135 |
conf.OpenVPN.Bypass.CommonNames = make(types.RegexpSlice, 0) |
| 136 |
conf.OpenVPN.AuthTokenUser = true |
| 137 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 138 |
|
| 139 |
return conf |
| 140 |
}(), |
| 141 |
state.State{Client: state.ClientIdentifier{CID: 0, KID: 1, CommonName: "name"}, IPAddr: "127.0.0.1", IPPort: "12345"}, |
| 142 |
false, |
| 143 |
"", |
| 144 |
true, |
| 145 |
true, |
| 146 |
}, |
| 147 |
{ |
| 148 |
"with userinfo enabled + missing validate groups", |
| 149 |
func() config.Config { |
| 150 |
conf := config.Defaults |
| 151 |
conf.HTTP.Secret = testsuite.Secret |
| 152 |
conf.HTTP.Check.IPAddr = false |
| 153 |
conf.OAuth2.Endpoints = config.OAuth2Endpoints{} |
| 154 |
conf.OAuth2.Scopes = []string{oauth2types.ScopeOpenID, oauth2types.ScopeProfile} |
| 155 |
conf.OAuth2.Validate.Groups = []string{"group0"} |
| 156 |
conf.OAuth2.UserInfo = true |
| 157 |
conf.OpenVPN.Bypass.CommonNames = make(types.RegexpSlice, 0) |
| 158 |
conf.OpenVPN.AuthTokenUser = true |
| 159 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 160 |
|
| 161 |
return conf |
| 162 |
}(), |
| 163 |
state.State{Client: state.ClientIdentifier{CID: 0, KID: 1, CommonName: "name"}, IPAddr: "127.0.0.1", IPPort: "12345"}, |
| 164 |
false, |
| 165 |
"", |
| 166 |
true, |
| 167 |
false, |
| 168 |
}, |
| 169 |
{ |
| 170 |
"with ipaddr", |
| 171 |
func() config.Config { |
| 172 |
conf := config.Defaults |
| 173 |
conf.HTTP.Secret = testsuite.Secret |
| 174 |
conf.HTTP.Check.IPAddr = true |
| 175 |
conf.OAuth2.Endpoints = config.OAuth2Endpoints{} |
| 176 |
conf.OAuth2.Scopes = []string{oauth2types.ScopeOpenID, oauth2types.ScopeProfile} |
| 177 |
conf.OpenVPN.Bypass.CommonNames = make(types.RegexpSlice, 0) |
| 178 |
conf.OpenVPN.AuthTokenUser = true |
| 179 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 180 |
|
| 181 |
return conf |
| 182 |
}(), |
| 183 |
state.State{Client: state.ClientIdentifier{CID: 0, KID: 1, CommonName: "name"}, IPAddr: "127.0.0.1", IPPort: "12345"}, |
| 184 |
false, |
| 185 |
"", |
| 186 |
true, |
| 187 |
true, |
| 188 |
}, |
| 189 |
{ |
| 190 |
"with ipaddr + forwarded-for", |
| 191 |
func() config.Config { |
| 192 |
conf := config.Defaults |
| 193 |
conf.HTTP.Secret = testsuite.Secret |
| 194 |
conf.HTTP.Check.IPAddr = true |
| 195 |
conf.HTTP.EnableProxyHeaders = true |
| 196 |
conf.HTTP.TrustedProxies = types.StringSlice{"127.0.0.1/32"} |
| 197 |
conf.OAuth2.Endpoints = config.OAuth2Endpoints{} |
| 198 |
conf.OAuth2.Scopes = []string{oauth2types.ScopeOpenID, oauth2types.ScopeProfile} |
| 199 |
conf.OpenVPN.Bypass.CommonNames = make(types.RegexpSlice, 0) |
| 200 |
conf.OpenVPN.AuthTokenUser = true |
| 201 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 202 |
|
| 203 |
return conf |
| 204 |
}(), |
| 205 |
state.State{Client: state.ClientIdentifier{CID: 0, KID: 1, CommonName: "name"}, IPAddr: "127.0.0.2", IPPort: "12345"}, |
| 206 |
false, |
| 207 |
"127.0.0.2", |
| 208 |
true, |
| 209 |
true, |
| 210 |
}, |
| 211 |
{ |
| 212 |
"with ipaddr + disabled forwarded-for", |
| 213 |
func() config.Config { |
| 214 |
conf := config.Defaults |
| 215 |
conf.HTTP.Secret = testsuite.Secret |
| 216 |
conf.HTTP.Check.IPAddr = true |
| 217 |
conf.HTTP.EnableProxyHeaders = false |
| 218 |
conf.OAuth2.Endpoints = config.OAuth2Endpoints{} |
| 219 |
conf.OAuth2.Scopes = []string{oauth2types.ScopeOpenID, oauth2types.ScopeProfile} |
| 220 |
conf.OpenVPN.Bypass.CommonNames = make(types.RegexpSlice, 0) |
| 221 |
conf.OpenVPN.AuthTokenUser = true |
| 222 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 223 |
|
| 224 |
return conf |
| 225 |
}(), |
| 226 |
state.State{Client: state.ClientIdentifier{CID: 0, KID: 1, CommonName: "name"}, IPAddr: "127.0.0.2", IPPort: "12345"}, |
| 227 |
false, |
| 228 |
"127.0.0.2", |
| 229 |
false, |
| 230 |
false, |
| 231 |
}, |
| 232 |
{ |
| 233 |
"with ipaddr + forwarded-for from trusted proxy", |
| 234 |
func() config.Config { |
| 235 |
conf := config.Defaults |
| 236 |
conf.HTTP.Secret = testsuite.Secret |
| 237 |
conf.HTTP.Check.IPAddr = true |
| 238 |
conf.HTTP.EnableProxyHeaders = true |
| 239 |
conf.HTTP.TrustedProxies = types.StringSlice{"127.0.0.1/32"} |
| 240 |
conf.OAuth2.Endpoints = config.OAuth2Endpoints{} |
| 241 |
conf.OAuth2.Scopes = []string{oauth2types.ScopeOpenID, oauth2types.ScopeProfile} |
| 242 |
conf.OpenVPN.Bypass.CommonNames = make(types.RegexpSlice, 0) |
| 243 |
conf.OpenVPN.AuthTokenUser = true |
| 244 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 245 |
|
| 246 |
return conf |
| 247 |
}(), |
| 248 |
state.State{Client: state.ClientIdentifier{CID: 0, KID: 1, CommonName: "name"}, IPAddr: "127.0.0.2", IPPort: "12345"}, |
| 249 |
false, |
| 250 |
"127.0.0.2", |
| 251 |
true, |
| 252 |
true, |
| 253 |
}, |
| 254 |
{ |
| 255 |
"with ipaddr + forwarded-for ipv6", |
| 256 |
func() config.Config { |
| 257 |
conf := config.Defaults |
| 258 |
conf.HTTP.Secret = testsuite.Secret |
| 259 |
conf.HTTP.Check.IPAddr = true |
| 260 |
conf.HTTP.EnableProxyHeaders = true |
| 261 |
conf.HTTP.TrustedProxies = types.StringSlice{"127.0.0.1/32"} |
| 262 |
conf.OAuth2.Endpoints = config.OAuth2Endpoints{} |
| 263 |
conf.OAuth2.Scopes = []string{oauth2types.ScopeOpenID, oauth2types.ScopeProfile} |
| 264 |
conf.OpenVPN.Bypass.CommonNames = make(types.RegexpSlice, 0) |
| 265 |
conf.OpenVPN.AuthTokenUser = true |
| 266 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 267 |
|
| 268 |
return conf |
| 269 |
}(), |
| 270 |
state.State{Client: state.ClientIdentifier{CID: 0, KID: 1, CommonName: "name"}, IPAddr: "2001:db8::1", IPPort: "12345"}, |
| 271 |
false, |
| 272 |
"2001:db8::1", |
| 273 |
true, |
| 274 |
true, |
| 275 |
}, |
| 276 |
{ |
| 277 |
"with cel validation result false", |
| 278 |
func() config.Config { |
| 279 |
conf := config.Defaults |
| 280 |
conf.HTTP.Secret = testsuite.Secret |
| 281 |
conf.HTTP.Check.IPAddr = true |
| 282 |
conf.HTTP.EnableProxyHeaders = true |
| 283 |
conf.HTTP.TrustedProxies = types.StringSlice{"127.0.0.1/32"} |
| 284 |
conf.OAuth2.Endpoints = config.OAuth2Endpoints{} |
| 285 |
conf.OAuth2.Scopes = []string{oauth2types.ScopeOpenID, oauth2types.ScopeProfile} |
| 286 |
conf.OAuth2.Validate.Expression = "false" |
| 287 |
conf.OpenVPN.Bypass.CommonNames = make(types.RegexpSlice, 0) |
| 288 |
conf.OpenVPN.AuthTokenUser = true |
| 289 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 290 |
|
| 291 |
return conf |
| 292 |
}(), |
| 293 |
state.State{Client: state.ClientIdentifier{CID: 0, KID: 1, CommonName: "name"}, IPAddr: "127.0.0.2", IPPort: "12345"}, |
| 294 |
false, |
| 295 |
"127.0.0.2", |
| 296 |
true, |
| 297 |
false, |
| 298 |
}, |
| 299 |
{ |
| 300 |
"with client config found", |
| 301 |
func() config.Config { |
| 302 |
conf := config.Defaults |
| 303 |
conf.HTTP.Secret = testsuite.Secret |
| 304 |
conf.HTTP.Check.IPAddr = true |
| 305 |
conf.HTTP.EnableProxyHeaders = true |
| 306 |
conf.HTTP.TrustedProxies = types.StringSlice{"127.0.0.1/32"} |
| 307 |
conf.OAuth2.Endpoints = config.OAuth2Endpoints{} |
| 308 |
conf.OAuth2.Scopes = []string{oauth2types.ScopeOpenID, oauth2types.ScopeProfile} |
| 309 |
conf.OpenVPN.Bypass.CommonNames = make(types.RegexpSlice, 0) |
| 310 |
conf.OpenVPN.AuthTokenUser = true |
| 311 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 312 |
conf.OpenVPN.ClientConfig.Enabled = true |
| 313 |
conf.OpenVPN.ClientConfig.Expression = "[string(token.claims." + testsuite.SubjectClaim + ")]" |
| 314 |
conf.OpenVPN.ClientConfig.Path = types.FS{ |
| 315 |
FS: fstest.MapFS{ |
| 316 |
"id1.conf": &fstest.MapFile{ |
| 317 |
Data: []byte("push \"ping 60\"\npush \"ping-restart 180\"\r\npush \"ping-timer-rem\" 0\r\n"), |
| 318 |
}, |
| 319 |
}, |
| 320 |
} |
| 321 |
|
| 322 |
return conf |
| 323 |
}(), |
| 324 |
state.State{Client: state.ClientIdentifier{CID: 0, KID: 1, CommonName: "name"}, IPAddr: "127.0.0.2", IPPort: "12345"}, |
| 325 |
false, |
| 326 |
"127.0.0.2", |
| 327 |
true, |
| 328 |
true, |
| 329 |
}, |
| 330 |
{ |
| 331 |
"with client config and custom claim", |
| 332 |
func() config.Config { |
| 333 |
conf := config.Defaults |
| 334 |
conf.HTTP.Secret = testsuite.Secret |
| 335 |
conf.HTTP.Check.IPAddr = true |
| 336 |
conf.HTTP.EnableProxyHeaders = true |
| 337 |
conf.HTTP.TrustedProxies = types.StringSlice{"127.0.0.1/32"} |
| 338 |
conf.OAuth2.Endpoints = config.OAuth2Endpoints{} |
| 339 |
conf.OAuth2.Scopes = []string{oauth2types.ScopeOpenID, oauth2types.ScopeProfile} |
| 340 |
conf.OpenVPN.Bypass.CommonNames = make(types.RegexpSlice, 0) |
| 341 |
conf.OpenVPN.AuthTokenUser = true |
| 342 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 343 |
conf.OpenVPN.ClientConfig.Enabled = true |
| 344 |
conf.OpenVPN.ClientConfig.Expression = "[string(token.claims." + testsuite.SubjectClaim + ")]" |
| 345 |
conf.OpenVPN.ClientConfig.Path = types.FS{ |
| 346 |
FS: fstest.MapFS{ |
| 347 |
"id1.conf": &fstest.MapFile{ |
| 348 |
Data: []byte("push \"ping 60\"\npush \"ping-restart 180\"\r\npush \"ping-timer-rem\" 0\r\n"), |
| 349 |
}, |
| 350 |
}, |
| 351 |
} |
| 352 |
|
| 353 |
return conf |
| 354 |
}(), |
| 355 |
state.State{Client: state.ClientIdentifier{CID: 0, KID: 1, CommonName: "name"}, IPAddr: "127.0.0.2", IPPort: "12345"}, |
| 356 |
false, |
| 357 |
"127.0.0.2", |
| 358 |
true, |
| 359 |
true, |
| 360 |
}, |
| 361 |
{ |
| 362 |
"with client config not found", |
| 363 |
func() config.Config { |
| 364 |
conf := config.Defaults |
| 365 |
conf.HTTP.Secret = testsuite.Secret |
| 366 |
conf.HTTP.Check.IPAddr = true |
| 367 |
conf.HTTP.EnableProxyHeaders = true |
| 368 |
conf.HTTP.TrustedProxies = types.StringSlice{"127.0.0.1/32"} |
| 369 |
conf.OAuth2.Endpoints = config.OAuth2Endpoints{} |
| 370 |
conf.OAuth2.Scopes = []string{oauth2types.ScopeOpenID, oauth2types.ScopeProfile} |
| 371 |
conf.OpenVPN.Bypass.CommonNames = make(types.RegexpSlice, 0) |
| 372 |
conf.OpenVPN.AuthTokenUser = true |
| 373 |
conf.OAuth2.OpenVPNUsername = "" |
| 374 |
conf.OpenVPN.ClientConfig.Enabled = true |
| 375 |
conf.OpenVPN.ClientConfig.Expression = "[openvpn.commonName]" |
| 376 |
conf.OpenVPN.ClientConfig.Path = types.FS{ |
| 377 |
FS: fstest.MapFS{}, |
| 378 |
} |
| 379 |
|
| 380 |
return conf |
| 381 |
}(), |
| 382 |
state.State{Client: state.ClientIdentifier{CID: 0, KID: 1, CommonName: "client"}, IPAddr: "127.0.0.2", IPPort: "12345"}, |
| 383 |
false, |
| 384 |
"127.0.0.2", |
| 385 |
true, |
| 386 |
true, |
| 387 |
}, |
| 388 |
{ |
| 389 |
"with client config selector + static values", |
| 390 |
func() config.Config { |
| 391 |
conf := config.Defaults |
| 392 |
conf.HTTP.Secret = testsuite.Secret |
| 393 |
conf.HTTP.Check.IPAddr = true |
| 394 |
conf.HTTP.EnableProxyHeaders = true |
| 395 |
conf.HTTP.TrustedProxies = types.StringSlice{"127.0.0.1/32"} |
| 396 |
conf.OAuth2.Endpoints = config.OAuth2Endpoints{} |
| 397 |
conf.OAuth2.Scopes = []string{oauth2types.ScopeOpenID, oauth2types.ScopeProfile} |
| 398 |
conf.OpenVPN.Bypass.CommonNames = make(types.RegexpSlice, 0) |
| 399 |
conf.OpenVPN.AuthTokenUser = true |
| 400 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 401 |
conf.OpenVPN.ClientConfig.Enabled = true |
| 402 |
conf.OpenVPN.ClientConfig.Strategy = config.OpenVPNConfigStrategyUserSelector |
| 403 |
conf.OpenVPN.ClientConfig.Expression = `["static"]` |
| 404 |
conf.OpenVPN.ClientConfig.Path = types.FS{ |
| 405 |
FS: fstest.MapFS{ |
| 406 |
"static.conf": &fstest.MapFile{ |
| 407 |
Data: []byte("push \"ping 60\"\npush \"ping-restart 180\"\r\npush \"ping-timer-rem\" 0\r\n"), |
| 408 |
}, |
| 409 |
}, |
| 410 |
} |
| 411 |
|
| 412 |
return conf |
| 413 |
}(), |
| 414 |
state.State{Client: state.ClientIdentifier{CID: 0, KID: 1, CommonName: "name"}, IPAddr: "127.0.0.2", IPPort: "12345"}, |
| 415 |
false, |
| 416 |
"127.0.0.2", |
| 417 |
true, |
| 418 |
true, |
| 419 |
}, |
| 420 |
{ |
| 421 |
"with client config selector + static values + claim string", |
| 422 |
func() config.Config { |
| 423 |
conf := config.Defaults |
| 424 |
conf.HTTP.Secret = testsuite.Secret |
| 425 |
conf.HTTP.Check.IPAddr = true |
| 426 |
conf.HTTP.EnableProxyHeaders = true |
| 427 |
conf.HTTP.TrustedProxies = types.StringSlice{"127.0.0.1/32"} |
| 428 |
conf.OAuth2.Endpoints = config.OAuth2Endpoints{} |
| 429 |
conf.OAuth2.Scopes = []string{oauth2types.ScopeOpenID, oauth2types.ScopeProfile} |
| 430 |
conf.OpenVPN.Bypass.CommonNames = make(types.RegexpSlice, 0) |
| 431 |
conf.OpenVPN.AuthTokenUser = true |
| 432 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 433 |
conf.OpenVPN.ClientConfig.Enabled = true |
| 434 |
conf.OpenVPN.ClientConfig.Strategy = config.OpenVPNConfigStrategyUserSelector |
| 435 |
conf.OpenVPN.ClientConfig.Expression = `["static", string(token.claims.` + testsuite.SubjectClaim + `)]` |
| 436 |
conf.OpenVPN.ClientConfig.Path = types.FS{ |
| 437 |
FS: fstest.MapFS{ |
| 438 |
"id1.conf": &fstest.MapFile{ |
| 439 |
Data: []byte("push \"ping 60\"\npush \"ping-restart 180\"\r\npush \"ping-timer-rem\" 0\r\n"), |
| 440 |
}, |
| 441 |
}, |
| 442 |
} |
| 443 |
|
| 444 |
return conf |
| 445 |
}(), |
| 446 |
state.State{Client: state.ClientIdentifier{CID: 0, KID: 1, CommonName: "name"}, IPAddr: "127.0.0.2", IPPort: "12345"}, |
| 447 |
false, |
| 448 |
"127.0.0.2", |
| 449 |
true, |
| 450 |
true, |
| 451 |
}, |
| 452 |
{ |
| 453 |
"with client config selector + static values + claim array", |
| 454 |
func() config.Config { |
| 455 |
conf := config.Defaults |
| 456 |
conf.HTTP.Secret = testsuite.Secret |
| 457 |
conf.HTTP.Check.IPAddr = true |
| 458 |
conf.HTTP.EnableProxyHeaders = true |
| 459 |
conf.HTTP.TrustedProxies = types.StringSlice{"127.0.0.1/32"} |
| 460 |
conf.OAuth2.Endpoints = config.OAuth2Endpoints{} |
| 461 |
conf.OAuth2.Scopes = []string{oauth2types.ScopeOpenID, oauth2types.ScopeProfile} |
| 462 |
conf.OpenVPN.Bypass.CommonNames = make(types.RegexpSlice, 0) |
| 463 |
conf.OpenVPN.AuthTokenUser = true |
| 464 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 465 |
conf.OpenVPN.ClientConfig.Enabled = true |
| 466 |
conf.OpenVPN.ClientConfig.Strategy = config.OpenVPNConfigStrategyUserSelector |
| 467 |
conf.OpenVPN.ClientConfig.Expression = `["aaa"] + token.claims.amr` |
| 468 |
conf.OpenVPN.ClientConfig.Path = types.FS{ |
| 469 |
FS: fstest.MapFS{ |
| 470 |
"pwd.conf": &fstest.MapFile{ |
| 471 |
Data: []byte("push \"ping 60\"\npush \"ping-restart 180\"\r\npush \"ping-timer-rem\" 0\r\n"), |
| 472 |
}, |
| 473 |
}, |
| 474 |
} |
| 475 |
|
| 476 |
return conf |
| 477 |
}(), |
| 478 |
state.State{Client: state.ClientIdentifier{CID: 0, KID: 1, CommonName: "name"}, IPAddr: "127.0.0.2", IPPort: "12345"}, |
| 479 |
false, |
| 480 |
"127.0.0.2", |
| 481 |
true, |
| 482 |
true, |
| 483 |
}, |
| 484 |
{ |
| 485 |
"with client config selector + static values + claim invalid", |
| 486 |
func() config.Config { |
| 487 |
conf := config.Defaults |
| 488 |
conf.HTTP.Secret = testsuite.Secret |
| 489 |
conf.HTTP.Check.IPAddr = true |
| 490 |
conf.HTTP.EnableProxyHeaders = true |
| 491 |
conf.HTTP.TrustedProxies = types.StringSlice{"127.0.0.1/32"} |
| 492 |
conf.OAuth2.Endpoints = config.OAuth2Endpoints{} |
| 493 |
conf.OAuth2.Scopes = []string{oauth2types.ScopeOpenID, oauth2types.ScopeProfile} |
| 494 |
conf.OpenVPN.Bypass.CommonNames = make(types.RegexpSlice, 0) |
| 495 |
conf.OpenVPN.AuthTokenUser = true |
| 496 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 497 |
conf.OpenVPN.ClientConfig.Enabled = true |
| 498 |
conf.OpenVPN.ClientConfig.Strategy = config.OpenVPNConfigStrategyUserSelector |
| 499 |
conf.OpenVPN.ClientConfig.Expression = `["static"]` |
| 500 |
conf.OpenVPN.ClientConfig.Path = types.FS{ |
| 501 |
FS: fstest.MapFS{ |
| 502 |
"static.conf": &fstest.MapFile{ |
| 503 |
Data: []byte("push \"ping 60\"\npush \"ping-restart 180\"\r\npush \"ping-timer-rem\" 0\r\n"), |
| 504 |
}, |
| 505 |
}, |
| 506 |
} |
| 507 |
|
| 508 |
return conf |
| 509 |
}(), |
| 510 |
state.State{Client: state.ClientIdentifier{CID: 0, KID: 1, CommonName: "name"}, IPAddr: "127.0.0.2", IPPort: "12345"}, |
| 511 |
false, |
| 512 |
"127.0.0.2", |
| 513 |
true, |
| 514 |
true, |
| 515 |
}, |
| 516 |
{ |
| 517 |
"with client config selector + static values + not found", |
| 518 |
func() config.Config { |
| 519 |
conf := config.Defaults |
| 520 |
conf.HTTP.Secret = testsuite.Secret |
| 521 |
conf.HTTP.Check.IPAddr = true |
| 522 |
conf.HTTP.EnableProxyHeaders = true |
| 523 |
conf.HTTP.TrustedProxies = types.StringSlice{"127.0.0.1/32"} |
| 524 |
conf.OAuth2.Endpoints = config.OAuth2Endpoints{} |
| 525 |
conf.OAuth2.Scopes = []string{oauth2types.ScopeOpenID, oauth2types.ScopeProfile} |
| 526 |
conf.OpenVPN.Bypass.CommonNames = make(types.RegexpSlice, 0) |
| 527 |
conf.OpenVPN.AuthTokenUser = true |
| 528 |
conf.OAuth2.OpenVPNUsername = "" |
| 529 |
conf.OpenVPN.ClientConfig.Enabled = true |
| 530 |
conf.OpenVPN.ClientConfig.Strategy = config.OpenVPNConfigStrategyUserSelector |
| 531 |
conf.OpenVPN.ClientConfig.Expression = `["not found"]` |
| 532 |
conf.OpenVPN.ClientConfig.Path = types.FS{ |
| 533 |
FS: fstest.MapFS{ |
| 534 |
"static.conf": &fstest.MapFile{ |
| 535 |
Data: []byte("push \"ping 60\"\npush \"ping-restart 180\"\r\npush \"ping-timer-rem\" 0\r\n"), |
| 536 |
}, |
| 537 |
}, |
| 538 |
} |
| 539 |
|
| 540 |
return conf |
| 541 |
}(), |
| 542 |
state.State{Client: state.ClientIdentifier{CID: 0, KID: 1, CommonName: "client"}, IPAddr: "127.0.0.2", IPPort: "12345"}, |
| 543 |
false, |
| 544 |
"127.0.0.2", |
| 545 |
true, |
| 546 |
true, |
| 547 |
}, |
| 548 |
{ |
| 549 |
"with client config selector + multiple static values", |
| 550 |
func() config.Config { |
| 551 |
conf := config.Defaults |
| 552 |
conf.HTTP.Secret = testsuite.Secret |
| 553 |
conf.HTTP.Check.IPAddr = true |
| 554 |
conf.HTTP.EnableProxyHeaders = true |
| 555 |
conf.HTTP.TrustedProxies = types.StringSlice{"127.0.0.1/32"} |
| 556 |
conf.OAuth2.Endpoints = config.OAuth2Endpoints{} |
| 557 |
conf.OAuth2.Scopes = []string{oauth2types.ScopeOpenID, oauth2types.ScopeProfile} |
| 558 |
conf.OpenVPN.Bypass.CommonNames = make(types.RegexpSlice, 0) |
| 559 |
conf.OpenVPN.AuthTokenUser = true |
| 560 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 561 |
conf.OpenVPN.ClientConfig.Enabled = true |
| 562 |
conf.OpenVPN.ClientConfig.Strategy = config.OpenVPNConfigStrategyUserSelector |
| 563 |
conf.OpenVPN.ClientConfig.Expression = `["group1", "group2"]` |
| 564 |
conf.OpenVPN.ClientConfig.Path = types.FS{ |
| 565 |
FS: fstest.MapFS{ |
| 566 |
"group2.conf": &fstest.MapFile{ |
| 567 |
Data: []byte("push \"ping 60\"\npush \"ping-restart 180\"\r\npush \"ping-timer-rem\" 0\r\n"), |
| 568 |
}, |
| 569 |
}, |
| 570 |
} |
| 571 |
|
| 572 |
return conf |
| 573 |
}(), |
| 574 |
state.State{Client: state.ClientIdentifier{CID: 0, KID: 1, CommonName: "name"}, IPAddr: "127.0.0.2", IPPort: "12345"}, |
| 575 |
false, |
| 576 |
"127.0.0.2", |
| 577 |
true, |
| 578 |
true, |
| 579 |
}, |
| 580 |
{ |
| 581 |
"with empty state", |
| 582 |
func() config.Config { |
| 583 |
conf := config.Defaults |
| 584 |
conf.HTTP.Secret = testsuite.Secret |
| 585 |
conf.HTTP.Check.IPAddr = true |
| 586 |
conf.HTTP.EnableProxyHeaders = true |
| 587 |
conf.HTTP.TrustedProxies = types.StringSlice{"127.0.0.1/32"} |
| 588 |
conf.OAuth2.Endpoints = config.OAuth2Endpoints{} |
| 589 |
conf.OAuth2.Scopes = []string{oauth2types.ScopeOpenID, oauth2types.ScopeProfile} |
| 590 |
conf.OpenVPN.Bypass.CommonNames = make(types.RegexpSlice, 0) |
| 591 |
conf.OpenVPN.AuthTokenUser = true |
| 592 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 593 |
|
| 594 |
return conf |
| 595 |
}(), |
| 596 |
state.State{}, |
| 597 |
false, |
| 598 |
"127.0.0.1", |
| 599 |
true, |
| 600 |
true, |
| 601 |
}, |
| 602 |
{ |
| 603 |
"with invalid state", |
| 604 |
func() config.Config { |
| 605 |
conf := config.Defaults |
| 606 |
conf.HTTP.Secret = testsuite.Secret |
| 607 |
conf.HTTP.Check.IPAddr = true |
| 608 |
conf.HTTP.EnableProxyHeaders = true |
| 609 |
conf.HTTP.TrustedProxies = types.StringSlice{"127.0.0.1/32"} |
| 610 |
conf.OAuth2.Endpoints = config.OAuth2Endpoints{} |
| 611 |
conf.OAuth2.Scopes = []string{oauth2types.ScopeOpenID, oauth2types.ScopeProfile} |
| 612 |
conf.OpenVPN.Bypass.CommonNames = make(types.RegexpSlice, 0) |
| 613 |
conf.OpenVPN.AuthTokenUser = true |
| 614 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 615 |
|
| 616 |
return conf |
| 617 |
}(), |
| 618 |
state.State{}, |
| 619 |
true, |
| 620 |
"127.0.0.1", |
| 621 |
true, |
| 622 |
true, |
| 623 |
}, |
| 624 |
} { |
| 625 |
t.Run(tc.name, func(t *testing.T) { |
| 626 |
t.Parallel() |
| 627 |
|
| 628 |
ctx, cancel := context.WithCancel(t.Context()) |
| 629 |
t.Cleanup(cancel) |
| 630 |
|
| 631 |
suite := testsuite.New(&tc.conf) |
| 632 |
suite.SetupMockEnvironment(ctx, t, nil) |
| 633 |
suite.ExpectVersionAndReleaseHold(t) |
| 634 |
|
| 635 |
httpClient := suite.GetHTTPClient() |
| 636 |
httpClient.CheckRedirect = func(_ *http.Request, _ []*http.Request) error { |
| 637 |
return http.ErrUseLastResponse |
| 638 |
} |
| 639 |
|
| 640 |
resp, _, err := suite.DoHTTPRequest(t, http.MethodGet, "/ready", nil, http.NoBody) //nolint:bodyclose |
| 641 |
require.NoError(t, err) |
| 642 |
require.Equal(t, http.StatusOK, resp.StatusCode) |
| 643 |
|
| 644 |
var session string |
| 645 |
|
| 646 |
switch { |
| 647 |
case tc.invalidState: |
| 648 |
session = invalid |
| 649 |
case tc.state == (state.State{}): |
| 650 |
session = "" |
| 651 |
default: |
| 652 |
session, err = state.Encrypt(testsuite.NewCipher(t), tc.state) |
| 653 |
require.NoError(t, err) |
| 654 |
} |
| 655 |
|
| 656 |
urlPath := "/oauth2/start?state=" + session |
| 657 |
|
| 658 |
header := make(http.Header) |
| 659 |
|
| 660 |
if tc.xForwardedFor != "" { |
| 661 |
header.Set("X-Forwarded-For", tc.xForwardedFor) |
| 662 |
} |
| 663 |
|
| 664 |
reqErrCh := make(chan error, 1) |
| 665 |
|
| 666 |
go func() { |
| 667 |
var err error |
| 668 |
|
| 669 |
resp, _, err = suite.DoHTTPRequest(t, http.MethodGet, urlPath, header, http.NoBody) //nolint:bodyclose |
| 670 |
|
| 671 |
reqErrCh <- err |
| 672 |
}() |
| 673 |
|
| 674 |
if !tc.preAllow { |
| 675 |
suite.ExpectMessage(t, `client-deny 0 1 "client rejected: http client ip 127.0.0.1 and vpn ip 127.0.0.2 is different"`) |
| 676 |
suite.SendMessagef(t, `SUCCESS: client-deny command succeeded`) |
| 677 |
} |
| 678 |
|
| 679 |
select { |
| 680 |
case err := <-reqErrCh: |
| 681 |
require.NoError(t, err) |
| 682 |
case <-time.After(1 * time.Second): |
| 683 |
t.Fatalf("timeout waiting for request to finish. Logs:\n\n%s", suite.Logs()) |
| 684 |
} |
| 685 |
|
| 686 |
if tc.state == (state.State{}) { |
| 687 |
require.Equal(t, http.StatusBadRequest, resp.StatusCode, suite.Logs()) |
| 688 |
|
| 689 |
return |
| 690 |
} |
| 691 |
|
| 692 |
if !tc.preAllow { |
| 693 |
require.Equal(t, http.StatusForbidden, resp.StatusCode, suite.Logs()) |
| 694 |
|
| 695 |
return |
| 696 |
} |
| 697 |
|
| 698 |
require.Equal(t, http.StatusFound, resp.StatusCode, suite.Logs()) |
| 699 |
require.NotEmpty(t, resp.Header.Get("Location"), "Location header is empty") |
| 700 |
require.NotEmpty(t, resp.Header.Get("Set-Cookie"), "Set-Cookie header is empty") |
| 701 |
require.Contains(t, resp.Header.Get("Set-Cookie"), "state=") |
| 702 |
require.Contains(t, resp.Header.Get("Set-Cookie"), "Path=/oauth2/") |
| 703 |
require.Contains(t, resp.Header.Get("Set-Cookie"), "HttpOnly") |
| 704 |
require.Contains(t, resp.Header.Get("Set-Cookie"), "Max-Age=185") |
| 705 |
|
| 706 |
httpClient.CheckRedirect = nil |
| 707 |
|
| 708 |
var body []byte |
| 709 |
|
| 710 |
go func() { |
| 711 |
var err error |
| 712 |
|
| 713 |
resp, body, err = suite.DoHTTPRequest(t, http.MethodGet, resp.Header.Get("Location"), header, http.NoBody) //nolint:bodyclose |
| 714 |
reqErrCh <- err |
| 715 |
}() |
| 716 |
|
| 717 |
clientConfigSelectorActive := tc.conf.OpenVPN.ClientConfig.Enabled && |
| 718 |
tc.conf.OpenVPN.ClientConfig.Strategy == config.OpenVPNConfigStrategyUserSelector && |
| 719 |
(strings.Contains(tc.conf.OpenVPN.ClientConfig.Expression, ",") || |
| 720 |
strings.Contains(tc.conf.OpenVPN.ClientConfig.Expression, "+ token.claims")) |
| 721 |
|
| 722 |
switch { |
| 723 |
case !tc.postAllow: |
| 724 |
suite.ExpectMessage(t, `client-deny 0 1 "client rejected"`) |
| 725 |
suite.SendMessagef(t, "SUCCESS: client-deny command succeeded") |
| 726 |
case tc.state.Client.UsernameIsDefined == 1: |
| 727 |
suite.ExpectMessage(t, "client-auth-nt 0 1") |
| 728 |
suite.SendMessagef(t, "SUCCESS: client-auth command succeeded") |
| 729 |
case clientConfigSelectorActive: |
| 730 |
// Expect profile selection |
| 731 |
case tc.conf.OpenVPN.ClientConfig.Enabled: |
| 732 |
if tc.state.Client.CommonName == "name" { |
| 733 |
suite.ExpectMessage(t, "client-auth 0 1\r\n"+ |
| 734 |
"push \"ping 60\"\r\n"+ |
| 735 |
"push \"ping-restart 180\"\r\n"+ |
| 736 |
"push \"ping-timer-rem\" 0\r\n"+ |
| 737 |
"push \"auth-token-user aWQx\"\r\n"+ |
| 738 |
"END") |
| 739 |
suite.SendMessagef(t, "SUCCESS: client-auth command succeeded") |
| 740 |
} else { |
| 741 |
suite.ExpectMessage(t, "client-auth 0 1\r\npush \"auth-token-user Y2xpZW50\"\r\nEND") |
| 742 |
suite.SendMessagef(t, "SUCCESS: client-auth command succeeded") |
| 743 |
} |
| 744 |
default: |
| 745 |
suite.ExpectMessage(t, "client-auth 0 1\r\npush \"auth-token-user aWQx\"\r\nEND") |
| 746 |
suite.SendMessagef(t, "SUCCESS: client-auth command succeeded") |
| 747 |
} |
| 748 |
|
| 749 |
select { |
| 750 |
case err := <-reqErrCh: |
| 751 |
require.NoError(t, err) |
| 752 |
case <-time.After(1 * time.Second): |
| 753 |
t.Fatalf("timeout waiting for request to finish. Logs:\n\n%s", suite.Logs()) |
| 754 |
} |
| 755 |
|
| 756 |
if !tc.postAllow { |
| 757 |
require.Equal(t, http.StatusForbidden, resp.StatusCode, suite.Logs(), string(body)) |
| 758 |
|
| 759 |
return |
| 760 |
} |
| 761 |
|
| 762 |
require.Equal(t, http.StatusOK, resp.StatusCode, suite.Logs(), string(body)) |
| 763 |
|
| 764 |
require.NotEmpty(t, resp.Header.Get("Set-Cookie")) |
| 765 |
require.Contains(t, resp.Header.Get("Set-Cookie"), "state=") |
| 766 |
require.Contains(t, resp.Header.Get("Set-Cookie"), "Path=/oauth2/") |
| 767 |
require.Contains(t, resp.Header.Get("Set-Cookie"), "HttpOnly") |
| 768 |
require.Contains(t, resp.Header.Get("Set-Cookie"), "Max-Age=0") |
| 769 |
|
| 770 |
switch { |
| 771 |
case clientConfigSelectorActive: |
| 772 |
require.Contains(t, string(body), "Please select your client configuration profile") |
| 773 |
|
| 774 |
reInput := regexp.MustCompile(`type="(?:hidden|submit)" name="([^"]+)" value="([^"]+)">`) |
| 775 |
|
| 776 |
matches := reInput.FindAllStringSubmatch(string(body), -1) |
| 777 |
require.NotEmpty(t, matches, "no input fields found in profile selection form") |
| 778 |
|
| 779 |
fields := make(map[string]string) |
| 780 |
|
| 781 |
for _, match := range matches { |
| 782 |
require.Len(t, match, 3, string(body)) |
| 783 |
|
| 784 |
fields[match[1]] = match[2] |
| 785 |
} |
| 786 |
|
| 787 |
require.Contains(t, fields, "token") |
| 788 |
|
| 789 |
header := make(http.Header) |
| 790 |
header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 791 |
|
| 792 |
var body []byte |
| 793 |
|
| 794 |
go func() { |
| 795 |
var err error |
| 796 |
|
| 797 |
//nolint:bodyclose |
| 798 |
resp, body, err = suite.DoHTTPRequest(t, http.MethodPost, "/oauth2/profile-submit", header, strings.NewReader(fmt.Sprintf( |
| 799 |
"token=%s&profile=%s", |
| 800 |
url.QueryEscape(fields["token"]), |
| 801 |
url.QueryEscape(fields["profile"]), |
| 802 |
))) |
| 803 |
|
| 804 |
reqErrCh <- err |
| 805 |
}() |
| 806 |
|
| 807 |
suite.ExpectMessage(t, "client-auth 0 1\r\n"+ |
| 808 |
"push \"ping 60\"\r\n"+ |
| 809 |
"push \"ping-restart 180\"\r\n"+ |
| 810 |
"push \"ping-timer-rem\" 0\r\n"+ |
| 811 |
"push \"auth-token-user aWQx\"\r\n"+ |
| 812 |
"END") |
| 813 |
suite.SendMessagef(t, "SUCCESS: client-auth command succeeded") |
| 814 |
|
| 815 |
select { |
| 816 |
case err := <-reqErrCh: |
| 817 |
require.NoError(t, err) |
| 818 |
case <-time.After(1 * time.Second): |
| 819 |
t.Fatalf("timeout waiting for request to finish. Logs:\n\n%s", suite.Logs()) |
| 820 |
} |
| 821 |
|
| 822 |
require.Equal(t, http.StatusOK, resp.StatusCode, suite.Logs()) |
| 823 |
require.Contains(t, string(body), "Access granted") |
| 824 |
|
| 825 |
//nolint:bodyclose |
| 826 |
replayResp, replayBody, err := suite.DoHTTPRequest(t, http.MethodPost, "/oauth2/profile-submit", header, strings.NewReader(fmt.Sprintf( |
| 827 |
"token=%s&profile=%s", |
| 828 |
url.QueryEscape(fields["token"]), |
| 829 |
url.QueryEscape(fields["profile"]), |
| 830 |
))) |
| 831 |
require.NoError(t, err) |
| 832 |
require.Equal(t, http.StatusBadRequest, replayResp.StatusCode, suite.Logs(), string(replayBody)) |
| 833 |
case tc.conf.HTTP.Template != config.Defaults.HTTP.Template: |
| 834 |
require.Contains(t, string(body), "Permission is hereby granted") |
| 835 |
default: |
| 836 |
require.Contains(t, string(body), "Access granted") |
| 837 |
} |
| 838 |
}) |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
func TestOAuth2ProfileSubmit(t *testing.T) { |
| 843 |
t.Parallel() |
| 844 |
|
| 845 |
for _, tc := range []struct { |
| 846 |
name string |
| 847 |
conf config.Config |
| 848 |
req func(t *testing.T) *http.Request |
| 849 |
expectedError string |
| 850 |
}{ |
| 851 |
{ |
| 852 |
"missing token", |
| 853 |
config.Defaults, |
| 854 |
func(t *testing.T) *http.Request { |
| 855 |
t.Helper() |
| 856 |
|
| 857 |
req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, "/oauth2/profile-submit", nil) |
| 858 |
require.NoError(t, err) |
| 859 |
|
| 860 |
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 861 |
|
| 862 |
return req |
| 863 |
}, |
| 864 |
"token is empty", |
| 865 |
}, |
| 866 |
{ |
| 867 |
"invalid encrypted token", |
| 868 |
config.Defaults, |
| 869 |
func(t *testing.T) *http.Request { |
| 870 |
t.Helper() |
| 871 |
|
| 872 |
req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, "/oauth2/profile-submit", |
| 873 |
strings.NewReader("token="+url.QueryEscape("-"))) |
| 874 |
|
| 875 |
require.NoError(t, err) |
| 876 |
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 877 |
|
| 878 |
return req |
| 879 |
}, |
| 880 |
"illegal base64 data at input", |
| 881 |
}, |
| 882 |
{ |
| 883 |
"invalid token content", |
| 884 |
config.Defaults, |
| 885 |
|
| 886 |
func(t *testing.T) *http.Request { |
| 887 |
t.Helper() |
| 888 |
|
| 889 |
token := fmt.Sprintf("%d -", time.Now().Unix()) |
| 890 |
|
| 891 |
encryptedToken, err := testsuite.NewCipher(t).EncryptBytes([]byte(token)) |
| 892 |
require.NoError(t, err) |
| 893 |
|
| 894 |
req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, "/oauth2/profile-submit", |
| 895 |
strings.NewReader("token="+url.QueryEscape(base64.RawURLEncoding.EncodeToString(encryptedToken)))) |
| 896 |
require.NoError(t, err) |
| 897 |
|
| 898 |
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 899 |
|
| 900 |
return req |
| 901 |
}, |
| 902 |
"unable to parse token", |
| 903 |
}, |
| 904 |
{ |
| 905 |
"empty token content", |
| 906 |
config.Defaults, |
| 907 |
func(t *testing.T) *http.Request { |
| 908 |
t.Helper() |
| 909 |
|
| 910 |
token := fmt.Sprintf(`%d {}`, time.Now().Unix()) |
| 911 |
|
| 912 |
encryptedToken, err := testsuite.NewCipher(t).EncryptBytes([]byte(token)) |
| 913 |
require.NoError(t, err) |
| 914 |
|
| 915 |
req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, "/oauth2/profile-submit", |
| 916 |
strings.NewReader("token="+url.QueryEscape(base64.RawURLEncoding.EncodeToString(encryptedToken)))) |
| 917 |
|
| 918 |
require.NoError(t, err) |
| 919 |
|
| 920 |
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 921 |
|
| 922 |
return req |
| 923 |
}, |
| 924 |
"Invalid State: decrypt state: ciphertext block size is too short", |
| 925 |
}, |
| 926 |
{ |
| 927 |
"no refresh token in storage", |
| 928 |
config.Defaults, |
| 929 |
func(t *testing.T) *http.Request { |
| 930 |
t.Helper() |
| 931 |
|
| 932 |
sessionState := state.State{Client: state.ClientIdentifier{CID: 1, KID: 2}, IPAddr: "127.0.0.1", IPPort: "12345"} |
| 933 |
|
| 934 |
encryptedState, err := state.Encrypt(testsuite.NewCipher(t), sessionState) |
| 935 |
require.NoError(t, err) |
| 936 |
|
| 937 |
token := fmt.Sprintf(`%d {"state": %q}`, time.Now().Unix(), encryptedState) |
| 938 |
|
| 939 |
encryptedToken, err := testsuite.NewCipher(t).EncryptBytes([]byte(token)) |
| 940 |
require.NoError(t, err) |
| 941 |
|
| 942 |
req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, "/oauth2/profile-submit", |
| 943 |
strings.NewReader("token="+url.QueryEscape(base64.RawURLEncoding.EncodeToString(encryptedToken)))) |
| 944 |
require.NoError(t, err) |
| 945 |
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 946 |
|
| 947 |
return req |
| 948 |
}, |
| 949 |
"unable to retrieve refresh token from storage: value does not exist", |
| 950 |
}, |
| 951 |
} { |
| 952 |
t.Run(tc.name, func(t *testing.T) { |
| 953 |
t.Parallel() |
| 954 |
|
| 955 |
ctx, cancel := context.WithCancel(t.Context()) |
| 956 |
t.Cleanup(cancel) |
| 957 |
|
| 958 |
suite := testsuite.New(&tc.conf) |
| 959 |
suite.SetupMockEnvironment(ctx, t, nil) |
| 960 |
suite.ExpectVersionAndReleaseHold(t) |
| 961 |
|
| 962 |
httpClient := suite.GetHTTPClient() |
| 963 |
|
| 964 |
request := tc.req(t) |
| 965 |
request.URL.Host = strings.Replace(suite.GetHTTPServerURL(), "http://", "", 1) |
| 966 |
request.URL.Scheme = "http" |
| 967 |
|
| 968 |
resp, err := httpClient.Do(request) |
| 969 |
require.NoError(t, err) |
| 970 |
|
| 971 |
require.Equal(t, http.StatusBadRequest, resp.StatusCode) |
| 972 |
require.Contains(t, suite.Logs(), tc.expectedError) |
| 973 |
|
| 974 |
_, err = io.Copy(io.Discard, resp.Body) |
| 975 |
require.NoError(t, err) |
| 976 |
|
| 977 |
err = resp.Body.Close() |
| 978 |
require.NoError(t, err) |
| 979 |
}) |
| 980 |
} |
| 981 |
} |
| 982 |
|
| 983 |
func TestOAuth2Callback(t *testing.T) { |
| 984 |
t.Parallel() |
| 985 |
|
| 986 |
for _, tc := range []struct { |
| 987 |
name string |
| 988 |
conf config.Config |
| 989 |
req func(t *testing.T) *http.Request |
| 990 |
expectedStatus int |
| 991 |
expectedError string |
| 992 |
}{ |
| 993 |
{ |
| 994 |
"empty state", |
| 995 |
config.Defaults, |
| 996 |
func(t *testing.T) *http.Request { |
| 997 |
t.Helper() |
| 998 |
|
| 999 |
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "/oauth2/callback", nil) |
| 1000 |
require.NoError(t, err) |
| 1001 |
|
| 1002 |
return req |
| 1003 |
}, |
| 1004 |
http.StatusBadRequest, |
| 1005 |
"state is empty", |
| 1006 |
}, |
| 1007 |
{ |
| 1008 |
"invalid state", |
| 1009 |
config.Defaults, |
| 1010 |
func(t *testing.T) *http.Request { |
| 1011 |
t.Helper() |
| 1012 |
|
| 1013 |
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "/oauth2/callback?state=invalid*", nil) |
| 1014 |
require.NoError(t, err) |
| 1015 |
|
| 1016 |
return req |
| 1017 |
}, |
| 1018 |
http.StatusBadRequest, |
| 1019 |
"illegal base64 data", |
| 1020 |
}, |
| 1021 |
{ |
| 1022 |
"invalid state", |
| 1023 |
config.Defaults, |
| 1024 |
func(t *testing.T) *http.Request { |
| 1025 |
t.Helper() |
| 1026 |
|
| 1027 |
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "/oauth2/callback?state="+base64.RawURLEncoding.EncodeToString([]byte("invalid")), nil) |
| 1028 |
require.NoError(t, err) |
| 1029 |
|
| 1030 |
return req |
| 1031 |
}, |
| 1032 |
http.StatusBadRequest, |
| 1033 |
"ciphertext block size is too short", |
| 1034 |
}, |
| 1035 |
} { |
| 1036 |
t.Run(tc.name, func(t *testing.T) { |
| 1037 |
t.Parallel() |
| 1038 |
|
| 1039 |
ctx, cancel := context.WithCancel(t.Context()) |
| 1040 |
t.Cleanup(cancel) |
| 1041 |
|
| 1042 |
suite := testsuite.New(&tc.conf) |
| 1043 |
suite.SetupMockEnvironment(ctx, t, nil) |
| 1044 |
suite.ExpectVersionAndReleaseHold(t) |
| 1045 |
|
| 1046 |
httpClient := suite.GetHTTPClient() |
| 1047 |
|
| 1048 |
request := tc.req(t) |
| 1049 |
request.URL.Host = strings.Replace(suite.GetHTTPServerURL(), "http://", "", 1) |
| 1050 |
request.URL.Scheme = "http" |
| 1051 |
|
| 1052 |
resp, err := httpClient.Do(request) |
| 1053 |
require.NoError(t, err) |
| 1054 |
|
| 1055 |
require.Equal(t, tc.expectedStatus, resp.StatusCode) |
| 1056 |
require.Contains(t, suite.Logs(), tc.expectedError) |
| 1057 |
|
| 1058 |
_, err = io.Copy(io.Discard, resp.Body) |
| 1059 |
require.NoError(t, err) |
| 1060 |
|
| 1061 |
err = resp.Body.Close() |
| 1062 |
require.NoError(t, err) |
| 1063 |
}) |
| 1064 |
} |
| 1065 |
} |