-
Notifications
You must be signed in to change notification settings - Fork 204
/
oidc-cognito-stack.tf
151 lines (131 loc) · 3.67 KB
/
oidc-cognito-stack.tf
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
// This is to server as a example of a full stack including Okta resources.
// This example is of a serverless SPA that allows users to authenticate via Okta credentials and invoke a Lambda function.
// For brevity only the core resources are defined.
locals = {
// Potentially could be scripted https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_create_oidc_verify-thumbprint.html
jwk_thumbprint = "myThumbprint"
uri = "https://something.com"
}
provider "okta" {
org_name = "something"
base_url = "okta.com"
}
data "okta_group" "peeps" {
name = "Peeps"
description = "For my peeps"
}
data "okta_user" "garth" {
first_name = "Garth"
last_name = "B"
login = "[email protected]"
email = "[email protected]"
group_memberships = [okta_group.peeps.id]
}
resource "okta_app_oauth" "app" {
label = "G Studios"
type = "browser"
client_uri = local.uri
redirect_uris = [local.uri]
groups = [okta_group.peeps.id]
response_types = ["token", "id_token"]
grant_types = ["implicit"]
}
resource "okta_trusted_origin" "app" {
name = "Something"
origin = local.uri
scopes = ["CORS", "REDIRECT"]
}
// AWS resources for auth to bring it full circle
resource "aws_cognito_identity_pool" "slick_idpool" {
identity_pool_name = "Cool Stuff Neat Stuff Slick stuff"
allow_unauthenticated_identities = false
openid_connect_provider_arns = [aws_iam_openid_connect_provider.openid.arn]
}
resource "aws_iam_openid_connect_provider" "openid" {
url = "https://articulate.okta.com"
client_id_list = [
okta_app_oauth.app.id,
]
thumbprint_list = [
local.jwk_thumbprint,
]
}
resource "aws_iam_role" "authenticated" {
name = "cognito_authenticated"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "cognito-identity.amazonaws.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"cognito-identity.amazonaws.com:aud": "${aws_cognito_identity_pool.slick_idpool.id}"
},
"ForAnyValue:StringLike": {
"cognito-identity.amazonaws.com:amr": "authenticated"
}
}
}
]
}
EOF
}
resource "aws_iam_role_policy" "authenticated" {
name = "auth"
role = aws_iam_role.authenticated.id
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"mobileanalytics:PutEvents",
"cognito-sync:*",
"cognito-identity:*"
],
"Resource": [
"*"
]
},
{
"Effect": "Allow",
"Action": [
"lambda:InvokeFunction",
"lambda:InvokeAsync"
],
"Resource": "${aws_lambda_function.doSlickStuff.arn}"
}
]
}
EOF
}
resource "aws_cognito_identity_pool_roles_attachment" "role_attachment" {
identity_pool_id = aws_cognito_identity_pool.slick_idpool.id
role_mapping {
identity_provider = aws_iam_openid_connect_provider.openid.arn
ambiguous_role_resolution = "AuthenticatedRole"
type = "Rules"
mapping_rule {
claim = "staff"
match_type = "Equals"
role_arn = aws_iam_role.authenticated.arn
value = "true"
}
mapping_rule {
claim = "aud"
match_type = "Equals"
role_arn = aws_iam_role.authenticated.arn
// Need to add data source for apps to Okta provider, oauth app only created in stage so hardcoding for now
value = okta_app_oauth.app.id
}
}
roles {
authenticated = aws_iam_role.authenticated.arn
}
}