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

environment_level_auth_configuration resource #23

Merged
merged 6 commits into from
Nov 22, 2024
Merged
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
62 changes: 62 additions & 0 deletions docs/resources/environment_level_auth_configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "propelauth_environment_level_auth_configuration Resource - propelauth"
subcategory: ""
description: |-
Environment-level Auth Configuration. This is for configuring elements of the signup and login experience in PropelAuth that you may want to differ between test and production environments.
---

# propelauth_environment_level_auth_configuration (Resource)

Environment-level Auth Configuration. This is for configuring elements of the signup and login experience in PropelAuth that you may want to differ between test and production environments.

## Example Usage

```terraform
resource "propelauth_environment_level_auth_configuration" "test_example" {
environment = "Test"
require_email_confirmation = false
allow_public_signups = true
}

resource "propelauth_custom_domain_verification" "my_custom_domain_verification" {
# Fields are incomplete here for simplicity.
# See the documentation for the "propelauth_custom_domain_verification" resource for more information
environment = "Prod"
}

# Prod and Staging environments don't exist until a domain has been verified for them,
# so we need to depend on the verification of the domain before creating the environment-level auth configuration
resource "propelauth_environment_level_auth_configuration" "prod_example" {
depends_on = [
propelauth_custom_domain_verification.my_custom_domain_verification
]
environment = "Prod"
require_email_confirmation = true
allow_public_signups = false
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `environment` (String) The environment for which you are configuring the login and signup experience. Accepted values are `Test`, `Staging`, and `Prod`.

### Optional

- `allow_public_signups` (Boolean) If true, new users will be able to sign up for your product directly in the PropelAuth hosted pages.The default setting is true for all environments.
- `require_email_confirmation` (Boolean) If true, all users are required to have confirmed email addresses. Whenever PropelAuth doesn't know for certain whether a user's email address is in fact owned by them, PropelAuth will trigger an email confirmation flow. The default setting is true for `Prod` and `Staging` environments but is false for `Test` for ease of development.
- `waitlist_users_require_email_confirmation` (Boolean) If true, all waitlisted users are required to have confirmed email addresses. Whenever PropelAuth doesn't know for certain whether a waitlisted user's email address is in fact owned by them, PropelAuth will trigger an email confirmation flow. The default setting is false for all environments.

## Import

Import is supported using the following syntax:

```shell
# Import using the target environment as the ID: `Test`, `Staging`, or `Prod`. For example:
terraform import propelauth_environment_level_auth_configuration.test_env_auth_config Test
# or
terraform import propelauth_environment_level_auth_configuration.prod_env_auth_config Prod
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Import using the target environment as the ID: `Test`, `Staging`, or `Prod`. For example:
terraform import propelauth_environment_level_auth_configuration.test_env_auth_config Test
# or
terraform import propelauth_environment_level_auth_configuration.prod_env_auth_config Prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
resource "propelauth_environment_level_auth_configuration" "test_example" {
environment = "Test"
require_email_confirmation = false
allow_public_signups = true
}

resource "propelauth_custom_domain_verification" "my_custom_domain_verification" {
# Fields are incomplete here for simplicity.
# See the documentation for the "propelauth_custom_domain_verification" resource for more information
environment = "Prod"
}

# Prod and Staging environments don't exist until a domain has been verified for them,
# so we need to depend on the verification of the domain before creating the environment-level auth configuration
resource "propelauth_environment_level_auth_configuration" "prod_example" {
depends_on = [
propelauth_custom_domain_verification.my_custom_domain_verification
]
environment = "Prod"
require_email_confirmation = true
allow_public_signups = false
}
2 changes: 1 addition & 1 deletion internal/propelauth/environment_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
)

// GetEnvironmentConfig - Returns a project metadata.
// GetEnvironmentConfig - Returns the current environment configuration for a project.
func (c *PropelAuthClient) GetEnvironmentConfig() (*EnvironmentConfigResponse, error) {
res, err := c.get("config")
if err != nil {
Expand Down
19 changes: 19 additions & 0 deletions internal/propelauth/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,25 @@ type ApiKeyExpirationOptions struct {
Never bool `json:"Never"`
}

type RealmConfigUpdate struct {
AutoConfirmEmails *bool `json:"auto_confirm_emails,omitempty"`
AllowPublicSignups *bool `json:"allow_public_signups,omitempty"`
WaitlistUsersRequireEmailConfirmation *bool `json:"waitlist_users_require_email_confirmation,omitempty"`
}

type RealmConfigsResponse struct {
Test RealmConfigResponse `json:"test"`
Staging *RealmConfigResponse `json:"staging"`
Prod *RealmConfigResponse `json:"prod"`
}

type RealmConfigResponse struct {
AutoConfirmEmails bool `json:"auto_confirm_emails"`
AllowPublicSignups bool `json:"allow_public_signups"`
WaitlistUsersRequireEmailConfirmation bool `json:"waitlist_users_require_email_confirmation"`
AuthHostname string `json:"auth_hostname"`
}

type UserProperties struct {
Fields []UserProperty `json:"fields"`
}
Expand Down
47 changes: 47 additions & 0 deletions internal/propelauth/realm_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package propelauth

import (
"encoding/json"
"fmt"
"strings"
)

// GetEnvironmentConfig - Get the realm's login/signup configuration.
func (c *PropelAuthClient) GetRealmConfig(environment string) (*RealmConfigResponse, error) {
res, err := c.get("realm")
if err != nil {
return nil, err
}

realmConfigs := RealmConfigsResponse{}
err = json.Unmarshal(res.BodyBytes, &realmConfigs)
if err != nil {
return nil, err
}

switch environment {
case "Test":
return &realmConfigs.Test, nil
case "Staging":
return realmConfigs.Staging, nil
case "Prod":
return realmConfigs.Prod, nil
default:
return nil, fmt.Errorf("invalid environment when fetching realm config: %s", environment)
}
}

// UpdateRealmConfig - Updates the realms login/signup configuration ignoring any null values.
func (c *PropelAuthClient) UpdateRealmConfig(environment string, realmConfig RealmConfigUpdate) (*RealmConfigResponse, error) {
body, err := json.Marshal(realmConfig)
if err != nil {
return nil, err
}

_, err = c.patch(fmt.Sprintf("realm/%s", strings.ToLower(environment)), body)
if err != nil {
return nil, err
}

return c.GetRealmConfig(environment)
}
12 changes: 12 additions & 0 deletions internal/propelauth/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,15 @@ func Contains(slice []string, target string) bool {
}
return false
}

func FlipBoolRef(b *bool) *bool {
if b == nil {
return nil
} else if *b {
new_b := false
return &new_b
} else {
new_b := true
return &new_b
}
}
Loading