2019-12-07 16:40:42 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2020-04-05 12:37:21 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2021-08-11 01:04:35 +00: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 16:40:42 +00:00
|
|
|
)
|
|
|
|
|
2020-06-21 13:40:37 +00:00
|
|
|
type SecondFactorAvailableMethodsFixture struct {
|
2019-12-07 16:40:42 +00:00
|
|
|
suite.Suite
|
|
|
|
mock *mocks.MockAutheliaCtx
|
|
|
|
}
|
|
|
|
|
2020-06-21 13:40:37 +00:00
|
|
|
func (s *SecondFactorAvailableMethodsFixture) SetupTest() {
|
2019-12-07 16:40:42 +00:00
|
|
|
s.mock = mocks.NewMockAutheliaCtx(s.T())
|
2021-06-18 01:38:01 +00:00
|
|
|
s.mock.Ctx.Providers.Authorizer = authorization.NewAuthorizer(&schema.Configuration{
|
|
|
|
AccessControl: schema.AccessControlConfiguration{
|
|
|
|
DefaultPolicy: "deny",
|
|
|
|
Rules: []schema.ACLRule{},
|
|
|
|
}})
|
2019-12-07 16:40:42 +00:00
|
|
|
}
|
|
|
|
|
2020-06-21 13:40:37 +00:00
|
|
|
func (s *SecondFactorAvailableMethodsFixture) TearDownTest() {
|
2019-12-07 16:40:42 +00:00
|
|
|
s.mock.Close()
|
|
|
|
}
|
|
|
|
|
2022-03-03 11:20:43 +00: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-04 23:28:09 +00:00
|
|
|
|
2022-04-08 04:13:47 +00:00
|
|
|
ConfigurationGET(s.mock.Ctx)
|
2022-03-03 11:20:43 +00:00
|
|
|
|
|
|
|
s.mock.Assert200OK(s.T(), configurationBody{
|
|
|
|
AvailableMethods: []string{"totp", "webauthn", "mobile_push"},
|
|
|
|
})
|
2020-04-04 23:28:09 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 11:20:43 +00:00
|
|
|
func (s *SecondFactorAvailableMethodsFixture) TestShouldRemoveTOTPFromAvailableMethodsWhenDisabled() {
|
2020-06-21 13:40:37 +00:00
|
|
|
s.mock.Ctx.Configuration = schema.Configuration{
|
|
|
|
DuoAPI: &schema.DuoAPIConfiguration{},
|
2022-03-03 11:20:43 +00: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-03 23:11:33 +00:00
|
|
|
|
2022-04-08 04:13:47 +00:00
|
|
|
ConfigurationGET(s.mock.Ctx)
|
2022-03-03 11:20:43 +00:00
|
|
|
|
|
|
|
s.mock.Assert200OK(s.T(), configurationBody{
|
|
|
|
AvailableMethods: []string{"webauthn", "mobile_push"},
|
|
|
|
})
|
2020-04-03 23:11:33 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 11:20:43 +00: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 01:38:01 +00:00
|
|
|
},
|
2022-03-03 11:20:43 +00:00
|
|
|
},
|
|
|
|
}}
|
|
|
|
|
|
|
|
s.mock.Ctx.Providers.Authorizer = authorization.NewAuthorizer(&s.mock.Ctx.Configuration)
|
|
|
|
|
2022-04-08 04:13:47 +00:00
|
|
|
ConfigurationGET(s.mock.Ctx)
|
2022-03-03 11:20:43 +00:00
|
|
|
|
2021-12-01 12:11:29 +00:00
|
|
|
s.mock.Assert200OK(s.T(), configurationBody{
|
2022-03-03 11:20:43 +00:00
|
|
|
AvailableMethods: []string{"totp", "mobile_push"},
|
2020-06-21 13:40:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-03 11:20:43 +00: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 01:38:01 +00:00
|
|
|
AccessControl: schema.AccessControlConfiguration{
|
2022-03-03 11:20:43 +00:00
|
|
|
DefaultPolicy: "deny",
|
2021-06-18 01:38:01 +00:00
|
|
|
Rules: []schema.ACLRule{
|
|
|
|
{
|
|
|
|
Domains: []string{"example.com"},
|
2022-03-03 11:20:43 +00:00
|
|
|
Policy: "two_factor",
|
2021-06-18 01:38:01 +00:00
|
|
|
},
|
2022-03-03 11:20:43 +00:00
|
|
|
},
|
|
|
|
}}
|
|
|
|
|
|
|
|
s.mock.Ctx.Providers.Authorizer = authorization.NewAuthorizer(&s.mock.Ctx.Configuration)
|
|
|
|
|
2022-04-08 04:13:47 +00:00
|
|
|
ConfigurationGET(s.mock.Ctx)
|
2022-03-03 11:20:43 +00: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 01:38:01 +00:00
|
|
|
{
|
2022-03-03 11:20:43 +00:00
|
|
|
Domains: []string{"example.com"},
|
|
|
|
Policy: "one_factor",
|
2021-06-18 01:38:01 +00:00
|
|
|
},
|
2020-06-21 13:40:37 +00:00
|
|
|
},
|
2022-03-03 11:20:43 +00:00
|
|
|
}}
|
|
|
|
|
|
|
|
s.mock.Ctx.Providers.Authorizer = authorization.NewAuthorizer(&s.mock.Ctx.Configuration)
|
|
|
|
|
2022-04-08 04:13:47 +00:00
|
|
|
ConfigurationGET(s.mock.Ctx)
|
2022-03-03 11:20:43 +00:00
|
|
|
|
2021-12-01 12:11:29 +00:00
|
|
|
s.mock.Assert200OK(s.T(), configurationBody{
|
2022-03-03 11:20:43 +00:00
|
|
|
AvailableMethods: []string{},
|
2020-06-21 13:40:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-03 11:20:43 +00: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 01:38:01 +00:00
|
|
|
},
|
2022-03-03 11:20:43 +00:00
|
|
|
},
|
|
|
|
}}
|
|
|
|
|
|
|
|
s.mock.Ctx.Providers.Authorizer = authorization.NewAuthorizer(&s.mock.Ctx.Configuration)
|
|
|
|
|
2022-04-08 04:13:47 +00:00
|
|
|
ConfigurationGET(s.mock.Ctx)
|
2022-03-03 11:20:43 +00:00
|
|
|
|
2021-12-01 12:11:29 +00:00
|
|
|
s.mock.Assert200OK(s.T(), configurationBody{
|
2022-03-03 11:20:43 +00:00
|
|
|
AvailableMethods: []string{},
|
2020-06-21 13:40:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunSuite(t *testing.T) {
|
|
|
|
s := new(SecondFactorAvailableMethodsFixture)
|
2020-04-03 23:11:33 +00:00
|
|
|
suite.Run(t, s)
|
|
|
|
}
|