-
Notifications
You must be signed in to change notification settings - Fork 51
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
feat(smtp): add smtp mail server and api-key check #45
Open
truongduy2611
wants to merge
2
commits into
TypingMind:main
Choose a base branch
from
truongduy2611:feature/smtp-email
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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,5 @@ | ||
import { z } from 'zod'; | ||
|
||
export const apiKeyHeader = z.object({ | ||
'x-api-key': z.string(), | ||
}); |
This file contains 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 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,23 @@ | ||
import { NextFunction, Request, Response } from 'express'; | ||
import { StatusCodes } from 'http-status-codes'; | ||
|
||
import { env } from '@/common/utils/envConfig'; | ||
|
||
import { ResponseStatus, ServiceResponse } from '../models/serviceResponse'; | ||
import { handleServiceResponse } from '../utils/httpHandlers'; | ||
|
||
export const apiKeyHandler = (req: Request, res: Response, next: NextFunction) => { | ||
const apiKeyHeader = req.headers['x-api-key']; | ||
if (!apiKeyHeader || apiKeyHeader !== env.API_KEY) { | ||
return handleServiceResponse( | ||
new ServiceResponse( | ||
ResponseStatus.Failed, | ||
'Please input your x-api-key in the header!', | ||
null, | ||
StatusCodes.UNAUTHORIZED | ||
), | ||
res | ||
); | ||
} | ||
next(); | ||
}; |
This file contains 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 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 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 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 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,58 @@ | ||
import { extendZodWithOpenApi } from '@asteasolutions/zod-to-openapi'; | ||
import { z } from 'zod'; | ||
|
||
extendZodWithOpenApi(z); | ||
|
||
export type SmtpRequest = z.infer<typeof SmtpRequestSchema>; | ||
|
||
export const SmtpRequestSchema = z | ||
.object({ | ||
host: z.string().openapi({ | ||
description: 'SMPT email host', | ||
example: 'smtp.example.com', | ||
}), | ||
port: z.number().openapi({ | ||
description: 'SMPT port', | ||
example: 465, | ||
}), | ||
secure: z.boolean().optional().default(false).openapi({ | ||
description: 'Use `true` for port 465, `false` for all other ports', | ||
}), | ||
auth: z.object({ | ||
username: z.string(), | ||
password: z.string(), | ||
}), | ||
mailContent: z.object({ | ||
from: z.string().openapi({ | ||
example: '[email protected], [email protected]', | ||
description: `The e-mail address of the sender. All e-mail addresses can be plain '[email protected]' or formatted 'Sender Name <[email protected]>'`, | ||
}), | ||
to: z.string().openapi({ | ||
example: '[email protected], [email protected]', | ||
description: 'Comma separated list of recipients e-mail addresses that will appear on the To: field', | ||
}), | ||
subject: z.string(), | ||
text: z.string().optional(), | ||
html: z.string().optional(), | ||
cc: z.string().optional().openapi({ | ||
example: '[email protected], [email protected]', | ||
description: 'Comma separated list of recipients e-mail addresses that will appear on the Cc: field', | ||
}), | ||
bcc: z.string().optional().openapi({ | ||
example: '[email protected], [email protected]', | ||
description: 'Comma separated list recipients e-mail addresses that will appear on the Bcc: field', | ||
}), | ||
}), | ||
}) | ||
.openapi({ | ||
description: 'Send SMPT Email Request', | ||
}); | ||
|
||
export type SmtpResponse = z.infer<typeof SmtpResponseSchema>; | ||
|
||
export const SmtpResponseSchema = z | ||
.object({ | ||
messageId: z.string().optional(), | ||
response: z.string().optional(), | ||
}) | ||
.optional(); |
This file contains 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,87 @@ | ||
import { OpenAPIRegistry } from '@asteasolutions/zod-to-openapi'; | ||
import express, { Request, Response, Router } from 'express'; | ||
import { StatusCodes } from 'http-status-codes'; | ||
import nodemailer from 'nodemailer'; | ||
|
||
import { apiKeyHeader } from '@/api-docs/openAPIHeaderBuilders'; | ||
import { createApiResponse } from '@/api-docs/openAPIResponseBuilders'; | ||
import { apiKeyHandler } from '@/common/middleware/apiKeyHandler'; | ||
import { ResponseStatus, ServiceResponse } from '@/common/models/serviceResponse'; | ||
import { handleServiceResponse, validateRequestBody } from '@/common/utils/httpHandlers'; | ||
|
||
import { SmtpRequest, SmtpRequestSchema, SmtpResponseSchema } from './smtpMailModel'; | ||
|
||
export const smtpMailRegistry = new OpenAPIRegistry(); | ||
smtpMailRegistry.register('SmtpRequest', SmtpRequestSchema); | ||
smtpMailRegistry.register('SmtpResponse', SmtpResponseSchema); | ||
|
||
smtpMailRegistry.registerPath({ | ||
method: 'post', | ||
path: '/send-smtp-mail', | ||
tags: ['SMTP Mail'], | ||
request: { | ||
headers: [apiKeyHeader], | ||
body: { | ||
content: { | ||
'application/json': { | ||
schema: SmtpRequestSchema, | ||
}, | ||
}, | ||
}, | ||
}, | ||
responses: { | ||
...createApiResponse(SmtpResponseSchema, 'OK'), | ||
...createApiResponse(SmtpResponseSchema, 'Please input your x-api-key', StatusCodes.UNAUTHORIZED), | ||
}, | ||
}); | ||
|
||
export const smtpMailRouter: Router = (() => { | ||
const router = express.Router(); | ||
|
||
router.use(apiKeyHandler); | ||
|
||
router.post('/', validateRequestBody(SmtpRequestSchema), async (_req: Request, res: Response) => { | ||
const { auth, host, port, secure, mailContent }: SmtpRequest = _req.body; | ||
try { | ||
const transporter = nodemailer.createTransport({ | ||
auth: { | ||
user: auth.username, | ||
pass: auth.password, | ||
}, | ||
port: port, | ||
host: host, | ||
secure: secure, | ||
}); | ||
|
||
const mailOptions = { | ||
from: mailContent.from, | ||
to: mailContent.to, | ||
subject: mailContent.subject || '', | ||
text: mailContent.text || '', | ||
html: mailContent.html || '', | ||
cc: mailContent.cc, | ||
bcc: mailContent.bcc, | ||
}; | ||
|
||
const info = await transporter.sendMail(mailOptions); | ||
const serviceResponse = new ServiceResponse( | ||
ResponseStatus.Success, | ||
'Your mail is sent!', | ||
{ | ||
messageId: info.messageId, | ||
response: info.response, | ||
}, | ||
StatusCodes.OK | ||
); | ||
handleServiceResponse(serviceResponse, res); | ||
} catch (error) { | ||
console.log(`${(error as Error).stack}`); | ||
const errorMessage = `Error sending your email: ${(error as Error).message}`; | ||
handleServiceResponse( | ||
new ServiceResponse(ResponseStatus.Failed, errorMessage, null, StatusCodes.INTERNAL_SERVER_ERROR), | ||
res | ||
); | ||
} | ||
}); | ||
return router; | ||
})(); |
This file contains 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 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.
Check failure
Code scanning / CodeQL
Client-side cross-site scripting High