| 1 |
package oauth2_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"encoding/json" |
| 7 |
"fmt" |
| 8 |
"io" |
| 9 |
"net/http" |
| 10 |
"net/http/httptest" |
| 11 |
"strings" |
| 12 |
"sync" |
| 13 |
"testing" |
| 14 |
"testing/fstest" |
| 15 |
"time" |
| 16 |
|
| 17 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/config" |
| 18 |
configtypes "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/config/types" |
| 19 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/providers/generic" |
| 20 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/providers/github" |
| 21 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/providers/google" |
| 22 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/types" |
| 23 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/openvpn" |
| 24 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/openvpn/connection" |
| 25 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/test/testsuite" |
| 26 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/tokenstorage" |
| 27 |
"github.com/stretchr/testify/assert" |
| 28 |
"github.com/stretchr/testify/require" |
| 29 |
"github.com/zitadel/oidc/v3/pkg/oidc" |
| 30 |
"github.com/zitadel/oidc/v3/pkg/op" |
| 31 |
) |
| 32 |
|
| 33 |
func configTypesFS(files map[string][]byte) configtypes.FS { |
| 34 |
mapFS := make(fstest.MapFS, len(files)) |
| 35 |
for name, data := range files { |
| 36 |
mapFS[name] = &fstest.MapFile{Data: data} |
| 37 |
} |
| 38 |
|
| 39 |
return configtypes.FS{FS: mapFS} |
| 40 |
} |
| 41 |
|
| 42 |
func expectProfileClientAuth(t *testing.T, suite *testsuite.Suite, cid, kid uint64) { |
| 43 |
t.Helper() |
| 44 |
|
| 45 |
suite.ExpectMessage(t, fmt.Sprintf("client-auth %d %d", cid, kid)) |
| 46 |
suite.ExpectMessage(t, `push "route 10.8.0.0 255.255.0.0"`) |
| 47 |
suite.ExpectMessage(t, "END") |
| 48 |
} |
| 49 |
|
| 50 |
type disconnectTokenStorage struct { |
| 51 |
getCount int |
| 52 |
} |
| 53 |
|
| 54 |
func (s *disconnectTokenStorage) Get(_ context.Context, _ string) (string, error) { |
| 55 |
s.getCount++ |
| 56 |
|
| 57 |
return "", tokenstorage.ErrNotExists |
| 58 |
} |
| 59 |
|
| 60 |
func (s *disconnectTokenStorage) Consume(_ context.Context, _ string) (string, error) { |
| 61 |
return "", tokenstorage.ErrNotExists |
| 62 |
} |
| 63 |
|
| 64 |
func (s *disconnectTokenStorage) Set(_ context.Context, _, _ string) error { |
| 65 |
return nil |
| 66 |
} |
| 67 |
|
| 68 |
func (s *disconnectTokenStorage) Delete(_ context.Context, _ string) error { |
| 69 |
return nil |
| 70 |
} |
| 71 |
|
| 72 |
func (s *disconnectTokenStorage) Close() error { |
| 73 |
return nil |
| 74 |
} |
| 75 |
|
| 76 |
func TestClientDisconnectRefreshDisabledSkipsTokenStorage(t *testing.T) { |
| 77 |
t.Parallel() |
| 78 |
|
| 79 |
ctx, cancel := context.WithCancel(t.Context()) |
| 80 |
t.Cleanup(cancel) |
| 81 |
|
| 82 |
conf := config.Defaults |
| 83 |
conf.OAuth2.Refresh.Enabled = false |
| 84 |
|
| 85 |
storage := &disconnectTokenStorage{} |
| 86 |
suite := testsuite.New(&conf) |
| 87 |
oAuth2Client, _ := suite.SetupOpenVPNOAuth2Clients(ctx, t, storage) |
| 88 |
|
| 89 |
oAuth2Client.ClientDisconnect(ctx, suite.GetLogger(), connection.Client{CID: 7}) |
| 90 |
|
| 91 |
assert.Equal(t, 0, storage.getCount) |
| 92 |
assert.NotContains(t, suite.Logs(), "error from token store") |
| 93 |
} |
| 94 |
|
| 95 |
func TestRefreshReAuth(t *testing.T) { |
| 96 |
t.Parallel() |
| 97 |
|
| 98 |
var refreshToken string |
| 99 |
|
| 100 |
for _, tc := range []struct { |
| 101 |
name string |
| 102 |
clientCommonName string |
| 103 |
nonInteractiveShouldWork bool |
| 104 |
conf config.Config |
| 105 |
rt http.RoundTripper |
| 106 |
opConf *op.Config |
| 107 |
}{ |
| 108 |
{ |
| 109 |
name: "Refresh", |
| 110 |
clientCommonName: "test", |
| 111 |
nonInteractiveShouldWork: true, |
| 112 |
conf: func() config.Config { |
| 113 |
conf := config.Defaults |
| 114 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 115 |
conf.OAuth2.Refresh.Enabled = true |
| 116 |
conf.OAuth2.Refresh.ValidateUser = true |
| 117 |
conf.OAuth2.Refresh.UseSessionID = false |
| 118 |
|
| 119 |
return conf |
| 120 |
}(), |
| 121 |
rt: http.DefaultTransport, |
| 122 |
}, |
| 123 |
{ |
| 124 |
name: "ReAuthentication disabled", |
| 125 |
clientCommonName: "test", |
| 126 |
nonInteractiveShouldWork: true, |
| 127 |
conf: func() config.Config { |
| 128 |
conf := config.Defaults |
| 129 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 130 |
conf.OpenVPN.ReAuthentication = false |
| 131 |
|
| 132 |
return conf |
| 133 |
}(), |
| 134 |
rt: http.DefaultTransport, |
| 135 |
}, |
| 136 |
{ |
| 137 |
name: "Refresh with empty common name", |
| 138 |
clientCommonName: "", |
| 139 |
nonInteractiveShouldWork: true, |
| 140 |
conf: func() config.Config { |
| 141 |
conf := config.Defaults |
| 142 |
conf.OAuth2.OpenVPNUsername = "" |
| 143 |
conf.OpenVPN.AuthTokenUser = true |
| 144 |
conf.OAuth2.Refresh.Enabled = true |
| 145 |
conf.OAuth2.Refresh.ValidateUser = true |
| 146 |
conf.OAuth2.Refresh.UseSessionID = false |
| 147 |
|
| 148 |
return conf |
| 149 |
}(), |
| 150 |
rt: http.DefaultTransport, |
| 151 |
}, |
| 152 |
{ |
| 153 |
name: "Refresh with OverrideUsername=true", |
| 154 |
clientCommonName: "test", |
| 155 |
nonInteractiveShouldWork: true, |
| 156 |
conf: func() config.Config { |
| 157 |
conf := config.Defaults |
| 158 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 159 |
conf.OpenVPN.AuthTokenUser = true |
| 160 |
conf.OpenVPN.OverrideUsername = true |
| 161 |
conf.OAuth2.Refresh.Enabled = true |
| 162 |
conf.OAuth2.Refresh.ValidateUser = true |
| 163 |
conf.OAuth2.Refresh.UseSessionID = false |
| 164 |
|
| 165 |
return conf |
| 166 |
}(), |
| 167 |
rt: http.DefaultTransport, |
| 168 |
}, |
| 169 |
{ |
| 170 |
name: "Refresh with ValidateUser=false", |
| 171 |
clientCommonName: "test", |
| 172 |
nonInteractiveShouldWork: true, |
| 173 |
conf: func() config.Config { |
| 174 |
conf := config.Defaults |
| 175 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 176 |
conf.OAuth2.Refresh.Enabled = true |
| 177 |
conf.OAuth2.Refresh.ValidateUser = false |
| 178 |
conf.OAuth2.Refresh.UseSessionID = false |
| 179 |
|
| 180 |
return conf |
| 181 |
}(), |
| 182 |
rt: http.DefaultTransport, |
| 183 |
}, |
| 184 |
{ |
| 185 |
name: "Refresh with ValidateUser=false and client config", |
| 186 |
clientCommonName: "test", |
| 187 |
nonInteractiveShouldWork: true, |
| 188 |
conf: func() config.Config { |
| 189 |
conf := config.Defaults |
| 190 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 191 |
conf.OAuth2.Refresh.Enabled = true |
| 192 |
conf.OAuth2.Refresh.ValidateUser = false |
| 193 |
conf.OAuth2.Refresh.UseSessionID = false |
| 194 |
conf.OpenVPN.ClientConfig.Enabled = true |
| 195 |
conf.OpenVPN.ClientConfig.Expression = `["profile"]` |
| 196 |
conf.OpenVPN.ClientConfig.Path = configTypesFS(map[string][]byte{ |
| 197 |
"profile.conf": []byte(`push "route 10.8.0.0 255.255.0.0"`), |
| 198 |
}) |
| 199 |
|
| 200 |
return conf |
| 201 |
}(), |
| 202 |
rt: http.DefaultTransport, |
| 203 |
}, |
| 204 |
{ |
| 205 |
name: "Refresh with UserInfo=true", |
| 206 |
clientCommonName: "test", |
| 207 |
nonInteractiveShouldWork: true, |
| 208 |
conf: func() config.Config { |
| 209 |
conf := config.Defaults |
| 210 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 211 |
conf.OAuth2.Validate.Groups = []string{"group1"} |
| 212 |
conf.OAuth2.UserInfo = true |
| 213 |
conf.OAuth2.Refresh.Enabled = true |
| 214 |
|
| 215 |
return conf |
| 216 |
}(), |
| 217 |
rt: http.DefaultTransport, |
| 218 |
}, |
| 219 |
{ |
| 220 |
name: "Refresh with SessionID=true + ValidateUser=false", |
| 221 |
clientCommonName: "test", |
| 222 |
nonInteractiveShouldWork: true, |
| 223 |
conf: func() config.Config { |
| 224 |
conf := config.Defaults |
| 225 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 226 |
conf.OpenVPN.AuthTokenUser = false |
| 227 |
conf.OAuth2.Refresh.Enabled = true |
| 228 |
conf.OAuth2.Refresh.ValidateUser = false |
| 229 |
conf.OAuth2.Refresh.UseSessionID = true |
| 230 |
|
| 231 |
return conf |
| 232 |
}(), |
| 233 |
rt: http.DefaultTransport, |
| 234 |
}, |
| 235 |
{ |
| 236 |
name: "Refresh with SessionID=true + ValidateUser=true", |
| 237 |
clientCommonName: "test", |
| 238 |
nonInteractiveShouldWork: true, |
| 239 |
conf: func() config.Config { |
| 240 |
conf := config.Defaults |
| 241 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 242 |
conf.OpenVPN.AuthTokenUser = false |
| 243 |
conf.OAuth2.Refresh.Enabled = true |
| 244 |
conf.OAuth2.Refresh.ValidateUser = true |
| 245 |
conf.OAuth2.Refresh.UseSessionID = true |
| 246 |
|
| 247 |
return conf |
| 248 |
}(), |
| 249 |
rt: http.DefaultTransport, |
| 250 |
}, |
| 251 |
{ |
| 252 |
name: "Refresh with provider=google", |
| 253 |
clientCommonName: "test", |
| 254 |
nonInteractiveShouldWork: true, |
| 255 |
conf: func() config.Config { |
| 256 |
conf := config.Defaults |
| 257 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 258 |
conf.OpenVPN.AuthTokenUser = false |
| 259 |
conf.OAuth2.Provider = google.Name |
| 260 |
conf.OAuth2.Scopes = []string{types.ScopeEmail, types.ScopeProfile, types.ScopeOpenID, types.ScopeOfflineAccess} |
| 261 |
conf.OAuth2.Refresh.Enabled = true |
| 262 |
conf.OAuth2.Refresh.ValidateUser = true |
| 263 |
conf.OAuth2.Refresh.UseSessionID = false |
| 264 |
|
| 265 |
return conf |
| 266 |
}(), |
| 267 |
rt: http.DefaultTransport, |
| 268 |
}, |
| 269 |
{ |
| 270 |
name: "Refresh with provider=github", |
| 271 |
clientCommonName: "test", |
| 272 |
nonInteractiveShouldWork: true, |
| 273 |
conf: func() config.Config { |
| 274 |
conf := config.Defaults |
| 275 |
conf.OAuth2.OpenVPNUsername = "user.username" |
| 276 |
conf.OpenVPN.AuthTokenUser = false |
| 277 |
conf.OAuth2.Provider = github.Name |
| 278 |
conf.OAuth2.Refresh.Enabled = true |
| 279 |
conf.OAuth2.Refresh.ValidateUser = true |
| 280 |
conf.OAuth2.Refresh.UseSessionID = false |
| 281 |
|
| 282 |
return conf |
| 283 |
}(), |
| 284 |
rt: http.DefaultTransport, |
| 285 |
}, |
| 286 |
{ |
| 287 |
name: "Refresh without server support", |
| 288 |
clientCommonName: "test", |
| 289 |
nonInteractiveShouldWork: false, |
| 290 |
conf: func() config.Config { |
| 291 |
conf := config.Defaults |
| 292 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 293 |
conf.OAuth2.Refresh.Enabled = true |
| 294 |
conf.OAuth2.Refresh.ValidateUser = true |
| 295 |
conf.OAuth2.Refresh.UseSessionID = false |
| 296 |
|
| 297 |
return conf |
| 298 |
}(), |
| 299 |
rt: http.DefaultTransport, |
| 300 |
opConf: &op.Config{ |
| 301 |
CryptoKey: testsuite.HashSecret, |
| 302 |
DefaultLogoutRedirectURI: "/", |
| 303 |
CodeMethodS256: true, |
| 304 |
AuthMethodPost: true, |
| 305 |
AuthMethodPrivateKeyJWT: true, |
| 306 |
GrantTypeRefreshToken: false, |
| 307 |
RequestObjectSupported: true, |
| 308 |
SupportedUILocales: testsuite.SupportedUILocales, |
| 309 |
SupportedScopes: []string{types.ScopeOpenID, types.ScopeProfile, types.ScopeOfflineAccess}, |
| 310 |
}, |
| 311 |
}, |
| 312 |
{ |
| 313 |
name: "Refresh with failed non-interactive authentication", |
| 314 |
clientCommonName: "test", |
| 315 |
nonInteractiveShouldWork: false, |
| 316 |
conf: func() config.Config { |
| 317 |
conf := config.Defaults |
| 318 |
conf.OAuth2.OpenVPNUsername = "user.username" |
| 319 |
conf.OpenVPN.AuthTokenUser = false |
| 320 |
conf.OAuth2.Provider = generic.Name |
| 321 |
conf.OAuth2.Refresh.Enabled = true |
| 322 |
conf.OAuth2.Refresh.ValidateUser = true |
| 323 |
conf.OAuth2.Refresh.UseSessionID = false |
| 324 |
|
| 325 |
return conf |
| 326 |
}(), |
| 327 |
rt: testsuite.NewRoundTripperFunc(http.DefaultTransport, func(rt http.RoundTripper, req *http.Request) (*http.Response, error) { |
| 328 |
if req.URL.Path != "/oauth/token" { |
| 329 |
return rt.RoundTrip(req) |
| 330 |
} |
| 331 |
|
| 332 |
requestBody, err := io.ReadAll(req.Body) |
| 333 |
if err != nil { |
| 334 |
return nil, err |
| 335 |
} |
| 336 |
|
| 337 |
// Initial request should work |
| 338 |
if strings.Contains(string(requestBody), `&code=`) { |
| 339 |
req.Body = io.NopCloser(bytes.NewReader(requestBody)) |
| 340 |
|
| 341 |
return rt.RoundTrip(req) |
| 342 |
} |
| 343 |
|
| 344 |
res := httptest.NewRecorder() |
| 345 |
res.WriteHeader(http.StatusInternalServerError) |
| 346 |
|
| 347 |
return res.Result(), nil |
| 348 |
}), |
| 349 |
}, |
| 350 |
{ |
| 351 |
name: "Refresh without returning refresh token", |
| 352 |
clientCommonName: "test", |
| 353 |
nonInteractiveShouldWork: true, |
| 354 |
conf: func() config.Config { |
| 355 |
conf := config.Defaults |
| 356 |
conf.OAuth2.OpenVPNUsername = "user.username" |
| 357 |
conf.OpenVPN.AuthTokenUser = false |
| 358 |
conf.OAuth2.Provider = generic.Name |
| 359 |
conf.OAuth2.Refresh.Enabled = true |
| 360 |
conf.OAuth2.Refresh.ValidateUser = true |
| 361 |
conf.OAuth2.Refresh.UseSessionID = false |
| 362 |
|
| 363 |
return conf |
| 364 |
}(), |
| 365 |
rt: testsuite.NewRoundTripperFunc(http.DefaultTransport, func(rt http.RoundTripper, req *http.Request) (*http.Response, error) { |
| 366 |
if req.URL.Path != "/oauth/token" { |
| 367 |
return rt.RoundTrip(req) |
| 368 |
} |
| 369 |
|
| 370 |
requestBody, err := io.ReadAll(req.Body) |
| 371 |
if err != nil { |
| 372 |
return nil, err |
| 373 |
} |
| 374 |
|
| 375 |
if refreshToken == "" { |
| 376 |
req.Body = io.NopCloser(bytes.NewReader(requestBody)) |
| 377 |
res, err := rt.RoundTrip(req) |
| 378 |
|
| 379 |
var tokenResponse oidc.AccessTokenResponse |
| 380 |
if err := json.NewDecoder(res.Body).Decode(&tokenResponse); err != nil { |
| 381 |
return nil, err |
| 382 |
} |
| 383 |
|
| 384 |
refreshToken = tokenResponse.RefreshToken |
| 385 |
|
| 386 |
var buf bytes.Buffer |
| 387 |
|
| 388 |
if err := json.NewEncoder(&buf).Encode(tokenResponse); err != nil { |
| 389 |
return nil, err |
| 390 |
} |
| 391 |
|
| 392 |
res.Body = io.NopCloser(&buf) |
| 393 |
|
| 394 |
return res, err |
| 395 |
} |
| 396 |
|
| 397 |
res := httptest.NewRecorder() |
| 398 |
if !strings.Contains(string(requestBody), refreshToken) { |
| 399 |
res.WriteHeader(http.StatusUnauthorized) |
| 400 |
} else { |
| 401 |
res.WriteHeader(http.StatusOK) |
| 402 |
} |
| 403 |
|
| 404 |
if _, err := res.WriteString(`{}`); err != nil { |
| 405 |
return nil, err |
| 406 |
} |
| 407 |
|
| 408 |
return res.Result(), nil |
| 409 |
}), |
| 410 |
}, |
| 411 |
{ |
| 412 |
name: "refresh with CEL denying non-interactive auth", |
| 413 |
clientCommonName: "test", |
| 414 |
nonInteractiveShouldWork: false, |
| 415 |
conf: func() config.Config { |
| 416 |
conf := config.Defaults |
| 417 |
conf.OAuth2.OpenVPNUsername = "token.claims." + testsuite.SubjectClaim |
| 418 |
conf.OpenVPN.AuthTokenUser = false |
| 419 |
conf.OAuth2.Provider = generic.Name |
| 420 |
conf.OAuth2.Refresh.Enabled = true |
| 421 |
conf.OAuth2.Refresh.ValidateUser = true |
| 422 |
conf.OAuth2.Refresh.UseSessionID = false |
| 423 |
conf.OAuth2.Validate.Expression = "auth.mode == 'interactive'" |
| 424 |
|
| 425 |
return conf |
| 426 |
}(), |
| 427 |
rt: http.DefaultTransport, |
| 428 |
}, |
| 429 |
} { |
| 430 |
t.Run(tc.name, func(t *testing.T) { |
| 431 |
t.Parallel() |
| 432 |
|
| 433 |
ctx, cancel := context.WithCancel(t.Context()) |
| 434 |
t.Cleanup(cancel) |
| 435 |
|
| 436 |
suite := testsuite.New(&tc.conf, testsuite.WithHTTPTransport(tc.rt)) |
| 437 |
errOpenVPNClientCh := suite.SetupMockEnvironment(ctx, t, tc.opConf) |
| 438 |
conf := suite.GetConfig() |
| 439 |
httpClient := suite.GetHTTPClient() |
| 440 |
suite.ExpectVersionAndReleaseHold(t) |
| 441 |
|
| 442 |
suite.SendMessagef( |
| 443 |
t, |
| 444 |
">CLIENT:CONNECT,1,2\r\n>CLIENT:ENV,untrusted_ip=127.0.0.1\r\n>CLIENT:ENV,common_name=%s\r\n>CLIENT:ENV,session_state=Initial\r\n>CLIENT:ENV,session_id=session_id\r\n>CLIENT:ENV,IV_SSO=webauth\r\n>CLIENT:ENV,END", |
| 445 |
tc.clientCommonName, |
| 446 |
) |
| 447 |
|
| 448 |
auth := suite.ReadLine(t) |
| 449 |
assert.Contains(t, auth, "client-pending-auth 1 2 \"WEB_AUTH::") |
| 450 |
suite.SendMessagef(t, "SUCCESS: %s command succeeded", strings.SplitN(auth, " ", 2)[0]) |
| 451 |
|
| 452 |
authURL := strings.TrimPrefix(strings.Split(auth, `"`)[1], "WEB_AUTH::") |
| 453 |
|
| 454 |
var ( |
| 455 |
resp *http.Response |
| 456 |
reqErr error |
| 457 |
) |
| 458 |
|
| 459 |
wg := sync.WaitGroup{} |
| 460 |
wg.Go(func() { |
| 461 |
resp, _, reqErr = testsuite.DoHTTPRequest(t, httpClient, "", http.MethodGet, authURL, nil, http.NoBody) //nolint:bodyclose |
| 462 |
}) |
| 463 |
|
| 464 |
t.Cleanup(func() { |
| 465 |
require.NoError(t, suite.GetManagementInterfaceConn().Close()) |
| 466 |
|
| 467 |
select { |
| 468 |
case err := <-errOpenVPNClientCh: |
| 469 |
require.NoError(t, err, suite.Logs()) |
| 470 |
case <-time.After(1 * time.Second): |
| 471 |
t.Fatalf("timeout waiting for connection to close. Logs:\n\n%s", suite.Logs()) |
| 472 |
} |
| 473 |
}) |
| 474 |
|
| 475 |
switch { |
| 476 |
case !tc.conf.OAuth2.Refresh.ValidateUser && tc.conf.OpenVPN.ClientConfig.Enabled: |
| 477 |
expectProfileClientAuth(t, suite, 1, 2) |
| 478 |
case !tc.conf.OAuth2.Refresh.ValidateUser: |
| 479 |
suite.ExpectMessage(t, "client-auth-nt 1 2") |
| 480 |
case tc.conf.OpenVPN.OverrideUsername: |
| 481 |
suite.ExpectMessage(t, "client-auth 1 2") |
| 482 |
|
| 483 |
switch { |
| 484 |
case tc.clientCommonName == "": |
| 485 |
suite.ExpectMessage(t, `override-username "username"`) |
| 486 |
case tc.conf.OAuth2.UserInfo: |
| 487 |
suite.ExpectMessage(t, `override-username "id1"`) |
| 488 |
default: |
| 489 |
suite.ExpectMessage(t, `override-username "id1"`) |
| 490 |
} |
| 491 |
|
| 492 |
suite.ExpectMessage(t, "END") |
| 493 |
case tc.conf.OpenVPN.AuthTokenUser: |
| 494 |
suite.ExpectMessage(t, "client-auth 1 2") |
| 495 |
|
| 496 |
switch { |
| 497 |
case tc.clientCommonName == "": |
| 498 |
suite.ExpectMessage(t, `push "auth-token-user dXNlcm5hbWU="`) |
| 499 |
case tc.conf.OAuth2.UserInfo: |
| 500 |
suite.ExpectMessage(t, `push "auth-token-user aWQx"`) |
| 501 |
default: |
| 502 |
suite.ExpectMessage(t, `push "auth-token-user aWQx"`) |
| 503 |
} |
| 504 |
|
| 505 |
suite.ExpectMessage(t, "END") |
| 506 |
default: |
| 507 |
suite.ExpectMessage(t, "client-auth-nt 1 2") |
| 508 |
} |
| 509 |
|
| 510 |
suite.SendMessagef(t, "SUCCESS: client-auth command succeeded") |
| 511 |
|
| 512 |
wg.Wait() |
| 513 |
|
| 514 |
require.NoError(t, reqErr) |
| 515 |
require.Equal(t, http.StatusOK, resp.StatusCode) |
| 516 |
|
| 517 |
// Testing ReAuth |
| 518 |
suite.SendMessagef( |
| 519 |
t, |
| 520 |
">CLIENT:ESTABLISHED,0\r\n>CLIENT:ENV,common_name=bypass\r\n>CLIENT:ENV,END\r\n", |
| 521 |
) |
| 522 |
|
| 523 |
// Testing ReAuth |
| 524 |
suite.SendMessagef( |
| 525 |
t, |
| 526 |
">CLIENT:REAUTH,1,3\r\n>CLIENT:ENV,untrusted_ip=127.0.0.1\r\n>CLIENT:ENV,common_name=%s\r\n>CLIENT:ENV,session_id=session_id\r\n>CLIENT:ENV,session_state=AuthenticatedEmptyUser\r\n>CLIENT:ENV,IV_SSO=webauth\r\n>CLIENT:ENV,END", |
| 527 |
tc.clientCommonName, |
| 528 |
) |
| 529 |
|
| 530 |
if !tc.conf.OpenVPN.ReAuthentication { |
| 531 |
suite.ExpectMessage(t, "client-deny 1 3 \"client re-authentication not enabled\"") |
| 532 |
suite.SendMessagef(t, "SUCCESS: client-deny command succeeded") |
| 533 |
|
| 534 |
return |
| 535 |
} |
| 536 |
|
| 537 |
if !tc.nonInteractiveShouldWork { |
| 538 |
auth := suite.ReadLine(t) |
| 539 |
assert.Contains(t, auth, "client-pending-auth 1 3 \"WEB_AUTH::") |
| 540 |
suite.SendMessagef(t, "SUCCESS: %s command succeeded", strings.SplitN(auth, " ", 2)[0]) |
| 541 |
|
| 542 |
return |
| 543 |
} |
| 544 |
|
| 545 |
switch { |
| 546 |
case !tc.conf.OAuth2.Refresh.ValidateUser && tc.conf.OpenVPN.ClientConfig.Enabled: |
| 547 |
expectProfileClientAuth(t, suite, 1, 3) |
| 548 |
case !tc.conf.OAuth2.Refresh.ValidateUser: |
| 549 |
suite.ExpectMessage(t, "client-auth-nt 1 3") |
| 550 |
case tc.conf.OpenVPN.OverrideUsername: |
| 551 |
suite.ExpectMessage(t, "client-auth 1 3") |
| 552 |
|
| 553 |
switch { |
| 554 |
case tc.clientCommonName == "": |
| 555 |
suite.ExpectMessage(t, `override-username "username"`) |
| 556 |
case tc.conf.OAuth2.UserInfo: |
| 557 |
suite.ExpectMessage(t, `override-username "id1"`) |
| 558 |
default: |
| 559 |
suite.ExpectMessage(t, `override-username "id1"`) |
| 560 |
} |
| 561 |
|
| 562 |
suite.ExpectMessage(t, "END") |
| 563 |
case tc.conf.OpenVPN.AuthTokenUser: |
| 564 |
suite.ExpectMessage(t, "client-auth 1 3") |
| 565 |
|
| 566 |
switch { |
| 567 |
case tc.clientCommonName == "": |
| 568 |
suite.ExpectMessage(t, `push "auth-token-user dXNlcm5hbWU="`) |
| 569 |
case tc.conf.OAuth2.UserInfo: |
| 570 |
suite.ExpectMessage(t, `push "auth-token-user aWQx"`) |
| 571 |
default: |
| 572 |
suite.ExpectMessage(t, `push "auth-token-user aWQx"`) |
| 573 |
} |
| 574 |
|
| 575 |
suite.ExpectMessage(t, "END") |
| 576 |
default: |
| 577 |
suite.ExpectMessage(t, "client-auth-nt 1 3") |
| 578 |
} |
| 579 |
|
| 580 |
suite.SendMessagef(t, "SUCCESS: client-auth command succeeded") |
| 581 |
|
| 582 |
// Testing ReAuth |
| 583 |
suite.SendMessagef( |
| 584 |
t, |
| 585 |
">CLIENT:REAUTH,1,4\r\n>CLIENT:ENV,untrusted_ip=127.0.0.1\r\n>CLIENT:ENV,common_name=%s\r\n>CLIENT:ENV,session_id=session_id\r\n>CLIENT:ENV,session_state=AuthenticatedEmptyUser\r\n>CLIENT:ENV,IV_SSO=webauth\r\n>CLIENT:ENV,END", |
| 586 |
tc.clientCommonName, |
| 587 |
) |
| 588 |
|
| 589 |
switch { |
| 590 |
case !tc.conf.OAuth2.Refresh.ValidateUser && tc.conf.OpenVPN.ClientConfig.Enabled: |
| 591 |
expectProfileClientAuth(t, suite, 1, 4) |
| 592 |
case !tc.conf.OAuth2.Refresh.ValidateUser: |
| 593 |
suite.ExpectMessage(t, "client-auth-nt 1 4") |
| 594 |
case tc.conf.OpenVPN.OverrideUsername: |
| 595 |
suite.ExpectMessage(t, "client-auth 1 4") |
| 596 |
|
| 597 |
switch { |
| 598 |
case tc.clientCommonName == "": |
| 599 |
suite.ExpectMessage(t, `override-username "username"`) |
| 600 |
case tc.conf.OAuth2.UserInfo: |
| 601 |
suite.ExpectMessage(t, `override-username "id1"`) |
| 602 |
default: |
| 603 |
suite.ExpectMessage(t, `override-username "id1"`) |
| 604 |
} |
| 605 |
|
| 606 |
suite.ExpectMessage(t, "END") |
| 607 |
case tc.conf.OpenVPN.AuthTokenUser: |
| 608 |
suite.ExpectMessage(t, "client-auth 1 4") |
| 609 |
|
| 610 |
switch { |
| 611 |
case tc.clientCommonName == "": |
| 612 |
suite.ExpectMessage(t, `push "auth-token-user dXNlcm5hbWU="`) |
| 613 |
case tc.conf.OAuth2.UserInfo: |
| 614 |
suite.ExpectMessage(t, `push "auth-token-user aWQx"`) |
| 615 |
default: |
| 616 |
suite.ExpectMessage(t, `push "auth-token-user aWQx"`) |
| 617 |
} |
| 618 |
|
| 619 |
suite.ExpectMessage(t, "END") |
| 620 |
default: |
| 621 |
suite.ExpectMessage(t, "client-auth-nt 1 4") |
| 622 |
} |
| 623 |
|
| 624 |
suite.SendMessagef(t, "SUCCESS: client-auth command succeeded") |
| 625 |
|
| 626 |
// Test Disconnect |
| 627 |
suite.SendMessagef(t, ">CLIENT:DISCONNECT,1\r\n>CLIENT:ENV,untrusted_ip=127.0.0.1\r\n>CLIENT:ENV,common_name=test\r\n>CLIENT:ENV,session_id=session_id\r\n>CLIENT:ENV,session_state=AuthenticatedEmptyUser\r\n>CLIENT:ENV,IV_SSO=webauth\r\n>CLIENT:ENV,END") |
| 628 |
|
| 629 |
// Test ReAuth after DC |
| 630 |
suite.SendMessagef(t, ">CLIENT:REAUTH,1,4\r\n>CLIENT:ENV,untrusted_ip=127.0.0.1\r\n>CLIENT:ENV,common_name=test\r\n>CLIENT:ENV,session_id=session_id\r\n>CLIENT:ENV,session_state=AuthenticatedEmptyUser\r\n>CLIENT:ENV,IV_SSO=webauth\r\n>CLIENT:ENV,END") |
| 631 |
|
| 632 |
switch { |
| 633 |
case conf.OAuth2.Refresh.UseSessionID: |
| 634 |
if !tc.conf.OAuth2.Refresh.ValidateUser || !tc.conf.OpenVPN.AuthTokenUser { |
| 635 |
suite.ExpectMessage(t, "client-auth-nt 1 4") |
| 636 |
} else { |
| 637 |
suite.ExpectMessage(t, "client-auth 1 4") |
| 638 |
|
| 639 |
switch { |
| 640 |
case tc.clientCommonName == "": |
| 641 |
suite.ExpectMessage(t, `push "auth-token-user dXNlcm5hbWU="`) |
| 642 |
case tc.conf.OAuth2.UserInfo: |
| 643 |
suite.ExpectMessage(t, `push "auth-token-user aWQx"`) |
| 644 |
default: |
| 645 |
suite.ExpectMessage(t, `push "auth-token-user aWQx"`) |
| 646 |
} |
| 647 |
} |
| 648 |
default: |
| 649 |
auth = suite.ReadLine(t) |
| 650 |
require.Contains(t, auth, "client-pending-auth 1 4 \"WEB_AUTH::") |
| 651 |
} |
| 652 |
|
| 653 |
suite.SendMessagef(t, "SUCCESS: %s command succeeded", strings.SplitN(auth, " ", 2)[0]) |
| 654 |
|
| 655 |
// Test ReAuth after DC with different CID |
| 656 |
suite.SendMessagef(t, ">CLIENT:CONNECT,2,3\r\n>CLIENT:ENV,untrusted_ip=127.0.0.1\r\n>CLIENT:ENV,common_name=test\r\n>CLIENT:ENV,session_id=session_id\r\n>CLIENT:ENV,session_state=AuthenticatedEmptyUser\r\n>CLIENT:ENV,IV_SSO=webauth\r\n>CLIENT:ENV,END") |
| 657 |
|
| 658 |
auth = suite.ReadLine(t) |
| 659 |
|
| 660 |
if conf.OAuth2.Refresh.UseSessionID { |
| 661 |
require.Contains(t, auth, "client-auth-nt 2 3") |
| 662 |
} else { |
| 663 |
require.Contains(t, auth, "client-pending-auth 2 3 \"WEB_AUTH::") |
| 664 |
} |
| 665 |
|
| 666 |
suite.SendMessagef(t, "SUCCESS: %s command succeeded", strings.SplitN(auth, " ", 2)[0]) |
| 667 |
|
| 668 |
// Test ReAuth after DC with different CID with invalid session |
| 669 |
suite.SendMessagef(t, ">CLIENT:CONNECT,3,3\r\n>CLIENT:ENV,untrusted_ip=127.0.0.1\r\n>CLIENT:ENV,common_name=test\r\n>CLIENT:ENV,session_id=session_id\r\n>CLIENT:ENV,session_state=Expired\r\n>CLIENT:ENV,IV_SSO=webauth\r\n>CLIENT:ENV,END") |
| 670 |
|
| 671 |
auth = suite.ReadLine(t) |
| 672 |
|
| 673 |
if conf.OAuth2.Refresh.UseSessionID { |
| 674 |
assert.Equal(t, fmt.Sprintf(`client-deny 3 3 "%s"`, openvpn.ReasonStateExpiredOrInvalid), auth) |
| 675 |
} else { |
| 676 |
assert.Contains(t, auth, `client-pending-auth 3 3 "WEB_AUTH::`) |
| 677 |
} |
| 678 |
|
| 679 |
suite.SendMessagef(t, "SUCCESS: %s command succeeded", strings.SplitN(auth, " ", 2)[0]) |
| 680 |
}) |
| 681 |
} |
| 682 |
} |