-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcognito.tf
More file actions
226 lines (184 loc) · 5.85 KB
/
cognito.tf
File metadata and controls
226 lines (184 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
resource "aws_cognito_user_pool" "this" {
name = "igorovo"
username_attributes = ["email"]
auto_verified_attributes = ["email"]
# Very simple password policy for testing
password_policy {
minimum_length = 6
require_lowercase = false
require_numbers = false
require_symbols = false
require_uppercase = false
}
# Simplified for demo - uncomment for production security
# user_pool_add_ons {
# advanced_security_mode = "ENFORCED"
# }
# Account recovery
account_recovery_setting {
recovery_mechanism {
name = "verified_email"
priority = 1
}
}
admin_create_user_config {
allow_admin_create_user_only = true
}
}
resource "aws_cognito_user_group" "admin" {
user_pool_id = aws_cognito_user_pool.this.id
name = "Admin"
precedence = 0
}
resource "aws_cognito_user_group" "viewer" {
user_pool_id = aws_cognito_user_pool.this.id
name = "Viewer"
precedence = 99
}
resource "aws_cognito_user_pool_client" "default" {
user_pool_id = aws_cognito_user_pool.this.id
name = "default"
# OAuth settings for Managed Login (modern replacement for deprecated Hosted UI)
generate_secret = false
callback_urls = concat(
["https://${module.cloudfront.cloudfront_distribution_domain_name}/callback"],
var.cognito_callback_urls_extra
)
logout_urls = concat(
["https://${module.cloudfront.cloudfront_distribution_domain_name}/"],
var.cognito_logout_urls_extra
)
allowed_oauth_flows = ["code"]
allowed_oauth_flows_user_pool_client = true
allowed_oauth_scopes = ["openid", "email", "profile"]
supported_identity_providers = ["COGNITO"]
# Enable advanced security features available in Managed Login
enable_token_revocation = true
prevent_user_existence_errors = "ENABLED"
token_validity_units {
access_token = "minutes"
id_token = "minutes"
refresh_token = "days"
}
access_token_validity = 60 # 1 hour
id_token_validity = 60 # 1 hour
refresh_token_validity = 7 # 7 days
explicit_auth_flows = [
"ALLOW_REFRESH_TOKEN_AUTH", # Required for token refresh
"ALLOW_USER_SRP_AUTH", # Used in test_auth.py for SRP authentication
"ALLOW_USER_PASSWORD_AUTH", # Used in test_auth.py for direct password auth
]
}
resource "awscc_cognito_managed_login_branding" "default" {
user_pool_id = aws_cognito_user_pool.this.id
client_id = aws_cognito_user_pool_client.default.id
use_cognito_provided_values = true
}
resource "aws_cognito_user_pool_domain" "this" {
domain = "igorovo-${random_string.domain_suffix.result}"
user_pool_id = aws_cognito_user_pool.this.id
# Use Managed Login (modern replacement for deprecated Hosted UI)
managed_login_version = 2
}
resource "random_string" "domain_suffix" {
length = 8
special = false
upper = false
}
data "aws_iam_policy_document" "authenticated" {
statement {
principals {
type = "Federated"
identifiers = ["cognito-identity.amazonaws.com"]
}
actions = ["sts:AssumeRoleWithWebIdentity"]
condition {
test = "StringEquals"
variable = "cognito-identity.amazonaws.com:aud"
values = [aws_cognito_identity_pool.this.id]
}
condition {
test = "ForAnyValue:StringLike"
variable = "cognito-identity.amazonaws.com:amr"
values = ["authenticated"]
}
}
}
resource "aws_iam_role" "authenticated" {
name = "IgorovoAuthenticated"
path = "/igorovo/"
assume_role_policy = data.aws_iam_policy_document.authenticated.json
}
data "aws_iam_policy_document" "authenticated_policy" {
statement {
actions = ["cognito-identity:GetCredentialsForIdentity"]
resources = ["*"]
}
statement {
actions = ["execute-api:Invoke"]
resources = [
"${module.api_gateway.api_execution_arn}/*/*",
]
}
}
resource "aws_iam_role_policy" "authenticated" {
name_prefix = "IgorovoAuthenticatedPolicy"
role = aws_iam_role.authenticated.id
policy = data.aws_iam_policy_document.authenticated_policy.json
}
data "aws_iam_policy_document" "unauthenticated" {
statement {
principals {
type = "Federated"
identifiers = ["cognito-identity.amazonaws.com"]
}
actions = ["sts:AssumeRoleWithWebIdentity"]
condition {
test = "StringEquals"
variable = "cognito-identity.amazonaws.com:aud"
values = [aws_cognito_identity_pool.this.id]
}
condition {
test = "ForAnyValue:StringLike"
variable = "cognito-identity.amazonaws.com:amr"
values = ["unauthenticated"]
}
}
}
resource "aws_iam_role" "unauthenticated" {
name = "IgorovoUnauthenticated"
path = "/igorovo/"
assume_role_policy = data.aws_iam_policy_document.unauthenticated.json
}
data "aws_iam_policy_document" "unauthenticated_policy" {
statement {
actions = ["cognito-identity:GetCredentialsForIdentity"]
resources = ["*"]
}
statement {
actions = ["execute-api:Invoke"]
resources = [
"${module.api_gateway.api_execution_arn}/*/GET/test/public",
]
}
}
resource "aws_iam_role_policy" "unauthenticated" {
name_prefix = "IgorovoUnauthenticatedPolicy"
role = aws_iam_role.unauthenticated.id
policy = data.aws_iam_policy_document.unauthenticated_policy.json
}
resource "aws_cognito_identity_pool" "this" {
identity_pool_name = "igorovo"
allow_unauthenticated_identities = true
cognito_identity_providers {
client_id = aws_cognito_user_pool_client.default.id
provider_name = aws_cognito_user_pool.this.endpoint
}
}
resource "aws_cognito_identity_pool_roles_attachment" "this" {
identity_pool_id = aws_cognito_identity_pool.this.id
roles = {
"authenticated" = aws_iam_role.authenticated.arn
"unauthenticated" = aws_iam_role.unauthenticated.arn
}
}