Skip to content
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

#2697 captcha to feedback from submission #2792

Merged
merged 18 commits into from
Sep 1, 2022
Merged
28 changes: 25 additions & 3 deletions verification/curator-service/api/openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -957,19 +957,19 @@ paths:
description: The northing coordinate (UTMY)
required: true
schema:
type: number
type: number
- name: e
in: query
description: The easting coordinate (UTMX)
required: true
schema:
type: number
type: number
- name: z
in: query
required: true
description: The UTM zone.
schema:
type: number
type: number
responses:
'200':
$ref: '#/components/responses/200Position'
Expand Down Expand Up @@ -1121,6 +1121,28 @@ paths:
$ref: '#/components/responses/422'
'500':
$ref: '#/components/responses/500'
/feedback:
post:
summary: Sends feedback email to global.health
tags: [User]
operationId: feeback
maciej-zarzeczny marked this conversation as resolved.
Show resolved Hide resolved
requestBody:
description: Email and message of User
required: true
content:
application/json:
schema:
description: Message send to global.health sa feedback
maciej-zarzeczny marked this conversation as resolved.
Show resolved Hide resolved
type: object
properties:
message:
type: string
responses:
'200':
$ref: '#/components/responses/200'
'500':
$ref: '#/components/responses/500'

components:
schemas:
Parser:
Expand Down
49 changes: 40 additions & 9 deletions verification/curator-service/api/src/controllers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import * as crypto from 'crypto';
import EmailClient from '../clients/email-client';
import { ObjectId } from 'mongodb';
import { baseURL, welcomeEmail } from '../util/instance-details';
import { validateRecaptchaToken } from '../util/validate-recaptcha-token';

// Global variable for newsletter acceptance
let isNewsletterAccepted: boolean;
Expand Down Expand Up @@ -192,7 +193,21 @@ export class AuthController {

this.router.post(
'/signup',
(req: Request, res: Response, next: NextFunction): void => {
async (
req: Request,
res: Response,
next: NextFunction,
): Promise<void> => {
const captchaResult = await validateRecaptchaToken(
req.body.token,
);

if (!captchaResult)
res.status(403).json({
message:
"Unfortunately, you didn't pass the captcha. Please, try again later.",
});

passport.authenticate(
'register',
(error: Error, user: IUser, info: any) => {
Expand All @@ -214,7 +229,21 @@ export class AuthController {

this.router.post(
'/signin',
(req: Request, res: Response, next: NextFunction): void => {
async (
req: Request,
res: Response,
next: NextFunction,
): Promise<void> => {
const captchaResult = await validateRecaptchaToken(
req.body.token,
);

if (!captchaResult)
res.status(403).json({
message:
"Unfortunately, you didn't pass the captcha. Please, try again later.",
});

passport.authenticate(
'login',
(error: Error, user: IUser, info: any) => {
Expand Down Expand Up @@ -660,12 +689,13 @@ export class AuthController {
},
async (req, email, password, done) => {
try {
const userPromise = await users().find({ email })
.collation({ locale: 'en_US', strength: 2 })
.toArray();
const userPromise = await users()
.find({ email })
.collation({ locale: 'en_US', strength: 2 })
.toArray();

const user = userPromise[0];

if (user) {
return done(null, false, {
message: 'Email address already exists',
Expand Down Expand Up @@ -712,9 +742,10 @@ export class AuthController {
},
async (email, password, done) => {
try {
const userPromise = await users().find({ email })
.collation({ locale: 'en_US', strength: 2 })
.toArray();
const userPromise = await users()
.find({ email })
.collation({ locale: 'en_US', strength: 2 })
.toArray();

const user = userPromise[0] as IUser;

Expand Down
23 changes: 23 additions & 0 deletions verification/curator-service/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,28 @@ async function makeApp() {

app.use('/api', apiRouter);

//Send feedback from user to global.health email
app.post(
'/feedback',
mustBeAuthenticated,
async (req: Request, res: Response) => {
const { message } = req.body;

try {
emailClient.send(
maciej-zarzeczny marked this conversation as resolved.
Show resolved Hide resolved
[env.EMAIL_USER_ADDRESS],
'Feedback regarding Covid-19 curator portal',
message,
);
res.status(200).send({ message: 'Email sent successfully' });
} catch (err) {
const error = err as Error;
logger.error(error);
return res.sendStatus(500);
}
},
);

// Basic health check handler.
app.get('/health', async (req: Request, res: Response) => {
try {
Expand Down Expand Up @@ -483,6 +505,7 @@ async function makeApp() {
format: winston.format.json(),
}),
);

return app;
}

Expand Down
8 changes: 8 additions & 0 deletions verification/curator-service/api/src/util/validate-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,13 @@ export default function validateEnv(): Readonly<{
desc: 'Name of the disease that should be displayed in Curator UI',
devDefault: 'COVID-19',
}),
RECAPTCHA_SITE_KEY: str({
desc: 'Key for recaptcha component',
devDefault: '',
}),
RECAPTCHA_SECRET_KEY: str({
desc: 'Key to validate recaptcha request',
devDefault: '',
}),
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import axios from 'axios';
import validateEnv from './validate-env';

export const validateRecaptchaToken = async (
token: string,
): Promise<boolean> => {
const env = validateEnv();
const response = await axios.post(
`https://www.google.com/recaptcha/api/siteverify?secret=${env.RECAPTCHA_SECRET_KEY}&response=${token}`,
);

return response.data.success;
};
1 change: 1 addition & 0 deletions verification/curator-service/ui/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Env files
.env
.env.staging
.env.development

# Don't include CSVs, except for Cypress fixtures.
*.csv
Expand Down
62 changes: 62 additions & 0 deletions verification/curator-service/ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions verification/curator-service/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"react-dom": "^16.13.1",
"react-draggable": "^4.4.4",
"react-google-button": "^0.7.2",
"react-google-recaptcha": "^2.1.0",
"react-gtm-module": "^2.0.11",
"react-helmet": "^6.1.0",
"react-highlight-words": "^0.17.0",
Expand Down Expand Up @@ -88,6 +89,7 @@
"@types/papaparse": "^5.3.0",
"@types/react": "^16.14.8",
"@types/react-dom": "^16.9.13",
"@types/react-google-recaptcha": "^2.1.5",
"@types/react-gtm-module": "^2.0.1",
"@types/react-helmet": "^6.1.4",
"@types/react-highlight-words": "^0.16.3",
Expand Down
Loading