2019-11-24 20:27:59 +00:00
|
|
|
package suites
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-11-30 16:49:52 +00:00
|
|
|
"encoding/json"
|
2019-11-24 20:27:59 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2019-11-30 16:49:52 +00:00
|
|
|
"strings"
|
2019-11-24 20:27:59 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-11-30 16:49:52 +00:00
|
|
|
mapset "github.com/deckarep/golang-set"
|
|
|
|
"github.com/stretchr/testify/require"
|
2019-11-24 20:27:59 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
"github.com/tebeka/selenium"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CustomHeadersScenario struct {
|
|
|
|
*SeleniumSuite
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCustomHeadersScenario() *CustomHeadersScenario {
|
|
|
|
return &CustomHeadersScenario{
|
|
|
|
SeleniumSuite: new(SeleniumSuite),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CustomHeadersScenario) SetupSuite() {
|
|
|
|
wds, err := StartWebDriver()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.WebDriverSession = wds
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CustomHeadersScenario) TearDownSuite() {
|
|
|
|
err := s.WebDriverSession.Stop()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CustomHeadersScenario) SetupTest() {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
s.doLogout(ctx, s.T())
|
|
|
|
s.doVisit(s.T(), HomeBaseURL)
|
|
|
|
s.verifyIsHome(ctx, s.T())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CustomHeadersScenario) TestShouldNotForwardCustomHeaderForUnauthenticatedUser() {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
s.doVisit(s.T(), fmt.Sprintf("%s/headers", PublicBaseURL))
|
|
|
|
|
|
|
|
body, err := s.WebDriver().FindElement(selenium.ByTagName, "body")
|
|
|
|
s.Assert().NoError(err)
|
2020-05-06 01:50:37 +00:00
|
|
|
s.WaitElementTextContains(ctx, s.T(), body, "\"Host\"")
|
|
|
|
|
|
|
|
b, err := body.Text()
|
|
|
|
s.Assert().NoError(err)
|
|
|
|
s.Assert().NotContains(b, "john")
|
|
|
|
s.Assert().NotContains(b, "admins")
|
2020-10-26 11:38:08 +00:00
|
|
|
s.Assert().NotContains(b, "John Doe")
|
|
|
|
s.Assert().NotContains(b, "john.doe@authelia.com")
|
2019-11-24 20:27:59 +00:00
|
|
|
}
|
|
|
|
|
2019-11-30 16:49:52 +00:00
|
|
|
type Headers struct {
|
2020-10-26 11:38:08 +00:00
|
|
|
ForwardedEmail string `json:"Remote-Email"`
|
2020-05-06 01:50:37 +00:00
|
|
|
ForwardedGroups string `json:"Remote-Groups"`
|
2020-10-26 11:38:08 +00:00
|
|
|
ForwardedName string `json:"Remote-Name"`
|
2020-05-06 01:50:37 +00:00
|
|
|
ForwardedUser string `json:"Remote-User"`
|
2019-11-30 16:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type HeadersPayload struct {
|
|
|
|
Headers Headers `json:"headers"`
|
|
|
|
}
|
|
|
|
|
2019-11-24 20:27:59 +00:00
|
|
|
func (s *CustomHeadersScenario) TestShouldForwardCustomHeaderForAuthenticatedUser() {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2019-11-30 16:49:52 +00:00
|
|
|
expectedGroups := mapset.NewSetWith("dev", "admins")
|
|
|
|
|
2019-11-24 20:27:59 +00:00
|
|
|
targetURL := fmt.Sprintf("%s/headers", PublicBaseURL)
|
|
|
|
s.doLoginOneFactor(ctx, s.T(), "john", "password", false, targetURL)
|
|
|
|
s.verifyURLIs(ctx, s.T(), targetURL)
|
|
|
|
|
2019-11-30 16:49:52 +00:00
|
|
|
err := s.Wait(ctx, func(d selenium.WebDriver) (bool, error) {
|
|
|
|
body, err := s.WebDriver().FindElement(selenium.ByTagName, "body")
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if body == nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err := body.Text()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
payload := HeadersPayload{}
|
|
|
|
if err := json.Unmarshal([]byte(content), &payload); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
groups := strings.Split(payload.Headers.ForwardedGroups, ",")
|
|
|
|
actualGroups := mapset.NewSet()
|
|
|
|
for _, group := range groups {
|
|
|
|
actualGroups.Add(group)
|
|
|
|
}
|
|
|
|
|
2020-10-26 11:38:08 +00:00
|
|
|
return strings.Contains(payload.Headers.ForwardedUser, "john") && expectedGroups.Equal(actualGroups) &&
|
|
|
|
strings.Contains(payload.Headers.ForwardedName, "John Doe") && strings.Contains(payload.Headers.ForwardedEmail, "john.doe@authelia.com"), nil
|
2019-11-30 16:49:52 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
require.NoError(s.T(), err)
|
2019-11-24 20:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCustomHeadersScenario(t *testing.T) {
|
|
|
|
suite.Run(t, NewCustomHeadersScenario())
|
|
|
|
}
|