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