2019-04-25 04:52:08 +07:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-05-21 05:03:15 +07:00
|
|
|
"sync"
|
2019-04-25 04:52:08 +07:00
|
|
|
"testing"
|
2020-05-21 05:03:15 +07:00
|
|
|
"time"
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/suite"
|
2020-04-05 19:37:21 +07:00
|
|
|
|
|
|
|
"github.com/authelia/authelia/internal/authentication"
|
2020-05-01 13:56:42 +07:00
|
|
|
"github.com/authelia/authelia/internal/authorization"
|
|
|
|
"github.com/authelia/authelia/internal/configuration/schema"
|
|
|
|
"github.com/authelia/authelia/internal/mocks"
|
|
|
|
"github.com/authelia/authelia/internal/models"
|
2019-04-25 04:52:08 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
type FirstFactorSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
mock *mocks.MockAutheliaCtx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FirstFactorSuite) SetupTest() {
|
|
|
|
s.mock = mocks.NewMockAutheliaCtx(s.T())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FirstFactorSuite) TearDownTest() {
|
|
|
|
s.mock.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FirstFactorSuite) TestShouldFailIfBodyIsNil() {
|
2020-05-21 05:03:15 +07:00
|
|
|
FirstFactorPost(0, false)(s.mock.Ctx)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
// No body
|
|
|
|
assert.Equal(s.T(), "Unable to parse body: unexpected end of JSON input", s.mock.Hook.LastEntry().Message)
|
2020-05-06 04:27:38 +07:00
|
|
|
s.mock.Assert401KO(s.T(), "Authentication failed. Check your credentials.")
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FirstFactorSuite) TestShouldFailIfBodyIsInBadFormat() {
|
|
|
|
// Missing password
|
|
|
|
s.mock.Ctx.Request.SetBodyString(`{
|
|
|
|
"username": "test"
|
|
|
|
}`)
|
2020-05-21 05:03:15 +07:00
|
|
|
FirstFactorPost(0, false)(s.mock.Ctx)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
assert.Equal(s.T(), "Unable to validate body: password: non zero value required", s.mock.Hook.LastEntry().Message)
|
2020-05-06 04:27:38 +07:00
|
|
|
s.mock.Assert401KO(s.T(), "Authentication failed. Check your credentials.")
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FirstFactorSuite) TestShouldFailIfUserProviderCheckPasswordFail() {
|
|
|
|
s.mock.UserProviderMock.
|
|
|
|
EXPECT().
|
|
|
|
CheckUserPassword(gomock.Eq("test"), gomock.Eq("hello")).
|
|
|
|
Return(false, fmt.Errorf("Failed"))
|
|
|
|
|
2019-11-30 21:33:45 +07:00
|
|
|
s.mock.StorageProviderMock.
|
|
|
|
EXPECT().
|
|
|
|
AppendAuthenticationLog(gomock.Eq(models.AuthenticationAttempt{
|
|
|
|
Username: "test",
|
|
|
|
Successful: false,
|
|
|
|
Time: s.mock.Clock.Now(),
|
|
|
|
}))
|
|
|
|
|
2019-04-25 04:52:08 +07:00
|
|
|
s.mock.Ctx.Request.SetBodyString(`{
|
|
|
|
"username": "test",
|
|
|
|
"password": "hello",
|
|
|
|
"keepMeLoggedIn": true
|
|
|
|
}`)
|
2020-05-21 05:03:15 +07:00
|
|
|
FirstFactorPost(0, false)(s.mock.Ctx)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
assert.Equal(s.T(), "Error while checking password for user test: Failed", s.mock.Hook.LastEntry().Message)
|
2020-05-06 04:27:38 +07:00
|
|
|
s.mock.Assert401KO(s.T(), "Authentication failed. Check your credentials.")
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2019-11-25 03:27:59 +07:00
|
|
|
func (s *FirstFactorSuite) TestShouldCheckAuthenticationIsMarkedWhenInvalidCredentials() {
|
|
|
|
s.mock.UserProviderMock.
|
|
|
|
EXPECT().
|
|
|
|
CheckUserPassword(gomock.Eq("test"), gomock.Eq("hello")).
|
|
|
|
Return(false, fmt.Errorf("Invalid credentials"))
|
|
|
|
|
|
|
|
s.mock.StorageProviderMock.
|
|
|
|
EXPECT().
|
|
|
|
AppendAuthenticationLog(gomock.Eq(models.AuthenticationAttempt{
|
|
|
|
Username: "test",
|
|
|
|
Successful: false,
|
2019-11-30 21:33:45 +07:00
|
|
|
Time: s.mock.Clock.Now(),
|
2019-11-25 03:27:59 +07:00
|
|
|
}))
|
|
|
|
|
|
|
|
s.mock.Ctx.Request.SetBodyString(`{
|
|
|
|
"username": "test",
|
|
|
|
"password": "hello",
|
|
|
|
"keepMeLoggedIn": true
|
|
|
|
}`)
|
|
|
|
|
2020-05-21 05:03:15 +07:00
|
|
|
FirstFactorPost(0, false)(s.mock.Ctx)
|
2019-11-25 03:27:59 +07:00
|
|
|
}
|
|
|
|
|
2019-04-25 04:52:08 +07:00
|
|
|
func (s *FirstFactorSuite) TestShouldFailIfUserProviderGetDetailsFail() {
|
|
|
|
s.mock.UserProviderMock.
|
|
|
|
EXPECT().
|
|
|
|
CheckUserPassword(gomock.Eq("test"), gomock.Eq("hello")).
|
|
|
|
Return(true, nil)
|
|
|
|
|
|
|
|
s.mock.StorageProviderMock.
|
|
|
|
EXPECT().
|
|
|
|
AppendAuthenticationLog(gomock.Any()).
|
|
|
|
Return(nil)
|
|
|
|
|
2019-11-30 21:33:45 +07:00
|
|
|
s.mock.UserProviderMock.
|
|
|
|
EXPECT().
|
|
|
|
GetDetails(gomock.Eq("test")).
|
|
|
|
Return(nil, fmt.Errorf("Failed"))
|
|
|
|
|
2019-04-25 04:52:08 +07:00
|
|
|
s.mock.Ctx.Request.SetBodyString(`{
|
|
|
|
"username": "test",
|
|
|
|
"password": "hello",
|
|
|
|
"keepMeLoggedIn": true
|
|
|
|
}`)
|
2020-05-21 05:03:15 +07:00
|
|
|
FirstFactorPost(0, false)(s.mock.Ctx)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
assert.Equal(s.T(), "Error while retrieving details from user test: Failed", s.mock.Hook.LastEntry().Message)
|
2020-05-06 04:27:38 +07:00
|
|
|
s.mock.Assert401KO(s.T(), "Authentication failed. Check your credentials.")
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2019-11-30 21:33:45 +07:00
|
|
|
func (s *FirstFactorSuite) TestShouldFailIfAuthenticationMarkFail() {
|
2019-04-25 04:52:08 +07:00
|
|
|
s.mock.UserProviderMock.
|
|
|
|
EXPECT().
|
|
|
|
CheckUserPassword(gomock.Eq("test"), gomock.Eq("hello")).
|
|
|
|
Return(true, nil)
|
|
|
|
|
|
|
|
s.mock.StorageProviderMock.
|
|
|
|
EXPECT().
|
|
|
|
AppendAuthenticationLog(gomock.Any()).
|
|
|
|
Return(fmt.Errorf("failed"))
|
|
|
|
|
|
|
|
s.mock.Ctx.Request.SetBodyString(`{
|
|
|
|
"username": "test",
|
|
|
|
"password": "hello",
|
|
|
|
"keepMeLoggedIn": true
|
|
|
|
}`)
|
2020-05-21 05:03:15 +07:00
|
|
|
FirstFactorPost(0, false)(s.mock.Ctx)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
assert.Equal(s.T(), "Unable to mark authentication: failed", s.mock.Hook.LastEntry().Message)
|
2020-05-06 04:27:38 +07:00
|
|
|
s.mock.Assert401KO(s.T(), "Authentication failed. Check your credentials.")
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2020-01-18 05:48:48 +07:00
|
|
|
func (s *FirstFactorSuite) TestShouldAuthenticateUserWithRememberMeChecked() {
|
2019-04-25 04:52:08 +07:00
|
|
|
s.mock.UserProviderMock.
|
|
|
|
EXPECT().
|
|
|
|
CheckUserPassword(gomock.Eq("test"), gomock.Eq("hello")).
|
|
|
|
Return(true, nil)
|
|
|
|
|
|
|
|
s.mock.UserProviderMock.
|
|
|
|
EXPECT().
|
|
|
|
GetDetails(gomock.Eq("test")).
|
|
|
|
Return(&authentication.UserDetails{
|
2020-03-15 14:10:25 +07:00
|
|
|
Username: "test",
|
|
|
|
Emails: []string{"test@example.com"},
|
|
|
|
Groups: []string{"dev", "admins"},
|
2019-04-25 04:52:08 +07:00
|
|
|
}, nil)
|
|
|
|
|
|
|
|
s.mock.StorageProviderMock.
|
|
|
|
EXPECT().
|
|
|
|
AppendAuthenticationLog(gomock.Any()).
|
|
|
|
Return(nil)
|
|
|
|
|
|
|
|
s.mock.Ctx.Request.SetBodyString(`{
|
2020-01-18 05:55:02 +07:00
|
|
|
"username": "test",
|
|
|
|
"password": "hello",
|
|
|
|
"keepMeLoggedIn": true
|
|
|
|
}`)
|
2020-05-21 05:03:15 +07:00
|
|
|
FirstFactorPost(0, false)(s.mock.Ctx)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
// Respond with 200.
|
|
|
|
assert.Equal(s.T(), 200, s.mock.Ctx.Response.StatusCode())
|
|
|
|
assert.Equal(s.T(), []byte("{\"status\":\"OK\"}"), s.mock.Ctx.Response.Body())
|
|
|
|
|
|
|
|
// And store authentication in session.
|
|
|
|
session := s.mock.Ctx.GetSession()
|
|
|
|
assert.Equal(s.T(), "test", session.Username)
|
2020-01-18 05:48:48 +07:00
|
|
|
assert.Equal(s.T(), true, session.KeepMeLoggedIn)
|
2019-04-25 04:52:08 +07:00
|
|
|
assert.Equal(s.T(), authentication.OneFactor, session.AuthenticationLevel)
|
|
|
|
assert.Equal(s.T(), []string{"test@example.com"}, session.Emails)
|
2019-11-30 23:49:52 +07:00
|
|
|
assert.Equal(s.T(), []string{"dev", "admins"}, session.Groups)
|
2020-01-18 05:48:48 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FirstFactorSuite) TestShouldAuthenticateUserWithRememberMeUnchecked() {
|
|
|
|
s.mock.UserProviderMock.
|
|
|
|
EXPECT().
|
|
|
|
CheckUserPassword(gomock.Eq("test"), gomock.Eq("hello")).
|
|
|
|
Return(true, nil)
|
|
|
|
|
|
|
|
s.mock.UserProviderMock.
|
|
|
|
EXPECT().
|
|
|
|
GetDetails(gomock.Eq("test")).
|
|
|
|
Return(&authentication.UserDetails{
|
2020-03-15 14:10:25 +07:00
|
|
|
Username: "test",
|
|
|
|
Emails: []string{"test@example.com"},
|
|
|
|
Groups: []string{"dev", "admins"},
|
2020-01-18 05:48:48 +07:00
|
|
|
}, nil)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2020-01-18 05:48:48 +07:00
|
|
|
s.mock.StorageProviderMock.
|
|
|
|
EXPECT().
|
|
|
|
AppendAuthenticationLog(gomock.Any()).
|
|
|
|
Return(nil)
|
|
|
|
|
|
|
|
s.mock.Ctx.Request.SetBodyString(`{
|
2020-01-18 05:55:02 +07:00
|
|
|
"username": "test",
|
|
|
|
"password": "hello",
|
2021-03-05 11:18:31 +07:00
|
|
|
"requestMethod": "GET",
|
2020-01-18 05:55:02 +07:00
|
|
|
"keepMeLoggedIn": false
|
|
|
|
}`)
|
2020-05-21 05:03:15 +07:00
|
|
|
FirstFactorPost(0, false)(s.mock.Ctx)
|
2020-01-18 05:48:48 +07:00
|
|
|
|
|
|
|
// Respond with 200.
|
|
|
|
assert.Equal(s.T(), 200, s.mock.Ctx.Response.StatusCode())
|
|
|
|
assert.Equal(s.T(), []byte("{\"status\":\"OK\"}"), s.mock.Ctx.Response.Body())
|
|
|
|
|
|
|
|
// And store authentication in session.
|
|
|
|
session := s.mock.Ctx.GetSession()
|
|
|
|
assert.Equal(s.T(), "test", session.Username)
|
|
|
|
assert.Equal(s.T(), false, session.KeepMeLoggedIn)
|
|
|
|
assert.Equal(s.T(), authentication.OneFactor, session.AuthenticationLevel)
|
|
|
|
assert.Equal(s.T(), []string{"test@example.com"}, session.Emails)
|
|
|
|
assert.Equal(s.T(), []string{"dev", "admins"}, session.Groups)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2020-03-15 14:10:25 +07:00
|
|
|
func (s *FirstFactorSuite) TestShouldSaveUsernameFromAuthenticationBackendInSession() {
|
|
|
|
s.mock.UserProviderMock.
|
|
|
|
EXPECT().
|
|
|
|
CheckUserPassword(gomock.Eq("test"), gomock.Eq("hello")).
|
|
|
|
Return(true, nil)
|
|
|
|
|
|
|
|
s.mock.UserProviderMock.
|
|
|
|
EXPECT().
|
|
|
|
GetDetails(gomock.Eq("test")).
|
|
|
|
Return(&authentication.UserDetails{
|
|
|
|
// This is the name in authentication backend, in some setups the binding is
|
|
|
|
// case insensitive but the user ID in session must match the user in LDAP
|
|
|
|
// for the other modules of Authelia to be coherent.
|
|
|
|
Username: "Test",
|
|
|
|
Emails: []string{"test@example.com"},
|
|
|
|
Groups: []string{"dev", "admins"},
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
s.mock.StorageProviderMock.
|
|
|
|
EXPECT().
|
|
|
|
AppendAuthenticationLog(gomock.Any()).
|
|
|
|
Return(nil)
|
|
|
|
|
|
|
|
s.mock.Ctx.Request.SetBodyString(`{
|
|
|
|
"username": "test",
|
|
|
|
"password": "hello",
|
2021-03-05 11:18:31 +07:00
|
|
|
"requestMethod": "GET",
|
2020-03-15 14:10:25 +07:00
|
|
|
"keepMeLoggedIn": true
|
|
|
|
}`)
|
2020-05-21 05:03:15 +07:00
|
|
|
FirstFactorPost(0, false)(s.mock.Ctx)
|
2020-03-15 14:10:25 +07:00
|
|
|
|
|
|
|
// Respond with 200.
|
|
|
|
assert.Equal(s.T(), 200, s.mock.Ctx.Response.StatusCode())
|
|
|
|
assert.Equal(s.T(), []byte("{\"status\":\"OK\"}"), s.mock.Ctx.Response.Body())
|
|
|
|
|
|
|
|
// And store authentication in session.
|
|
|
|
session := s.mock.Ctx.GetSession()
|
|
|
|
assert.Equal(s.T(), "Test", session.Username)
|
|
|
|
assert.Equal(s.T(), true, session.KeepMeLoggedIn)
|
|
|
|
assert.Equal(s.T(), authentication.OneFactor, session.AuthenticationLevel)
|
|
|
|
assert.Equal(s.T(), []string{"test@example.com"}, session.Emails)
|
|
|
|
assert.Equal(s.T(), []string{"dev", "admins"}, session.Groups)
|
|
|
|
}
|
|
|
|
|
2020-02-05 04:18:02 +07:00
|
|
|
type FirstFactorRedirectionSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
mock *mocks.MockAutheliaCtx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FirstFactorRedirectionSuite) SetupTest() {
|
|
|
|
s.mock = mocks.NewMockAutheliaCtx(s.T())
|
|
|
|
s.mock.Ctx.Configuration.DefaultRedirectionURL = "https://default.local"
|
2020-03-06 07:31:09 +07:00
|
|
|
s.mock.Ctx.Configuration.AccessControl.DefaultPolicy = "bypass"
|
|
|
|
s.mock.Ctx.Configuration.AccessControl.Rules = []schema.ACLRule{
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2020-04-16 07:18:11 +07:00
|
|
|
Domains: []string{"default.local"},
|
|
|
|
Policy: "one_factor",
|
2020-03-06 07:31:09 +07:00
|
|
|
},
|
|
|
|
}
|
2021-06-18 08:38:01 +07:00
|
|
|
s.mock.Ctx.Providers.Authorizer = authorization.NewAuthorizer(&s.mock.Ctx.Configuration)
|
2020-02-05 04:18:02 +07:00
|
|
|
|
|
|
|
s.mock.UserProviderMock.
|
|
|
|
EXPECT().
|
|
|
|
CheckUserPassword(gomock.Eq("test"), gomock.Eq("hello")).
|
|
|
|
Return(true, nil)
|
|
|
|
|
|
|
|
s.mock.UserProviderMock.
|
|
|
|
EXPECT().
|
|
|
|
GetDetails(gomock.Eq("test")).
|
|
|
|
Return(&authentication.UserDetails{
|
2020-03-15 14:10:25 +07:00
|
|
|
Username: "test",
|
|
|
|
Emails: []string{"test@example.com"},
|
|
|
|
Groups: []string{"dev", "admins"},
|
2020-02-05 04:18:02 +07:00
|
|
|
}, nil)
|
|
|
|
|
|
|
|
s.mock.StorageProviderMock.
|
|
|
|
EXPECT().
|
|
|
|
AppendAuthenticationLog(gomock.Any()).
|
|
|
|
Return(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FirstFactorRedirectionSuite) TearDownTest() {
|
|
|
|
s.mock.Close()
|
|
|
|
}
|
|
|
|
|
2020-03-06 07:31:09 +07:00
|
|
|
// When:
|
|
|
|
// 1/ the target url is unknown
|
|
|
|
// 2/ two_factor is disabled (no policy is set to two_factor)
|
|
|
|
// 3/ default_redirect_url is provided
|
|
|
|
// Then:
|
|
|
|
// the user should be redirected to the default url.
|
|
|
|
func (s *FirstFactorRedirectionSuite) TestShouldRedirectToDefaultURLWhenNoTargetURLProvidedAndTwoFactorDisabled() {
|
2020-02-05 04:18:02 +07:00
|
|
|
s.mock.Ctx.Request.SetBodyString(`{
|
|
|
|
"username": "test",
|
|
|
|
"password": "hello",
|
2021-03-05 11:18:31 +07:00
|
|
|
"requestMethod": "GET",
|
2020-02-05 04:18:02 +07:00
|
|
|
"keepMeLoggedIn": false
|
|
|
|
}`)
|
2020-05-21 05:03:15 +07:00
|
|
|
FirstFactorPost(0, false)(s.mock.Ctx)
|
2020-02-05 04:18:02 +07:00
|
|
|
|
|
|
|
// Respond with 200.
|
2020-03-06 07:31:09 +07:00
|
|
|
s.mock.Assert200OK(s.T(), redirectResponse{Redirect: "https://default.local"})
|
2020-02-05 04:18:02 +07:00
|
|
|
}
|
|
|
|
|
2020-03-06 07:31:09 +07:00
|
|
|
// When:
|
|
|
|
// 1/ the target url is unsafe
|
|
|
|
// 2/ two_factor is disabled (no policy is set to two_factor)
|
|
|
|
// 3/ default_redirect_url is provided
|
|
|
|
// Then:
|
|
|
|
// the user should be redirected to the default url.
|
|
|
|
func (s *FirstFactorRedirectionSuite) TestShouldRedirectToDefaultURLWhenURLIsUnsafeAndTwoFactorDisabled() {
|
2020-02-05 04:18:02 +07:00
|
|
|
s.mock.Ctx.Request.SetBodyString(`{
|
|
|
|
"username": "test",
|
|
|
|
"password": "hello",
|
2021-03-05 11:18:31 +07:00
|
|
|
"requestMethod": "GET",
|
2020-02-05 04:18:02 +07:00
|
|
|
"keepMeLoggedIn": false,
|
|
|
|
"targetURL": "http://notsafe.local"
|
|
|
|
}`)
|
2020-05-05 02:39:25 +07:00
|
|
|
|
2020-05-21 05:03:15 +07:00
|
|
|
FirstFactorPost(0, false)(s.mock.Ctx)
|
2020-02-05 04:18:02 +07:00
|
|
|
|
|
|
|
// Respond with 200.
|
2020-03-06 07:31:09 +07:00
|
|
|
s.mock.Assert200OK(s.T(), redirectResponse{Redirect: "https://default.local"})
|
|
|
|
}
|
|
|
|
|
|
|
|
// When:
|
|
|
|
// 1/ two_factor is enabled (default policy)
|
|
|
|
// Then:
|
|
|
|
// the user should receive 200 without redirection URL.
|
|
|
|
func (s *FirstFactorRedirectionSuite) TestShouldReply200WhenNoTargetURLProvidedAndTwoFactorEnabled() {
|
2021-06-18 08:38:01 +07:00
|
|
|
s.mock.Ctx.Providers.Authorizer = authorization.NewAuthorizer(&schema.Configuration{
|
|
|
|
AccessControl: schema.AccessControlConfiguration{
|
|
|
|
DefaultPolicy: "two_factor",
|
|
|
|
},
|
2020-03-06 07:31:09 +07:00
|
|
|
})
|
|
|
|
s.mock.Ctx.Request.SetBodyString(`{
|
|
|
|
"username": "test",
|
|
|
|
"password": "hello",
|
2021-03-05 11:18:31 +07:00
|
|
|
"requestMethod": "GET",
|
2020-03-06 07:31:09 +07:00
|
|
|
"keepMeLoggedIn": false
|
|
|
|
}`)
|
2020-05-05 02:39:25 +07:00
|
|
|
|
2020-05-21 05:03:15 +07:00
|
|
|
FirstFactorPost(0, false)(s.mock.Ctx)
|
2020-03-06 07:31:09 +07:00
|
|
|
|
|
|
|
// Respond with 200.
|
|
|
|
s.mock.Assert200OK(s.T(), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// When:
|
|
|
|
// 1/ two_factor is enabled (some rule)
|
|
|
|
// Then:
|
|
|
|
// the user should receive 200 without redirection URL.
|
|
|
|
func (s *FirstFactorRedirectionSuite) TestShouldReply200WhenUnsafeTargetURLProvidedAndTwoFactorEnabled() {
|
2021-06-18 08:38:01 +07:00
|
|
|
s.mock.Ctx.Providers.Authorizer = authorization.NewAuthorizer(&schema.Configuration{
|
|
|
|
AccessControl: schema.AccessControlConfiguration{
|
|
|
|
DefaultPolicy: "one_factor",
|
|
|
|
Rules: []schema.ACLRule{
|
|
|
|
{
|
|
|
|
Domains: []string{"test.example.com"},
|
|
|
|
Policy: "one_factor",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Domains: []string{"example.com"},
|
|
|
|
Policy: "two_factor",
|
|
|
|
},
|
2020-03-06 07:31:09 +07:00
|
|
|
},
|
2021-06-18 08:38:01 +07:00
|
|
|
}})
|
2020-03-06 07:31:09 +07:00
|
|
|
s.mock.Ctx.Request.SetBodyString(`{
|
|
|
|
"username": "test",
|
|
|
|
"password": "hello",
|
2021-03-05 11:18:31 +07:00
|
|
|
"requestMethod": "GET",
|
2020-03-06 07:31:09 +07:00
|
|
|
"keepMeLoggedIn": false
|
|
|
|
}`)
|
2020-05-05 02:39:25 +07:00
|
|
|
|
2020-05-21 05:03:15 +07:00
|
|
|
FirstFactorPost(0, false)(s.mock.Ctx)
|
2020-03-06 07:31:09 +07:00
|
|
|
|
|
|
|
// Respond with 200.
|
|
|
|
s.mock.Assert200OK(s.T(), nil)
|
2020-02-05 04:18:02 +07:00
|
|
|
}
|
|
|
|
|
2019-04-25 04:52:08 +07:00
|
|
|
func TestFirstFactorSuite(t *testing.T) {
|
2020-02-05 04:18:02 +07:00
|
|
|
suite.Run(t, new(FirstFactorSuite))
|
|
|
|
suite.Run(t, new(FirstFactorRedirectionSuite))
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
2020-05-21 05:03:15 +07:00
|
|
|
|
|
|
|
func TestFirstFactorDelayAverages(t *testing.T) {
|
|
|
|
execDuration := time.Millisecond * 500
|
|
|
|
oneSecond := time.Millisecond * 1000
|
|
|
|
durations := []time.Duration{oneSecond, oneSecond, oneSecond, oneSecond, oneSecond, oneSecond, oneSecond, oneSecond, oneSecond, oneSecond}
|
|
|
|
cursor := 0
|
|
|
|
mutex := &sync.Mutex{}
|
|
|
|
avgExecDuration := movingAverageIteration(execDuration, false, &cursor, &durations, mutex)
|
|
|
|
assert.Equal(t, avgExecDuration, float64(1000))
|
|
|
|
|
|
|
|
execDurations := []time.Duration{
|
|
|
|
time.Millisecond * 500, time.Millisecond * 500, time.Millisecond * 500, time.Millisecond * 500,
|
|
|
|
time.Millisecond * 500, time.Millisecond * 500, time.Millisecond * 500, time.Millisecond * 500,
|
|
|
|
time.Millisecond * 500, time.Millisecond * 500, time.Millisecond * 500, time.Millisecond * 500,
|
|
|
|
}
|
|
|
|
|
|
|
|
current := float64(1000)
|
|
|
|
|
|
|
|
// Execute at 500ms for 12 requests.
|
|
|
|
for _, execDuration = range execDurations {
|
|
|
|
// Should not dip below 500, and should decrease in value by 50 each iteration.
|
|
|
|
if current > 500 {
|
|
|
|
current -= 50
|
|
|
|
}
|
|
|
|
|
|
|
|
avgExecDuration := movingAverageIteration(execDuration, true, &cursor, &durations, mutex)
|
|
|
|
assert.Equal(t, avgExecDuration, current)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFirstFactorDelayCalculations(t *testing.T) {
|
|
|
|
mock := mocks.NewMockAutheliaCtx(t)
|
|
|
|
successful := false
|
|
|
|
|
|
|
|
execDuration := 500 * time.Millisecond
|
|
|
|
avgExecDurationMs := 1000.0
|
|
|
|
expectedMinimumDelayMs := avgExecDurationMs - float64(execDuration.Milliseconds())
|
|
|
|
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
delay := calculateActualDelay(mock.Ctx, execDuration, avgExecDurationMs, &successful)
|
|
|
|
assert.True(t, delay >= expectedMinimumDelayMs)
|
|
|
|
assert.True(t, delay <= expectedMinimumDelayMs+float64(msMaximumRandomDelay))
|
|
|
|
}
|
|
|
|
|
|
|
|
execDuration = 5 * time.Millisecond
|
|
|
|
avgExecDurationMs = 5.0
|
|
|
|
expectedMinimumDelayMs = msMinimumDelay1FA - float64(execDuration.Milliseconds())
|
|
|
|
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
delay := calculateActualDelay(mock.Ctx, execDuration, avgExecDurationMs, &successful)
|
|
|
|
assert.True(t, delay >= expectedMinimumDelayMs)
|
|
|
|
assert.True(t, delay <= expectedMinimumDelayMs+float64(msMaximumRandomDelay))
|
|
|
|
}
|
|
|
|
}
|