Skip to content

Commit 9d50392

Browse files
committed
auth setup
1 parent a0ee9b3 commit 9d50392

File tree

12 files changed

+19
-194
lines changed

12 files changed

+19
-194
lines changed

src/app.module.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ import { GlobalProvidersModule } from './shared/global/globalProviders.module';
66
import { TopcoderModule } from './shared/topcoder/topcoder.module';
77
import { HealthCheckController } from './api/health-check/healthCheck.controller';
88
import { TokenValidatorMiddleware } from './core/auth/middleware/tokenValidator.middleware';
9-
import { CreateRequestStoreMiddleware } from './core/request/createRequestStore.middleware';
10-
import { AuthGuard, RolesGuard } from './core/auth/guards';
11-
import { APP_GUARD } from '@nestjs/core';
129

1310
@Module({
1411
imports: [
@@ -20,27 +17,15 @@ import { APP_GUARD } from '@nestjs/core';
2017
sessionIdGenerator: () => randomUUID(),
2118
statelessMode: false,
2219
},
23-
// guards: [AuthGuard, RolesGuard],
2420
}),
2521
GlobalProvidersModule,
2622
TopcoderModule,
2723
],
2824
controllers: [HealthCheckController],
29-
providers: [
30-
// {
31-
// provide: APP_GUARD,
32-
// useClass: AuthGuard,
33-
// },
34-
// {
35-
// provide: APP_GUARD,
36-
// useClass: RolesGuard,
37-
// },
38-
QueryChallengesTool,
39-
],
25+
providers: [QueryChallengesTool],
4026
})
4127
export class AppModule implements NestModule {
4228
configure(consumer: MiddlewareConsumer) {
43-
// consumer.apply(TokenValidatorMiddleware).forRoutes('*');
44-
// consumer.apply(CreateRequestStoreMiddleware).forRoutes('*');
29+
consumer.apply(TokenValidatorMiddleware).forRoutes('*');
4530
}
4631
}

src/core/auth/auth.constants.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ export enum Role {
22
User = 'Topcoder User',
33
}
44

5-
export enum M2mScope {}
5+
export enum M2mScope {
6+
QueryPublicChallenges = 'query:public:challenges',
7+
}

src/core/auth/decorators/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export * from './m2m.decorator';
22
export * from './m2mScope.decorator';
33
export * from './public.decorator';
4-
export * from './roles.decorator';
54
export * from './user.decorator';

src/core/auth/decorators/roles.decorator.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/core/auth/guards/auth.guard.ts

Lines changed: 7 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,17 @@
1-
import {
2-
CanActivate,
3-
ExecutionContext,
4-
Injectable,
5-
UnauthorizedException,
6-
} from '@nestjs/common';
1+
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
72
import { Reflector } from '@nestjs/core';
8-
import { IS_PUBLIC_KEY } from '../decorators/public.decorator';
9-
import { IS_M2M_KEY } from '../decorators/m2m.decorator';
10-
import { M2mScope } from '../auth.constants';
11-
import { SCOPES_KEY } from '../decorators/m2mScope.decorator';
3+
import { Logger } from 'src/shared/global';
124

135
@Injectable()
146
export class AuthGuard implements CanActivate {
7+
private readonly logger = new Logger(AuthGuard.name);
8+
159
constructor(private reflector: Reflector) {}
1610

1711
canActivate(context: ExecutionContext): boolean {
18-
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
19-
context.getHandler(),
20-
context.getClass(),
21-
]);
22-
23-
if (isPublic) return true;
24-
25-
const req = context.switchToHttp().getRequest();
26-
const isM2M = this.reflector.getAllAndOverride<boolean>(IS_M2M_KEY, [
27-
context.getHandler(),
28-
context.getClass(),
29-
]);
30-
31-
const { m2mUserId } = req;
32-
if (m2mUserId) {
33-
req.user = {
34-
id: m2mUserId,
35-
handle: '',
36-
};
37-
}
38-
39-
// Regular authentication - check that we have user's email and have verified the id token
40-
if (!isM2M) {
41-
return Boolean(req.email && req.idTokenVerified);
42-
}
43-
44-
// M2M authentication - check scopes
45-
if (!req.idTokenVerified || !req.m2mTokenScope)
46-
throw new UnauthorizedException();
47-
48-
const allowedM2mScopes = this.reflector.getAllAndOverride<M2mScope[]>(
49-
SCOPES_KEY,
50-
[context.getHandler(), context.getClass()],
51-
);
12+
this.logger.log('AuthGuard canActivate called...');
13+
// Check if the route is marked as public...
5214

53-
const reqScopes = req.m2mTokenScope.split(' ');
54-
if (reqScopes.some((reqScope) => allowedM2mScopes.includes(reqScope))) {
55-
return true;
56-
}
57-
return false;
15+
return true;
5816
}
5917
}

src/core/auth/guards/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
export * from './auth.guard';
2-
export * from './roles.guard';

src/core/auth/guards/roles.guard.ts

Lines changed: 0 additions & 57 deletions
This file was deleted.

src/core/request/createRequestStore.middleware.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/core/request/requestStore.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/main.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { Logger } from 'src/shared/global';
44
import { ENV_CONFIG } from './config';
55

66
async function bootstrap() {
7-
const app = await NestFactory.create(AppModule);
7+
const app = await NestFactory.create(AppModule, {
8+
logger: ['error', 'warn', 'log'],
9+
});
810

911
const logger = new Logger('bootstrap()');
1012

0 commit comments

Comments
 (0)