Skip to content
Snippets Groups Projects
Commit 457da998 authored by Steve Reis's avatar Steve Reis
Browse files

Auth module done

parent 9d045618
No related branches found
No related tags found
No related merge requests found
...@@ -17,10 +17,10 @@ export class AuthService { ...@@ -17,10 +17,10 @@ export class AuthService {
return await this.engineService.login?.(username, password); return await this.engineService.login?.(username, password);
} }
login(user: User): Pick<AuthenticationOutput, 'accessToken'> { async login(user: User): Promise<Pick<AuthenticationOutput, 'accessToken'>> {
const payload = { username: user.username, sub: user }; const payload = { username: user.username, sub: user };
return { return Promise.resolve({
accessToken: this.jwtService.sign(payload), accessToken: this.jwtService.sign(payload),
}; });
} }
} }
import { HttpService } from '@nestjs/axios'; import { HttpService } from '@nestjs/axios';
import { Inject, Logger } from '@nestjs/common'; import { Inject, Logger, NotImplementedException } from '@nestjs/common';
import { REQUEST } from '@nestjs/core'; import { REQUEST } from '@nestjs/core';
import { Request } from 'express'; import { Request } from 'express';
import { firstValueFrom, Observable } from 'rxjs'; import { catchError, firstValueFrom, Observable } from 'rxjs';
import { User } from 'src/auth/models/user.model';
import { MIME_TYPES } from 'src/common/interfaces/utilities.interface'; import { MIME_TYPES } from 'src/common/interfaces/utilities.interface';
import { errorAxiosHandler } from 'src/common/utilities';
import { ENGINE_MODULE_OPTIONS } from 'src/engine/engine.constants'; import { ENGINE_MODULE_OPTIONS } from 'src/engine/engine.constants';
import { import {
IConfiguration, IConfiguration,
...@@ -43,22 +45,50 @@ export default class DataShieldService implements IEngineService { ...@@ -43,22 +45,50 @@ export default class DataShieldService implements IEngineService {
return {}; return {};
} }
logout(): void { async login(username: string, password: string): Promise<User> {
throw new Error('Method not implemented.'); const loginPath = this.options.baseurl + 'login';
const user: User = {
id: username,
username,
extraFields: {
sid: '',
},
};
const loginData = await firstValueFrom(
this.httpService
.get(loginPath, {
auth: { username, password },
})
.pipe(catchError((e) => errorAxiosHandler(e))),
);
const cookies = (loginData.headers['set-cookie'] as string[]) ?? [];
if (loginData.headers && loginData.headers['set-cookie']) {
cookies.forEach((cookie) => {
const [key, value] = cookie.split(/={1}/);
if (key === 'sid') {
user.extraFields.sid = value;
}
});
}
return user;
} }
getAlgorithms(): Algorithm[] | Promise<Algorithm[]> { getAlgorithms(): Algorithm[] | Promise<Algorithm[]> {
throw new Error('Method not implemented.'); throw new NotImplementedException();
} }
async getHistogram(variable: string): Promise<RawResult> { async getHistogram(variable: string, cookie?: string): Promise<RawResult> {
const path = const path =
this.options.baseurl + `histogram?var=${variable}&type=combine`; this.options.baseurl + `histogram?var=${variable}&type=combine`;
const response = await firstValueFrom( const response = await firstValueFrom(
this.httpService.get(path, { this.httpService.get(path, {
headers: { headers: {
cookie: this.req['req'].headers['cookie'], cookie,
}, },
}), }),
); );
...@@ -87,13 +117,16 @@ export default class DataShieldService implements IEngineService { ...@@ -87,13 +117,16 @@ export default class DataShieldService implements IEngineService {
}; };
} }
async getDescriptiveStats(variable: string): Promise<TableResult> { async getDescriptiveStats(
variable: string,
cookie?: string,
): Promise<TableResult> {
const path = this.options.baseurl + `quantiles?var=${variable}&type=split`; const path = this.options.baseurl + `quantiles?var=${variable}&type=split`;
const response = await firstValueFrom( const response = await firstValueFrom(
this.httpService.get(path, { this.httpService.get(path, {
headers: { headers: {
cookie: this.req['req'].headers['cookie'], cookie,
}, },
}), }),
); );
...@@ -111,6 +144,10 @@ export default class DataShieldService implements IEngineService { ...@@ -111,6 +144,10 @@ export default class DataShieldService implements IEngineService {
data: ExperimentCreateInput, data: ExperimentCreateInput,
isTransient: boolean, isTransient: boolean,
): Promise<Experiment> { ): Promise<Experiment> {
const user = this.req.user as User;
const cookie = [`sid=${user.extraFields['sid']}`, `user=${user.id}`].join(
';',
);
const expResult: Experiment = { const expResult: Experiment = {
id: `${data.algorithm.id}-${Date.now()}`, id: `${data.algorithm.id}-${Date.now()}`,
variables: data.variables, variables: data.variables,
...@@ -126,7 +163,7 @@ export default class DataShieldService implements IEngineService { ...@@ -126,7 +163,7 @@ export default class DataShieldService implements IEngineService {
case 'MULTIPLE_HISTOGRAMS': { case 'MULTIPLE_HISTOGRAMS': {
expResult.results = await Promise.all<RawResult>( expResult.results = await Promise.all<RawResult>(
data.variables.map( data.variables.map(
async (variable) => await this.getHistogram(variable), async (variable) => await this.getHistogram(variable, cookie),
), ),
); );
break; break;
...@@ -134,7 +171,8 @@ export default class DataShieldService implements IEngineService { ...@@ -134,7 +171,8 @@ export default class DataShieldService implements IEngineService {
case 'DESCRIPTIVE_STATS': { case 'DESCRIPTIVE_STATS': {
expResult.results = await Promise.all<TableResult>( expResult.results = await Promise.all<TableResult>(
[...data.variables, ...data.coVariables].map( [...data.variables, ...data.coVariables].map(
async (variable) => await this.getDescriptiveStats(variable), async (variable) =>
await this.getDescriptiveStats(variable, cookie),
), ),
); );
break; break;
...@@ -157,40 +195,23 @@ export default class DataShieldService implements IEngineService { ...@@ -157,40 +195,23 @@ export default class DataShieldService implements IEngineService {
} }
getExperiment(id: string): Experiment | Promise<Experiment> { getExperiment(id: string): Experiment | Promise<Experiment> {
throw new Error('Method not implemented.'); throw new NotImplementedException();
} }
removeExperiment(id: string): PartialExperiment | Promise<PartialExperiment> { removeExperiment(id: string): PartialExperiment | Promise<PartialExperiment> {
throw new Error('Method not implemented.'); throw new NotImplementedException();
} }
editExperient( editExperient(
id: string, id: string,
expriment: ExperimentEditInput, expriment: ExperimentEditInput,
): Experiment | Promise<Experiment> { ): Experiment | Promise<Experiment> {
throw new Error('Method not implemented.'); throw new NotImplementedException();
} }
async getDomains(): Promise<Domain[]> { async getDomains(): Promise<Domain[]> {
const loginPath = this.options.baseurl + 'login'; const user = this.req.user as User;
const cookies = [`sid=${user.extraFields['sid']}`, `user=${user.id}`];
const loginData = await firstValueFrom(
this.httpService.get(loginPath, {
auth: { username: 'guest', password: 'guest123' },
}),
);
const cookies = (loginData.headers['set-cookie'] as string[]) ?? [];
if (loginData.headers && loginData.headers['set-cookie']) {
cookies.forEach((cookie) => {
const [key, value] = cookie.split(/={1}/);
this.req.res.cookie(key, value, {
httpOnly: true,
//sameSite: 'none',
});
});
}
const path = this.options.baseurl + 'getvars'; const path = this.options.baseurl + 'getvars';
const response = await firstValueFrom( const response = await firstValueFrom(
...@@ -216,27 +237,27 @@ export default class DataShieldService implements IEngineService { ...@@ -216,27 +237,27 @@ export default class DataShieldService implements IEngineService {
} }
editActiveUser(): Observable<string> { editActiveUser(): Observable<string> {
throw new Error('Method not implemented.'); throw new NotImplementedException();
} }
getExperimentREST(): Observable<string> { getExperimentREST(): Observable<string> {
throw new Error('Method not implemented.'); throw new NotImplementedException();
} }
deleteExperiment(): Observable<string> { deleteExperiment(): Observable<string> {
throw new Error('Method not implemented.'); throw new NotImplementedException();
} }
editExperimentREST(): Observable<string> { editExperimentREST(): Observable<string> {
throw new Error('Method not implemented.'); throw new NotImplementedException();
} }
startExperimentTransient(): Observable<string> { startExperimentTransient(): Observable<string> {
throw new Error('Method not implemented.'); throw new NotImplementedException();
} }
startExperiment(): Observable<string> { startExperiment(): Observable<string> {
throw new Error('Method not implemented.'); throw new NotImplementedException();
} }
getExperiments(): string { getExperiments(): string {
......
...@@ -2,6 +2,7 @@ import { HttpModule, HttpService } from '@nestjs/axios'; ...@@ -2,6 +2,7 @@ import { HttpModule, HttpService } from '@nestjs/axios';
import { DynamicModule, Global, Logger, Module } from '@nestjs/common'; import { DynamicModule, Global, Logger, Module } from '@nestjs/common';
import { REQUEST } from '@nestjs/core'; import { REQUEST } from '@nestjs/core';
import { Request } from 'express'; import { Request } from 'express';
import { IncomingMessage } from 'http';
import { ENGINE_MODULE_OPTIONS, ENGINE_SERVICE } from './engine.constants'; import { ENGINE_MODULE_OPTIONS, ENGINE_SERVICE } from './engine.constants';
import { EngineController } from './engine.controller'; import { EngineController } from './engine.controller';
import { IEngineOptions, IEngineService } from './engine.interfaces'; import { IEngineOptions, IEngineService } from './engine.interfaces';
...@@ -50,7 +51,10 @@ export class EngineModule { ...@@ -50,7 +51,10 @@ export class EngineModule {
): Promise<IEngineService> { ): Promise<IEngineService> {
try { try {
const service = await import(`./connectors/${opt.type}/main.connector`); const service = await import(`./connectors/${opt.type}/main.connector`);
const engine = new service.default(opt, httpService, req); const gqlRequest = req && req['req']; // graphql headers exception
const request =
gqlRequest && gqlRequest instanceof IncomingMessage ? gqlRequest : req;
const engine = new service.default(opt, httpService, request);
return engine; return engine;
} catch (e) { } catch (e) {
......
import { Inject } from '@nestjs/common'; import { Inject, UseGuards } from '@nestjs/common';
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql'; import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
import { JwtAuthGuard } from 'src/auth/guards/jwt-auth.guard';
import { Md5 } from 'ts-md5'; import { Md5 } from 'ts-md5';
import { ENGINE_MODULE_OPTIONS, ENGINE_SERVICE } from './engine.constants'; import { ENGINE_MODULE_OPTIONS, ENGINE_SERVICE } from './engine.constants';
import { IEngineOptions, IEngineService } from './engine.interfaces'; import { IEngineOptions, IEngineService } from './engine.interfaces';
...@@ -14,6 +15,7 @@ import { ExperimentCreateInput } from './models/experiment/input/experiment-crea ...@@ -14,6 +15,7 @@ import { ExperimentCreateInput } from './models/experiment/input/experiment-crea
import { ExperimentEditInput } from './models/experiment/input/experiment-edit.input'; import { ExperimentEditInput } from './models/experiment/input/experiment-edit.input';
import { ListExperiments } from './models/experiment/list-experiments.model'; import { ListExperiments } from './models/experiment/list-experiments.model';
@UseGuards(JwtAuthGuard)
@Resolver() @Resolver()
export class EngineResolver { export class EngineResolver {
constructor( constructor(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment