Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MultiplefactorConfig.state support in Project and Tenant config #569

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions auth/multi_factor_config_mgt.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ const (
// MultiFactorConfig represents a multi-factor configuration for a tenant or project.
// This can be used to define whether multi-factor authentication is enabled or disabled and the list of second factor challenges that are supported.
type MultiFactorConfig struct {
// The multi-factor config state.
State MultiFactorConfigState `json:"state,omitempty"`
// A slice of pointers to ProviderConfig structs, each outlining the specific second factor authorization method.
ProviderConfigs []*ProviderConfig `json:"providerConfigs,omitempty"`
}
Expand All @@ -53,8 +55,12 @@ func (mfa *MultiFactorConfig) validate() error {
if mfa == nil {
return nil
}
if len(mfa.ProviderConfigs) == 0 {
return fmt.Errorf("\"ProviderConfigs\" must be a non-empty array of type \"ProviderConfig\"s")
if mfa.State == "" && len(mfa.ProviderConfigs) == 0 {
return fmt.Errorf("\"State\" or \"ProviderConfigs\" must be a non-empty")
}
state := string(mfa.State)
if state != "" && state != string(Enabled) && state != string(Disabled) {
return fmt.Errorf("\"MultiFactorConfig.State\" must be 'Enabled' or 'Disabled'")
}
for _, providerConfig := range mfa.ProviderConfigs {
if providerConfig == nil {
Expand Down
18 changes: 15 additions & 3 deletions auth/multi_factor_config_mgt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

func TestMultiFactorConfig(t *testing.T) {
mfa := MultiFactorConfig{
State: Enabled,
ProviderConfigs: []*ProviderConfig{{
State: Disabled,
TOTPProviderConfig: &TOTPProviderConfig{
Expand All @@ -31,9 +32,20 @@ func TestMultiFactorConfig(t *testing.T) {
t.Errorf("MultiFactorConfig not valid")
}
}
func TestMultiFactorConfigNoProviderConfigs(t *testing.T) {

func TestMultiFactorConfigNoStateNoProviderConfigs(t *testing.T) {
mfa := MultiFactorConfig{}
want := "\"ProviderConfigs\" must be a non-empty array of type \"ProviderConfig\"s"
want := "\"State\" or \"ProviderConfigs\" must be a non-empty"
if err := mfa.validate(); err.Error() != want {
t.Errorf("MultiFactorConfig.validate(nil) = %v, want = %q", err, want)
}
}

func TestMultiFactorConfigInvalidState(t *testing.T) {
mfa := MultiFactorConfig{
State: "invalid",
}
want := "\"MultiFactorConfig.State\" must be 'Enabled' or 'Disabled'"
if err := mfa.validate(); err.Error() != want {
t.Errorf("MultiFactorConfig.validate(nil) = %v, want = %q", err, want)
}
Expand All @@ -43,7 +55,7 @@ func TestMultiFactorConfigNilProviderConfigs(t *testing.T) {
mfa := MultiFactorConfig{
ProviderConfigs: nil,
}
want := "\"ProviderConfigs\" must be a non-empty array of type \"ProviderConfig\"s"
want := "\"State\" or \"ProviderConfigs\" must be a non-empty"
if err := mfa.validate(); err.Error() != want {
t.Errorf("MultiFactorConfig.validate(nil) = %v, want = %q", err, want)
}
Expand Down
3 changes: 3 additions & 0 deletions auth/project_config_mgt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

const projectConfigResponse = `{
"mfa": {
"state":"ENABLED",
"providerConfigs": [
{
"state":"ENABLED",
Expand All @@ -41,6 +42,7 @@ const projectConfigResponse = `{

var testProjectConfig = &ProjectConfig{
MultiFactorConfig: &MultiFactorConfig{
State: Enabled,
ProviderConfigs: []*ProviderConfig{
{
State: Enabled,
Expand Down Expand Up @@ -84,6 +86,7 @@ func TestUpdateProjectConfig(t *testing.T) {
}
wantBody := map[string]interface{}{
"mfa": map[string]interface{}{
"state": "ENABLED",
"providerConfigs": []interface{}{
map[string]interface{}{
"state": "ENABLED",
Expand Down
6 changes: 6 additions & 0 deletions auth/tenant_mgt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,7 @@ const tenantResponse = `{
"enableEmailLinkSignin": true,
"enableAnonymousUser": true,
"mfaConfig": {
"state": "ENABLED",
"providerConfigs": [
{
"state":"ENABLED",
Expand All @@ -1105,6 +1106,7 @@ const tenantResponse2 = `{
"enableEmailLinkSignin": true,
"enableAnonymousUser": true,
"mfaConfig": {
"state": "ENABLED",
"providerConfigs": [
{
"state":"ENABLED",
Expand All @@ -1129,6 +1131,7 @@ var testTenant = &Tenant{
EnableEmailLinkSignIn: true,
EnableAnonymousUsers: true,
MultiFactorConfig: &MultiFactorConfig{
State: Enabled,
ProviderConfigs: []*ProviderConfig{
{
State: Enabled,
Expand All @@ -1147,6 +1150,7 @@ var testTenant2 = &Tenant{
EnableEmailLinkSignIn: true,
EnableAnonymousUsers: true,
MultiFactorConfig: &MultiFactorConfig{
State: Enabled,
ProviderConfigs: []*ProviderConfig{
{
State: Enabled,
Expand Down Expand Up @@ -1239,6 +1243,7 @@ func TestCreateTenant(t *testing.T) {
"enableEmailLinkSignin": testTenant.EnableEmailLinkSignIn,
"enableAnonymousUser": testTenant.EnableAnonymousUsers,
"mfaConfig": map[string]interface{}{
"state": "ENABLED",
"providerConfigs": []interface{}{
map[string]interface{}{
"state": "ENABLED",
Expand Down Expand Up @@ -1351,6 +1356,7 @@ func TestUpdateTenant(t *testing.T) {
"enableEmailLinkSignin": testTenant.EnableEmailLinkSignIn,
"enableAnonymousUser": testTenant.EnableAnonymousUsers,
"mfaConfig": map[string]interface{}{
"state": "ENABLED",
"providerConfigs": []interface{}{
map[string]interface{}{
"state": "ENABLED",
Expand Down
2 changes: 2 additions & 0 deletions integration/auth/project_config_mgt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

func TestProjectConfig(t *testing.T) {
mfaObject := &auth.MultiFactorConfig{
State: auth.Enabled,
ProviderConfigs: []*auth.ProviderConfig{
{
State: auth.Enabled,
Expand All @@ -35,6 +36,7 @@ func TestProjectConfig(t *testing.T) {
want := &auth.ProjectConfig{
MultiFactorConfig: mfaObject,
}

t.Run("UpdateProjectConfig()", func(t *testing.T) {
mfaConfigReq := *want.MultiFactorConfig
req := (&auth.ProjectConfigToUpdate{}).
Expand Down
60 changes: 34 additions & 26 deletions integration/auth/tenant_mgt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,37 +138,45 @@ func TestTenantManager(t *testing.T) {
})

t.Run("UpdateTenant()", func(t *testing.T) {
mfaObject := &auth.MultiFactorConfig{
ProviderConfigs: []*auth.ProviderConfig{
{
State: auth.Enabled,
TOTPProviderConfig: &auth.TOTPProviderConfig{
AdjacentIntervals: 5,
mfaObjects := []*auth.MultiFactorConfig{
&auth.MultiFactorConfig{
State: auth.Enabled,
},
&auth.MultiFactorConfig{
ProviderConfigs: []*auth.ProviderConfig{
{
State: auth.Enabled,
TOTPProviderConfig: &auth.TOTPProviderConfig{
AdjacentIntervals: 5,
},
},
},
},
}
want = &auth.Tenant{
ID: id,
DisplayName: "updated-go-tenant",
AllowPasswordSignUp: false,
EnableEmailLinkSignIn: false,
EnableAnonymousUsers: false,
MultiFactorConfig: mfaObject,
}
req := (&auth.TenantToUpdate{}).
DisplayName("updated-go-tenant").
AllowPasswordSignUp(false).
EnableEmailLinkSignIn(false).
EnableAnonymousUsers(false).
MultiFactorConfig(*mfaObject)
tenant, err := client.TenantManager.UpdateTenant(context.Background(), id, req)
if err != nil {
t.Fatalf("UpdateTenant() = %v", err)
}

if !reflect.DeepEqual(tenant, want) {
t.Errorf("UpdateTenant() = %#v; want = %#v", tenant, want)
for _, mfaObject := range mfaObjects {
want = &auth.Tenant{
ID: id,
DisplayName: "updated-go-tenant",
AllowPasswordSignUp: false,
EnableEmailLinkSignIn: false,
EnableAnonymousUsers: false,
MultiFactorConfig: mfaObject,
}
req := (&auth.TenantToUpdate{}).
DisplayName("updated-go-tenant").
AllowPasswordSignUp(false).
EnableEmailLinkSignIn(false).
EnableAnonymousUsers(false).
MultiFactorConfig(*mfaObject)
tenant, err := client.TenantManager.UpdateTenant(context.Background(), id, req)
if err != nil {
t.Fatalf("UpdateTenant() = %v", err)
}

if !reflect.DeepEqual(tenant, want) {
t.Errorf("UpdateTenant() = %#v; want = %#v", tenant, want)
}
}
})

Expand Down