2019-11-02 14:32:58 +00:00
|
|
|
package suites
|
|
|
|
|
|
|
|
import (
|
2019-11-24 20:27:59 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2020-01-18 14:57:42 +00:00
|
|
|
"net/url"
|
2019-11-02 14:32:58 +00:00
|
|
|
"testing"
|
2019-11-24 20:27:59 +00:00
|
|
|
"time"
|
2019-11-02 14:32:58 +00:00
|
|
|
|
2019-12-07 16:40:42 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-11-02 14:32:58 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
2020-04-05 12:37:21 +00:00
|
|
|
|
|
|
|
"github.com/authelia/authelia/internal/storage"
|
2021-07-22 03:52:37 +00:00
|
|
|
"github.com/authelia/authelia/internal/utils"
|
2019-11-02 14:32:58 +00:00
|
|
|
)
|
|
|
|
|
2019-11-24 20:27:59 +00:00
|
|
|
type StandaloneWebDriverSuite struct {
|
2019-11-02 14:32:58 +00:00
|
|
|
*SeleniumSuite
|
|
|
|
}
|
|
|
|
|
2019-11-24 20:27:59 +00:00
|
|
|
func NewStandaloneWebDriverSuite() *StandaloneWebDriverSuite {
|
|
|
|
return &StandaloneWebDriverSuite{SeleniumSuite: new(SeleniumSuite)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StandaloneWebDriverSuite) SetupSuite() {
|
|
|
|
wds, err := StartWebDriver()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.WebDriverSession = wds
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StandaloneWebDriverSuite) TearDownSuite() {
|
|
|
|
err := s.WebDriverSession.Stop()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StandaloneWebDriverSuite) SetupTest() {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
s.doLogout(ctx, s.T())
|
|
|
|
s.WebDriverSession.doVisit(s.T(), HomeBaseURL)
|
|
|
|
s.verifyIsHome(ctx, s.T())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StandaloneWebDriverSuite) TestShouldLetUserKnowHeIsAlreadyAuthenticated() {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
_ = s.doRegisterAndLogin2FA(ctx, s.T(), "john", "password", false, "")
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// Visit home page to change context.
|
2019-11-24 20:27:59 +00:00
|
|
|
s.doVisit(s.T(), HomeBaseURL)
|
|
|
|
s.verifyIsHome(ctx, s.T())
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// Visit the login page and wait for redirection to 2FA page with success icon displayed.
|
2020-05-27 11:55:44 +00:00
|
|
|
s.doVisit(s.T(), GetLoginBaseURL())
|
2020-02-04 21:18:02 +00:00
|
|
|
s.verifyIsAuthenticatedPage(ctx, s.T())
|
2019-11-24 20:27:59 +00:00
|
|
|
}
|
|
|
|
|
2019-12-07 16:40:42 +00:00
|
|
|
func (s *StandaloneWebDriverSuite) TestShouldCheckUserIsAskedToRegisterDevice() {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
username := "john"
|
|
|
|
password := "password"
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// Clean up any TOTP secret already in DB.
|
2020-01-17 19:46:51 +00:00
|
|
|
provider := storage.NewSQLiteProvider("/tmp/db.sqlite3")
|
2019-12-07 16:40:42 +00:00
|
|
|
require.NoError(s.T(), provider.DeleteTOTPSecret(username))
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// Login one factor.
|
2019-12-07 16:40:42 +00:00
|
|
|
s.doLoginOneFactor(ctx, s.T(), username, password, false, "")
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// Check the user is asked to register a new device.
|
2019-12-07 16:40:42 +00:00
|
|
|
s.WaitElementLocatedByClassName(ctx, s.T(), "state-not-registered")
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// Then register the TOTP factor.
|
2019-12-07 16:40:42 +00:00
|
|
|
s.doRegisterTOTP(ctx, s.T())
|
2020-05-02 05:06:39 +00:00
|
|
|
// And logout.
|
2019-12-07 16:40:42 +00:00
|
|
|
s.doLogout(ctx, s.T())
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// Login one factor again.
|
2019-12-07 16:40:42 +00:00
|
|
|
s.doLoginOneFactor(ctx, s.T(), username, password, false, "")
|
|
|
|
|
|
|
|
// now the user should be asked to perform 2FA
|
|
|
|
s.WaitElementLocatedByClassName(ctx, s.T(), "state-method")
|
|
|
|
}
|
|
|
|
|
2019-11-24 20:27:59 +00:00
|
|
|
type StandaloneSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
}
|
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
func NewStandaloneSuite() *StandaloneSuite {
|
2019-11-24 20:27:59 +00:00
|
|
|
return &StandaloneSuite{}
|
|
|
|
}
|
|
|
|
|
2021-03-05 04:18:31 +00:00
|
|
|
func (s *StandaloneSuite) TestShouldRespectMethodsACL() {
|
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/verify?rd=%s", AutheliaBaseURL, GetLoginBaseURL()), nil)
|
|
|
|
s.Assert().NoError(err)
|
|
|
|
req.Header.Set("X-Forwarded-Method", "GET")
|
|
|
|
req.Header.Set("X-Forwarded-Proto", "https")
|
|
|
|
req.Header.Set("X-Forwarded-Host", fmt.Sprintf("secure.%s", BaseDomain))
|
|
|
|
req.Header.Set("X-Forwarded-URI", "/")
|
2021-07-22 03:52:37 +00:00
|
|
|
req.Header.Set("Accept", "text/html; charset=utf8")
|
2021-03-05 04:18:31 +00:00
|
|
|
|
|
|
|
client := NewHTTPClient()
|
|
|
|
res, err := client.Do(req)
|
|
|
|
s.Assert().NoError(err)
|
|
|
|
s.Assert().Equal(res.StatusCode, 302)
|
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
|
|
|
s.Assert().NoError(err)
|
|
|
|
|
|
|
|
urlEncodedAdminURL := url.QueryEscape(SecureBaseURL + "/")
|
2021-07-22 03:52:37 +00:00
|
|
|
s.Assert().Equal(fmt.Sprintf("<a href=\"%s\">Found</a>", utils.StringHTMLEscape(fmt.Sprintf("%s/?rd=%s&rm=GET", GetLoginBaseURL(), urlEncodedAdminURL))), string(body))
|
2021-03-05 04:18:31 +00:00
|
|
|
|
|
|
|
req.Header.Set("X-Forwarded-Method", "OPTIONS")
|
|
|
|
|
|
|
|
res, err = client.Do(req)
|
|
|
|
s.Assert().NoError(err)
|
|
|
|
s.Assert().Equal(res.StatusCode, 200)
|
|
|
|
}
|
|
|
|
|
2021-07-16 03:43:48 +00:00
|
|
|
func (s *StandaloneSuite) TestShouldRespondWithCorrectStatusCode() {
|
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/verify?rd=%s", AutheliaBaseURL, GetLoginBaseURL()), nil)
|
|
|
|
s.Assert().NoError(err)
|
|
|
|
req.Header.Set("X-Forwarded-Method", "GET")
|
|
|
|
req.Header.Set("X-Forwarded-Proto", "https")
|
|
|
|
req.Header.Set("X-Forwarded-Host", fmt.Sprintf("secure.%s", BaseDomain))
|
|
|
|
req.Header.Set("X-Forwarded-URI", "/")
|
2021-07-22 03:52:37 +00:00
|
|
|
req.Header.Set("Accept", "text/html; charset=utf8")
|
2021-07-16 03:43:48 +00:00
|
|
|
|
|
|
|
client := NewHTTPClient()
|
|
|
|
res, err := client.Do(req)
|
|
|
|
s.Assert().NoError(err)
|
|
|
|
s.Assert().Equal(res.StatusCode, 302)
|
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
|
|
|
s.Assert().NoError(err)
|
|
|
|
|
|
|
|
urlEncodedAdminURL := url.QueryEscape(SecureBaseURL + "/")
|
2021-07-22 03:52:37 +00:00
|
|
|
s.Assert().Equal(fmt.Sprintf("<a href=\"%s\">Found</a>", utils.StringHTMLEscape(fmt.Sprintf("%s/?rd=%s&rm=GET", GetLoginBaseURL(), urlEncodedAdminURL))), string(body))
|
2021-07-16 03:43:48 +00:00
|
|
|
|
|
|
|
req.Header.Set("X-Forwarded-Method", "POST")
|
|
|
|
|
|
|
|
res, err = client.Do(req)
|
|
|
|
s.Assert().NoError(err)
|
|
|
|
s.Assert().Equal(res.StatusCode, 303)
|
|
|
|
body, err = ioutil.ReadAll(res.Body)
|
|
|
|
s.Assert().NoError(err)
|
|
|
|
|
|
|
|
urlEncodedAdminURL = url.QueryEscape(SecureBaseURL + "/")
|
2021-07-22 03:52:37 +00:00
|
|
|
s.Assert().Equal(fmt.Sprintf("<a href=\"%s\">See Other</a>", utils.StringHTMLEscape(fmt.Sprintf("%s/?rd=%s&rm=POST", GetLoginBaseURL(), urlEncodedAdminURL))), string(body))
|
2021-07-16 03:43:48 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// Standard case using nginx.
|
2021-07-22 03:52:37 +00:00
|
|
|
func (s *StandaloneSuite) TestShouldVerifyAPIVerifyUnauthorized() {
|
2019-11-24 20:27:59 +00:00
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/verify", AutheliaBaseURL), nil)
|
|
|
|
s.Assert().NoError(err)
|
|
|
|
req.Header.Set("X-Forwarded-Proto", "https")
|
|
|
|
req.Header.Set("X-Original-URL", AdminBaseURL)
|
2021-07-22 03:52:37 +00:00
|
|
|
req.Header.Set("Accept", "text/html; charset=utf8")
|
2019-11-24 20:27:59 +00:00
|
|
|
|
|
|
|
client := NewHTTPClient()
|
|
|
|
res, err := client.Do(req)
|
|
|
|
s.Assert().NoError(err)
|
|
|
|
s.Assert().Equal(res.StatusCode, 401)
|
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
|
|
|
s.Assert().NoError(err)
|
2021-07-22 03:52:37 +00:00
|
|
|
s.Assert().Equal("Unauthorized", string(body))
|
2019-11-24 20:27:59 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// Standard case using Kubernetes.
|
2019-11-24 20:27:59 +00:00
|
|
|
func (s *StandaloneSuite) TestShouldVerifyAPIVerifyRedirectFromXOriginalURL() {
|
2020-05-27 11:55:44 +00:00
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/verify?rd=%s", AutheliaBaseURL, GetLoginBaseURL()), nil)
|
2019-11-24 20:27:59 +00:00
|
|
|
s.Assert().NoError(err)
|
|
|
|
req.Header.Set("X-Forwarded-Proto", "https")
|
|
|
|
req.Header.Set("X-Original-URL", AdminBaseURL)
|
2021-07-22 03:52:37 +00:00
|
|
|
req.Header.Set("Accept", "text/html; charset=utf8")
|
2019-11-24 20:27:59 +00:00
|
|
|
|
|
|
|
client := NewHTTPClient()
|
|
|
|
res, err := client.Do(req)
|
|
|
|
s.Assert().NoError(err)
|
|
|
|
s.Assert().Equal(res.StatusCode, 302)
|
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
|
|
|
s.Assert().NoError(err)
|
2020-01-18 14:57:42 +00:00
|
|
|
|
|
|
|
urlEncodedAdminURL := url.QueryEscape(AdminBaseURL)
|
2021-07-22 03:52:37 +00:00
|
|
|
s.Assert().Equal(fmt.Sprintf("<a href=\"%s\">Found</a>", utils.StringHTMLEscape(fmt.Sprintf("%s/?rd=%s", GetLoginBaseURL(), urlEncodedAdminURL))), string(body))
|
2019-11-24 20:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StandaloneSuite) TestShouldVerifyAPIVerifyRedirectFromXOriginalHostURI() {
|
2020-05-27 11:55:44 +00:00
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/verify?rd=%s", AutheliaBaseURL, GetLoginBaseURL()), nil)
|
2019-11-24 20:27:59 +00:00
|
|
|
s.Assert().NoError(err)
|
|
|
|
req.Header.Set("X-Forwarded-Proto", "https")
|
|
|
|
req.Header.Set("X-Forwarded-Host", "secure.example.com:8080")
|
|
|
|
req.Header.Set("X-Forwarded-URI", "/")
|
2021-07-22 03:52:37 +00:00
|
|
|
req.Header.Set("Accept", "text/html; charset=utf8")
|
2019-11-24 20:27:59 +00:00
|
|
|
|
|
|
|
client := NewHTTPClient()
|
|
|
|
res, err := client.Do(req)
|
|
|
|
s.Assert().NoError(err)
|
|
|
|
s.Assert().Equal(res.StatusCode, 302)
|
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
|
|
|
s.Assert().NoError(err)
|
2020-01-18 14:57:42 +00:00
|
|
|
|
|
|
|
urlEncodedAdminURL := url.QueryEscape(SecureBaseURL + "/")
|
2021-07-22 03:52:37 +00:00
|
|
|
s.Assert().Equal(fmt.Sprintf("<a href=\"%s\">Found</a>", utils.StringHTMLEscape(fmt.Sprintf("%s/?rd=%s", GetLoginBaseURL(), urlEncodedAdminURL))), string(body))
|
2019-11-24 20:27:59 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 21:35:03 +00:00
|
|
|
func (s *StandaloneSuite) TestStandaloneWebDriverScenario() {
|
|
|
|
suite.Run(s.T(), NewStandaloneWebDriverSuite())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StandaloneSuite) TestOneFactorScenario() {
|
|
|
|
suite.Run(s.T(), NewOneFactorScenario())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StandaloneSuite) TestTwoFactorScenario() {
|
|
|
|
suite.Run(s.T(), NewTwoFactorScenario())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StandaloneSuite) TestBypassPolicyScenario() {
|
|
|
|
suite.Run(s.T(), NewBypassPolicyScenario())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StandaloneSuite) TestBackendProtectionScenario() {
|
|
|
|
suite.Run(s.T(), NewBackendProtectionScenario())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StandaloneSuite) TestResetPasswordScenario() {
|
|
|
|
suite.Run(s.T(), NewResetPasswordScenario())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StandaloneSuite) TestAvailableMethodsScenario() {
|
2021-02-12 05:59:42 +00:00
|
|
|
suite.Run(s.T(), NewAvailableMethodsScenario([]string{"TIME-BASED ONE-TIME PASSWORD"}))
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 14:57:42 +00:00
|
|
|
func (s *StandaloneSuite) TestRedirectionURLScenario() {
|
|
|
|
suite.Run(s.T(), NewRedirectionURLScenario())
|
|
|
|
}
|
|
|
|
|
2021-04-13 08:38:12 +00:00
|
|
|
func (s *StandaloneSuite) TestRedirectionCheckScenario() {
|
|
|
|
suite.Run(s.T(), NewRedirectionCheckScenario())
|
|
|
|
}
|
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
func TestStandaloneSuite(t *testing.T) {
|
2021-03-14 07:08:26 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping suite test in short mode")
|
|
|
|
}
|
|
|
|
|
2019-11-24 20:27:59 +00:00
|
|
|
suite.Run(t, NewStandaloneSuite())
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|