2020-02-01 19:54:50 +07:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-03-01 06:13:33 +07:00
|
|
|
"regexp"
|
2020-02-01 19:54:50 +07:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/golang/mock/gomock"
|
2020-12-16 08:47:31 +07:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-02-01 19:54:50 +07:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
"github.com/tstranex/u2f"
|
2020-04-05 19:37:21 +07:00
|
|
|
|
2021-08-11 08:04:35 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/mocks"
|
2021-11-23 16:45:38 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/models"
|
2021-11-29 10:09:14 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/regulation"
|
2021-08-11 08:04:35 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/session"
|
2020-02-01 19:54:50 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
type HandlerSignTOTPSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
mock *mocks.MockAutheliaCtx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HandlerSignTOTPSuite) SetupTest() {
|
|
|
|
s.mock = mocks.NewMockAutheliaCtx(s.T())
|
|
|
|
userSession := s.mock.Ctx.GetSession()
|
2020-05-02 23:20:40 +07:00
|
|
|
userSession.Username = testUsername
|
2020-02-01 19:54:50 +07:00
|
|
|
userSession.U2FChallenge = &u2f.Challenge{}
|
|
|
|
userSession.U2FRegistration = &session.U2FRegistration{}
|
2020-12-16 08:47:31 +07:00
|
|
|
err := s.mock.Ctx.SaveSession(userSession)
|
|
|
|
require.NoError(s.T(), err)
|
2020-02-01 19:54:50 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HandlerSignTOTPSuite) TearDownTest() {
|
|
|
|
s.mock.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HandlerSignTOTPSuite) TestShouldRedirectUserToDefaultURL() {
|
2021-11-25 08:56:58 +07:00
|
|
|
config := models.TOTPConfiguration{ID: 1, Username: "john", Digits: 6, Secret: []byte("secret"), Period: 30, Algorithm: "SHA1"}
|
2021-11-23 16:45:38 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
s.mock.StorageMock.EXPECT().
|
2021-11-23 16:45:38 +07:00
|
|
|
LoadTOTPConfiguration(s.mock.Ctx, gomock.Any()).
|
|
|
|
Return(&config, nil)
|
2020-02-01 19:54:50 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
s.mock.StorageMock.
|
2021-11-29 10:09:14 +07:00
|
|
|
EXPECT().
|
|
|
|
AppendAuthenticationLog(s.mock.Ctx, gomock.Eq(models.AuthenticationAttempt{
|
|
|
|
Username: "john",
|
|
|
|
Successful: true,
|
|
|
|
Banned: false,
|
|
|
|
Time: s.mock.Clock.Now(),
|
|
|
|
Type: regulation.AuthTypeTOTP,
|
|
|
|
RemoteIP: models.NewIPAddressFromString("0.0.0.0"),
|
|
|
|
}))
|
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
s.mock.TOTPMock.EXPECT().Validate(gomock.Eq("abc"), gomock.Eq(&config)).Return(true, nil)
|
2020-02-01 19:54:50 +07:00
|
|
|
|
2020-05-02 23:20:40 +07:00
|
|
|
s.mock.Ctx.Configuration.DefaultRedirectionURL = testRedirectionURL
|
2020-02-01 19:54:50 +07:00
|
|
|
|
|
|
|
bodyBytes, err := json.Marshal(signTOTPRequestBody{
|
|
|
|
Token: "abc",
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.mock.Ctx.Request.SetBody(bodyBytes)
|
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
SecondFactorTOTPPost(s.mock.Ctx)
|
2020-02-01 19:54:50 +07:00
|
|
|
s.mock.Assert200OK(s.T(), redirectResponse{
|
2020-05-02 23:20:40 +07:00
|
|
|
Redirect: testRedirectionURL,
|
2020-02-01 19:54:50 +07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HandlerSignTOTPSuite) TestShouldNotReturnRedirectURL() {
|
2021-11-25 08:56:58 +07:00
|
|
|
config := models.TOTPConfiguration{ID: 1, Username: "john", Digits: 6, Secret: []byte("secret"), Period: 30, Algorithm: "SHA1"}
|
2021-11-23 16:45:38 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
s.mock.StorageMock.EXPECT().
|
2021-11-23 16:45:38 +07:00
|
|
|
LoadTOTPConfiguration(s.mock.Ctx, gomock.Any()).
|
|
|
|
Return(&config, nil)
|
2020-02-01 19:54:50 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
s.mock.StorageMock.
|
2021-11-29 10:09:14 +07:00
|
|
|
EXPECT().
|
|
|
|
AppendAuthenticationLog(s.mock.Ctx, gomock.Eq(models.AuthenticationAttempt{
|
|
|
|
Username: "john",
|
|
|
|
Successful: true,
|
|
|
|
Banned: false,
|
|
|
|
Time: s.mock.Clock.Now(),
|
|
|
|
Type: regulation.AuthTypeTOTP,
|
|
|
|
RemoteIP: models.NewIPAddressFromString("0.0.0.0"),
|
|
|
|
}))
|
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
s.mock.TOTPMock.EXPECT().Validate(gomock.Eq("abc"), gomock.Eq(&config)).Return(true, nil)
|
2020-02-01 19:54:50 +07:00
|
|
|
|
|
|
|
bodyBytes, err := json.Marshal(signTOTPRequestBody{
|
|
|
|
Token: "abc",
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.mock.Ctx.Request.SetBody(bodyBytes)
|
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
SecondFactorTOTPPost(s.mock.Ctx)
|
2020-02-01 19:54:50 +07:00
|
|
|
s.mock.Assert200OK(s.T(), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HandlerSignTOTPSuite) TestShouldRedirectUserToSafeTargetURL() {
|
2021-11-25 08:56:58 +07:00
|
|
|
config := models.TOTPConfiguration{ID: 1, Username: "john", Digits: 6, Secret: []byte("secret"), Period: 30, Algorithm: "SHA1"}
|
2021-11-23 16:45:38 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
s.mock.StorageMock.EXPECT().
|
2021-11-23 16:45:38 +07:00
|
|
|
LoadTOTPConfiguration(s.mock.Ctx, gomock.Any()).
|
|
|
|
Return(&config, nil)
|
2020-02-01 19:54:50 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
s.mock.StorageMock.
|
2021-11-29 10:09:14 +07:00
|
|
|
EXPECT().
|
|
|
|
AppendAuthenticationLog(s.mock.Ctx, gomock.Eq(models.AuthenticationAttempt{
|
|
|
|
Username: "john",
|
|
|
|
Successful: true,
|
|
|
|
Banned: false,
|
|
|
|
Time: s.mock.Clock.Now(),
|
|
|
|
Type: regulation.AuthTypeTOTP,
|
|
|
|
RemoteIP: models.NewIPAddressFromString("0.0.0.0"),
|
|
|
|
}))
|
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
s.mock.TOTPMock.EXPECT().Validate(gomock.Eq("abc"), gomock.Eq(&config)).Return(true, nil)
|
2020-02-01 19:54:50 +07:00
|
|
|
|
|
|
|
bodyBytes, err := json.Marshal(signTOTPRequestBody{
|
|
|
|
Token: "abc",
|
|
|
|
TargetURL: "https://mydomain.local",
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.mock.Ctx.Request.SetBody(bodyBytes)
|
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
SecondFactorTOTPPost(s.mock.Ctx)
|
2020-02-01 19:54:50 +07:00
|
|
|
s.mock.Assert200OK(s.T(), redirectResponse{
|
|
|
|
Redirect: "https://mydomain.local",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HandlerSignTOTPSuite) TestShouldNotRedirectToUnsafeURL() {
|
2021-12-01 19:11:29 +07:00
|
|
|
s.mock.StorageMock.EXPECT().
|
2021-11-23 16:45:38 +07:00
|
|
|
LoadTOTPConfiguration(s.mock.Ctx, gomock.Any()).
|
2021-11-25 08:56:58 +07:00
|
|
|
Return(&models.TOTPConfiguration{Secret: []byte("secret")}, nil)
|
2020-02-01 19:54:50 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
s.mock.StorageMock.
|
2021-11-29 10:09:14 +07:00
|
|
|
EXPECT().
|
|
|
|
AppendAuthenticationLog(s.mock.Ctx, gomock.Eq(models.AuthenticationAttempt{
|
|
|
|
Username: "john",
|
|
|
|
Successful: true,
|
|
|
|
Banned: false,
|
|
|
|
Time: s.mock.Clock.Now(),
|
|
|
|
Type: regulation.AuthTypeTOTP,
|
|
|
|
RemoteIP: models.NewIPAddressFromString("0.0.0.0"),
|
|
|
|
}))
|
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
s.mock.TOTPMock.EXPECT().
|
|
|
|
Validate(gomock.Eq("abc"), gomock.Eq(&models.TOTPConfiguration{Secret: []byte("secret")})).
|
2020-03-25 08:48:20 +07:00
|
|
|
Return(true, nil)
|
2020-02-01 19:54:50 +07:00
|
|
|
|
|
|
|
bodyBytes, err := json.Marshal(signTOTPRequestBody{
|
|
|
|
Token: "abc",
|
|
|
|
TargetURL: "http://mydomain.local",
|
|
|
|
})
|
2021-12-01 19:11:29 +07:00
|
|
|
|
2020-02-01 19:54:50 +07:00
|
|
|
s.Require().NoError(err)
|
|
|
|
s.mock.Ctx.Request.SetBody(bodyBytes)
|
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
SecondFactorTOTPPost(s.mock.Ctx)
|
2020-02-01 19:54:50 +07:00
|
|
|
s.mock.Assert200OK(s.T(), nil)
|
|
|
|
}
|
|
|
|
|
2020-03-01 06:13:33 +07:00
|
|
|
func (s *HandlerSignTOTPSuite) TestShouldRegenerateSessionForPreventingSessionFixation() {
|
2021-11-25 08:56:58 +07:00
|
|
|
config := models.TOTPConfiguration{ID: 1, Username: "john", Digits: 6, Secret: []byte("secret"), Period: 30, Algorithm: "SHA1"}
|
2021-11-23 16:45:38 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
s.mock.StorageMock.EXPECT().
|
2021-11-23 16:45:38 +07:00
|
|
|
LoadTOTPConfiguration(s.mock.Ctx, gomock.Any()).
|
|
|
|
Return(&config, nil)
|
2020-03-01 06:13:33 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
s.mock.StorageMock.
|
2021-11-29 10:09:14 +07:00
|
|
|
EXPECT().
|
|
|
|
AppendAuthenticationLog(s.mock.Ctx, gomock.Eq(models.AuthenticationAttempt{
|
|
|
|
Username: "john",
|
|
|
|
Successful: true,
|
|
|
|
Banned: false,
|
|
|
|
Time: s.mock.Clock.Now(),
|
|
|
|
Type: regulation.AuthTypeTOTP,
|
|
|
|
RemoteIP: models.NewIPAddressFromString("0.0.0.0"),
|
|
|
|
}))
|
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
s.mock.TOTPMock.EXPECT().
|
|
|
|
Validate(gomock.Eq("abc"), gomock.Eq(&config)).
|
2020-03-25 08:48:20 +07:00
|
|
|
Return(true, nil)
|
2020-03-01 06:13:33 +07:00
|
|
|
|
|
|
|
bodyBytes, err := json.Marshal(signTOTPRequestBody{
|
|
|
|
Token: "abc",
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.mock.Ctx.Request.SetBody(bodyBytes)
|
|
|
|
|
|
|
|
r := regexp.MustCompile("^authelia_session=(.*); path=")
|
|
|
|
res := r.FindAllStringSubmatch(string(s.mock.Ctx.Response.Header.PeekCookie("authelia_session")), -1)
|
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
SecondFactorTOTPPost(s.mock.Ctx)
|
2020-03-01 06:13:33 +07:00
|
|
|
s.mock.Assert200OK(s.T(), nil)
|
|
|
|
|
|
|
|
s.Assert().NotEqual(
|
|
|
|
res[0][1],
|
|
|
|
string(s.mock.Ctx.Request.Header.Cookie("authelia_session")))
|
|
|
|
}
|
|
|
|
|
2020-02-01 19:54:50 +07:00
|
|
|
func TestRunHandlerSignTOTPSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(HandlerSignTOTPSuite))
|
|
|
|
}
|