Skip to content

Commit

Permalink
feat(emails): ajoute un CRON pour vérifier les emails des laboratoires (
Browse files Browse the repository at this point in the history
  • Loading branch information
vmaubert authored Dec 18, 2024
1 parent f2fbd63 commit 81cb488
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 19 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ S3_BUCKET=maestro
MAILER_HOST=localhost
MAILER_PORT=1025
MAILER_USER=
MAILER_PASSWORD=
MAILER_PASSWORD=

M2M_BASIC_TOKEN=goodtoken
3 changes: 3 additions & 0 deletions .github/workflows/github-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ jobs:
S3_REGION: region
S3_ACCESS_KEY_ID: key
S3_SECRET_ACCESS_KEY: secret
M2M_BASIC_TOKEN: fakeBasicToken
- name: Frontend linter
run: npm --prefix frontend run lint
- name: Server linter
Expand All @@ -78,8 +79,10 @@ jobs:
# env:
# AUTH_SECRET: abcd1234
# DATABASE_URL: postgres://postgres:postgres@localhost:5432/maestro
# M2M_BASIC_TOKEN: fakeBasicToken
- name: Server tests
run: npm run test
env:
AUTH_SECRET: abcd1234
DATABASE_URL: postgres://postgres:postgres@localhost:5432/maestro
M2M_BASIC_TOKEN: fakeBasicToken
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ S3_ACCESS_KEY_ID
S3_SECRET_ACCESS_KEY
S3_BUCKET
S3_FILE_PATH
M2M_BASIC_TOKEN
REACT_APP_PUBLIC_URL
REACT_APP_API_URL
Expand Down
8 changes: 8 additions & 0 deletions cron.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"jobs": [
{
"command": "*/10 * * * * curl ${REACT_APP_API_URL}/api/m2m/checkLaboratoryEmails -H \"authorization: ${M2M_BASIC_TOKEN}\"",
"size": "M"
}
]
}
14 changes: 14 additions & 0 deletions server/controllers/checkLaboratoryEmailsController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { constants } from 'http2';
import {Request, Response} from 'express';

export const checkLaboratoryEmails = async (_request: Request, response: Response): Promise<void> => {


console.info('Cheking emails...');


//TODO


response.status(constants.HTTP_STATUS_OK).send('OK');
};
26 changes: 25 additions & 1 deletion server/middlewares/checks/authCheck.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NextFunction, Request, Response } from 'express';
import express, { NextFunction, Request, Response } from 'express';
import { expressjwt } from 'express-jwt';

import _ from 'lodash';
Expand All @@ -11,6 +11,7 @@ import { UserPermission } from '../../../shared/schema/User/UserPermission';
import { UserRole } from '../../../shared/schema/User/UserRole';
import userRepository from '../../repositories/userRepository';
import config from '../../utils/config';
import { constants } from 'http2';

