Skip to content
Snippets Groups Projects
Commit e907ab8c authored by michael.minelli's avatar michael.minelli
Browse files

Add Exercice authentification by secret

parent f963b8c4
No related branches found
No related tags found
No related merge requests found
...@@ -5,7 +5,7 @@ import Config from '../config/Config'; ...@@ -5,7 +5,7 @@ import Config from '../config/Config';
import express from 'express'; import express from 'express';
import ApiRequest from '../types/ApiRequest'; import ApiRequest from '../types/ApiRequest';
import UserManager from '../managers/UserManager'; import UserManager from '../managers/UserManager';
import DojoResponse from '../shared/types/DojoResponse'; import DojoResponse from '../shared/types/Dojo/DojoResponse';
import { User } from '../types/DatabaseTypes'; import { User } from '../types/DatabaseTypes';
...@@ -26,6 +26,7 @@ class Session { ...@@ -26,6 +26,7 @@ class Session {
async initSession(req: ApiRequest) { async initSession(req: ApiRequest) {
const authorization = req.headers.authorization; const authorization = req.headers.authorization;
if ( authorization ) { if ( authorization ) {
if ( authorization.startsWith('Bearer ') ) {
const jwtToken = authorization.replace('Bearer ', ''); const jwtToken = authorization.replace('Bearer ', '');
try { try {
...@@ -38,9 +39,10 @@ class Session { ...@@ -38,9 +39,10 @@ class Session {
} catch ( err ) { } } catch ( err ) { }
} }
} }
}
private static getToken(profileJson: any): string { private static getToken(profileJson: any): string {
return profileJson.id === null ? null : jwt.sign({ profile: profileJson }, Config.jwtConfig.secret, Config.jwtConfig.expiresIn > 0 ? { expiresIn: Config.jwtConfig.expiresIn } : {}); return profileJson === null ? null : jwt.sign({ profile: profileJson }, Config.jwtConfig.secret, Config.jwtConfig.expiresIn > 0 ? { expiresIn: Config.jwtConfig.expiresIn } : {});
} }
private async getResponse<T>(code: number, data: T, descriptionOverride?: string): Promise<DojoResponse<T>> { private async getResponse<T>(code: number, data: T, descriptionOverride?: string): Promise<DojoResponse<T>> {
......
import { Prisma } from '@prisma/client';
import { Enonce } from '../types/DatabaseTypes';
import db from '../helpers/DatabaseHelper';
class ExerciceManager {
get(id: string, include: Prisma.ExerciceInclude | undefined = undefined): Promise<Enonce | undefined> {
return db.exercice.findUnique({
where : {
id: id
},
include: include
});
}
}
export default new ExerciceManager();
...@@ -3,6 +3,7 @@ import ApiRequest from '../types/ApiRequest'; ...@@ -3,6 +3,7 @@ import ApiRequest from '../types/ApiRequest';
import express from 'express'; import express from 'express';
import { StatusCodes } from 'http-status-codes'; import { StatusCodes } from 'http-status-codes';
import EnonceManager from '../managers/EnonceManager'; import EnonceManager from '../managers/EnonceManager';
import ExerciceManager from '../managers/ExerciceManager';
class ParamsCallbackManager { class ParamsCallbackManager {
...@@ -24,7 +25,8 @@ class ParamsCallbackManager { ...@@ -24,7 +25,8 @@ class ParamsCallbackManager {
initBoundParams(req: ApiRequest) { initBoundParams(req: ApiRequest) {
if ( !req.boundParams ) { if ( !req.boundParams ) {
req.boundParams = { req.boundParams = {
enonce: null enonce : null,
exercice: null
}; };
} }
} }
...@@ -34,6 +36,12 @@ class ParamsCallbackManager { ...@@ -34,6 +36,12 @@ class ParamsCallbackManager {
exercices: true, exercices: true,
staff : true staff : true
} ], 'enonce'); } ], 'enonce');
this.listenParam('exerciceId', backend, ExerciceManager.get.bind(ExerciceManager), [ {
enonce : true,
members: true,
results: true
} ], 'exercice');
} }
} }
......
...@@ -11,7 +11,7 @@ class SecurityMiddleware { ...@@ -11,7 +11,7 @@ class SecurityMiddleware {
check(checkIfConnected: boolean, ...checkTypes: Array<SecurityCheckType>): (req: ApiRequest, res: express.Response, next: express.NextFunction) => void { check(checkIfConnected: boolean, ...checkTypes: Array<SecurityCheckType>): (req: ApiRequest, res: express.Response, next: express.NextFunction) => void {
return async (req: ApiRequest, res: express.Response, next: express.NextFunction) => { return async (req: ApiRequest, res: express.Response, next: express.NextFunction) => {
if ( checkIfConnected ) { if ( checkIfConnected ) {
if ( req.session.profile.id === null ) { if ( req.session.profile === null ) {
return req.session.sendResponse(res, StatusCodes.UNAUTHORIZED); return req.session.sendResponse(res, StatusCodes.UNAUTHORIZED);
} }
} }
...@@ -19,9 +19,9 @@ class SecurityMiddleware { ...@@ -19,9 +19,9 @@ class SecurityMiddleware {
let isAllowed = checkTypes.length === 0; let isAllowed = checkTypes.length === 0;
if ( !isAllowed ) { if ( !isAllowed ) {
for ( let checkType of checkTypes ) { for ( const checkType of checkTypes ) {
try { try {
switch ( checkType ) { switch ( String(checkType) ) {
case SecurityCheckType.TEACHING_STAFF: case SecurityCheckType.TEACHING_STAFF:
isAllowed = isAllowed || req.session.profile.isTeachingStaff; isAllowed = isAllowed || req.session.profile.isTeachingStaff;
break; break;
...@@ -31,8 +31,10 @@ class SecurityMiddleware { ...@@ -31,8 +31,10 @@ class SecurityMiddleware {
case SecurityCheckType.ENONCE_IS_PUBLISHED: case SecurityCheckType.ENONCE_IS_PUBLISHED:
isAllowed = isAllowed || req.boundParams.enonce.published; isAllowed = isAllowed || req.boundParams.enonce.published;
break; break;
case SecurityCheckType.EXERCICE_SECRET:
isAllowed = isAllowed || (req.headers.authorization && req.headers.authorization && req.headers.authorization.replace('ExerciceSecret ', '') === req.boundParams.exercice.secret);
break;
default: default:
isAllowed = isAllowed || false;
break; break;
} }
} catch ( e ) { } catch ( e ) {
......
import express from 'express'; import express from 'express';
import Session from '../controllers/Session'; import Session from '../controllers/Session';
import { Enonce } from './DatabaseTypes'; import { Enonce, Exercice } from './DatabaseTypes';
type ApiRequest = express.Request & { type ApiRequest = express.Request & {
session: Session, boundParams: { session: Session, boundParams: {
enonce: Enonce enonce: Enonce, exercice: Exercice
} }
} }
......
...@@ -2,6 +2,7 @@ enum SecurityCheckType { ...@@ -2,6 +2,7 @@ enum SecurityCheckType {
TEACHING_STAFF = 'teachingStaff', TEACHING_STAFF = 'teachingStaff',
ENONCE_STAFF = 'enonceStaff', ENONCE_STAFF = 'enonceStaff',
ENONCE_IS_PUBLISHED = 'enonceIsPublished', ENONCE_IS_PUBLISHED = 'enonceIsPublished',
EXERCICE_SECRET = 'exerciceSecret',
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment