2019-04-24 21:52:08 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2020-02-01 12:54:50 +00:00
|
|
|
"encoding/json"
|
2019-04-24 21:52:08 +00:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2020-02-29 23:13:33 +00:00
|
|
|
"regexp"
|
2019-04-24 21:52:08 +00:00
|
|
|
"testing"
|
|
|
|
|
2019-12-24 02:14:52 +00:00
|
|
|
"github.com/authelia/authelia/internal/duo"
|
|
|
|
"github.com/authelia/authelia/internal/mocks"
|
2019-04-24 21:52:08 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SecondFactorDuoPostSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
mock *mocks.MockAutheliaCtx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SecondFactorDuoPostSuite) SetupTest() {
|
|
|
|
s.mock = mocks.NewMockAutheliaCtx(s.T())
|
|
|
|
userSession := s.mock.Ctx.GetSession()
|
|
|
|
userSession.Username = "john"
|
|
|
|
s.mock.Ctx.SaveSession(userSession)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SecondFactorDuoPostSuite) TearDownTest() {
|
|
|
|
s.mock.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SecondFactorDuoPostSuite) TestShouldCallDuoAPIAndAllowAccess() {
|
|
|
|
duoMock := mocks.NewMockAPI(s.mock.Ctrl)
|
|
|
|
|
|
|
|
values := url.Values{}
|
|
|
|
values.Set("username", "john")
|
|
|
|
values.Set("ipaddr", s.mock.Ctx.RemoteIP().String())
|
|
|
|
values.Set("factor", "push")
|
|
|
|
values.Set("device", "auto")
|
|
|
|
values.Set("pushinfo", "target%20url=https://target.example.com")
|
|
|
|
|
|
|
|
response := duo.Response{}
|
|
|
|
response.Response.Result = "allow"
|
|
|
|
|
2020-03-01 00:51:11 +00:00
|
|
|
duoMock.EXPECT().Call(gomock.Eq(values), s.mock.Ctx).Return(&response, nil)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
|
|
|
s.mock.Ctx.Request.SetBodyString("{\"targetURL\": \"https://target.example.com\"}")
|
|
|
|
|
|
|
|
SecondFactorDuoPost(duoMock)(s.mock.Ctx)
|
|
|
|
|
|
|
|
assert.Equal(s.T(), s.mock.Ctx.Response.StatusCode(), 200)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SecondFactorDuoPostSuite) TestShouldCallDuoAPIAndDenyAccess() {
|
|
|
|
duoMock := mocks.NewMockAPI(s.mock.Ctrl)
|
|
|
|
|
|
|
|
values := url.Values{}
|
|
|
|
values.Set("username", "john")
|
|
|
|
values.Set("ipaddr", s.mock.Ctx.RemoteIP().String())
|
|
|
|
values.Set("factor", "push")
|
|
|
|
values.Set("device", "auto")
|
|
|
|
values.Set("pushinfo", "target%20url=https://target.example.com")
|
|
|
|
|
|
|
|
response := duo.Response{}
|
|
|
|
response.Response.Result = "deny"
|
|
|
|
|
2020-03-01 00:51:11 +00:00
|
|
|
duoMock.EXPECT().Call(gomock.Eq(values), s.mock.Ctx).Return(&response, nil)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
|
|
|
s.mock.Ctx.Request.SetBodyString("{\"targetURL\": \"https://target.example.com\"}")
|
|
|
|
|
|
|
|
SecondFactorDuoPost(duoMock)(s.mock.Ctx)
|
|
|
|
|
|
|
|
assert.Equal(s.T(), s.mock.Ctx.Response.StatusCode(), 401)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SecondFactorDuoPostSuite) TestShouldCallDuoAPIAndFail() {
|
|
|
|
duoMock := mocks.NewMockAPI(s.mock.Ctrl)
|
|
|
|
|
|
|
|
values := url.Values{}
|
|
|
|
values.Set("username", "john")
|
|
|
|
values.Set("ipaddr", s.mock.Ctx.RemoteIP().String())
|
|
|
|
values.Set("factor", "push")
|
|
|
|
values.Set("device", "auto")
|
|
|
|
values.Set("pushinfo", "target%20url=https://target.example.com")
|
|
|
|
|
2020-03-01 00:51:11 +00:00
|
|
|
duoMock.EXPECT().Call(gomock.Eq(values), s.mock.Ctx).Return(nil, fmt.Errorf("Connnection error"))
|
2019-04-24 21:52:08 +00:00
|
|
|
|
|
|
|
s.mock.Ctx.Request.SetBodyString("{\"targetURL\": \"https://target.example.com\"}")
|
|
|
|
|
|
|
|
SecondFactorDuoPost(duoMock)(s.mock.Ctx)
|
|
|
|
|
|
|
|
s.mock.Assert200KO(s.T(), "Authentication failed, please retry later.")
|
|
|
|
}
|
|
|
|
|
2020-02-01 12:54:50 +00:00
|
|
|
func (s *SecondFactorDuoPostSuite) TestShouldRedirectUserToDefaultURL() {
|
|
|
|
duoMock := mocks.NewMockAPI(s.mock.Ctrl)
|
|
|
|
|
|
|
|
response := duo.Response{}
|
|
|
|
response.Response.Result = "allow"
|
|
|
|
|
2020-03-01 00:51:11 +00:00
|
|
|
duoMock.EXPECT().Call(gomock.Any(), s.mock.Ctx).Return(&response, nil)
|
2020-02-01 12:54:50 +00:00
|
|
|
|
|
|
|
s.mock.Ctx.Configuration.DefaultRedirectionURL = "http://redirection.local"
|
|
|
|
|
|
|
|
bodyBytes, err := json.Marshal(signDuoRequestBody{})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.mock.Ctx.Request.SetBody(bodyBytes)
|
|
|
|
|
|
|
|
SecondFactorDuoPost(duoMock)(s.mock.Ctx)
|
|
|
|
s.mock.Assert200OK(s.T(), redirectResponse{
|
|
|
|
Redirect: "http://redirection.local",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SecondFactorDuoPostSuite) TestShouldNotReturnRedirectURL() {
|
|
|
|
duoMock := mocks.NewMockAPI(s.mock.Ctrl)
|
|
|
|
|
|
|
|
response := duo.Response{}
|
|
|
|
response.Response.Result = "allow"
|
|
|
|
|
2020-03-01 00:51:11 +00:00
|
|
|
duoMock.EXPECT().Call(gomock.Any(), s.mock.Ctx).Return(&response, nil)
|
2020-02-01 12:54:50 +00:00
|
|
|
|
|
|
|
bodyBytes, err := json.Marshal(signDuoRequestBody{})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.mock.Ctx.Request.SetBody(bodyBytes)
|
|
|
|
|
|
|
|
SecondFactorDuoPost(duoMock)(s.mock.Ctx)
|
|
|
|
s.mock.Assert200OK(s.T(), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SecondFactorDuoPostSuite) TestShouldRedirectUserToSafeTargetURL() {
|
|
|
|
duoMock := mocks.NewMockAPI(s.mock.Ctrl)
|
|
|
|
|
|
|
|
response := duo.Response{}
|
|
|
|
response.Response.Result = "allow"
|
|
|
|
|
2020-03-01 00:51:11 +00:00
|
|
|
duoMock.EXPECT().Call(gomock.Any(), s.mock.Ctx).Return(&response, nil)
|
2020-02-01 12:54:50 +00:00
|
|
|
|
|
|
|
bodyBytes, err := json.Marshal(signDuoRequestBody{
|
|
|
|
TargetURL: "https://mydomain.local",
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.mock.Ctx.Request.SetBody(bodyBytes)
|
|
|
|
|
|
|
|
SecondFactorDuoPost(duoMock)(s.mock.Ctx)
|
|
|
|
s.mock.Assert200OK(s.T(), redirectResponse{
|
|
|
|
Redirect: "https://mydomain.local",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SecondFactorDuoPostSuite) TestShouldNotRedirectToUnsafeURL() {
|
|
|
|
duoMock := mocks.NewMockAPI(s.mock.Ctrl)
|
|
|
|
|
|
|
|
response := duo.Response{}
|
|
|
|
response.Response.Result = "allow"
|
|
|
|
|
2020-03-01 00:51:11 +00:00
|
|
|
duoMock.EXPECT().Call(gomock.Any(), s.mock.Ctx).Return(&response, nil)
|
2020-02-01 12:54:50 +00:00
|
|
|
|
|
|
|
bodyBytes, err := json.Marshal(signDuoRequestBody{
|
|
|
|
TargetURL: "http://mydomain.local",
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.mock.Ctx.Request.SetBody(bodyBytes)
|
|
|
|
|
|
|
|
SecondFactorDuoPost(duoMock)(s.mock.Ctx)
|
|
|
|
s.mock.Assert200OK(s.T(), nil)
|
|
|
|
}
|
|
|
|
|
2020-02-29 23:13:33 +00:00
|
|
|
func (s *SecondFactorDuoPostSuite) TestShouldRegenerateSessionForPreventingSessionFixation() {
|
|
|
|
duoMock := mocks.NewMockAPI(s.mock.Ctrl)
|
|
|
|
|
|
|
|
response := duo.Response{}
|
|
|
|
response.Response.Result = "allow"
|
|
|
|
|
2020-03-01 00:51:11 +00:00
|
|
|
duoMock.EXPECT().Call(gomock.Any(), s.mock.Ctx).Return(&response, nil)
|
2020-02-29 23:13:33 +00:00
|
|
|
|
|
|
|
bodyBytes, err := json.Marshal(signDuoRequestBody{
|
|
|
|
TargetURL: "http://mydomain.local",
|
|
|
|
})
|
|
|
|
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)
|
|
|
|
|
|
|
|
SecondFactorDuoPost(duoMock)(s.mock.Ctx)
|
|
|
|
s.mock.Assert200OK(s.T(), nil)
|
|
|
|
|
|
|
|
s.Assert().NotEqual(
|
|
|
|
res[0][1],
|
|
|
|
string(s.mock.Ctx.Request.Header.Cookie("authelia_session")))
|
|
|
|
}
|
|
|
|
|
2020-01-21 00:10:00 +00:00
|
|
|
func TestRunSecondFactorDuoPostSuite(t *testing.T) {
|
2019-04-24 21:52:08 +00:00
|
|
|
s := new(SecondFactorDuoPostSuite)
|
|
|
|
suite.Run(t, s)
|
|
|
|
}
|