Skip to content
This repository has been archived by the owner on Mar 8, 2022. It is now read-only.

Commit

Permalink
Adding guardian example configuration, removing empty line
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Vedder committed Jan 24, 2022
1 parent f01555e commit 5ebd83f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 52 deletions.
73 changes: 21 additions & 52 deletions auth0/resource_auth0_guardian.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package auth0

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
Expand Down Expand Up @@ -48,7 +50,6 @@ func newGuardian() *schema.Resource {
"message_types": {
Type: schema.TypeList,
Required: true,

Elem: &schema.Schema{
Type: schema.TypeString,
},
Expand Down Expand Up @@ -91,28 +92,17 @@ func newGuardian() *schema.Resource {
},
},
},
"email": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
}
}

func createGuardian(d *schema.ResourceData, m interface{}) error {
d.SetId(resource.UniqueId())
return updateGuardian(d, m)
}

func deleteGuardian(d *schema.ResourceData, m interface{}) error {
api := m.(*management.Management)
if err := api.Guardian.MultiFactor.Phone.Enable(false); err != nil {
return err
}
if err := api.Guardian.MultiFactor.Email.Enable(false); err != nil {
return err
}
api.Guardian.MultiFactor.Phone.Enable(false)
d.SetId("")
return nil
}
Expand All @@ -128,46 +118,24 @@ func updateGuardian(d *schema.ResourceData, m interface{}) (err error) {
err = api.Guardian.MultiFactor.UpdatePolicy(&management.MultiFactorPolicies{p})
}
}
if err := updatePhoneFactor(d, api); err != nil {
return err
}
if err := updateEmailFactor(d, api); err != nil {
return err
}
return readGuardian(d, m)
}

func updatePhoneFactor(d *schema.ResourceData, api *management.Management) error {
//TODO: Extend for other MFA types
ok, err := factorShouldBeUpdated(d, "phone")
if err != nil {
return err
}
if ok {
if err := api.Guardian.MultiFactor.Phone.Enable(true); err != nil {
return err
}
api.Guardian.MultiFactor.Phone.Enable(true)
if err := configurePhone(d, api); err != nil {
return err
}
} else {
if err := api.Guardian.MultiFactor.Phone.Enable(false); err != nil {
return err
}
api.Guardian.MultiFactor.Phone.Enable(false)
}
return nil
}

func updateEmailFactor(d *schema.ResourceData, api *management.Management) error {
if changed := d.HasChange("email"); changed {
enabled := d.Get("email").(bool)
if err := api.Guardian.MultiFactor.Email.Enable(enabled); err != nil {
return err
}
}
return nil
return readGuardian(d, m)
}

func configurePhone(d *schema.ResourceData, api *management.Management) (err error) {

md := make(MapData)
List(d, "phone").Elem(func(d ResourceData) {
md.Set("provider", String(d, "provider", HasChange()))
Expand Down Expand Up @@ -273,7 +241,6 @@ func readGuardian(d *schema.ResourceData, m interface{}) error {
if err != nil {
return err
}

ok, err := factorShouldBeUpdated(d, "phone")
if err != nil {
return err
Expand All @@ -287,17 +254,6 @@ func readGuardian(d *schema.ResourceData, m interface{}) error {
if err != nil {
return err
}

factors, err := api.Guardian.MultiFactor.List()
if err != nil {
return err
}
for _, v := range factors {
switch *v.Name {
case "email":
d.Set("email", v.Enabled)
}
}
return nil
}

Expand Down Expand Up @@ -352,6 +308,19 @@ func typeAssertToStringArray(from []interface{}) *[]string {
return &stringArray
}

func isFactorEnabled(factor string, api *management.Management) (*bool, error) {
mfs, err := api.Guardian.MultiFactor.List()
if err != nil {
return nil, err
}
for _, mf := range mfs {
if *mf.Name == factor {
return mf.Enabled, nil
}
}
return nil, fmt.Errorf("factor %s is not among the possible factors", factor)
}

// Determines if the factor should be updated. This depends on if it is in the state, if it is about to be added to the state.
func factorShouldBeUpdated(d *schema.ResourceData, factor string) (bool, error) {
_, ok := d.GetOk(factor)
Expand Down
14 changes: 14 additions & 0 deletions example/guardian/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
provider "auth0" {}

resource "auth0_guardian" "guardian"{
email = false
policy = "all-applications"
phone{
provider = "auth0"
message_types = ["sms","voice"]
options{
verification_message = "{{code}} is your verification code for {{tenant.friendly_name}}. Please enter this code to verify your enrollment."
enrollment_message = "{{code}} is your verification code for {{tenant.friendly_name}}."
}
}
}

0 comments on commit 5ebd83f

Please sign in to comment.