summaryrefslogtreecommitdiff
path: root/internal/handlers/handler_firstfactor_test.go
diff options
context:
space:
mode:
authorClément Michaud <clement.michaud34@gmail.com>2020-02-04 22:18:02 +0100
committerGitHub <noreply@github.com>2020-02-05 08:18:02 +1100
commitd1d02d9eaed3ba0ae493d8f9558befc532d60616 (patch)
tree8f9dcf87685edb3b7d1ce6e33d89efccfb0bef3b /internal/handlers/handler_firstfactor_test.go
parent9c9d8518ebb2350e8f7ad4c025d6f8ebebd25ea6 (diff)
[FIX] Redirect to default URL after 1FA when default policy is one_factor. (#611)
* Redirect to default URL after 1FA when default policy is one_factor. User is now redirected to the default redirection URL after 1FA if the default policy is set to one_factor and there is no target URL or if the target URL is unsafe. Also, if the default policy is set to one_factor and the user is already authenticated, if she visits the login portal, the 'already authenticated' view is displayed with a logout button. This fixes #581. * Update users.yml * Fix permissions issue causing suite test failure
Diffstat (limited to 'internal/handlers/handler_firstfactor_test.go')
-rw-r--r--internal/handlers/handler_firstfactor_test.go74
1 files changed, 72 insertions, 2 deletions
diff --git a/internal/handlers/handler_firstfactor_test.go b/internal/handlers/handler_firstfactor_test.go
index 655d20452..afb1b0c7a 100644
--- a/internal/handlers/handler_firstfactor_test.go
+++ b/internal/handlers/handler_firstfactor_test.go
@@ -4,6 +4,7 @@ import (
"fmt"
"testing"
+ "github.com/authelia/authelia/internal/authorization"
"github.com/authelia/authelia/internal/mocks"
"github.com/authelia/authelia/internal/models"
@@ -229,7 +230,76 @@ func (s *FirstFactorSuite) TestShouldAuthenticateUserWithRememberMeUnchecked() {
assert.Equal(s.T(), []string{"dev", "admins"}, session.Groups)
}
+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"
+ s.mock.Ctx.Configuration.AccessControl.DefaultPolicy = "one_factor"
+ s.mock.Ctx.Providers.Authorizer = authorization.NewAuthorizer(
+ s.mock.Ctx.Configuration.AccessControl)
+
+ 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{
+ Emails: []string{"test@example.com"},
+ Groups: []string{"dev", "admins"},
+ }, nil)
+
+ s.mock.StorageProviderMock.
+ EXPECT().
+ AppendAuthenticationLog(gomock.Any()).
+ Return(nil)
+}
+
+func (s *FirstFactorRedirectionSuite) TearDownTest() {
+ s.mock.Close()
+}
+
+// When the target url is unknown, default policy is to one_factor and default_redirect_url
+// is provided, the user should be redirected to the default url.
+func (s *FirstFactorRedirectionSuite) TestShouldRedirectUserToDefaultRedirectionURLWhenNoTargetURLProvided() {
+ s.mock.Ctx.Request.SetBodyString(`{
+ "username": "test",
+ "password": "hello",
+ "keepMeLoggedIn": false
+ }`)
+ FirstFactorPost(s.mock.Ctx)
+
+ // Respond with 200.
+ s.mock.Assert200OK(s.T(), redirectResponse{
+ Redirect: "https://default.local",
+ })
+}
+
+// When the target url is unsafe, default policy is set to one_factor and default_redirect_url
+// is provided, the user should be redirected to the default url.
+func (s *FirstFactorRedirectionSuite) TestShouldRedirectUserToDefaultRedirectionURLWhenURLIsUnsafe() {
+ s.mock.Ctx.Request.SetBodyString(`{
+ "username": "test",
+ "password": "hello",
+ "keepMeLoggedIn": false,
+ "targetURL": "http://notsafe.local"
+ }`)
+ FirstFactorPost(s.mock.Ctx)
+
+ // Respond with 200.
+ s.mock.Assert200OK(s.T(), redirectResponse{
+ Redirect: "https://default.local",
+ })
+}
+
func TestFirstFactorSuite(t *testing.T) {
- firstFactorSuite := new(FirstFactorSuite)
- suite.Run(t, firstFactorSuite)
+ suite.Run(t, new(FirstFactorSuite))
+ suite.Run(t, new(FirstFactorRedirectionSuite))
}