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()
|
|
|
|
}
|
|
|
|
|
2022-03-03 18:20:43 +07:00
|
|
|
func (s *SecondFactorAvailableMethodsFixture) TestShouldHaveAllConfiguredMethods() {
|
|
|
|
s.mock.Ctx.Configuration = schema.Configuration{
|
|
|
|
DuoAPI: &schema.DuoAPIConfiguration{},
|
|
|
|
TOTP: schema.TOTPConfiguration{
|
|
|
|
Disable: false,
|
|
|
|
},
|
|
|
|
Webauthn: schema.WebauthnConfiguration{
|
|
|
|
Disable: false,
|
|
|
|
},
|
|
|
|
AccessControl: schema.AccessControlConfiguration{
|
|
|
|
DefaultPolicy: "deny",
|
|
|
|
Rules: []schema.ACLRule{
|
|
|
|
{
|
|
|
|
Domains: []string{"example.com"},
|
|
|
|
Policy: "two_factor",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
|
|
|
|
s.mock.Ctx.Providers.Authorizer = authorization.NewAuthorizer(&s.mock.Ctx.Configuration)
|
2020-04-05 06:28:09 +07:00
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
ConfigurationGET(s.mock.Ctx)
|
2022-03-03 18:20:43 +07:00
|
|
|
|
|
|
|
s.mock.Assert200OK(s.T(), configurationBody{
|
|
|
|
AvailableMethods: []string{"totp", "webauthn", "mobile_push"},
|
|
|
|
})
|
2020-04-05 06:28:09 +07:00
|
|
|
}
|
|
|
|
|
2022-03-03 18:20:43 +07:00
|
|
|
func (s *SecondFactorAvailableMethodsFixture) TestShouldRemoveTOTPFromAvailableMethodsWhenDisabled() {
|
2020-06-21 20:40:37 +07:00
|
|
|
s.mock.Ctx.Configuration = schema.Configuration{
|
|
|
|
DuoAPI: &schema.DuoAPIConfiguration{},
|
2022-03-03 18:20:43 +07:00
|
|
|
TOTP: schema.TOTPConfiguration{
|
|
|
|
Disable: true,
|
|
|
|
},
|
|
|
|
Webauthn: schema.WebauthnConfiguration{
|
|
|
|
Disable: false,
|
|
|
|
},
|
|
|
|
AccessControl: schema.AccessControlConfiguration{
|
|
|
|
DefaultPolicy: "deny",
|
|
|
|
Rules: []schema.ACLRule{
|
|
|
|
{
|
|
|
|
Domains: []string{"example.com"},
|
|
|
|
Policy: "two_factor",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
|
|
|
|
s.mock.Ctx.Providers.Authorizer = authorization.NewAuthorizer(&s.mock.Ctx.Configuration)
|
2020-04-04 06:11:33 +07:00
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
ConfigurationGET(s.mock.Ctx)
|
2022-03-03 18:20:43 +07:00
|
|
|
|
|
|
|
s.mock.Assert200OK(s.T(), configurationBody{
|
|
|
|
AvailableMethods: []string{"webauthn", "mobile_push"},
|
|
|
|
})
|
2020-04-04 06:11:33 +07:00
|
|
|
}
|
|
|
|
|
2022-03-03 18:20:43 +07:00
|
|
|
func (s *SecondFactorAvailableMethodsFixture) TestShouldRemoveWebauthnFromAvailableMethodsWhenDisabled() {
|
|
|
|
s.mock.Ctx.Configuration = schema.Configuration{
|
|
|
|
DuoAPI: &schema.DuoAPIConfiguration{},
|
|
|
|
TOTP: schema.TOTPConfiguration{
|
|
|
|
Disable: false,
|
|
|
|
},
|
|
|
|
Webauthn: schema.WebauthnConfiguration{
|
|
|
|
Disable: true,
|
|
|
|
},
|
|
|
|
AccessControl: schema.AccessControlConfiguration{
|
|
|
|
DefaultPolicy: "deny",
|
|
|
|
Rules: []schema.ACLRule{
|
|
|
|
{
|
|
|
|
Domains: []string{"example.com"},
|
|
|
|
Policy: "two_factor",
|
2021-06-18 08:38:01 +07:00
|
|
|
},
|
2022-03-03 18:20:43 +07:00
|
|
|
},
|
|
|
|
}}
|
|
|
|
|
|
|
|
s.mock.Ctx.Providers.Authorizer = authorization.NewAuthorizer(&s.mock.Ctx.Configuration)
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
ConfigurationGET(s.mock.Ctx)
|
2022-03-03 18:20:43 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
s.mock.Assert200OK(s.T(), configurationBody{
|
2022-03-03 18:20:43 +07:00
|
|
|
AvailableMethods: []string{"totp", "mobile_push"},
|
2020-06-21 20:40:37 +07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-03 18:20:43 +07:00
|
|
|
func (s *SecondFactorAvailableMethodsFixture) TestShouldRemoveDuoFromAvailableMethodsWhenNotConfigured() {
|
|
|
|
s.mock.Ctx.Configuration = schema.Configuration{
|
|
|
|
DuoAPI: nil,
|
|
|
|
TOTP: schema.TOTPConfiguration{
|
|
|
|
Disable: false,
|
|
|
|
},
|
|
|
|
Webauthn: schema.WebauthnConfiguration{
|
|
|
|
Disable: false,
|
|
|
|
},
|
2021-06-18 08:38:01 +07:00
|
|
|
AccessControl: schema.AccessControlConfiguration{
|
2022-03-03 18:20:43 +07:00
|
|
|
DefaultPolicy: "deny",
|
2021-06-18 08:38:01 +07:00
|
|
|
Rules: []schema.ACLRule{
|
|
|
|
{
|
|
|
|
Domains: []string{"example.com"},
|
2022-03-03 18:20:43 +07:00
|
|
|
Policy: "two_factor",
|
2021-06-18 08:38:01 +07:00
|
|
|
},
|
2022-03-03 18:20:43 +07:00
|
|
|
},
|
|
|
|
}}
|
|
|
|
|
|
|
|
s.mock.Ctx.Providers.Authorizer = authorization.NewAuthorizer(&s.mock.Ctx.Configuration)
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
ConfigurationGET(s.mock.Ctx)
|
2022-03-03 18:20:43 +07:00
|
|
|
|
|
|
|
s.mock.Assert200OK(s.T(), configurationBody{
|
|
|
|
AvailableMethods: []string{"totp", "webauthn"},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SecondFactorAvailableMethodsFixture) TestShouldRemoveAllMethodsWhenNoTwoFactorACLRulesConfigured() {
|
|
|
|
s.mock.Ctx.Configuration = schema.Configuration{
|
|
|
|
DuoAPI: &schema.DuoAPIConfiguration{},
|
|
|
|
TOTP: schema.TOTPConfiguration{
|
|
|
|
Disable: false,
|
|
|
|
},
|
|
|
|
Webauthn: schema.WebauthnConfiguration{
|
|
|
|
Disable: false,
|
|
|
|
},
|
|
|
|
AccessControl: schema.AccessControlConfiguration{
|
|
|
|
DefaultPolicy: "deny",
|
|
|
|
Rules: []schema.ACLRule{
|
2021-06-18 08:38:01 +07:00
|
|
|
{
|
2022-03-03 18:20:43 +07:00
|
|
|
Domains: []string{"example.com"},
|
|
|
|
Policy: "one_factor",
|
2021-06-18 08:38:01 +07:00
|
|
|
},
|
2020-06-21 20:40:37 +07:00
|
|
|
},
|
2022-03-03 18:20:43 +07:00
|
|
|
}}
|
|
|
|
|
|
|
|
s.mock.Ctx.Providers.Authorizer = authorization.NewAuthorizer(&s.mock.Ctx.Configuration)
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
ConfigurationGET(s.mock.Ctx)
|
2022-03-03 18:20:43 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
s.mock.Assert200OK(s.T(), configurationBody{
|
2022-03-03 18:20:43 +07:00
|
|
|
AvailableMethods: []string{},
|
2020-06-21 20:40:37 +07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-03 18:20:43 +07:00
|
|
|
func (s *SecondFactorAvailableMethodsFixture) TestShouldRemoveAllMethodsWhenAllDisabledOrNotConfigured() {
|
|
|
|
s.mock.Ctx.Configuration = schema.Configuration{
|
|
|
|
DuoAPI: nil,
|
|
|
|
TOTP: schema.TOTPConfiguration{
|
|
|
|
Disable: true,
|
|
|
|
},
|
|
|
|
Webauthn: schema.WebauthnConfiguration{
|
|
|
|
Disable: true,
|
|
|
|
},
|
|
|
|
AccessControl: schema.AccessControlConfiguration{
|
|
|
|
DefaultPolicy: "deny",
|
|
|
|
Rules: []schema.ACLRule{
|
|
|
|
{
|
|
|
|
Domains: []string{"example.com"},
|
|
|
|
Policy: "two_factor",
|
2021-06-18 08:38:01 +07:00
|
|
|
},
|
2022-03-03 18:20:43 +07:00
|
|
|
},
|
|
|
|
}}
|
|
|
|
|
|
|
|
s.mock.Ctx.Providers.Authorizer = authorization.NewAuthorizer(&s.mock.Ctx.Configuration)
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
ConfigurationGET(s.mock.Ctx)
|
2022-03-03 18:20:43 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
s.mock.Assert200OK(s.T(), configurationBody{
|
2022-03-03 18:20:43 +07:00
|
|
|
AvailableMethods: []string{},
|
2020-06-21 20:40:37 +07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunSuite(t *testing.T) {
|
|
|
|
s := new(SecondFactorAvailableMethodsFixture)
|
2020-04-04 06:11:33 +07:00
|
|
|
suite.Run(t, s)
|
|
|
|
}
|