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{
|
2023-01-26 02:23:47 +00:00
|
|
|
AccessControl: schema.AccessControl{
|
2021-06-18 01:38:01 +00:00
|
|
|
DefaultPolicy: "deny",
|
2023-01-26 02:23:47 +00:00
|
|
|
Rules: []schema.AccessControlRule{},
|
2021-06-18 01:38:01 +00:00
|
|
|
}})
|
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{
|
2023-01-26 02:23:47 +00:00
|
|
|
DuoAPI: schema.DuoAPI{
|
2022-04-15 23:34:26 +00:00
|
|
|
Disable: false,
|
|
|
|
},
|
2023-01-26 02:23:47 +00:00
|
|
|
TOTP: schema.TOTP{
|
2022-03-03 11:20:43 +00:00
|
|
|
Disable: false,
|
|
|
|
},
|
2023-01-26 02:23:47 +00:00
|
|
|
WebAuthn: schema.WebAuthn{
|
2022-03-03 11:20:43 +00:00
|
|
|
Disable: false,
|
|
|
|
},
|
2023-01-26 02:23:47 +00:00
|
|
|
AccessControl: schema.AccessControl{
|
2022-03-03 11:20:43 +00:00
|
|
|
DefaultPolicy: "deny",
|
2023-01-26 02:23:47 +00:00
|
|
|
Rules: []schema.AccessControlRule{
|
2022-03-03 11:20:43 +00:00
|
|
|
{
|
|
|
|
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{
|
2023-01-26 02:23:47 +00:00
|
|
|
DuoAPI: schema.DuoAPI{
|
2022-04-15 23:34:26 +00:00
|
|
|
Disable: false,
|
|
|
|
},
|
2023-01-26 02:23:47 +00:00
|
|
|
TOTP: schema.TOTP{
|
2022-03-03 11:20:43 +00:00
|
|
|
Disable: true,
|
|
|
|
},
|
2023-01-26 02:23:47 +00:00
|
|
|
WebAuthn: schema.WebAuthn{
|
2022-03-03 11:20:43 +00:00
|
|
|
Disable: false,
|
|
|
|
},
|
2023-01-26 02:23:47 +00:00
|
|
|
AccessControl: schema.AccessControl{
|
2022-03-03 11:20:43 +00:00
|
|
|
DefaultPolicy: "deny",
|
2023-01-26 02:23:47 +00:00
|
|
|
Rules: []schema.AccessControlRule{
|
2022-03-03 11:20:43 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-04-14 16:04:42 +00:00
|
|
|
func (s *SecondFactorAvailableMethodsFixture) TestShouldRemoveWebAuthnFromAvailableMethodsWhenDisabled() {
|
2022-03-03 11:20:43 +00:00
|
|
|
s.mock.Ctx.Configuration = schema.Configuration{
|
2023-01-26 02:23:47 +00:00
|
|
|
DuoAPI: schema.DuoAPI{
|
2022-04-15 23:34:26 +00:00
|
|
|
Disable: false,
|
|
|
|
},
|
2023-01-26 02:23:47 +00:00
|
|
|
TOTP: schema.TOTP{
|
2022-03-03 11:20:43 +00:00
|
|
|
Disable: false,
|
|
|
|
},
|
2023-01-26 02:23:47 +00:00
|
|
|
WebAuthn: schema.WebAuthn{
|
2022-03-03 11:20:43 +00:00
|
|
|
Disable: true,
|
|
|
|
},
|
2023-01-26 02:23:47 +00:00
|
|
|
AccessControl: schema.AccessControl{
|
2022-03-03 11:20:43 +00:00
|
|
|
DefaultPolicy: "deny",
|
2023-01-26 02:23:47 +00:00
|
|
|
Rules: []schema.AccessControlRule{
|
2022-03-03 11:20:43 +00:00
|
|
|
{
|
|
|
|
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{
|
2023-01-26 02:23:47 +00:00
|
|
|
DuoAPI: schema.DuoAPI{
|
2022-04-15 23:34:26 +00:00
|
|
|
Disable: true,
|
|
|
|
},
|
2023-01-26 02:23:47 +00:00
|
|
|
TOTP: schema.TOTP{
|
2022-03-03 11:20:43 +00:00
|
|
|
Disable: false,
|
|
|
|
},
|
2023-01-26 02:23:47 +00:00
|
|
|
WebAuthn: schema.WebAuthn{
|
2022-03-03 11:20:43 +00:00
|
|
|
Disable: false,
|
|
|
|
},
|
2023-01-26 02:23:47 +00:00
|
|
|
AccessControl: schema.AccessControl{
|
2022-03-03 11:20:43 +00:00
|
|
|
DefaultPolicy: "deny",
|
2023-01-26 02:23:47 +00:00
|
|
|
Rules: []schema.AccessControlRule{
|
2021-06-18 01:38:01 +00:00
|
|
|
{
|
|
|
|
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{
|
2023-01-26 02:23:47 +00:00
|
|
|
DuoAPI: schema.DuoAPI{
|
2022-04-15 23:34:26 +00:00
|
|
|
Disable: false,
|
|
|
|
},
|
2023-01-26 02:23:47 +00:00
|
|
|
TOTP: schema.TOTP{
|
2022-03-03 11:20:43 +00:00
|
|
|
Disable: false,
|
|
|
|
},
|
2023-01-26 02:23:47 +00:00
|
|
|
WebAuthn: schema.WebAuthn{
|
2022-03-03 11:20:43 +00:00
|
|
|
Disable: false,
|
|
|
|
},
|
2023-01-26 02:23:47 +00:00
|
|
|
AccessControl: schema.AccessControl{
|
2022-03-03 11:20:43 +00:00
|
|
|
DefaultPolicy: "deny",
|
2023-01-26 02:23:47 +00:00
|
|
|
Rules: []schema.AccessControlRule{
|
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{
|
2023-01-26 02:23:47 +00:00
|
|
|
DuoAPI: schema.DuoAPI{
|
2022-04-15 23:34:26 +00:00
|
|
|
Disable: true,
|
|
|
|
},
|
2023-01-26 02:23:47 +00:00
|
|
|
TOTP: schema.TOTP{
|
2022-03-03 11:20:43 +00:00
|
|
|
Disable: true,
|
|
|
|
},
|
2023-01-26 02:23:47 +00:00
|
|
|
WebAuthn: schema.WebAuthn{
|
2022-03-03 11:20:43 +00:00
|
|
|
Disable: true,
|
|
|
|
},
|
2023-01-26 02:23:47 +00:00
|
|
|
AccessControl: schema.AccessControl{
|
2022-03-03 11:20:43 +00:00
|
|
|
DefaultPolicy: "deny",
|
2023-01-26 02:23:47 +00:00
|
|
|
Rules: []schema.AccessControlRule{
|
2022-03-03 11:20:43 +00:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
}
|