Skip to content

Commit

Permalink
merge nest js
Browse files Browse the repository at this point in the history
  • Loading branch information
artembuslaev committed May 26, 2023
2 parents a12f3df + 2c6541f commit 1be51d7
Show file tree
Hide file tree
Showing 86 changed files with 4,502 additions and 3,757 deletions.
Binary file added Methodology Library/CDM/CDM.policy
Binary file not shown.
4 changes: 4 additions & 0 deletions api-gateway/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
"dependencies": {
"@guardian/common": "^2.12.0",
"@guardian/interfaces": "^2.12.0",
"@nestjs/common": "^9.4.1",
"@nestjs/core": "^9.4.1",
"@nestjs/microservices": "^9.4.1",
"@nestjs/platform-express": "^9.4.2",
"@types/express-fileupload": "^1.4.1",
"dotenv": "^16.0.0",
"express": "^4.17.1",
Expand Down
209 changes: 118 additions & 91 deletions api-gateway/src/api/service/account.ts
Original file line number Diff line number Diff line change
@@ -1,117 +1,144 @@
import { Request, Response, Router, NextFunction } from 'express';
import { permissionHelper, authorizationHelper } from '@auth/authorization-helper';
import { Users } from '@helpers/users';
import { AuthenticatedRequest, Logger } from '@guardian/common';
import { Logger } from '@guardian/common';
import { Guardians } from '@helpers/guardians';
import { UserRole } from '@guardian/interfaces';
import validate, { prepareValidationResponse } from '@middlewares/validation';
import { registerSchema, loginSchema } from '@middlewares/validation/schemas/accounts';
import { ClientProxy } from '@nestjs/microservices';
import { Body, Controller, Get, Headers, Inject, Post } from '@nestjs/common';

/**
* User account route
*/
export const accountAPI = Router();
@Controller('accounts')
export class AccountApi {

accountAPI.get('/session', authorizationHelper, async (req: Request, res: Response, next: NextFunction) => {
const users = new Users();
try {
const authHeader = req.headers.authorization;
const token = authHeader.split(' ')[1];
res.json(await users.getUserByToken(token));
} catch (error) {
new Logger().error(error, ['API_GATEWAY']);
return next(error);
constructor(@Inject('GUARDIANS') public readonly client: ClientProxy) {
}
});

accountAPI.post('/register', validate(registerSchema()),async (req: Request, res: Response, next: NextFunction) => {
const users = new Users();
try {
const { username, password } = req.body;
let { role } = req.body;
// @deprecated 2022-10-01
if (role === 'ROOT_AUTHORITY') {
role = UserRole.STANDARD_REGISTRY;
}
res.status(201).json(await users.registerNewUser(username, password, role));
} catch (error) {
new Logger().error(error, ['API_GATEWAY']);
if (error.message.includes('already exists')) {
return res.status(422).json(prepareValidationResponse('An account with the same name already exists.'));
/**
* getSession
* @param headers
*/
@Get('/session')
async getSession(@Headers() headers): Promise<any> {
const users = new Users();
try {
const authHeader = headers.authorization;
const token = authHeader.split(' ')[1];
return await users.getUserByToken(token);
} catch (error) {
new Logger().error(error, ['API_GATEWAY']);
throw error;
}
next(error)
}
});

accountAPI.post('/login', validate(loginSchema()), async (req: Request, res: Response, next: NextFunction) => {
const users = new Users();
try {
const { username, password } = req.body;
res.json(await users.generateNewToken(username, password));
} catch (error) {
new Logger().error(error, ['API_GATEWAY']);
next(error)
}
});

accountAPI.get('/', [authorizationHelper, permissionHelper(UserRole.STANDARD_REGISTRY)],
async (req: AuthenticatedRequest, res: Response, next: NextFunction) => {
try {
/**
* register
* @param body
*/
@Post('/register')
async register(@Body() body): Promise<any> {
const users = new Users();
res.json(await users.getAllUserAccounts());
} catch (error) {
new Logger().error(error, ['API_GATEWAY']);
next(error)
try {
const {username, password} = body;
let {role} = body;
// @deprecated 2022-10-01
if (role === 'ROOT_AUTHORITY') {
role = UserRole.STANDARD_REGISTRY;
}
return await users.registerNewUser(username, password, role);
} catch (error) {
new Logger().error(error, ['API_GATEWAY']);
// if (error.message.includes('already exists')) {
// return res.status(422).json(prepareValidationResponse('An account with the same name already exists.'));
// }
throw error;
}
}
});

/**
* @deprecated 2022-10-01
*/
accountAPI.get('/root-authorities', authorizationHelper, async (req: Request, res: Response, next: NextFunction) => {
try {
/**
* Login
*/
@Post('/login')
async login(@Body() body): Promise<any> {
const users = new Users();
const standardRegistries = await users.getAllStandardRegistryAccounts();
res.json(standardRegistries);
} catch (error) {
new Logger().error(error.message, ['API_GATEWAY']);
return next(error)
try {
const {username, password} = body;
return await users.generateNewToken(username, password);
} catch (error) {
new Logger().error(error, ['API_GATEWAY']);
throw error;
}
}
});

accountAPI.get('/standard-registries', authorizationHelper, async (req: Request, res: Response, next: NextFunction) => {
try {
const users = new Users();
const standardRegistries = await users.getAllStandardRegistryAccounts();
res.json(standardRegistries);
} catch (error) {
new Logger().error(error.message, ['API_GATEWAY']);
return next(error);
/**
* Accounts
*/
@Get()
async getAllAccounts(): Promise<any> {
try {
const users = new Users();
return await users.getAllUserAccounts();
} catch (error) {
new Logger().error(error, ['API_GATEWAY']);
throw error
}
}
});

accountAPI.get('/balance', authorizationHelper, async (req: Request, res: Response, next: NextFunction) => {
try {
const authHeader = req.headers.authorization;
const users = new Users();
if (authHeader) {
const token = authHeader.split(' ')[1];
try {
const user = await users.getUserByToken(token) as any;
if (user) {
const guardians = new Guardians();
const balance = await guardians.getBalance(user.username);
return res.json(balance);
}
return res.json({});
/**
* Get root authorities
* @deprecated 2022-10-01
*/
@Get('/root-authorities')
async getRootAuthorities(): Promise<any> {
try {
const users = new Users();
return await users.getAllStandardRegistryAccounts();
} catch (error) {
new Logger().error(error.message, ['API_GATEWAY']);
throw error;
}
}

} catch (error) {
return res.json({});
/**
* Get SAs
*/
@Get('/standard-registries')
async getStandatdRegistries(): Promise<any> {
try {
const users = new Users();
return await users.getAllStandardRegistryAccounts();
} catch (error) {
new Logger().error(error.message, ['API_GATEWAY']);
throw error;
}
}

@Get('/balance')
async getBalance(@Headers() headers): Promise<any> {
try {
const authHeader = headers.authorization;
const users = new Users();
if (authHeader) {
const token = authHeader.split(' ')[1];
try {
const user = await users.getUserByToken(token) as any;
if (user) {
const guardians = new Guardians();
return await guardians.getBalance(user.username);
// const balance = await this.client.send(MessageAPI.GET_BALANCE, { username: user.username }).toPromise()
// return balance;
}
return {};

} catch (error) {
return {};
}
}
return {};
} catch (error) {
new Logger().error(error, ['API_GATEWAY']);
throw error;
}
res.json({});
} catch (error) {
new Logger().error(error, ['API_GATEWAY']);
return next(error)
}
});
}
Loading

0 comments on commit 1be51d7

Please sign in to comment.