Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • add_route_assignments
  • ask-user-to-delete-exercises-on-duplicates
  • bedran_exercise-list
  • jw_sonar
  • jw_sonar_backup
  • main
  • update-dependencies
  • v6.0.0
  • 2.0.0
  • 2.1.0
  • 2.2.0
  • 3.0.0
  • 3.0.1
  • 3.1.0
  • 3.1.1
  • 3.1.2
  • 3.1.3
  • 3.2.0
  • 3.3.0
  • 3.4.0
  • 3.4.1
  • 3.4.2
  • 3.5.0
  • 3.5.1
  • 3.5.2
  • 3.5.3
  • 4.0.0
  • 4.1.0
  • 5.0.0
  • 5.0.1
  • 6.0.0-dev
  • v1.0.1
32 results

Target

Select target project
  • Dojo_Project_Nguyen/backend/dojobackendapi
  • dojo_project/projects/backend/dojobackendapi
2 results
Select Git revision
Show changes
Commits on Source (2)
import { getReasonPhrase } from 'http-status-codes'; import { getReasonPhrase, StatusCodes } from 'http-status-codes';
import * as jwt from 'jsonwebtoken'; import * as jwt from 'jsonwebtoken';
import { JwtPayload } from 'jsonwebtoken'; import { JwtPayload } from 'jsonwebtoken';
import Config from '../config/Config'; import Config from '../config/Config';
...@@ -22,7 +22,7 @@ class Session { ...@@ -22,7 +22,7 @@ class Session {
constructor() { } constructor() { }
async initSession(req: express.Request) { async initSession(req: express.Request, res: express.Response) {
const authorization = req.headers.authorization; const authorization = req.headers.authorization;
if ( authorization ) { if ( authorization ) {
if ( authorization.startsWith('Bearer ') ) { if ( authorization.startsWith('Bearer ') ) {
...@@ -35,7 +35,9 @@ class Session { ...@@ -35,7 +35,9 @@ class Session {
this.profile = jwtData.profile; this.profile = jwtData.profile;
this.profile = await UserManager.getById(this.profile.id!) ?? this.profile; this.profile = await UserManager.getById(this.profile.id!) ?? this.profile;
} }
} catch ( err ) { } } catch ( err ) {
res.sendStatus(StatusCodes.UNAUTHORIZED).end();
}
} }
} }
} }
......
...@@ -18,22 +18,27 @@ class GitlabManager { ...@@ -18,22 +18,27 @@ class GitlabManager {
return `${ Config.gitlab.apiURL }${ route }`; return `${ Config.gitlab.apiURL }${ route }`;
} }
private async getGitlabUser(paramToSearch: string | number, paramName: string): Promise<GitlabUser | undefined> { public async getUserById(id: number): Promise<GitlabUser | undefined> {
try { try {
const params: any = {}; const params: any = {};
params[paramName] = paramToSearch; const user = (await axios.get<GitlabUser>(`${ this.getApiUrl(GitlabRoute.USERS_GET) }/${ String(id) }`, { params: params })).data;
return (await axios.get<Array<GitlabUser>>(this.getApiUrl(GitlabRoute.USERS_GET), { params: params })).data[0];
return user.id === id ? user : undefined;
} catch ( e ) { } } catch ( e ) { }
return undefined; return undefined;
} }
public async getUserById(id: number): Promise<GitlabUser | undefined> {
return await this.getGitlabUser(id, 'id');
}
public async getUserByUsername(username: string): Promise<GitlabUser | undefined> { public async getUserByUsername(username: string): Promise<GitlabUser | undefined> {
return await this.getGitlabUser(username, 'search'); try {
const params: any = {};
params['search'] = username;
const user = (await axios.get<Array<GitlabUser>>(this.getApiUrl(GitlabRoute.USERS_GET), { params: params })).data[0];
return user.username === username ? user : undefined;
} catch ( e ) { }
return undefined;
} }
async getRepository(idOrNamespace: string): Promise<GitlabRepository> { async getRepository(idOrNamespace: string): Promise<GitlabRepository> {
......
...@@ -6,7 +6,7 @@ class SessionMiddleware { ...@@ -6,7 +6,7 @@ class SessionMiddleware {
register(): (req: express.Request, res: express.Response, next: express.NextFunction) => void { register(): (req: express.Request, res: express.Response, next: express.NextFunction) => void {
return async (req: express.Request, res: express.Response, next: express.NextFunction) => { return async (req: express.Request, res: express.Response, next: express.NextFunction) => {
req.session = new Session(); req.session = new Session();
await req.session.initSession(req); await req.session.initSession(req, res);
return next(); return next();
}; };
......