export const jwtCheck = (credentialsRequired: boolean) =>
expressjwt({
Expand Down Expand Up @@ -69,3 +70,26 @@ export const permissionsCheck = (permissions: UserPermission[]) =>

next();
};

export const basicAuthCheck =
async (req: Request, res: express.Response, next: express.NextFunction) => {
try {
const token = req.headers.authorization
if (token !== config.m2mBasicToken) {
res.status(constants.HTTP_STATUS_UNAUTHORIZED);
res.send('Authentication Required');

return;
}

} catch (e) {
console.error(e);
res.status(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR);
res.send('Internal error');

return;
}


next()
};
34 changes: 34 additions & 0 deletions server/middlewares/test/authCheck.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import express from 'express';
import { constants } from 'http2';
import request from 'supertest';
import { describe, test } from 'vitest';
import config from '../../utils/config';
import { basicAuthCheck } from '../checks/authCheck';

describe('basicAuth', () => {
const app = express();

const myM2MRoute = '/basic-auth-route';
app.use(myM2MRoute, basicAuthCheck, (_request, response) =>
response.sendStatus(constants.HTTP_STATUS_OK)
);

test('should respond with the status 200 with good basic token', async () => {
await request(app)
.get(myM2MRoute)
.set('authorization', config.m2mBasicToken)
.expect(constants.HTTP_STATUS_OK);
});

test('should respond with the status 401 with wrong basic token', async () => {
await request(app)
.get(myM2MRoute)
.set('authorization', 'pas bon token')
.expect(constants.HTTP_STATUS_UNAUTHORIZED);
});
test('should respond with the status 401 with no basic token', async () => {
await request(app)
.get(myM2MRoute)
.expect(constants.HTTP_STATUS_UNAUTHORIZED);
});
});
8 changes: 8 additions & 0 deletions server/routers/m2mProtected.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import express from 'express';
import { checkLaboratoryEmails } from '../controllers/checkLaboratoryEmailsController';
import { basicAuthCheck } from '../middlewares/checks/authCheck';

export const m2mProtectedRouter = express.Router();

m2mProtectedRouter.use(basicAuthCheck);
m2mProtectedRouter.use('/checkLaboratoryEmails', checkLaboratoryEmails)
31 changes: 15 additions & 16 deletions server/routers/protected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,25 @@ import sampleRouter from './sample.router';
import substanceRouter from './substance.router';
import userRouter from './user.router';

const router = express.Router();
export const protectedRouter = express.Router();

router.use(jwtCheck(true));
router.use(userCheck(true));
protectedRouter.use(jwtCheck(true));
protectedRouter.use(userCheck(true));

router.use('/analysis', analysisRouter);
router.use('/addresses', addressRouter);
router.use('/companies', companyRouter);
router.use('/documents', documentRouter);
router.use('/laboratories', laboratoryRouter);
router.use('/prescriptions', prescriptionRouter);
router.use('/prescriptions', regionalPrescriptionRouter);
router.use('/programming-plans', programmingPlanRouter);
router.use('/samples', sampleRouter);
router.use('/substances', substanceRouter);
router.use('/users', userRouter);
protectedRouter.use('/analysis', analysisRouter);
protectedRouter.use('/addresses', addressRouter);
protectedRouter.use('/companies', companyRouter);
protectedRouter.use('/documents', documentRouter);
protectedRouter.use('/laboratories', laboratoryRouter);
protectedRouter.use('/prescriptions', prescriptionRouter);
protectedRouter.use('/prescriptions', regionalPrescriptionRouter);
protectedRouter.use('/programming-plans', programmingPlanRouter);
protectedRouter.use('/samples', sampleRouter);
protectedRouter.use('/substances', substanceRouter);
protectedRouter.use('/users', userRouter);

router.get('/regions.geojson', (req, res) => {
protectedRouter.get('/regions.geojson', (req, res) => {
res.setHeader('Content-Type', 'application/json');
fs.createReadStream(__dirname + '/../data/regions.json').pipe(res);
});

export default router;
4 changes: 3 additions & 1 deletion server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import helmet from 'helmet';
import path from 'path';
import RouteNotFoundError from './errors/routeNotFoundError';
import errorHandler from './middlewares/error-handler';
import protectedRouter from './routers/protected';
import { protectedRouter } from './routers/protected';
import unprotectedRouter from './routers/unprotected';
import config from './utils/config';
import sentry from './utils/sentry';
import { m2mProtectedRouter } from './routers/m2mProtected';

const PORT = config.serverPort;

Expand Down Expand Up @@ -80,6 +81,7 @@ export function createServer(): Server {
app.use(rateLimiter);
app.set('trust proxy', 1);

app.use('/api/m2m', m2mProtectedRouter);
app.use('/api', unprotectedRouter);
app.use('/api', protectedRouter);

Expand Down
7 changes: 7 additions & 0 deletions server/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ interface Config {
url: string;
};
};
m2mBasicToken: string
}

const config = convict<Config>({
Expand Down Expand Up @@ -282,6 +283,12 @@ const config = convict<Config>({
default: 'https://recherche-entreprises.api.gouv.fr'
}
}
},
m2mBasicToken: {
env: 'M2M_BASIC_TOKEN',
format: String,
sensitive: true,
default: null
}
})
.validate({ allowed: 'strict' })
Expand Down

0 comments on commit 81cb488

Please sign in to comment.