2019-04-24 21:52:08 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-04-05 12:37:21 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2020-03-06 00:31:09 +00:00
|
|
|
"github.com/authelia/authelia/internal/authorization"
|
2020-03-25 01:48:20 +00:00
|
|
|
"github.com/authelia/authelia/internal/configuration/schema"
|
2019-12-24 02:14:52 +00:00
|
|
|
"github.com/authelia/authelia/internal/mocks"
|
2019-04-24 21:52:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type SecondFactorAvailableMethodsFixture struct {
|
|
|
|
suite.Suite
|
|
|
|
mock *mocks.MockAutheliaCtx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SecondFactorAvailableMethodsFixture) SetupTest() {
|
|
|
|
s.mock = mocks.NewMockAutheliaCtx(s.T())
|
2020-03-06 00:31:09 +00:00
|
|
|
s.mock.Ctx.Providers.Authorizer = authorization.NewAuthorizer(schema.AccessControlConfiguration{
|
|
|
|
DefaultPolicy: "deny",
|
|
|
|
Rules: []schema.ACLRule{},
|
|
|
|
})
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SecondFactorAvailableMethodsFixture) TearDownTest() {
|
|
|
|
s.mock.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SecondFactorAvailableMethodsFixture) TestShouldServeDefaultMethods() {
|
2020-03-25 01:48:20 +00:00
|
|
|
s.mock.Ctx.Configuration = schema.Configuration{
|
|
|
|
TOTP: &schema.TOTPConfiguration{
|
2020-04-05 12:37:21 +00:00
|
|
|
Period: schema.DefaultTOTPConfiguration.Period,
|
2020-03-25 01:48:20 +00:00
|
|
|
},
|
|
|
|
}
|
2019-12-07 16:40:42 +00:00
|
|
|
expectedBody := ExtendedConfigurationBody{
|
2020-03-06 00:31:09 +00:00
|
|
|
AvailableMethods: []string{"totp", "u2f"},
|
|
|
|
SecondFactorEnabled: false,
|
2020-04-05 12:37:21 +00:00
|
|
|
TOTPPeriod: schema.DefaultTOTPConfiguration.Period,
|
2019-12-07 16:40:42 +00:00
|
|
|
}
|
|
|
|
ExtendedConfigurationGet(s.mock.Ctx)
|
|
|
|
s.mock.Assert200OK(s.T(), expectedBody)
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2019-12-07 17:51:47 +00:00
|
|
|
func (s *SecondFactorAvailableMethodsFixture) TestShouldServeDefaultMethodsAndMobilePush() {
|
2019-04-24 21:52:08 +00:00
|
|
|
s.mock.Ctx.Configuration = schema.Configuration{
|
|
|
|
DuoAPI: &schema.DuoAPIConfiguration{},
|
2020-03-25 01:48:20 +00:00
|
|
|
TOTP: &schema.TOTPConfiguration{
|
2020-04-05 12:37:21 +00:00
|
|
|
Period: schema.DefaultTOTPConfiguration.Period,
|
2020-03-25 01:48:20 +00:00
|
|
|
},
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
2019-12-07 16:40:42 +00:00
|
|
|
expectedBody := ExtendedConfigurationBody{
|
2020-03-06 00:31:09 +00:00
|
|
|
AvailableMethods: []string{"totp", "u2f", "mobile_push"},
|
|
|
|
SecondFactorEnabled: false,
|
2020-04-05 12:37:21 +00:00
|
|
|
TOTPPeriod: schema.DefaultTOTPConfiguration.Period,
|
2019-12-07 16:40:42 +00:00
|
|
|
}
|
|
|
|
ExtendedConfigurationGet(s.mock.Ctx)
|
|
|
|
s.mock.Assert200OK(s.T(), expectedBody)
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-06 00:31:09 +00:00
|
|
|
func (s *SecondFactorAvailableMethodsFixture) TestShouldCheckSecondFactorIsDisabledWhenNoRuleIsSetToTwoFactor() {
|
2020-03-25 01:48:20 +00:00
|
|
|
s.mock.Ctx.Configuration = schema.Configuration{
|
|
|
|
TOTP: &schema.TOTPConfiguration{
|
2020-04-05 12:37:21 +00:00
|
|
|
Period: schema.DefaultTOTPConfiguration.Period,
|
2020-03-25 01:48:20 +00:00
|
|
|
},
|
|
|
|
}
|
2020-03-06 00:31:09 +00:00
|
|
|
s.mock.Ctx.Providers.Authorizer = authorization.NewAuthorizer(schema.AccessControlConfiguration{
|
|
|
|
DefaultPolicy: "bypass",
|
|
|
|
Rules: []schema.ACLRule{
|
2020-04-09 01:05:17 +00:00
|
|
|
{
|
2020-03-06 00:31:09 +00:00
|
|
|
Domain: "example.com",
|
|
|
|
Policy: "deny",
|
|
|
|
},
|
2020-04-09 01:05:17 +00:00
|
|
|
{
|
2020-03-06 00:31:09 +00:00
|
|
|
Domain: "abc.example.com",
|
|
|
|
Policy: "single_factor",
|
|
|
|
},
|
2020-04-09 01:05:17 +00:00
|
|
|
{
|
2020-03-06 00:31:09 +00:00
|
|
|
Domain: "def.example.com",
|
|
|
|
Policy: "bypass",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
ExtendedConfigurationGet(s.mock.Ctx)
|
|
|
|
s.mock.Assert200OK(s.T(), ExtendedConfigurationBody{
|
|
|
|
AvailableMethods: []string{"totp", "u2f"},
|
|
|
|
SecondFactorEnabled: false,
|
2020-04-05 12:37:21 +00:00
|
|
|
TOTPPeriod: schema.DefaultTOTPConfiguration.Period,
|
2020-03-06 00:31:09 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SecondFactorAvailableMethodsFixture) TestShouldCheckSecondFactorIsEnabledWhenDefaultPolicySetToTwoFactor() {
|
2020-03-25 01:48:20 +00:00
|
|
|
s.mock.Ctx.Configuration = schema.Configuration{
|
|
|
|
TOTP: &schema.TOTPConfiguration{
|
2020-04-05 12:37:21 +00:00
|
|
|
Period: schema.DefaultTOTPConfiguration.Period,
|
2020-03-25 01:48:20 +00:00
|
|
|
},
|
|
|
|
}
|
2020-03-06 00:31:09 +00:00
|
|
|
s.mock.Ctx.Providers.Authorizer = authorization.NewAuthorizer(schema.AccessControlConfiguration{
|
|
|
|
DefaultPolicy: "two_factor",
|
|
|
|
Rules: []schema.ACLRule{
|
2020-04-09 01:05:17 +00:00
|
|
|
{
|
2020-03-06 00:31:09 +00:00
|
|
|
Domain: "example.com",
|
|
|
|
Policy: "deny",
|
|
|
|
},
|
2020-04-09 01:05:17 +00:00
|
|
|
{
|
2020-03-06 00:31:09 +00:00
|
|
|
Domain: "abc.example.com",
|
|
|
|
Policy: "single_factor",
|
|
|
|
},
|
2020-04-09 01:05:17 +00:00
|
|
|
{
|
2020-03-06 00:31:09 +00:00
|
|
|
Domain: "def.example.com",
|
|
|
|
Policy: "bypass",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
ExtendedConfigurationGet(s.mock.Ctx)
|
|
|
|
s.mock.Assert200OK(s.T(), ExtendedConfigurationBody{
|
|
|
|
AvailableMethods: []string{"totp", "u2f"},
|
|
|
|
SecondFactorEnabled: true,
|
2020-04-05 12:37:21 +00:00
|
|
|
TOTPPeriod: schema.DefaultTOTPConfiguration.Period,
|
2020-03-06 00:31:09 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SecondFactorAvailableMethodsFixture) TestShouldCheckSecondFactorIsEnabledWhenSomePolicySetToTwoFactor() {
|
2020-03-25 01:48:20 +00:00
|
|
|
s.mock.Ctx.Configuration = schema.Configuration{
|
|
|
|
TOTP: &schema.TOTPConfiguration{
|
2020-04-05 12:37:21 +00:00
|
|
|
Period: schema.DefaultTOTPConfiguration.Period,
|
2020-03-25 01:48:20 +00:00
|
|
|
},
|
|
|
|
}
|
2020-03-06 00:31:09 +00:00
|
|
|
s.mock.Ctx.Providers.Authorizer = authorization.NewAuthorizer(schema.AccessControlConfiguration{
|
|
|
|
DefaultPolicy: "bypass",
|
|
|
|
Rules: []schema.ACLRule{
|
2020-04-09 01:05:17 +00:00
|
|
|
{
|
2020-03-06 00:31:09 +00:00
|
|
|
Domain: "example.com",
|
|
|
|
Policy: "deny",
|
|
|
|
},
|
2020-04-09 01:05:17 +00:00
|
|
|
{
|
2020-03-06 00:31:09 +00:00
|
|
|
Domain: "abc.example.com",
|
|
|
|
Policy: "two_factor",
|
|
|
|
},
|
2020-04-09 01:05:17 +00:00
|
|
|
{
|
2020-03-06 00:31:09 +00:00
|
|
|
Domain: "def.example.com",
|
|
|
|
Policy: "bypass",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
ExtendedConfigurationGet(s.mock.Ctx)
|
|
|
|
s.mock.Assert200OK(s.T(), ExtendedConfigurationBody{
|
|
|
|
AvailableMethods: []string{"totp", "u2f"},
|
|
|
|
SecondFactorEnabled: true,
|
2020-04-05 12:37:21 +00:00
|
|
|
TOTPPeriod: schema.DefaultTOTPConfiguration.Period,
|
2020-03-06 00:31:09 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
func TestRunSuite(t *testing.T) {
|
|
|
|
s := new(SecondFactorAvailableMethodsFixture)
|
|
|
|
suite.Run(t, s)
|
|
|
|
}
|