Skip to content

Commit

Permalink
chore: added main, bridge, auth guard and gateway guard
Browse files Browse the repository at this point in the history
  • Loading branch information
apsantiso committed Jul 11, 2024
1 parent 289583c commit 0fce904
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 4 deletions.
25 changes: 25 additions & 0 deletions src/externals/bridge/bridge.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,31 @@ export class BridgeService {
}
}

async setStorage(email: UserAttributes['email'], bytes: number) {
try {
const url = this.configService.get('apis.storage.url');
const username = this.configService.get('apis.storage.auth.username');
const password = this.configService.get('apis.storage.auth.password');

const params = {
headers: { 'Content-Type': 'application/json' },
auth: { username, password },
};

await this.httpClient.post(
`${url}/gateway/upgrade`,
{ email, bytes },
params,
);
} catch (error) {
Logger.error(`
[BRIDGESERVICE/SETSTORAGE]: There was an error while trying to set user storage space Error: ${JSON.stringify(
error,
)}
`);
}
}

async getLimit(
networkUser: UserAttributes['bridgeUser'],
networkPass: UserAttributes['userId'],
Expand Down
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ async function bootstrap() {
'internxt-mnemonic',
'x-share-password',
'X-Internxt-Captcha',
'x-internxt-workspace',
'internxt-resources-token',
],
exposedHeaders: ['sessionId'],
origin: '*',
Expand Down Expand Up @@ -68,6 +70,7 @@ async function bootstrap() {
.setDescription('Drive API')
.setVersion('1.0')
.addBearerAuth()
.addBearerAuth(undefined, 'gateway')
.build();

const document = SwaggerModule.createDocument(app, swaggerConfig);
Expand Down
12 changes: 8 additions & 4 deletions src/modules/auth/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ export class AuthGuard extends PassportAuthGuard([JwtStrategy.id]) {
}

canActivate(context: ExecutionContext) {
const isPublic = this.reflector.get<boolean>(
'isPublic',
context.getHandler(),
const handlerContext = context.getHandler();
const classContext = context.getClass();

const isPublic = this.reflector.get<boolean>('isPublic', handlerContext);
const disableGlobalAuth = this.reflector.getAllAndOverride<boolean>(
'disableGlobalAuth',
[handlerContext, classContext],
);

if (isPublic) {
if (isPublic || disableGlobalAuth) {
return true;
}

Expand Down
17 changes: 17 additions & 0 deletions src/modules/auth/gateway.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ExecutionContext, Injectable } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { AuthGuard as PassportAuthGuard } from '@nestjs/passport';
import { GatewayRS256JwtStrategy } from './gateway-rs256jwt.strategy';

@Injectable()
export class GatewayGuard extends PassportAuthGuard(
GatewayRS256JwtStrategy.id,
) {
constructor(private readonly reflector: Reflector) {
super();
}

canActivate(context: ExecutionContext) {
return super.canActivate(context);
}
}
69 changes: 69 additions & 0 deletions src/modules/workspaces/pipes/validate-uuid.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { ValidateUUIDPipe } from './validate-uuid.pipe';
import { BadRequestException, ArgumentMetadata } from '@nestjs/common';

describe('ValidateUUIDPipe', () => {
let pipe: ValidateUUIDPipe;

beforeEach(() => {
pipe = new ValidateUUIDPipe();
});

it('should be defined', () => {
expect(pipe).toBeDefined();
});

it('When uuid given is valid, then it should return the same value', () => {
const validUUID = '123e4567-e89b-12d3-a456-426614174000';
const metadata: ArgumentMetadata = {
type: 'body',
metatype: null,
data: 'testUUID',
};
expect(pipe.transform(validUUID, metadata)).toEqual(validUUID);
});

it('When UUID is invalid, then it should throw', () => {
const invalidUUID = 'invalid-uuid';
const metadata: ArgumentMetadata = {
type: 'body',
metatype: null,
data: 'testUUID',
};
expect(() => pipe.transform(invalidUUID, metadata)).toThrow(
BadRequestException,
);
expect(() => pipe.transform(invalidUUID, metadata)).toThrow(
`Value of 'testUUID' is not a valid UUID.`,
);
});

it('When UUID is null, then it should throw', () => {
const nullUUID = null;
const metadata: ArgumentMetadata = {
type: 'body',
metatype: null,
data: 'testUUID',
};
expect(() => pipe.transform(nullUUID, metadata)).toThrow(
BadRequestException,
);
expect(() => pipe.transform(nullUUID, metadata)).toThrow(
`Value of 'testUUID' is not a valid UUID.`,
);
});

it('When UUID is undefined, then it should throw', () => {
const undefinedUUID = undefined;
const metadata: ArgumentMetadata = {
type: 'body',
metatype: null,
data: 'testUUID',
};
expect(() => pipe.transform(undefinedUUID, metadata)).toThrow(
BadRequestException,
);
expect(() => pipe.transform(undefinedUUID, metadata)).toThrow(
`Value of 'testUUID' is not a valid UUID.`,
);
});
});
18 changes: 18 additions & 0 deletions src/modules/workspaces/pipes/validate-uuid.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {
PipeTransform,
Injectable,
ArgumentMetadata,
BadRequestException,
} from '@nestjs/common';
import { isUUID } from 'class-validator';

@Injectable()
export class ValidateUUIDPipe implements PipeTransform {
transform(value: any, metadata: ArgumentMetadata) {
if (!value || !isUUID(value))
throw new BadRequestException(
`Value of '${metadata.data}' is not a valid UUID.`,
);
return value;
}
}

0 comments on commit 0fce904

Please sign in to comment.