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