2019-04-25 04:52:08 +07:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
2021-02-24 06:35:04 +07:00
|
|
|
"regexp"
|
2019-04-25 04:52:08 +07:00
|
|
|
"testing"
|
2020-01-18 05:48:48 +07:00
|
|
|
"time"
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-02-06 09:24:25 +07:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-11-30 21:33:45 +07:00
|
|
|
"github.com/stretchr/testify/suite"
|
2022-07-05 06:58:35 +07:00
|
|
|
"github.com/valyala/fasthttp"
|
2020-04-05 19:37:21 +07:00
|
|
|
|
2021-08-11 08:04:35 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/authentication"
|
|
|
|
"github.com/authelia/authelia/v4/internal/authorization"
|
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
|
|
|
"github.com/authelia/authelia/v4/internal/mocks"
|
|
|
|
"github.com/authelia/authelia/v4/internal/session"
|
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2019-04-25 04:52:08 +07:00
|
|
|
)
|
|
|
|
|
2020-05-05 02:39:25 +07:00
|
|
|
var verifyGetCfg = schema.AuthenticationBackendConfiguration{
|
|
|
|
RefreshInterval: schema.RefreshIntervalDefault,
|
2021-04-16 08:44:37 +07:00
|
|
|
LDAP: &schema.LDAPAuthenticationBackendConfiguration{},
|
2020-05-05 02:39:25 +07:00
|
|
|
}
|
|
|
|
|
2019-04-25 04:52:08 +07:00
|
|
|
func TestShouldRaiseWhenTargetUrlIsMalformed(t *testing.T) {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(t)
|
|
|
|
defer mock.Close()
|
|
|
|
mock.Ctx.Request.Header.Set("X-Forwarded-Proto", "https")
|
|
|
|
mock.Ctx.Request.Header.Set("X-Forwarded-Host", "home.example.com")
|
|
|
|
mock.Ctx.Request.Header.Set("X-Forwarded-URI", "/abc")
|
2021-05-05 05:06:05 +07:00
|
|
|
originalURL, err := mock.Ctx.GetOriginalURL()
|
2019-04-25 04:52:08 +07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
expectedURL, err := url.ParseRequestURI("https://home.example.com/abc")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expectedURL, originalURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldRaiseWhenNoHeaderProvidedToDetectTargetURL(t *testing.T) {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(t)
|
|
|
|
defer mock.Close()
|
2021-05-05 05:06:05 +07:00
|
|
|
_, err := mock.Ctx.GetOriginalURL()
|
2019-04-25 04:52:08 +07:00
|
|
|
assert.Error(t, err)
|
2022-02-06 20:37:28 +07:00
|
|
|
assert.Equal(t, "Missing header X-Forwarded-Host", err.Error())
|
2020-02-06 09:24:25 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldRaiseWhenNoXForwardedHostHeaderProvidedToDetectTargetURL(t *testing.T) {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(t)
|
|
|
|
defer mock.Close()
|
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("X-Forwarded-Proto", "https")
|
2021-05-05 05:06:05 +07:00
|
|
|
_, err := mock.Ctx.GetOriginalURL()
|
2020-02-06 09:24:25 +07:00
|
|
|
assert.Error(t, err)
|
2020-08-21 08:15:20 +07:00
|
|
|
assert.Equal(t, "Missing header X-Forwarded-Host", err.Error())
|
2020-02-06 09:24:25 +07:00
|
|
|
}
|
|
|
|
|
2020-05-05 02:39:25 +07:00
|
|
|
func TestShouldRaiseWhenXForwardedProtoIsNotParsable(t *testing.T) {
|
2020-02-06 09:24:25 +07:00
|
|
|
mock := mocks.NewMockAutheliaCtx(t)
|
|
|
|
defer mock.Close()
|
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("X-Forwarded-Proto", "!:;;:,")
|
|
|
|
mock.Ctx.Request.Header.Set("X-Forwarded-Host", "myhost.local")
|
2020-05-05 02:39:25 +07:00
|
|
|
|
2021-05-05 05:06:05 +07:00
|
|
|
_, err := mock.Ctx.GetOriginalURL()
|
2020-02-06 09:24:25 +07:00
|
|
|
assert.Error(t, err)
|
2022-02-06 20:37:28 +07:00
|
|
|
assert.Equal(t, "Unable to parse URL !:;;:,://myhost.local/: parse \"!:;;:,://myhost.local/\": invalid URI for request", err.Error())
|
2020-02-06 09:24:25 +07:00
|
|
|
}
|
|
|
|
|
2020-05-05 02:39:25 +07:00
|
|
|
func TestShouldRaiseWhenXForwardedURIIsNotParsable(t *testing.T) {
|
2020-02-06 09:24:25 +07:00
|
|
|
mock := mocks.NewMockAutheliaCtx(t)
|
|
|
|
defer mock.Close()
|
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("X-Forwarded-Proto", "https")
|
|
|
|
mock.Ctx.Request.Header.Set("X-Forwarded-Host", "myhost.local")
|
|
|
|
mock.Ctx.Request.Header.Set("X-Forwarded-URI", "!:;;:,")
|
2020-05-05 02:39:25 +07:00
|
|
|
|
2021-05-05 05:06:05 +07:00
|
|
|
_, err := mock.Ctx.GetOriginalURL()
|
2020-02-06 09:24:25 +07:00
|
|
|
require.Error(t, err)
|
2020-06-02 16:09:13 +07:00
|
|
|
assert.Equal(t, "Unable to parse URL https://myhost.local!:;;:,: parse \"https://myhost.local!:;;:,\": invalid port \":,\" after host", err.Error())
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2020-05-02 12:06:39 +07:00
|
|
|
// Test parseBasicAuth.
|
2019-04-25 04:52:08 +07:00
|
|
|
func TestShouldRaiseWhenHeaderDoesNotContainBasicPrefix(t *testing.T) {
|
2021-12-02 09:21:46 +07:00
|
|
|
_, _, err := parseBasicAuth(headerProxyAuthorization, "alzefzlfzemjfej==")
|
2019-04-25 04:52:08 +07:00
|
|
|
assert.Error(t, err)
|
2020-02-06 09:24:25 +07:00
|
|
|
assert.Equal(t, "Basic prefix not found in Proxy-Authorization header", err.Error())
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldRaiseWhenCredentialsAreNotInBase64(t *testing.T) {
|
2021-12-02 09:21:46 +07:00
|
|
|
_, _, err := parseBasicAuth(headerProxyAuthorization, "Basic alzefzlfzemjfej==")
|
2019-04-25 04:52:08 +07:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, "illegal base64 data at input byte 16", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldRaiseWhenCredentialsAreNotInCorrectForm(t *testing.T) {
|
2020-05-05 02:39:25 +07:00
|
|
|
// The decoded format should be user:password.
|
2021-12-02 09:21:46 +07:00
|
|
|
_, _, err := parseBasicAuth(headerProxyAuthorization, "Basic am9obiBwYXNzd29yZA==")
|
2019-04-25 04:52:08 +07:00
|
|
|
assert.Error(t, err)
|
2021-09-17 12:53:40 +07:00
|
|
|
assert.Equal(t, "format of Proxy-Authorization header must be user:password", err.Error())
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2021-02-24 06:35:04 +07:00
|
|
|
func TestShouldUseProvidedHeaderName(t *testing.T) {
|
|
|
|
// The decoded format should be user:password.
|
2021-12-02 09:21:46 +07:00
|
|
|
_, _, err := parseBasicAuth([]byte("HeaderName"), "")
|
2021-02-24 06:35:04 +07:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, "Basic prefix not found in HeaderName header", err.Error())
|
|
|
|
}
|
|
|
|
|
2019-04-25 04:52:08 +07:00
|
|
|
func TestShouldReturnUsernameAndPassword(t *testing.T) {
|
|
|
|
// the decoded format should be user:password.
|
2021-12-02 09:21:46 +07:00
|
|
|
user, password, err := parseBasicAuth(headerProxyAuthorization, "Basic am9objpwYXNzd29yZA==")
|
2019-04-25 04:52:08 +07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "john", user)
|
|
|
|
assert.Equal(t, "password", password)
|
|
|
|
}
|
|
|
|
|
2020-05-02 12:06:39 +07:00
|
|
|
// Test isTargetURLAuthorized.
|
2019-04-25 04:52:08 +07:00
|
|
|
func TestShouldCheckAuthorizationMatching(t *testing.T) {
|
|
|
|
type Rule struct {
|
|
|
|
Policy string
|
|
|
|
AuthLevel authentication.Level
|
|
|
|
ExpectedMatching authorizationMatching
|
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2019-04-25 04:52:08 +07:00
|
|
|
rules := []Rule{
|
2020-04-09 08:05:17 +07:00
|
|
|
{"bypass", authentication.NotAuthenticated, Authorized},
|
|
|
|
{"bypass", authentication.OneFactor, Authorized},
|
|
|
|
{"bypass", authentication.TwoFactor, Authorized},
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2020-04-09 08:05:17 +07:00
|
|
|
{"one_factor", authentication.NotAuthenticated, NotAuthorized},
|
|
|
|
{"one_factor", authentication.OneFactor, Authorized},
|
|
|
|
{"one_factor", authentication.TwoFactor, Authorized},
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2020-04-09 08:05:17 +07:00
|
|
|
{"two_factor", authentication.NotAuthenticated, NotAuthorized},
|
|
|
|
{"two_factor", authentication.OneFactor, NotAuthorized},
|
|
|
|
{"two_factor", authentication.TwoFactor, Authorized},
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2020-04-09 08:05:17 +07:00
|
|
|
{"deny", authentication.NotAuthenticated, NotAuthorized},
|
|
|
|
{"deny", authentication.OneFactor, Forbidden},
|
|
|
|
{"deny", authentication.TwoFactor, Forbidden},
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2021-09-17 12:53:40 +07:00
|
|
|
u, _ := url.ParseRequestURI("https://test.example.com")
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
for _, rule := range rules {
|
2021-06-18 08:38:01 +07:00
|
|
|
authorizer := authorization.NewAuthorizer(&schema.Configuration{
|
|
|
|
AccessControl: schema.AccessControlConfiguration{
|
|
|
|
DefaultPolicy: "deny",
|
|
|
|
Rules: []schema.ACLRule{{
|
|
|
|
Domains: []string{"test.example.com"},
|
|
|
|
Policy: rule.Policy,
|
|
|
|
}},
|
|
|
|
}})
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
username := ""
|
|
|
|
if rule.AuthLevel > authentication.NotAuthenticated {
|
2020-05-02 23:20:40 +07:00
|
|
|
username = testUsername
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2021-09-17 12:53:40 +07:00
|
|
|
matching := isTargetURLAuthorized(authorizer, *u, username, []string{}, net.ParseIP("127.0.0.1"), []byte("GET"), rule.AuthLevel)
|
2019-04-25 04:52:08 +07:00
|
|
|
assert.Equal(t, rule.ExpectedMatching, matching, "policy=%s, authLevel=%v, expected=%v, actual=%v",
|
|
|
|
rule.Policy, rule.AuthLevel, rule.ExpectedMatching, matching)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-02 12:06:39 +07:00
|
|
|
// Test verifyBasicAuth.
|
2019-04-25 04:52:08 +07:00
|
|
|
func TestShouldVerifyWrongCredentials(t *testing.T) {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(t)
|
|
|
|
defer mock.Close()
|
|
|
|
|
|
|
|
mock.UserProviderMock.EXPECT().
|
|
|
|
CheckUserPassword(gomock.Eq("john"), gomock.Eq("password")).
|
|
|
|
Return(false, nil)
|
|
|
|
|
2021-12-02 09:21:46 +07:00
|
|
|
_, _, _, _, _, err := verifyBasicAuth(mock.Ctx, headerProxyAuthorization, []byte("Basic am9objpwYXNzd29yZA=="))
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2019-11-30 21:33:45 +07:00
|
|
|
type BasicAuthorizationSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
}
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2019-11-30 21:33:45 +07:00
|
|
|
func NewBasicAuthorizationSuite() *BasicAuthorizationSuite {
|
|
|
|
return &BasicAuthorizationSuite{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BasicAuthorizationSuite) TestShouldNotBeAbleToParseBasicAuth() {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(s.T())
|
|
|
|
defer mock.Close()
|
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("Proxy-Authorization", "Basic am9objpaaaaaaaaaaaaaaaa")
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://test.example.com")
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
2019-11-30 21:33:45 +07:00
|
|
|
|
|
|
|
assert.Equal(s.T(), 401, mock.Ctx.Response.StatusCode())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BasicAuthorizationSuite) TestShouldApplyDefaultPolicy() {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(s.T())
|
|
|
|
defer mock.Close()
|
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("Proxy-Authorization", "Basic am9objpwYXNzd29yZA==")
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://test.example.com")
|
|
|
|
|
|
|
|
mock.UserProviderMock.EXPECT().
|
|
|
|
CheckUserPassword(gomock.Eq("john"), gomock.Eq("password")).
|
|
|
|
Return(true, nil)
|
|
|
|
|
|
|
|
mock.UserProviderMock.EXPECT().
|
|
|
|
GetDetails(gomock.Eq("john")).
|
|
|
|
Return(&authentication.UserDetails{
|
|
|
|
Emails: []string{"john@example.com"},
|
2019-11-30 23:49:52 +07:00
|
|
|
Groups: []string{"dev", "admins"},
|
2019-11-30 21:33:45 +07:00
|
|
|
}, nil)
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
2019-11-30 21:33:45 +07:00
|
|
|
|
|
|
|
assert.Equal(s.T(), 403, mock.Ctx.Response.StatusCode())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BasicAuthorizationSuite) TestShouldApplyPolicyOfBypassDomain() {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(s.T())
|
|
|
|
defer mock.Close()
|
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("Proxy-Authorization", "Basic am9objpwYXNzd29yZA==")
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://bypass.example.com")
|
|
|
|
|
|
|
|
mock.UserProviderMock.EXPECT().
|
|
|
|
CheckUserPassword(gomock.Eq("john"), gomock.Eq("password")).
|
|
|
|
Return(true, nil)
|
|
|
|
|
|
|
|
mock.UserProviderMock.EXPECT().
|
|
|
|
GetDetails(gomock.Eq("john")).
|
|
|
|
Return(&authentication.UserDetails{
|
|
|
|
Emails: []string{"john@example.com"},
|
2019-11-30 23:49:52 +07:00
|
|
|
Groups: []string{"dev", "admins"},
|
2019-11-30 21:33:45 +07:00
|
|
|
}, nil)
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
2019-11-30 21:33:45 +07:00
|
|
|
|
|
|
|
assert.Equal(s.T(), 200, mock.Ctx.Response.StatusCode())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BasicAuthorizationSuite) TestShouldApplyPolicyOfOneFactorDomain() {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(s.T())
|
|
|
|
defer mock.Close()
|
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("Proxy-Authorization", "Basic am9objpwYXNzd29yZA==")
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://one-factor.example.com")
|
|
|
|
|
|
|
|
mock.UserProviderMock.EXPECT().
|
|
|
|
CheckUserPassword(gomock.Eq("john"), gomock.Eq("password")).
|
|
|
|
Return(true, nil)
|
|
|
|
|
|
|
|
mock.UserProviderMock.EXPECT().
|
|
|
|
GetDetails(gomock.Eq("john")).
|
|
|
|
Return(&authentication.UserDetails{
|
|
|
|
Emails: []string{"john@example.com"},
|
2019-11-30 23:49:52 +07:00
|
|
|
Groups: []string{"dev", "admins"},
|
2019-11-30 21:33:45 +07:00
|
|
|
}, nil)
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
2019-11-30 21:33:45 +07:00
|
|
|
|
|
|
|
assert.Equal(s.T(), 200, mock.Ctx.Response.StatusCode())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BasicAuthorizationSuite) TestShouldApplyPolicyOfTwoFactorDomain() {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(s.T())
|
|
|
|
defer mock.Close()
|
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("Proxy-Authorization", "Basic am9objpwYXNzd29yZA==")
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://two-factor.example.com")
|
|
|
|
|
|
|
|
mock.UserProviderMock.EXPECT().
|
|
|
|
CheckUserPassword(gomock.Eq("john"), gomock.Eq("password")).
|
|
|
|
Return(true, nil)
|
|
|
|
|
|
|
|
mock.UserProviderMock.EXPECT().
|
|
|
|
GetDetails(gomock.Eq("john")).
|
|
|
|
Return(&authentication.UserDetails{
|
|
|
|
Emails: []string{"john@example.com"},
|
2019-11-30 23:49:52 +07:00
|
|
|
Groups: []string{"dev", "admins"},
|
2019-11-30 21:33:45 +07:00
|
|
|
}, nil)
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
2019-11-30 21:33:45 +07:00
|
|
|
|
|
|
|
assert.Equal(s.T(), 401, mock.Ctx.Response.StatusCode())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BasicAuthorizationSuite) TestShouldApplyPolicyOfDenyDomain() {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(s.T())
|
|
|
|
defer mock.Close()
|
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("Proxy-Authorization", "Basic am9objpwYXNzd29yZA==")
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://deny.example.com")
|
|
|
|
|
|
|
|
mock.UserProviderMock.EXPECT().
|
|
|
|
CheckUserPassword(gomock.Eq("john"), gomock.Eq("password")).
|
|
|
|
Return(true, nil)
|
|
|
|
|
|
|
|
mock.UserProviderMock.EXPECT().
|
|
|
|
GetDetails(gomock.Eq("john")).
|
|
|
|
Return(&authentication.UserDetails{
|
|
|
|
Emails: []string{"john@example.com"},
|
2019-11-30 23:49:52 +07:00
|
|
|
Groups: []string{"dev", "admins"},
|
2019-11-30 21:33:45 +07:00
|
|
|
}, nil)
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
2019-11-30 21:33:45 +07:00
|
|
|
|
|
|
|
assert.Equal(s.T(), 403, mock.Ctx.Response.StatusCode())
|
|
|
|
}
|
|
|
|
|
2021-02-24 06:35:04 +07:00
|
|
|
func (s *BasicAuthorizationSuite) TestShouldVerifyAuthBasicArgOk() {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(s.T())
|
|
|
|
defer mock.Close()
|
|
|
|
|
|
|
|
mock.Ctx.QueryArgs().Add("auth", "basic")
|
|
|
|
mock.Ctx.Request.Header.Set("Authorization", "Basic am9objpwYXNzd29yZA==")
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://one-factor.example.com")
|
|
|
|
|
|
|
|
mock.UserProviderMock.EXPECT().
|
|
|
|
CheckUserPassword(gomock.Eq("john"), gomock.Eq("password")).
|
|
|
|
Return(true, nil)
|
|
|
|
|
|
|
|
mock.UserProviderMock.EXPECT().
|
|
|
|
GetDetails(gomock.Eq("john")).
|
|
|
|
Return(&authentication.UserDetails{
|
|
|
|
Emails: []string{"john@example.com"},
|
|
|
|
Groups: []string{"dev", "admins"},
|
|
|
|
}, nil)
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
2021-02-24 06:35:04 +07:00
|
|
|
|
|
|
|
assert.Equal(s.T(), 200, mock.Ctx.Response.StatusCode())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BasicAuthorizationSuite) TestShouldVerifyAuthBasicArgFailingNoHeader() {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(s.T())
|
|
|
|
defer mock.Close()
|
|
|
|
|
|
|
|
mock.Ctx.QueryArgs().Add("auth", "basic")
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://one-factor.example.com")
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
2021-02-24 06:35:04 +07:00
|
|
|
|
|
|
|
assert.Equal(s.T(), 401, mock.Ctx.Response.StatusCode())
|
2022-07-08 19:18:52 +07:00
|
|
|
assert.Equal(s.T(), "401 Unauthorized", string(mock.Ctx.Response.Body()))
|
2021-02-24 06:35:04 +07:00
|
|
|
assert.NotEmpty(s.T(), mock.Ctx.Response.Header.Peek("WWW-Authenticate"))
|
|
|
|
assert.Regexp(s.T(), regexp.MustCompile("^Basic realm="), string(mock.Ctx.Response.Header.Peek("WWW-Authenticate")))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BasicAuthorizationSuite) TestShouldVerifyAuthBasicArgFailingEmptyHeader() {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(s.T())
|
|
|
|
defer mock.Close()
|
|
|
|
|
|
|
|
mock.Ctx.QueryArgs().Add("auth", "basic")
|
|
|
|
mock.Ctx.Request.Header.Set("Authorization", "")
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://one-factor.example.com")
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
2021-02-24 06:35:04 +07:00
|
|
|
|
|
|
|
assert.Equal(s.T(), 401, mock.Ctx.Response.StatusCode())
|
2022-07-08 19:18:52 +07:00
|
|
|
assert.Equal(s.T(), "401 Unauthorized", string(mock.Ctx.Response.Body()))
|
2021-02-24 06:35:04 +07:00
|
|
|
assert.NotEmpty(s.T(), mock.Ctx.Response.Header.Peek("WWW-Authenticate"))
|
|
|
|
assert.Regexp(s.T(), regexp.MustCompile("^Basic realm="), string(mock.Ctx.Response.Header.Peek("WWW-Authenticate")))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BasicAuthorizationSuite) TestShouldVerifyAuthBasicArgFailingWrongPassword() {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(s.T())
|
|
|
|
defer mock.Close()
|
|
|
|
|
|
|
|
mock.Ctx.QueryArgs().Add("auth", "basic")
|
|
|
|
mock.Ctx.Request.Header.Set("Authorization", "Basic am9objpwYXNzd29yZA==")
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://one-factor.example.com")
|
|
|
|
|
|
|
|
mock.UserProviderMock.EXPECT().
|
|
|
|
CheckUserPassword(gomock.Eq("john"), gomock.Eq("password")).
|
|
|
|
Return(false, nil)
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
2021-02-24 06:35:04 +07:00
|
|
|
|
|
|
|
assert.Equal(s.T(), 401, mock.Ctx.Response.StatusCode())
|
2022-07-08 19:18:52 +07:00
|
|
|
assert.Equal(s.T(), "401 Unauthorized", string(mock.Ctx.Response.Body()))
|
2021-02-24 06:35:04 +07:00
|
|
|
assert.NotEmpty(s.T(), mock.Ctx.Response.Header.Peek("WWW-Authenticate"))
|
|
|
|
assert.Regexp(s.T(), regexp.MustCompile("^Basic realm="), string(mock.Ctx.Response.Header.Peek("WWW-Authenticate")))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BasicAuthorizationSuite) TestShouldVerifyAuthBasicArgFailingWrongHeader() {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(s.T())
|
|
|
|
defer mock.Close()
|
|
|
|
|
|
|
|
mock.Ctx.QueryArgs().Add("auth", "basic")
|
|
|
|
mock.Ctx.Request.Header.Set("Proxy-Authorization", "Basic am9objpwYXNzd29yZA==")
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://one-factor.example.com")
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
2021-02-24 06:35:04 +07:00
|
|
|
|
|
|
|
assert.Equal(s.T(), 401, mock.Ctx.Response.StatusCode())
|
2022-07-08 19:18:52 +07:00
|
|
|
assert.Equal(s.T(), "401 Unauthorized", string(mock.Ctx.Response.Body()))
|
2021-02-24 06:35:04 +07:00
|
|
|
assert.NotEmpty(s.T(), mock.Ctx.Response.Header.Peek("WWW-Authenticate"))
|
|
|
|
assert.Regexp(s.T(), regexp.MustCompile("^Basic realm="), string(mock.Ctx.Response.Header.Peek("WWW-Authenticate")))
|
|
|
|
}
|
|
|
|
|
2019-11-30 21:33:45 +07:00
|
|
|
func TestShouldVerifyAuthorizationsUsingBasicAuth(t *testing.T) {
|
|
|
|
suite.Run(t, NewBasicAuthorizationSuite())
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldVerifyWrongCredentialsInBasicAuth(t *testing.T) {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(t)
|
|
|
|
defer mock.Close()
|
|
|
|
|
|
|
|
mock.UserProviderMock.EXPECT().
|
|
|
|
CheckUserPassword(gomock.Eq("john"), gomock.Eq("wrongpass")).
|
|
|
|
Return(false, nil)
|
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("Proxy-Authorization", "Basic am9objp3cm9uZ3Bhc3M=")
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://test.example.com")
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
2019-04-25 04:52:08 +07:00
|
|
|
expStatus, actualStatus := 401, mock.Ctx.Response.StatusCode()
|
|
|
|
assert.Equal(t, expStatus, actualStatus, "URL=%s -> StatusCode=%d != ExpectedStatusCode=%d",
|
|
|
|
"https://test.example.com", actualStatus, expStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldVerifyFailingPasswordCheckingInBasicAuth(t *testing.T) {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(t)
|
|
|
|
defer mock.Close()
|
|
|
|
|
|
|
|
mock.UserProviderMock.EXPECT().
|
|
|
|
CheckUserPassword(gomock.Eq("john"), gomock.Eq("wrongpass")).
|
|
|
|
Return(false, fmt.Errorf("Failed"))
|
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("Proxy-Authorization", "Basic am9objp3cm9uZ3Bhc3M=")
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://test.example.com")
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
2019-04-25 04:52:08 +07:00
|
|
|
expStatus, actualStatus := 401, mock.Ctx.Response.StatusCode()
|
|
|
|
assert.Equal(t, expStatus, actualStatus, "URL=%s -> StatusCode=%d != ExpectedStatusCode=%d",
|
|
|
|
"https://test.example.com", actualStatus, expStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldVerifyFailingDetailsFetchingInBasicAuth(t *testing.T) {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(t)
|
|
|
|
defer mock.Close()
|
|
|
|
|
|
|
|
mock.UserProviderMock.EXPECT().
|
|
|
|
CheckUserPassword(gomock.Eq("john"), gomock.Eq("password")).
|
|
|
|
Return(true, nil)
|
|
|
|
|
|
|
|
mock.UserProviderMock.EXPECT().
|
|
|
|
GetDetails(gomock.Eq("john")).
|
|
|
|
Return(nil, fmt.Errorf("Failed"))
|
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("Proxy-Authorization", "Basic am9objpwYXNzd29yZA==")
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://test.example.com")
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
2019-04-25 04:52:08 +07:00
|
|
|
expStatus, actualStatus := 401, mock.Ctx.Response.StatusCode()
|
|
|
|
assert.Equal(t, expStatus, actualStatus, "URL=%s -> StatusCode=%d != ExpectedStatusCode=%d",
|
|
|
|
"https://test.example.com", actualStatus, expStatus)
|
|
|
|
}
|
|
|
|
|
2020-11-16 18:22:16 +07:00
|
|
|
func TestShouldNotCrashOnEmptyEmail(t *testing.T) {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(t)
|
|
|
|
defer mock.Close()
|
|
|
|
|
2021-02-02 08:01:46 +07:00
|
|
|
mock.Clock.Set(time.Now())
|
|
|
|
|
2020-11-16 18:22:16 +07:00
|
|
|
userSession := mock.Ctx.GetSession()
|
|
|
|
userSession.Username = testUsername
|
|
|
|
userSession.Emails = nil
|
|
|
|
userSession.AuthenticationLevel = authentication.OneFactor
|
2021-02-02 08:01:46 +07:00
|
|
|
userSession.RefreshTTL = mock.Clock.Now().Add(5 * time.Minute)
|
2020-12-02 06:03:44 +07:00
|
|
|
|
2021-02-02 08:01:46 +07:00
|
|
|
fmt.Printf("Time is %v\n", userSession.RefreshTTL)
|
2020-12-02 06:03:44 +07:00
|
|
|
err := mock.Ctx.SaveSession(userSession)
|
|
|
|
require.NoError(t, err)
|
2020-11-16 18:22:16 +07:00
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://bypass.example.com")
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
2020-11-16 18:22:16 +07:00
|
|
|
|
|
|
|
expStatus, actualStatus := 200, mock.Ctx.Response.StatusCode()
|
|
|
|
assert.Equal(t, expStatus, actualStatus, "URL=%s -> StatusCode=%d != ExpectedStatusCode=%d",
|
|
|
|
"https://bypass.example.com", actualStatus, expStatus)
|
|
|
|
assert.Equal(t, []byte(nil), mock.Ctx.Response.Header.Peek("Remote-Email"))
|
|
|
|
}
|
|
|
|
|
2019-04-25 04:52:08 +07:00
|
|
|
type Pair struct {
|
|
|
|
URL string
|
|
|
|
Username string
|
2020-10-26 18:38:08 +07:00
|
|
|
Emails []string
|
2019-04-25 04:52:08 +07:00
|
|
|
AuthenticationLevel authentication.Level
|
|
|
|
ExpectedStatusCode int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p Pair) String() string {
|
|
|
|
return fmt.Sprintf("url=%s, username=%s, auth_lvl=%d, exp_status=%d",
|
|
|
|
p.URL, p.Username, p.AuthenticationLevel, p.ExpectedStatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldVerifyAuthorizationsUsingSessionCookie(t *testing.T) {
|
|
|
|
testCases := []Pair{
|
2020-10-26 18:38:08 +07:00
|
|
|
{"https://test.example.com", "", nil, authentication.NotAuthenticated, 401},
|
|
|
|
{"https://bypass.example.com", "", nil, authentication.NotAuthenticated, 200},
|
|
|
|
{"https://one-factor.example.com", "", nil, authentication.NotAuthenticated, 401},
|
|
|
|
{"https://two-factor.example.com", "", nil, authentication.NotAuthenticated, 401},
|
|
|
|
{"https://deny.example.com", "", nil, authentication.NotAuthenticated, 401},
|
|
|
|
|
|
|
|
{"https://test.example.com", "john", []string{"john.doe@example.com"}, authentication.OneFactor, 403},
|
|
|
|
{"https://bypass.example.com", "john", []string{"john.doe@example.com"}, authentication.OneFactor, 200},
|
|
|
|
{"https://one-factor.example.com", "john", []string{"john.doe@example.com"}, authentication.OneFactor, 200},
|
|
|
|
{"https://two-factor.example.com", "john", []string{"john.doe@example.com"}, authentication.OneFactor, 401},
|
|
|
|
{"https://deny.example.com", "john", []string{"john.doe@example.com"}, authentication.OneFactor, 403},
|
|
|
|
|
|
|
|
{"https://test.example.com", "john", []string{"john.doe@example.com"}, authentication.TwoFactor, 403},
|
|
|
|
{"https://bypass.example.com", "john", []string{"john.doe@example.com"}, authentication.TwoFactor, 200},
|
|
|
|
{"https://one-factor.example.com", "john", []string{"john.doe@example.com"}, authentication.TwoFactor, 200},
|
|
|
|
{"https://two-factor.example.com", "john", []string{"john.doe@example.com"}, authentication.TwoFactor, 200},
|
|
|
|
{"https://deny.example.com", "john", []string{"john.doe@example.com"}, authentication.TwoFactor, 403},
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
testCase := testCase
|
|
|
|
t.Run(testCase.String(), func(t *testing.T) {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(t)
|
|
|
|
defer mock.Close()
|
|
|
|
|
2021-02-02 08:01:46 +07:00
|
|
|
mock.Clock.Set(time.Now())
|
|
|
|
|
2019-04-25 04:52:08 +07:00
|
|
|
userSession := mock.Ctx.GetSession()
|
|
|
|
userSession.Username = testCase.Username
|
2020-10-26 18:38:08 +07:00
|
|
|
userSession.Emails = testCase.Emails
|
2019-04-25 04:52:08 +07:00
|
|
|
userSession.AuthenticationLevel = testCase.AuthenticationLevel
|
2021-02-02 08:01:46 +07:00
|
|
|
userSession.RefreshTTL = mock.Clock.Now().Add(5 * time.Minute)
|
2020-12-02 06:03:44 +07:00
|
|
|
|
|
|
|
err := mock.Ctx.SaveSession(userSession)
|
|
|
|
require.NoError(t, err)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", testCase.URL)
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
2019-04-25 04:52:08 +07:00
|
|
|
expStatus, actualStatus := testCase.ExpectedStatusCode, mock.Ctx.Response.StatusCode()
|
|
|
|
assert.Equal(t, expStatus, actualStatus, "URL=%s -> AuthLevel=%d, StatusCode=%d != ExpectedStatusCode=%d",
|
|
|
|
testCase.URL, testCase.AuthenticationLevel, actualStatus, expStatus)
|
|
|
|
|
|
|
|
if testCase.ExpectedStatusCode == 200 && testCase.Username != "" {
|
|
|
|
assert.Equal(t, []byte(testCase.Username), mock.Ctx.Response.Header.Peek("Remote-User"))
|
2020-10-26 18:38:08 +07:00
|
|
|
assert.Equal(t, []byte("john.doe@example.com"), mock.Ctx.Response.Header.Peek("Remote-Email"))
|
2019-04-25 04:52:08 +07:00
|
|
|
} else {
|
|
|
|
assert.Equal(t, []byte(nil), mock.Ctx.Response.Header.Peek("Remote-User"))
|
2020-10-26 18:38:08 +07:00
|
|
|
assert.Equal(t, []byte(nil), mock.Ctx.Response.Header.Peek("Remote-Email"))
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-01-18 05:48:48 +07:00
|
|
|
|
|
|
|
func TestShouldDestroySessionWhenInactiveForTooLong(t *testing.T) {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(t)
|
|
|
|
defer mock.Close()
|
|
|
|
|
|
|
|
clock := mocks.TestingClock{}
|
|
|
|
clock.Set(time.Now())
|
2020-04-25 06:29:36 +07:00
|
|
|
past := clock.Now().Add(-1 * time.Hour)
|
2020-01-18 05:48:48 +07:00
|
|
|
|
2020-05-02 23:20:40 +07:00
|
|
|
mock.Ctx.Configuration.Session.Inactivity = testInactivity
|
2020-05-02 12:06:39 +07:00
|
|
|
// Reload the session provider since the configuration is indirect.
|
feat(session): add redis sentinel provider (#1768)
* feat(session): add redis sentinel provider
* refactor(session): use int for ports as per go standards
* refactor(configuration): adjust tests and validation
* refactor(configuration): add err format consts
* refactor(configuration): explicitly map redis structs
* refactor(session): merge redis/redis sentinel providers
* refactor(session): add additional checks to redis providers
* feat(session): add redis cluster provider
* fix: update config for new values
* fix: provide nil certpool to affected tests/mocks
* test: add additional tests to cover uncovered code
* docs: expand explanation of host and nodes relation for redis
* ci: add redis-sentinel to suite highavailability, add redis-sentinel quorum
* fix(session): sentinel password
* test: use redis alpine library image for redis sentinel, use expose instead of ports, use redis ip, adjust redis ip range, adjust redis config
* test: make entrypoint.sh executable, fix entrypoint.sh if/elif
* test: add redis failover tests
* test: defer docker start, adjust sleep, attempt logout before login, attempt visit before login and tune timeouts, add additional logging
* test: add sentinel integration test
* test: add secondary node failure to tests, fix password usage, bump test timeout, add sleep
* feat: use sentinel failover cluster
* fix: renamed addrs to sentineladdrs upstream
* test(session): sentinel failover
* test: add redis standard back into testing
* test: move redis standalone test to traefik2
* fix/docs: apply suggestions from code review
2021-03-10 06:03:05 +07:00
|
|
|
mock.Ctx.Providers.SessionProvider = session.NewProvider(mock.Ctx.Configuration.Session, nil)
|
2020-04-05 19:37:21 +07:00
|
|
|
assert.Equal(t, time.Second*10, mock.Ctx.Providers.SessionProvider.Inactivity)
|
|
|
|
|
|
|
|
userSession := mock.Ctx.GetSession()
|
2020-05-02 23:20:40 +07:00
|
|
|
userSession.Username = testUsername
|
2020-04-05 19:37:21 +07:00
|
|
|
userSession.AuthenticationLevel = authentication.TwoFactor
|
2020-04-25 06:29:36 +07:00
|
|
|
userSession.LastActivity = past.Unix()
|
2020-12-02 06:03:44 +07:00
|
|
|
|
|
|
|
err := mock.Ctx.SaveSession(userSession)
|
|
|
|
require.NoError(t, err)
|
2020-04-05 19:37:21 +07:00
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://two-factor.example.com")
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
2020-04-05 19:37:21 +07:00
|
|
|
|
2020-05-02 12:06:39 +07:00
|
|
|
// The session has been destroyed.
|
2020-04-05 19:37:21 +07:00
|
|
|
newUserSession := mock.Ctx.GetSession()
|
|
|
|
assert.Equal(t, "", newUserSession.Username)
|
|
|
|
assert.Equal(t, authentication.NotAuthenticated, newUserSession.AuthenticationLevel)
|
2020-04-25 06:29:36 +07:00
|
|
|
|
|
|
|
// Check the inactivity timestamp has been updated to current time in the new session.
|
|
|
|
assert.Equal(t, clock.Now().Unix(), newUserSession.LastActivity)
|
2020-04-05 19:37:21 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldDestroySessionWhenInactiveForTooLongUsingDurationNotation(t *testing.T) {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(t)
|
|
|
|
defer mock.Close()
|
|
|
|
|
|
|
|
clock := mocks.TestingClock{}
|
|
|
|
clock.Set(time.Now())
|
|
|
|
|
2022-03-02 13:40:26 +07:00
|
|
|
mock.Ctx.Configuration.Session.Inactivity = time.Second * 10
|
2020-05-02 12:06:39 +07:00
|
|
|
// Reload the session provider since the configuration is indirect.
|
feat(session): add redis sentinel provider (#1768)
* feat(session): add redis sentinel provider
* refactor(session): use int for ports as per go standards
* refactor(configuration): adjust tests and validation
* refactor(configuration): add err format consts
* refactor(configuration): explicitly map redis structs
* refactor(session): merge redis/redis sentinel providers
* refactor(session): add additional checks to redis providers
* feat(session): add redis cluster provider
* fix: update config for new values
* fix: provide nil certpool to affected tests/mocks
* test: add additional tests to cover uncovered code
* docs: expand explanation of host and nodes relation for redis
* ci: add redis-sentinel to suite highavailability, add redis-sentinel quorum
* fix(session): sentinel password
* test: use redis alpine library image for redis sentinel, use expose instead of ports, use redis ip, adjust redis ip range, adjust redis config
* test: make entrypoint.sh executable, fix entrypoint.sh if/elif
* test: add redis failover tests
* test: defer docker start, adjust sleep, attempt logout before login, attempt visit before login and tune timeouts, add additional logging
* test: add sentinel integration test
* test: add secondary node failure to tests, fix password usage, bump test timeout, add sleep
* feat: use sentinel failover cluster
* fix: renamed addrs to sentineladdrs upstream
* test(session): sentinel failover
* test: add redis standard back into testing
* test: move redis standalone test to traefik2
* fix/docs: apply suggestions from code review
2021-03-10 06:03:05 +07:00
|
|
|
mock.Ctx.Providers.SessionProvider = session.NewProvider(mock.Ctx.Configuration.Session, nil)
|
2020-04-05 19:37:21 +07:00
|
|
|
assert.Equal(t, time.Second*10, mock.Ctx.Providers.SessionProvider.Inactivity)
|
2020-01-18 05:48:48 +07:00
|
|
|
|
|
|
|
userSession := mock.Ctx.GetSession()
|
2020-05-02 23:20:40 +07:00
|
|
|
userSession.Username = testUsername
|
2020-01-18 05:48:48 +07:00
|
|
|
userSession.AuthenticationLevel = authentication.TwoFactor
|
|
|
|
userSession.LastActivity = clock.Now().Add(-1 * time.Hour).Unix()
|
2020-12-02 06:03:44 +07:00
|
|
|
|
|
|
|
err := mock.Ctx.SaveSession(userSession)
|
|
|
|
require.NoError(t, err)
|
2020-01-18 05:48:48 +07:00
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://two-factor.example.com")
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
2020-01-18 05:48:48 +07:00
|
|
|
|
2020-05-02 12:06:39 +07:00
|
|
|
// The session has been destroyed.
|
2020-01-18 05:48:48 +07:00
|
|
|
newUserSession := mock.Ctx.GetSession()
|
|
|
|
assert.Equal(t, "", newUserSession.Username)
|
|
|
|
assert.Equal(t, authentication.NotAuthenticated, newUserSession.AuthenticationLevel)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldKeepSessionWhenUserCheckedRememberMeAndIsInactiveForTooLong(t *testing.T) {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(t)
|
|
|
|
defer mock.Close()
|
|
|
|
|
2021-02-02 08:01:46 +07:00
|
|
|
mock.Clock.Set(time.Now())
|
2020-01-18 05:48:48 +07:00
|
|
|
|
2020-05-02 23:20:40 +07:00
|
|
|
mock.Ctx.Configuration.Session.Inactivity = testInactivity
|
2020-01-18 05:48:48 +07:00
|
|
|
|
|
|
|
userSession := mock.Ctx.GetSession()
|
2020-05-02 23:20:40 +07:00
|
|
|
userSession.Username = testUsername
|
2020-10-26 18:38:08 +07:00
|
|
|
userSession.Emails = []string{"john.doe@example.com"}
|
2020-01-18 05:48:48 +07:00
|
|
|
userSession.AuthenticationLevel = authentication.TwoFactor
|
2020-04-25 06:29:36 +07:00
|
|
|
userSession.LastActivity = 0
|
2020-01-18 05:48:48 +07:00
|
|
|
userSession.KeepMeLoggedIn = true
|
2021-02-02 08:01:46 +07:00
|
|
|
userSession.RefreshTTL = mock.Clock.Now().Add(5 * time.Minute)
|
2020-12-02 06:03:44 +07:00
|
|
|
|
|
|
|
err := mock.Ctx.SaveSession(userSession)
|
|
|
|
require.NoError(t, err)
|
2020-01-18 05:48:48 +07:00
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://two-factor.example.com")
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
2020-01-18 05:48:48 +07:00
|
|
|
|
2020-05-05 02:39:25 +07:00
|
|
|
// Check the session is still active.
|
2020-01-18 05:48:48 +07:00
|
|
|
newUserSession := mock.Ctx.GetSession()
|
|
|
|
assert.Equal(t, "john", newUserSession.Username)
|
|
|
|
assert.Equal(t, authentication.TwoFactor, newUserSession.AuthenticationLevel)
|
2020-04-25 06:29:36 +07:00
|
|
|
|
|
|
|
// Check the inactivity timestamp is set to 0 in case remember me is checked.
|
|
|
|
assert.Equal(t, int64(0), newUserSession.LastActivity)
|
2020-01-18 05:48:48 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldKeepSessionWhenInactivityTimeoutHasNotBeenExceeded(t *testing.T) {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(t)
|
|
|
|
defer mock.Close()
|
|
|
|
|
2021-02-02 08:01:46 +07:00
|
|
|
mock.Clock.Set(time.Now())
|
2020-01-18 05:48:48 +07:00
|
|
|
|
2020-05-02 23:20:40 +07:00
|
|
|
mock.Ctx.Configuration.Session.Inactivity = testInactivity
|
2020-01-18 05:48:48 +07:00
|
|
|
|
2021-02-02 08:01:46 +07:00
|
|
|
past := mock.Clock.Now().Add(-1 * time.Hour)
|
2020-04-25 06:29:36 +07:00
|
|
|
|
2020-01-18 05:48:48 +07:00
|
|
|
userSession := mock.Ctx.GetSession()
|
2020-05-02 23:20:40 +07:00
|
|
|
userSession.Username = testUsername
|
2020-10-26 18:38:08 +07:00
|
|
|
userSession.Emails = []string{"john.doe@example.com"}
|
2020-01-18 05:48:48 +07:00
|
|
|
userSession.AuthenticationLevel = authentication.TwoFactor
|
2020-04-25 06:29:36 +07:00
|
|
|
userSession.LastActivity = past.Unix()
|
2021-02-02 08:01:46 +07:00
|
|
|
userSession.RefreshTTL = mock.Clock.Now().Add(5 * time.Minute)
|
2020-12-02 06:03:44 +07:00
|
|
|
|
|
|
|
err := mock.Ctx.SaveSession(userSession)
|
|
|
|
require.NoError(t, err)
|
2020-01-18 05:48:48 +07:00
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://two-factor.example.com")
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
2020-01-18 05:48:48 +07:00
|
|
|
|
2020-05-02 12:06:39 +07:00
|
|
|
// The session has been destroyed.
|
2020-01-18 05:48:48 +07:00
|
|
|
newUserSession := mock.Ctx.GetSession()
|
|
|
|
assert.Equal(t, "john", newUserSession.Username)
|
|
|
|
assert.Equal(t, authentication.TwoFactor, newUserSession.AuthenticationLevel)
|
2020-04-25 06:29:36 +07:00
|
|
|
|
|
|
|
// Check the inactivity timestamp has been updated to current time in the new session.
|
2021-02-02 08:01:46 +07:00
|
|
|
assert.Equal(t, mock.Clock.Now().Unix(), newUserSession.LastActivity)
|
2020-04-25 06:29:36 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// In the case of Traefik and Nginx ingress controller in Kube, the response to an inactive
|
|
|
|
// session is 302 instead of 401.
|
|
|
|
func TestShouldRedirectWhenSessionInactiveForTooLongAndRDParamProvided(t *testing.T) {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(t)
|
|
|
|
defer mock.Close()
|
|
|
|
|
|
|
|
clock := mocks.TestingClock{}
|
|
|
|
clock.Set(time.Now())
|
|
|
|
|
2020-05-02 23:20:40 +07:00
|
|
|
mock.Ctx.Configuration.Session.Inactivity = testInactivity
|
2020-05-02 12:06:39 +07:00
|
|
|
// Reload the session provider since the configuration is indirect.
|
feat(session): add redis sentinel provider (#1768)
* feat(session): add redis sentinel provider
* refactor(session): use int for ports as per go standards
* refactor(configuration): adjust tests and validation
* refactor(configuration): add err format consts
* refactor(configuration): explicitly map redis structs
* refactor(session): merge redis/redis sentinel providers
* refactor(session): add additional checks to redis providers
* feat(session): add redis cluster provider
* fix: update config for new values
* fix: provide nil certpool to affected tests/mocks
* test: add additional tests to cover uncovered code
* docs: expand explanation of host and nodes relation for redis
* ci: add redis-sentinel to suite highavailability, add redis-sentinel quorum
* fix(session): sentinel password
* test: use redis alpine library image for redis sentinel, use expose instead of ports, use redis ip, adjust redis ip range, adjust redis config
* test: make entrypoint.sh executable, fix entrypoint.sh if/elif
* test: add redis failover tests
* test: defer docker start, adjust sleep, attempt logout before login, attempt visit before login and tune timeouts, add additional logging
* test: add sentinel integration test
* test: add secondary node failure to tests, fix password usage, bump test timeout, add sleep
* feat: use sentinel failover cluster
* fix: renamed addrs to sentineladdrs upstream
* test(session): sentinel failover
* test: add redis standard back into testing
* test: move redis standalone test to traefik2
* fix/docs: apply suggestions from code review
2021-03-10 06:03:05 +07:00
|
|
|
mock.Ctx.Providers.SessionProvider = session.NewProvider(mock.Ctx.Configuration.Session, nil)
|
2020-04-25 06:29:36 +07:00
|
|
|
assert.Equal(t, time.Second*10, mock.Ctx.Providers.SessionProvider.Inactivity)
|
|
|
|
|
|
|
|
past := clock.Now().Add(-1 * time.Hour)
|
|
|
|
|
|
|
|
userSession := mock.Ctx.GetSession()
|
2020-05-02 23:20:40 +07:00
|
|
|
userSession.Username = testUsername
|
2020-04-25 06:29:36 +07:00
|
|
|
userSession.AuthenticationLevel = authentication.TwoFactor
|
|
|
|
userSession.LastActivity = past.Unix()
|
2020-12-02 06:03:44 +07:00
|
|
|
|
|
|
|
err := mock.Ctx.SaveSession(userSession)
|
|
|
|
require.NoError(t, err)
|
2020-04-25 06:29:36 +07:00
|
|
|
|
|
|
|
mock.Ctx.QueryArgs().Add("rd", "https://login.example.com")
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://two-factor.example.com")
|
2021-03-05 11:18:31 +07:00
|
|
|
mock.Ctx.Request.Header.Set("X-Forwarded-Method", "GET")
|
2021-07-22 10:52:37 +07:00
|
|
|
mock.Ctx.Request.Header.Set("Accept", "text/html; charset=utf-8")
|
2022-04-08 11:13:47 +07:00
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
2020-04-25 06:29:36 +07:00
|
|
|
|
2022-07-08 19:18:52 +07:00
|
|
|
assert.Equal(t, "<a href=\"https://login.example.com/?rd=https%3A%2F%2Ftwo-factor.example.com&rm=GET\">302 Found</a>",
|
2020-04-25 06:29:36 +07:00
|
|
|
string(mock.Ctx.Response.Body()))
|
|
|
|
assert.Equal(t, 302, mock.Ctx.Response.StatusCode())
|
|
|
|
|
|
|
|
// Check the inactivity timestamp has been updated to current time in the new session.
|
|
|
|
newUserSession := mock.Ctx.GetSession()
|
|
|
|
assert.Equal(t, clock.Now().Unix(), newUserSession.LastActivity)
|
|
|
|
}
|
|
|
|
|
2021-07-16 10:43:48 +07:00
|
|
|
func TestShouldRedirectWithCorrectStatusCodeBasedOnRequestMethod(t *testing.T) {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(t)
|
|
|
|
defer mock.Close()
|
|
|
|
|
|
|
|
mock.Ctx.QueryArgs().Add("rd", "https://login.example.com")
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://two-factor.example.com")
|
|
|
|
mock.Ctx.Request.Header.Set("X-Forwarded-Method", "GET")
|
2021-07-22 10:52:37 +07:00
|
|
|
mock.Ctx.Request.Header.Set("Accept", "text/html; charset=utf-8")
|
2021-07-16 10:43:48 +07:00
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
2021-07-16 10:43:48 +07:00
|
|
|
|
2022-07-08 19:18:52 +07:00
|
|
|
assert.Equal(t, "<a href=\"https://login.example.com/?rd=https%3A%2F%2Ftwo-factor.example.com&rm=GET\">302 Found</a>",
|
2021-07-16 10:43:48 +07:00
|
|
|
string(mock.Ctx.Response.Body()))
|
|
|
|
assert.Equal(t, 302, mock.Ctx.Response.StatusCode())
|
|
|
|
|
|
|
|
mock.Ctx.QueryArgs().Add("rd", "https://login.example.com")
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://two-factor.example.com")
|
|
|
|
mock.Ctx.Request.Header.Set("X-Forwarded-Method", "POST")
|
2021-07-22 10:52:37 +07:00
|
|
|
mock.Ctx.Request.Header.Set("Accept", "text/html; charset=utf-8")
|
2021-07-16 10:43:48 +07:00
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
2021-07-16 10:43:48 +07:00
|
|
|
|
2022-07-08 19:18:52 +07:00
|
|
|
assert.Equal(t, "<a href=\"https://login.example.com/?rd=https%3A%2F%2Ftwo-factor.example.com&rm=POST\">303 See Other</a>",
|
2021-07-16 10:43:48 +07:00
|
|
|
string(mock.Ctx.Response.Body()))
|
|
|
|
assert.Equal(t, 303, mock.Ctx.Response.StatusCode())
|
|
|
|
}
|
|
|
|
|
2020-04-25 06:29:36 +07:00
|
|
|
func TestShouldUpdateInactivityTimestampEvenWhenHittingForbiddenResources(t *testing.T) {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(t)
|
|
|
|
defer mock.Close()
|
|
|
|
|
2021-02-02 08:01:46 +07:00
|
|
|
mock.Clock.Set(time.Now())
|
2020-04-25 06:29:36 +07:00
|
|
|
|
2020-05-02 23:20:40 +07:00
|
|
|
mock.Ctx.Configuration.Session.Inactivity = testInactivity
|
2020-04-25 06:29:36 +07:00
|
|
|
|
2021-02-02 08:01:46 +07:00
|
|
|
past := mock.Clock.Now().Add(-1 * time.Hour)
|
2020-04-25 06:29:36 +07:00
|
|
|
|
|
|
|
userSession := mock.Ctx.GetSession()
|
2020-05-02 23:20:40 +07:00
|
|
|
userSession.Username = testUsername
|
2020-04-25 06:29:36 +07:00
|
|
|
userSession.AuthenticationLevel = authentication.TwoFactor
|
|
|
|
userSession.LastActivity = past.Unix()
|
2021-02-02 08:01:46 +07:00
|
|
|
userSession.RefreshTTL = mock.Clock.Now().Add(5 * time.Minute)
|
2020-12-02 06:03:44 +07:00
|
|
|
|
|
|
|
err := mock.Ctx.SaveSession(userSession)
|
|
|
|
require.NoError(t, err)
|
2020-04-25 06:29:36 +07:00
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://deny.example.com")
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
2020-04-25 06:29:36 +07:00
|
|
|
|
2020-05-02 12:06:39 +07:00
|
|
|
// The resource if forbidden.
|
2020-04-25 06:29:36 +07:00
|
|
|
assert.Equal(t, 403, mock.Ctx.Response.StatusCode())
|
|
|
|
|
|
|
|
// Check the inactivity timestamp has been updated to current time in the new session.
|
|
|
|
newUserSession := mock.Ctx.GetSession()
|
2021-02-02 08:01:46 +07:00
|
|
|
assert.Equal(t, mock.Clock.Now().Unix(), newUserSession.LastActivity)
|
2020-01-18 05:48:48 +07:00
|
|
|
}
|
2020-01-18 21:57:42 +07:00
|
|
|
|
|
|
|
func TestShouldURLEncodeRedirectionURLParameter(t *testing.T) {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(t)
|
|
|
|
defer mock.Close()
|
|
|
|
|
2021-02-02 08:01:46 +07:00
|
|
|
mock.Clock.Set(time.Now())
|
|
|
|
|
2020-01-18 21:57:42 +07:00
|
|
|
userSession := mock.Ctx.GetSession()
|
2020-05-02 23:20:40 +07:00
|
|
|
userSession.Username = testUsername
|
2020-01-18 21:57:42 +07:00
|
|
|
userSession.AuthenticationLevel = authentication.NotAuthenticated
|
2021-02-02 08:01:46 +07:00
|
|
|
userSession.RefreshTTL = mock.Clock.Now().Add(5 * time.Minute)
|
2020-12-02 06:03:44 +07:00
|
|
|
|
|
|
|
err := mock.Ctx.SaveSession(userSession)
|
|
|
|
require.NoError(t, err)
|
2020-01-18 21:57:42 +07:00
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://two-factor.example.com")
|
2021-07-22 10:52:37 +07:00
|
|
|
mock.Ctx.Request.Header.Set("Accept", "text/html; charset=utf-8")
|
2020-01-18 21:57:42 +07:00
|
|
|
mock.Ctx.Request.SetHost("mydomain.com")
|
|
|
|
mock.Ctx.Request.SetRequestURI("/?rd=https://auth.mydomain.com")
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
2020-01-18 21:57:42 +07:00
|
|
|
|
2022-07-08 19:18:52 +07:00
|
|
|
assert.Equal(t, "<a href=\"https://auth.mydomain.com/?rd=https%3A%2F%2Ftwo-factor.example.com\">302 Found</a>",
|
2020-01-18 21:57:42 +07:00
|
|
|
string(mock.Ctx.Response.Body()))
|
|
|
|
}
|
2020-02-19 05:39:07 +07:00
|
|
|
|
|
|
|
func TestIsDomainProtected(t *testing.T) {
|
|
|
|
GetURL := func(u string) *url.URL {
|
|
|
|
x, err := url.ParseRequestURI(u)
|
|
|
|
require.NoError(t, err)
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2020-02-19 05:39:07 +07:00
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.True(t, isURLUnderProtectedDomain(
|
|
|
|
GetURL("http://mytest.example.com/abc/?query=abc"), "example.com"))
|
|
|
|
|
|
|
|
assert.True(t, isURLUnderProtectedDomain(
|
|
|
|
GetURL("http://example.com/abc/?query=abc"), "example.com"))
|
|
|
|
|
|
|
|
assert.True(t, isURLUnderProtectedDomain(
|
|
|
|
GetURL("https://mytest.example.com/abc/?query=abc"), "example.com"))
|
|
|
|
|
2020-05-02 12:06:39 +07:00
|
|
|
// Cookies readable by a service on a machine is also readable by a service on the same machine
|
|
|
|
// with a different port as mentioned in https://tools.ietf.org/html/rfc6265#section-8.5.
|
2020-02-19 05:39:07 +07:00
|
|
|
assert.True(t, isURLUnderProtectedDomain(
|
|
|
|
GetURL("https://mytest.example.com:8080/abc/?query=abc"), "example.com"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSchemeIsHTTPS(t *testing.T) {
|
|
|
|
GetURL := func(u string) *url.URL {
|
|
|
|
x, err := url.ParseRequestURI(u)
|
|
|
|
require.NoError(t, err)
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2020-02-19 05:39:07 +07:00
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.False(t, isSchemeHTTPS(
|
|
|
|
GetURL("http://mytest.example.com/abc/?query=abc")))
|
2020-02-28 06:28:53 +07:00
|
|
|
assert.False(t, isSchemeHTTPS(
|
|
|
|
GetURL("ws://mytest.example.com/abc/?query=abc")))
|
|
|
|
assert.False(t, isSchemeHTTPS(
|
|
|
|
GetURL("wss://mytest.example.com/abc/?query=abc")))
|
2020-02-19 05:39:07 +07:00
|
|
|
assert.True(t, isSchemeHTTPS(
|
|
|
|
GetURL("https://mytest.example.com/abc/?query=abc")))
|
|
|
|
}
|
2020-02-28 06:28:53 +07:00
|
|
|
|
|
|
|
func TestSchemeIsWSS(t *testing.T) {
|
|
|
|
GetURL := func(u string) *url.URL {
|
|
|
|
x, err := url.ParseRequestURI(u)
|
|
|
|
require.NoError(t, err)
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2020-02-28 06:28:53 +07:00
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.False(t, isSchemeWSS(
|
|
|
|
GetURL("ws://mytest.example.com/abc/?query=abc")))
|
|
|
|
assert.False(t, isSchemeWSS(
|
|
|
|
GetURL("http://mytest.example.com/abc/?query=abc")))
|
|
|
|
assert.False(t, isSchemeWSS(
|
|
|
|
GetURL("https://mytest.example.com/abc/?query=abc")))
|
|
|
|
assert.True(t, isSchemeWSS(
|
|
|
|
GetURL("wss://mytest.example.com/abc/?query=abc")))
|
|
|
|
}
|
2020-05-05 02:39:25 +07:00
|
|
|
|
|
|
|
func TestShouldNotRefreshUserGroupsFromBackend(t *testing.T) {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(t)
|
|
|
|
defer mock.Close()
|
|
|
|
|
|
|
|
// Setup pointer to john so we can adjust it during the test.
|
|
|
|
user := &authentication.UserDetails{
|
|
|
|
Username: "john",
|
|
|
|
Groups: []string{
|
|
|
|
"admin",
|
|
|
|
"users",
|
|
|
|
},
|
|
|
|
Emails: []string{
|
|
|
|
"john@example.com",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := verifyGetCfg
|
|
|
|
cfg.RefreshInterval = "disable"
|
2022-04-08 11:13:47 +07:00
|
|
|
verifyGet := VerifyGET(cfg)
|
2020-05-05 02:39:25 +07:00
|
|
|
|
|
|
|
mock.UserProviderMock.EXPECT().GetDetails("john").Times(0)
|
|
|
|
|
|
|
|
clock := mocks.TestingClock{}
|
|
|
|
clock.Set(time.Now())
|
|
|
|
|
|
|
|
userSession := mock.Ctx.GetSession()
|
|
|
|
userSession.Username = user.Username
|
|
|
|
userSession.AuthenticationLevel = authentication.TwoFactor
|
|
|
|
userSession.LastActivity = clock.Now().Unix()
|
|
|
|
userSession.Groups = user.Groups
|
|
|
|
userSession.Emails = user.Emails
|
|
|
|
userSession.KeepMeLoggedIn = true
|
|
|
|
err := mock.Ctx.SaveSession(userSession)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://two-factor.example.com")
|
|
|
|
verifyGet(mock.Ctx)
|
|
|
|
assert.Equal(t, 200, mock.Ctx.Response.StatusCode())
|
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://admin.example.com")
|
|
|
|
verifyGet(mock.Ctx)
|
|
|
|
assert.Equal(t, 200, mock.Ctx.Response.StatusCode())
|
|
|
|
|
|
|
|
// Check Refresh TTL has not been updated.
|
|
|
|
userSession = mock.Ctx.GetSession()
|
|
|
|
|
|
|
|
// Check user groups are correct.
|
|
|
|
require.Len(t, userSession.Groups, len(user.Groups))
|
|
|
|
assert.Equal(t, utils.RFC3339Zero, userSession.RefreshTTL.Unix())
|
|
|
|
assert.Equal(t, "admin", userSession.Groups[0])
|
|
|
|
assert.Equal(t, "users", userSession.Groups[1])
|
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://admin.example.com")
|
|
|
|
verifyGet(mock.Ctx)
|
|
|
|
assert.Equal(t, 200, mock.Ctx.Response.StatusCode())
|
|
|
|
|
|
|
|
// Check admin group is not removed from the session.
|
|
|
|
userSession = mock.Ctx.GetSession()
|
|
|
|
assert.Equal(t, utils.RFC3339Zero, userSession.RefreshTTL.Unix())
|
|
|
|
require.Len(t, userSession.Groups, 2)
|
|
|
|
assert.Equal(t, "admin", userSession.Groups[0])
|
|
|
|
assert.Equal(t, "users", userSession.Groups[1])
|
|
|
|
}
|
|
|
|
|
2021-02-02 08:01:46 +07:00
|
|
|
func TestShouldNotRefreshUserGroupsFromBackendWhenDisabled(t *testing.T) {
|
2020-05-05 02:39:25 +07:00
|
|
|
mock := mocks.NewMockAutheliaCtx(t)
|
|
|
|
defer mock.Close()
|
|
|
|
|
|
|
|
// Setup user john.
|
|
|
|
user := &authentication.UserDetails{
|
|
|
|
Username: "john",
|
|
|
|
Groups: []string{
|
|
|
|
"admin",
|
|
|
|
"users",
|
|
|
|
},
|
|
|
|
Emails: []string{
|
|
|
|
"john@example.com",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
mock.UserProviderMock.EXPECT().GetDetails("john").Times(0)
|
|
|
|
|
|
|
|
clock := mocks.TestingClock{}
|
|
|
|
clock.Set(time.Now())
|
|
|
|
|
|
|
|
userSession := mock.Ctx.GetSession()
|
|
|
|
userSession.Username = user.Username
|
|
|
|
userSession.AuthenticationLevel = authentication.TwoFactor
|
|
|
|
userSession.LastActivity = clock.Now().Unix()
|
|
|
|
userSession.RefreshTTL = clock.Now().Add(-1 * time.Minute)
|
|
|
|
userSession.Groups = user.Groups
|
|
|
|
userSession.Emails = user.Emails
|
|
|
|
userSession.KeepMeLoggedIn = true
|
|
|
|
err := mock.Ctx.SaveSession(userSession)
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://two-factor.example.com")
|
2021-02-02 08:01:46 +07:00
|
|
|
|
|
|
|
config := verifyGetCfg
|
|
|
|
config.RefreshInterval = schema.ProfileRefreshDisabled
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
VerifyGET(config)(mock.Ctx)
|
2020-05-05 02:39:25 +07:00
|
|
|
assert.Equal(t, 200, mock.Ctx.Response.StatusCode())
|
|
|
|
|
|
|
|
// Session time should NOT have been updated, it should still have a refresh TTL 1 minute in the past.
|
|
|
|
userSession = mock.Ctx.GetSession()
|
|
|
|
assert.Equal(t, clock.Now().Add(-1*time.Minute).Unix(), userSession.RefreshTTL.Unix())
|
|
|
|
}
|
|
|
|
|
2021-02-02 08:01:46 +07:00
|
|
|
func TestShouldDestroySessionWhenUserNotExist(t *testing.T) {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(t)
|
|
|
|
defer mock.Close()
|
|
|
|
|
|
|
|
// Setup user john.
|
|
|
|
user := &authentication.UserDetails{
|
|
|
|
Username: "john",
|
|
|
|
Groups: []string{
|
|
|
|
"admin",
|
|
|
|
"users",
|
|
|
|
},
|
|
|
|
Emails: []string{
|
|
|
|
"john@example.com",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
mock.UserProviderMock.EXPECT().GetDetails("john").Return(user, nil).Times(1)
|
|
|
|
|
|
|
|
clock := mocks.TestingClock{}
|
|
|
|
clock.Set(time.Now())
|
|
|
|
|
|
|
|
userSession := mock.Ctx.GetSession()
|
|
|
|
userSession.Username = user.Username
|
|
|
|
userSession.AuthenticationLevel = authentication.TwoFactor
|
|
|
|
userSession.LastActivity = clock.Now().Unix()
|
|
|
|
userSession.RefreshTTL = clock.Now().Add(-1 * time.Minute)
|
|
|
|
userSession.Groups = user.Groups
|
|
|
|
userSession.Emails = user.Emails
|
|
|
|
userSession.KeepMeLoggedIn = true
|
|
|
|
err := mock.Ctx.SaveSession(userSession)
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://two-factor.example.com")
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
2021-02-02 08:01:46 +07:00
|
|
|
assert.Equal(t, 200, mock.Ctx.Response.StatusCode())
|
|
|
|
|
|
|
|
// Session time should NOT have been updated, it should still have a refresh TTL 1 minute in the past.
|
|
|
|
userSession = mock.Ctx.GetSession()
|
|
|
|
assert.Equal(t, clock.Now().Add(5*time.Minute).Unix(), userSession.RefreshTTL.Unix())
|
|
|
|
|
2022-01-31 12:25:15 +07:00
|
|
|
// Simulate a Deleted User.
|
2021-02-02 08:01:46 +07:00
|
|
|
userSession.RefreshTTL = clock.Now().Add(-1 * time.Minute)
|
|
|
|
err = mock.Ctx.SaveSession(userSession)
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
mock.UserProviderMock.EXPECT().GetDetails("john").Return(nil, authentication.ErrUserNotFound).Times(1)
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
2021-02-02 08:01:46 +07:00
|
|
|
|
|
|
|
assert.Equal(t, 401, mock.Ctx.Response.StatusCode())
|
|
|
|
|
|
|
|
userSession = mock.Ctx.GetSession()
|
|
|
|
assert.Equal(t, "", userSession.Username)
|
|
|
|
assert.Equal(t, authentication.NotAuthenticated, userSession.AuthenticationLevel)
|
|
|
|
}
|
|
|
|
|
2020-05-05 02:39:25 +07:00
|
|
|
func TestShouldGetRemovedUserGroupsFromBackend(t *testing.T) {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(t)
|
|
|
|
defer mock.Close()
|
|
|
|
|
|
|
|
// Setup pointer to john so we can adjust it during the test.
|
|
|
|
user := &authentication.UserDetails{
|
|
|
|
Username: "john",
|
|
|
|
Groups: []string{
|
|
|
|
"admin",
|
|
|
|
"users",
|
|
|
|
},
|
|
|
|
Emails: []string{
|
|
|
|
"john@example.com",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
verifyGet := VerifyGET(verifyGetCfg)
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2020-05-05 02:39:25 +07:00
|
|
|
mock.UserProviderMock.EXPECT().GetDetails("john").Return(user, nil).Times(2)
|
|
|
|
|
|
|
|
clock := mocks.TestingClock{}
|
|
|
|
clock.Set(time.Now())
|
|
|
|
|
|
|
|
userSession := mock.Ctx.GetSession()
|
|
|
|
userSession.Username = user.Username
|
|
|
|
userSession.AuthenticationLevel = authentication.TwoFactor
|
|
|
|
userSession.LastActivity = clock.Now().Unix()
|
|
|
|
userSession.RefreshTTL = clock.Now().Add(-1 * time.Minute)
|
|
|
|
userSession.Groups = user.Groups
|
|
|
|
userSession.Emails = user.Emails
|
|
|
|
userSession.KeepMeLoggedIn = true
|
|
|
|
err := mock.Ctx.SaveSession(userSession)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://two-factor.example.com")
|
|
|
|
verifyGet(mock.Ctx)
|
|
|
|
assert.Equal(t, 200, mock.Ctx.Response.StatusCode())
|
|
|
|
|
|
|
|
// Request should get refresh settings and new user details.
|
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://admin.example.com")
|
|
|
|
verifyGet(mock.Ctx)
|
|
|
|
assert.Equal(t, 200, mock.Ctx.Response.StatusCode())
|
|
|
|
|
|
|
|
// Check Refresh TTL has been updated since admin.example.com has a group subject and refresh is enabled.
|
|
|
|
userSession = mock.Ctx.GetSession()
|
|
|
|
|
|
|
|
// Check user groups are correct.
|
|
|
|
require.Len(t, userSession.Groups, len(user.Groups))
|
|
|
|
assert.Equal(t, clock.Now().Add(5*time.Minute).Unix(), userSession.RefreshTTL.Unix())
|
|
|
|
assert.Equal(t, "admin", userSession.Groups[0])
|
|
|
|
assert.Equal(t, "users", userSession.Groups[1])
|
|
|
|
|
|
|
|
// Remove the admin group, and force the next request to refresh.
|
|
|
|
user.Groups = []string{"users"}
|
|
|
|
userSession.RefreshTTL = clock.Now().Add(-1 * time.Second)
|
|
|
|
err = mock.Ctx.SaveSession(userSession)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://admin.example.com")
|
|
|
|
verifyGet(mock.Ctx)
|
|
|
|
assert.Equal(t, 403, mock.Ctx.Response.StatusCode())
|
|
|
|
|
|
|
|
// Check admin group is removed from the session.
|
|
|
|
userSession = mock.Ctx.GetSession()
|
|
|
|
assert.Equal(t, clock.Now().Add(5*time.Minute).Unix(), userSession.RefreshTTL.Unix())
|
|
|
|
require.Len(t, userSession.Groups, 1)
|
|
|
|
assert.Equal(t, "users", userSession.Groups[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldGetAddedUserGroupsFromBackend(t *testing.T) {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(t)
|
|
|
|
|
|
|
|
// Setup pointer to john so we can adjust it during the test.
|
|
|
|
user := &authentication.UserDetails{
|
|
|
|
Username: "john",
|
|
|
|
Groups: []string{
|
|
|
|
"admin",
|
|
|
|
"users",
|
|
|
|
},
|
|
|
|
Emails: []string{
|
|
|
|
"john@example.com",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-02-02 08:01:46 +07:00
|
|
|
mock.UserProviderMock.EXPECT().GetDetails("john").Return(user, nil).Times(1)
|
2020-05-05 02:39:25 +07:00
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
verifyGet := VerifyGET(verifyGetCfg)
|
2020-05-05 02:39:25 +07:00
|
|
|
|
2021-02-02 08:01:46 +07:00
|
|
|
mock.Clock.Set(time.Now())
|
2020-05-05 02:39:25 +07:00
|
|
|
|
|
|
|
userSession := mock.Ctx.GetSession()
|
|
|
|
userSession.Username = user.Username
|
|
|
|
userSession.AuthenticationLevel = authentication.TwoFactor
|
2021-02-02 08:01:46 +07:00
|
|
|
userSession.LastActivity = mock.Clock.Now().Unix()
|
|
|
|
userSession.RefreshTTL = mock.Clock.Now().Add(-1 * time.Minute)
|
2020-05-05 02:39:25 +07:00
|
|
|
userSession.Groups = user.Groups
|
|
|
|
userSession.Emails = user.Emails
|
|
|
|
userSession.KeepMeLoggedIn = true
|
|
|
|
err := mock.Ctx.SaveSession(userSession)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://two-factor.example.com")
|
|
|
|
verifyGet(mock.Ctx)
|
|
|
|
assert.Equal(t, 200, mock.Ctx.Response.StatusCode())
|
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://grafana.example.com")
|
|
|
|
verifyGet(mock.Ctx)
|
|
|
|
assert.Equal(t, 403, mock.Ctx.Response.StatusCode())
|
|
|
|
|
|
|
|
// Check Refresh TTL has been updated since grafana.example.com has a group subject and refresh is enabled.
|
|
|
|
userSession = mock.Ctx.GetSession()
|
|
|
|
|
|
|
|
// Check user groups are correct.
|
|
|
|
require.Len(t, userSession.Groups, len(user.Groups))
|
2021-02-02 08:01:46 +07:00
|
|
|
assert.Equal(t, mock.Clock.Now().Add(5*time.Minute).Unix(), userSession.RefreshTTL.Unix())
|
2020-05-05 02:39:25 +07:00
|
|
|
assert.Equal(t, "admin", userSession.Groups[0])
|
|
|
|
assert.Equal(t, "users", userSession.Groups[1])
|
|
|
|
|
|
|
|
// Add the grafana group, and force the next request to refresh.
|
|
|
|
user.Groups = append(user.Groups, "grafana")
|
2021-02-02 08:01:46 +07:00
|
|
|
userSession.RefreshTTL = mock.Clock.Now().Add(-1 * time.Second)
|
2020-05-05 02:39:25 +07:00
|
|
|
err = mock.Ctx.SaveSession(userSession)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Reset otherwise we get the last 403 when we check the Response. Is there a better way to do this?
|
|
|
|
mock.Close()
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2020-05-05 02:39:25 +07:00
|
|
|
mock = mocks.NewMockAutheliaCtx(t)
|
|
|
|
defer mock.Close()
|
|
|
|
err = mock.Ctx.SaveSession(userSession)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-02-02 08:01:46 +07:00
|
|
|
mock.Clock.Set(time.Now())
|
|
|
|
|
2020-05-05 02:39:25 +07:00
|
|
|
gomock.InOrder(
|
|
|
|
mock.UserProviderMock.EXPECT().GetDetails("john").Return(user, nil).Times(1),
|
|
|
|
)
|
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://grafana.example.com")
|
2022-04-08 11:13:47 +07:00
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
2020-05-05 02:39:25 +07:00
|
|
|
assert.Equal(t, 200, mock.Ctx.Response.StatusCode())
|
|
|
|
|
|
|
|
// Check admin group is removed from the session.
|
|
|
|
userSession = mock.Ctx.GetSession()
|
|
|
|
assert.Equal(t, true, userSession.KeepMeLoggedIn)
|
|
|
|
assert.Equal(t, authentication.TwoFactor, userSession.AuthenticationLevel)
|
2021-02-02 08:01:46 +07:00
|
|
|
assert.Equal(t, mock.Clock.Now().Add(5*time.Minute).Unix(), userSession.RefreshTTL.Unix())
|
2020-05-05 02:39:25 +07:00
|
|
|
require.Len(t, userSession.Groups, 3)
|
|
|
|
assert.Equal(t, "admin", userSession.Groups[0])
|
|
|
|
assert.Equal(t, "users", userSession.Groups[1])
|
|
|
|
assert.Equal(t, "grafana", userSession.Groups[2])
|
|
|
|
}
|
2020-12-02 06:03:44 +07:00
|
|
|
|
|
|
|
func TestShouldCheckValidSessionUsernameHeaderAndReturn200(t *testing.T) {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(t)
|
|
|
|
defer mock.Close()
|
|
|
|
|
2021-02-02 08:01:46 +07:00
|
|
|
mock.Clock.Set(time.Now())
|
|
|
|
|
2020-12-02 06:03:44 +07:00
|
|
|
expectedStatusCode := 200
|
|
|
|
|
|
|
|
userSession := mock.Ctx.GetSession()
|
|
|
|
userSession.Username = testUsername
|
|
|
|
userSession.AuthenticationLevel = authentication.OneFactor
|
2021-02-02 08:01:46 +07:00
|
|
|
userSession.RefreshTTL = mock.Clock.Now().Add(5 * time.Minute)
|
2020-12-02 06:03:44 +07:00
|
|
|
|
|
|
|
err := mock.Ctx.SaveSession(userSession)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://one-factor.example.com")
|
2021-12-02 09:21:46 +07:00
|
|
|
mock.Ctx.Request.Header.SetBytesK(headerSessionUsername, testUsername)
|
2022-04-08 11:13:47 +07:00
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
2020-12-02 06:03:44 +07:00
|
|
|
|
|
|
|
assert.Equal(t, expectedStatusCode, mock.Ctx.Response.StatusCode())
|
|
|
|
assert.Equal(t, "", string(mock.Ctx.Response.Body()))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldCheckInvalidSessionUsernameHeaderAndReturn401(t *testing.T) {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(t)
|
|
|
|
defer mock.Close()
|
|
|
|
|
2021-02-02 08:01:46 +07:00
|
|
|
mock.Clock.Set(time.Now())
|
|
|
|
|
2020-12-02 06:03:44 +07:00
|
|
|
expectedStatusCode := 401
|
|
|
|
|
|
|
|
userSession := mock.Ctx.GetSession()
|
|
|
|
userSession.Username = testUsername
|
|
|
|
userSession.AuthenticationLevel = authentication.OneFactor
|
2021-02-02 08:01:46 +07:00
|
|
|
userSession.RefreshTTL = mock.Clock.Now().Add(5 * time.Minute)
|
2020-12-02 06:03:44 +07:00
|
|
|
|
|
|
|
err := mock.Ctx.SaveSession(userSession)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://one-factor.example.com")
|
2021-12-02 09:21:46 +07:00
|
|
|
mock.Ctx.Request.Header.SetBytesK(headerSessionUsername, "root")
|
2022-04-08 11:13:47 +07:00
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
2020-12-02 06:03:44 +07:00
|
|
|
|
|
|
|
assert.Equal(t, expectedStatusCode, mock.Ctx.Response.StatusCode())
|
2022-07-08 19:18:52 +07:00
|
|
|
assert.Equal(t, "401 Unauthorized", string(mock.Ctx.Response.Body()))
|
2020-12-02 06:03:44 +07:00
|
|
|
}
|
2021-02-02 08:01:46 +07:00
|
|
|
|
|
|
|
func TestGetProfileRefreshSettings(t *testing.T) {
|
|
|
|
cfg := verifyGetCfg
|
|
|
|
|
|
|
|
refresh, interval := getProfileRefreshSettings(cfg)
|
|
|
|
|
|
|
|
assert.Equal(t, true, refresh)
|
|
|
|
assert.Equal(t, 5*time.Minute, interval)
|
|
|
|
|
|
|
|
cfg.RefreshInterval = schema.ProfileRefreshDisabled
|
|
|
|
|
|
|
|
refresh, interval = getProfileRefreshSettings(cfg)
|
|
|
|
|
|
|
|
assert.Equal(t, false, refresh)
|
|
|
|
assert.Equal(t, time.Duration(0), interval)
|
|
|
|
|
|
|
|
cfg.RefreshInterval = schema.ProfileRefreshAlways
|
|
|
|
|
|
|
|
refresh, interval = getProfileRefreshSettings(cfg)
|
|
|
|
|
|
|
|
assert.Equal(t, true, refresh)
|
|
|
|
assert.Equal(t, time.Duration(0), interval)
|
|
|
|
}
|
2022-07-05 06:58:35 +07:00
|
|
|
|
|
|
|
func TestShouldNotRedirectRequestsForBypassACLWhenInactiveForTooLong(t *testing.T) {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(t)
|
|
|
|
defer mock.Close()
|
|
|
|
|
|
|
|
clock := mocks.TestingClock{}
|
|
|
|
clock.Set(time.Now())
|
|
|
|
past := clock.Now().Add(-1 * time.Hour)
|
|
|
|
|
|
|
|
mock.Ctx.Configuration.Session.Inactivity = testInactivity
|
|
|
|
// Reload the session provider since the configuration is indirect.
|
|
|
|
mock.Ctx.Providers.SessionProvider = session.NewProvider(mock.Ctx.Configuration.Session, nil)
|
|
|
|
assert.Equal(t, time.Second*10, mock.Ctx.Providers.SessionProvider.Inactivity)
|
|
|
|
|
|
|
|
userSession := mock.Ctx.GetSession()
|
|
|
|
userSession.Username = testUsername
|
|
|
|
userSession.AuthenticationLevel = authentication.TwoFactor
|
|
|
|
userSession.LastActivity = past.Unix()
|
|
|
|
|
|
|
|
err := mock.Ctx.SaveSession(userSession)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Should respond 200 OK.
|
|
|
|
mock.Ctx.QueryArgs().Add("rd", "https://login.example.com")
|
|
|
|
mock.Ctx.Request.Header.Set("X-Forwarded-Method", "GET")
|
|
|
|
mock.Ctx.Request.Header.Set("Accept", "text/html; charset=utf-8")
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://bypass.example.com")
|
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
|
|
|
assert.Equal(t, fasthttp.StatusOK, mock.Ctx.Response.StatusCode())
|
|
|
|
assert.Nil(t, mock.Ctx.Response.Header.Peek("Location"))
|
|
|
|
|
|
|
|
// Should respond 302 Found.
|
|
|
|
mock.Ctx.QueryArgs().Add("rd", "https://login.example.com")
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://two-factor.example.com")
|
|
|
|
mock.Ctx.Request.Header.Set("X-Forwarded-Method", "GET")
|
|
|
|
mock.Ctx.Request.Header.Set("Accept", "text/html; charset=utf-8")
|
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
|
|
|
assert.Equal(t, fasthttp.StatusFound, mock.Ctx.Response.StatusCode())
|
|
|
|
assert.Equal(t, "https://login.example.com/?rd=https%3A%2F%2Ftwo-factor.example.com&rm=GET", string(mock.Ctx.Response.Header.Peek("Location")))
|
|
|
|
|
|
|
|
// Should respond 401 Unauthorized.
|
|
|
|
mock.Ctx.QueryArgs().Del("rd")
|
|
|
|
mock.Ctx.Request.Header.Set("X-Original-URL", "https://two-factor.example.com")
|
|
|
|
mock.Ctx.Request.Header.Set("X-Forwarded-Method", "GET")
|
|
|
|
mock.Ctx.Request.Header.Set("Accept", "text/html; charset=utf-8")
|
|
|
|
VerifyGET(verifyGetCfg)(mock.Ctx)
|
|
|
|
assert.Equal(t, fasthttp.StatusUnauthorized, mock.Ctx.Response.StatusCode())
|
|
|
|
assert.Nil(t, mock.Ctx.Response.Header.Peek("Location"))
|
|
|
|
}
|
2022-07-08 09:32:43 +07:00
|
|
|
|
|
|
|
func TestIsSessionInactiveTooLong(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
have *session.UserSession
|
|
|
|
now time.Time
|
|
|
|
inactivity time.Duration
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "ShouldNotBeInactiveTooLong",
|
|
|
|
have: &session.UserSession{Username: "john", LastActivity: 1656994960},
|
|
|
|
now: time.Unix(1656994970, 0),
|
|
|
|
inactivity: time.Second * 90,
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ShouldNotBeInactiveTooLongIfAnonymous",
|
|
|
|
have: &session.UserSession{Username: "", LastActivity: 1656994960},
|
|
|
|
now: time.Unix(1656994990, 0),
|
|
|
|
inactivity: time.Second * 20,
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ShouldNotBeInactiveTooLongIfRemembered",
|
|
|
|
have: &session.UserSession{Username: "john", LastActivity: 1656994960, KeepMeLoggedIn: true},
|
|
|
|
now: time.Unix(1656994990, 0),
|
|
|
|
inactivity: time.Second * 20,
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ShouldNotBeInactiveTooLongIfDisabled",
|
|
|
|
have: &session.UserSession{Username: "john", LastActivity: 1656994960},
|
|
|
|
now: time.Unix(1656994990, 0),
|
|
|
|
inactivity: time.Second * 0,
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ShouldBeInactiveTooLong",
|
|
|
|
have: &session.UserSession{Username: "john", LastActivity: 1656994960},
|
|
|
|
now: time.Unix(4656994990, 0),
|
|
|
|
inactivity: time.Second * 1,
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
ctx := mocks.NewMockAutheliaCtx(t)
|
|
|
|
|
|
|
|
defer ctx.Close()
|
|
|
|
|
|
|
|
ctx.Ctx.Configuration.Session.Inactivity = tc.inactivity
|
|
|
|
ctx.Ctx.Providers.SessionProvider = session.NewProvider(ctx.Ctx.Configuration.Session, nil)
|
|
|
|
|
|
|
|
ctx.Clock.Set(tc.now)
|
|
|
|
ctx.Ctx.Clock = &ctx.Clock
|
|
|
|
|
|
|
|
actual := isSessionInactiveTooLong(ctx.Ctx, tc.have, tc.have.Username == "")
|
|
|
|
|
|
|
|
assert.Equal(t, tc.expected, actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|