Skip to content
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
19 changes: 13 additions & 6 deletions modules/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const { sendMail, magicLinkHTML, otpText } = require('./email.js')
const { updateUser, selectUser, getUserWL } = require('./db')
const { claimOTP, redeemOTP } = require('./auth-otp')
const { AuthorizationError, APIError, LOG_LEVEL_ERROR } = require('./errors')
const { PREFIX_APP_REVIEWER, PREFIX_DEV, PREFIX_MOBILE_SDK, PRODUCT_ATOM, PRODUCT_LOCUS } = require('../constants.js')
const { PREFIX_APP_REVIEWER, PREFIX_DEV, PREFIX_MOBILE_SDK, PRODUCT_ATOM, PRODUCT_LOCUS, PRODUCT_CLEARLAKE } = require('../constants.js')
const { capitalizeFirstLetter } = require('./utils')


const {
Expand Down Expand Up @@ -117,10 +118,16 @@ const loginUser = async ({ user, redirect, zone='utc', product = PRODUCT_ATOM, n
// get user WL info
const { rows = [] } = await getUserWL(user)

const DEFAULT_EMAIL = 'dev@eqworks.com'
const productSender = product === PRODUCT_CLEARLAKE
? (process.env.CLEARLAKE_SENDER || DEFAULT_EMAIL)
: DEFAULT_EMAIL
const supportEmail = product === PRODUCT_CLEARLAKE
? (process.env.CLEARLAKE_SUPPORT_EMAIL || DEFAULT_EMAIL)
: DEFAULT_EMAIL

// TODO: add logo in when email template has logo
let { sender, company } = rows[0] || {}
sender = sender || 'dev@eqworks.com'
company = company || 'EQ Works'
const { sender = productSender, company = 'EQ Works' } = rows[0] || {}

const { prefix: userPrefix, api_access } = await getUserInfo({ email: user })
// Check if user has access to the requested product
Expand Down Expand Up @@ -159,12 +166,12 @@ const loginUser = async ({ user, redirect, zone='utc', product = PRODUCT_ATOM, n
text: otpText({ otp, ttl, company, product }),
} : {
text: otpText({ link, otp, ttl, company, product }),
html: magicLinkHTML({ link, otp, ttl, company, product }),
html: magicLinkHTML({ link, otp, ttl, company, product, supportEmail }),
}
return sendMail({
from: sender,
to: user,
subject: `${product} (${company}) Login`,
subject: `${capitalizeFirstLetter(product)} (${company}) Login`,
...message,
})
}
Expand Down
13 changes: 7 additions & 6 deletions modules/email.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const nodemailer = require('nodemailer')
const AWS = require('@aws-sdk/client-ses')
const { capitalizeFirstLetter } = require('./utils')


module.exports.sendMail = async message => {
Expand Down Expand Up @@ -27,15 +28,15 @@ module.exports.sendMail = async message => {
return transport.sendMail(message)
}

module.exports.magicLinkHTML = ({ link, otp, ttl, company, product }) => `
module.exports.magicLinkHTML = ({ link, otp, ttl, company, product, supportEmail }) => `
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
<head>
<meta charset="utf-8"> <!-- utf-8 works for most cases -->
<meta name="viewport" content="width=device-width"> <!-- Forcing initial-scale shouldn't be necessary -->
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <!-- Use the latest (edge) version of IE rendering engine -->
<meta name="x-apple-disable-message-reformatting"> <!-- Disable auto-scale in iOS 10 Mail entirely -->
<title></title> <!-- The title tag shows in email notifications, like Android 4.4. -->
<title>${capitalizeFirstLetter(product)} (${company}) Login Magic Link</title> <!-- The title tag shows in email notifications, like Android 4.4. -->

<!-- Web Font / @font-face : BEGIN -->
<!-- NOTE: If web fonts are not required, lines 10 - 27 can be safely removed. -->
Expand Down Expand Up @@ -228,7 +229,7 @@ module.exports.magicLinkHTML = ({ link, otp, ttl, company, product }) => `

<!-- Visually Hidden Preheader Text : BEGIN -->
<div style="display: none; font-size: 1px; line-height: 1px; max-height: 0px; max-width: 0px; opacity: 0; overflow: hidden; mso-hide: all; font-family: sans-serif;">
${product.toUpperCase()} (${company}) Login Magic Link
${capitalizeFirstLetter(product)} (${company}) Login Magic Link
</div>
<!-- Visually Hidden Preheader Text : END -->

Expand Down Expand Up @@ -259,7 +260,7 @@ module.exports.magicLinkHTML = ({ link, otp, ttl, company, product }) => `
<table role="presentation" cellspacing="0" cellpadding="0" border="0" width="100%">
<tr>
<td style="padding: 10px; font-family: sans-serif; font-size: 15px; line-height: 20px; color: #555555; text-align: center;">
<h1 style="margin: 0 0 10px 0; font-family: sans-serif; font-size: 25px; line-height: 30px; color: #333333; font-weight: normal;">Welcome to ${product.toUpperCase()} (${company})</h1>
<h1 style="margin: 0 0 10px 0; font-family: sans-serif; font-size: 25px; line-height: 30px; color: #333333; font-weight: normal;">Welcome to ${capitalizeFirstLetter(product)} (${company})</h1>
</td>
</tr>
<tr>
Expand Down Expand Up @@ -298,7 +299,7 @@ module.exports.magicLinkHTML = ({ link, otp, ttl, company, product }) => `
<tr>
<td style="padding: 5px; font-family: sans-serif; font-size: 12px; line-height: 15px; text-align: center; color: #888888;">
<hr>
<p>Having an issue? <a style="color: #6BA4F8;" href="mailto:dev@eqworks.com?subject=${product.toUpperCase()} (${company}) Login issue">Contact Us</a></p>
<p>Having an issue? <a style="color: #6BA4F8;" href="mailto:${supportEmail}?subject=${capitalizeFirstLetter(product)} (${company}) Login issue">Contact Us</a></p>
</td>
</tr>
</table>
Expand All @@ -321,7 +322,7 @@ module.exports.magicLinkHTML = ({ link, otp, ttl, company, product }) => `
`

module.exports.otpText = ({ link, otp, ttl, company, product }) => `
Welcome to ${product.toUpperCase()} (${company})\n
Welcome to ${capitalizeFirstLetter(product)} (${company})\n
${link ? `Please login with the magic link ${link}\n` : ''}
${otp && ttl ? `Or manually enter: ${otp} \n
This will expire after ${ttl}, and all previous email should be discarded.` : ''}
Expand Down
13 changes: 13 additions & 0 deletions modules/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Capitalizes the first letter of a string and makes the rest lowercase
* @param {string} str - The string to capitalize
* @returns {string} The capitalized string
*/
function capitalizeFirstLetter(str) {
if (!str) return ''
return str[0].toUpperCase() + str.slice(1).toLowerCase()
}

module.exports = {
capitalizeFirstLetter,
}
2 changes: 2 additions & 0 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ provider:
OTP_TTL: ${env:OTP_TTL}
KEYWARDEN_VER: ${env:KEYWARDEN_VER}
STAGE: ${opt:stage, self:provider.stage}
CLEARLAKE_SENDER: ${env:CLEARLAKE_SENDER}
CLEARLAKE_SUPPORT_EMAIL: ${env:CLEARLAKE_SUPPORT_EMAIL}
REDIS_URI:
# cloudformation functions to form redis://<host>:<port>
"Fn::Join": [
Expand Down