-
Notifications
You must be signed in to change notification settings - Fork 2
Experimental frontend and Cognito user domain #21
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
Draft
eoinsha
wants to merge
16
commits into
main
Choose a base branch
from
user-pool-domain
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
400105a
Bump CLI version to 1.1.0
66b5870
Add draft domain config - WIP
347d200
feat(auth) add cognito custom domain
eoinsha 426ce60
feat(auth): add invitation email using Mailgun
eoinsha f3e075f
feat: add basic login and invitation example
eoinsha 82dc74b
chore: user next eslintrc config
eoinsha b35e94c
feat: add shadcn/ui Login and Invitation components
eoinsha a6029d0
feat: add password reset flow to UI
eoinsha d177fa5
chore: add frontend deployment to CloudFront
eoinsha 166cd77
feat!: switch API to use api. subdomain
eoinsha c23b966
chore: style hosted UI similar to frontend
eoinsha 1757cf5
chore: add UI routes for device verifiction
eoinsha 1784c48
chore: fix up navigation
eoinsha 0999454
fix: don't cache any HTML files on CloudFront
eoinsha acd17a8
fix: prevent infinite re-rendering in auth provider
eoinsha 087e0fd
chore: use default next config
eoinsha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import Mailgun from 'mailgun.js' | ||
| import formData from 'form-data' | ||
| import middy from '@middy/core' | ||
| import middySsm from '@middy/ssm' | ||
| import encryptionSdk from '@aws-crypto/client-node' | ||
| const { BASE_URL, STAGE, KEY_ALIAS, KEY_ARN } = process.env | ||
| const EMAIL_DOMAIN = 'sandbox4e5d8a28162548389a95edc71719a3a4.mailgun.org' | ||
|
|
||
| const { decrypt } = encryptionSdk.buildClient(encryptionSdk.CommitmentPolicy.FORBID_ENCRYPT_ALLOW_DECRYPT) | ||
| const keyring = new encryptionSdk.KmsKeyringNode({ generatorKeyId: KEY_ARN, keyIds: [KEY_ALIAS] }) | ||
|
|
||
| const globals = {} | ||
|
|
||
| /** | ||
| * Using Mailgun, send an invitation email when the event indicates an Invitation is being created | ||
| * using a Cognito password reset flow | ||
| * | ||
| * @param {*} event | ||
| */ | ||
| export async function cognitoCustomEmailSenderHandler (event) { | ||
| console.log(event) | ||
| const { request } = event | ||
| if (event.triggerSource === 'CustomEmailSender_ForgotPassword') { | ||
| const { email, email_verified: emailVerified } = request.userAttributes | ||
| if (emailVerified !== 'true') { | ||
| throw new Error('Email is not verified - unexpected ForgotPassword request: ' + JSON.stringify(request.userAttributes)) | ||
| } else { | ||
| const encryptedCode = Buffer.from(request.code, 'base64') | ||
| const { plaintext: code } = await decrypt(keyring, encryptedCode) | ||
| const isInvitation = request?.clientMetadata?.InitationFlow === 'true' | ||
| const url = new URL(`${BASE_URL}/${isInvitation ? 'invitation' : 'confirm-reset'}`) | ||
| url.searchParams.set('email', email) | ||
| url.searchParams.set('code', code) | ||
|
|
||
| let htmlMessage | ||
| if (isInvitation) { | ||
| htmlMessage = ` | ||
| <h1>You are invited 🤩</h1> | ||
|
|
||
| You have been invited to join Weshare. | ||
|
|
||
| <a href="${url}">Click here to accept the invitation</a> | ||
| ` | ||
| } else { | ||
| htmlMessage = ` | ||
| <h1>You forgot your password 🤪</h1> | ||
|
|
||
| <a href="${url}">Click here to reset your password</a> | ||
| ` | ||
| } | ||
| await sendEmail(email, htmlMessage) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async function sendEmail (email, htmlMessage) { | ||
| const data = { | ||
| from: `Mailgun Sandbox <postmaster@${EMAIL_DOMAIN}>`, | ||
| to: [email], | ||
| subject: 'Hello', | ||
| html: htmlMessage | ||
| } | ||
| const response = await globals.mailgun.messages.create(EMAIL_DOMAIN, data) | ||
| console.log(response) | ||
| } | ||
|
|
||
| export const handler = middy() | ||
| .use( | ||
| middySsm({ | ||
| fetchData: { | ||
| mailgunApiKey: `/weshare/${STAGE}/mailgunApiKey` | ||
| }, | ||
| setToContext: true | ||
| }) | ||
| ) | ||
| .before((request) => { | ||
| const { mailgunApiKey } = request.context | ||
| const mailgun = new Mailgun(formData).client({ username: 'api', key: mailgunApiKey }) | ||
| globals.mailgun = mailgun | ||
| }) | ||
| .handler(cognitoCustomEmailSenderHandler) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,8 @@ | ||
| export const TOKEN_REQUEST_INTERVAL_SECONDS = 2 | ||
| export const CODE_EXPIRY_SECONDS = 5 * 60 | ||
| export const { AWS_REGION, BASE_URL, TABLE_NAME, USER_POOL_DOMAIN, CLIENT_ID } = process.env | ||
| export const VERIFICATION_URI = `${BASE_URL}/auth/verify` | ||
| export const REDIRECT_URI = `${BASE_URL}/auth/callback` | ||
| export const COGNITO_OAUTH_BASE_URI = `https://${USER_POOL_DOMAIN}.auth.${AWS_REGION}.amazoncognito.com/oauth2` | ||
| export const { AWS_REGION, BASE_URL, API_BASE_URL, TABLE_NAME, USER_POOL_DOMAIN, CLIENT_ID } = process.env | ||
| export const VERIFICATION_URI = `${BASE_URL}/verify` // This is the front end page displayed for confirmation before beginning the format device code flow | ||
| export const REDIRECT_URI = `${API_BASE_URL}/auth/callback` | ||
| export const COGNITO_OAUTH_BASE_URI = `https://${USER_POOL_DOMAIN}/oauth2` | ||
| export const COGNITO_OAUTH_CODE_URI = `${COGNITO_OAUTH_BASE_URI}/authorize` | ||
| export const COGNITO_OAUTH_TOKEN_URI = `${COGNITO_OAUTH_BASE_URI}/token` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| { | ||
| "version": "1", | ||
| "triggerSource": "CustomEmailSender_ForgotPassword", | ||
| "region": "eu-west-1", | ||
| "userPoolId": "eu-west-1_Jn1Hv0kUL", | ||
| "userName": "5c20be82-1b91-4e0c-837a-319545fa4940", | ||
| "callerContext": { | ||
| "awsSdkVersion": "aws-sdk-js-3.515.0", | ||
| "clientId": null | ||
| }, | ||
| "request": { | ||
| "type": "customEmailSenderRequestV1", | ||
| "code": "AYA..................afEQc86+g=", | ||
| "clientMetadata": { | ||
| "InitationFlow": "true" | ||
| }, | ||
| "userAttributes": { | ||
| "sub": "5c20be82-1b91-4e0c-837a-319545fa4940", | ||
| "cognito:user_status": "CONFIRMED", | ||
| "email_verified": "true", | ||
| "email": "eoin.shanaghy@gmail.com" | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will change this to a proper
from:domain when I set up Mailgun with this!