c668485e6f
cognito and permit shared prod environment support * code complete * user creattion tool test harness
60 lines
2.3 KiB
Go
60 lines
2.3 KiB
Go
// envid.go provides validation and constants for the COGNITO_ENVIRONMENT_ID feature.
|
|
// This feature allows multiple production environments to share a single Cognito user pool
|
|
// while keeping user visibility scoped to each environment.
|
|
package usermanagement
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
)
|
|
|
|
// CognitoEnvIDMaxLen is the maximum allowed length for a COGNITO_ENVIRONMENT_ID value.
|
|
// This is a conservative limit for human-readable environment abbreviations.
|
|
const CognitoEnvIDMaxLen = 50
|
|
|
|
// CognitoEnvIDAttrName is the Cognito custom attribute name used to tag users
|
|
// with their environment. Follows Cognito's "custom:" prefix convention.
|
|
const CognitoEnvIDAttrName = "custom:environment_id"
|
|
|
|
// CognitoEnvIDEnvVar is the server-side environment variable that sets the
|
|
// environment identity for this service instance.
|
|
const CognitoEnvIDEnvVar = "COGNITO_ENVIRONMENT_ID"
|
|
|
|
// envIDPattern matches a valid environment ID: starts with lowercase alpha,
|
|
// ends with lowercase alphanumeric, middle characters allow lowercase alphanumeric
|
|
// and single underscores (no consecutive underscores). Minimum 1 char.
|
|
var envIDPattern = regexp.MustCompile(`^[a-z]([a-z0-9]|_[a-z0-9])*$`)
|
|
|
|
// ValidateCognitoEnvironmentID checks whether the given environment ID is valid.
|
|
// An empty string is valid and means "not set". A valid non-empty value must:
|
|
// - Start with a lowercase alpha character (a-z)
|
|
// - End with a lowercase alphanumeric character (a-z, 0-9)
|
|
// - Contain only lowercase alphanumeric characters and underscores
|
|
// - Not have consecutive underscores
|
|
// - Not start or end with an underscore
|
|
// - Be at most CognitoEnvIDMaxLen (50) characters long
|
|
//
|
|
// Parameters:
|
|
// - id: the environment ID string to validate
|
|
//
|
|
// Returns:
|
|
// - error: nil if valid, descriptive error if invalid
|
|
func ValidateCognitoEnvironmentID(id string) error {
|
|
if id == "" {
|
|
return nil
|
|
}
|
|
|
|
if len(id) > CognitoEnvIDMaxLen {
|
|
return fmt.Errorf("cognito environment ID %q exceeds maximum length of %d characters (got %d)",
|
|
id, CognitoEnvIDMaxLen, len(id))
|
|
}
|
|
|
|
if !envIDPattern.MatchString(id) {
|
|
return fmt.Errorf("cognito environment ID %q is invalid: must start with lowercase letter, "+
|
|
"end with lowercase alphanumeric, contain only lowercase alphanumeric and non-consecutive underscores",
|
|
id)
|
|
}
|
|
|
|
return nil
|
|
}
|