Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

created separate events JWT access secret #2146

Merged
merged 1 commit into from Mar 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/altair-api/.env.example
@@ -1,4 +1,5 @@
JWT_ACCESS_SECRET=
EVENTS_JWT_ACCESS_SECRET=
JWT_REFRESH_SECRET=
GOOGLE_OAUTH_CLIENT_ID=
GOOGLE_OAUTH_CLIENT_SECRET=
Expand Down
4 changes: 2 additions & 2 deletions packages/altair-api/src/app.controller.ts
Expand Up @@ -4,7 +4,7 @@ import { Request, Response } from 'express';
import { PrismaService } from 'nestjs-prisma';
import { map, Subject } from 'rxjs';
import { AppService } from './app.service';
import { ShortJwtAuthGuard } from './auth/guards/short-jwt-auth.guard';
import { EventsJwtAuthGuard } from './auth/guards/events-jwt-auth.guard';
import { EVENTS } from './common/events';

@Controller()
Expand All @@ -20,7 +20,7 @@ export class AppController {
return res.redirect('https://altairgraphql.dev');
}

@UseGuards(ShortJwtAuthGuard)
@UseGuards(EventsJwtAuthGuard)
@Sse('events')
handleUserEvents(@Req() req: Request) {
const subject$ = new Subject();
Expand Down
4 changes: 2 additions & 2 deletions packages/altair-api/src/auth/auth.controller.ts
Expand Up @@ -50,7 +50,7 @@ export class AuthController {

@Get('slt')
@UseGuards(JwtAuthGuard)
getShortlivedToken(@Req() req: Request) {
return { slt: this.authService.getShortLivedToken(req.user.id) };
getShortlivedEventsToken(@Req() req: Request) {
return { slt: this.authService.getShortLivedEventsToken(req.user.id) };
}
}
4 changes: 2 additions & 2 deletions packages/altair-api/src/auth/auth.module.ts
Expand Up @@ -8,7 +8,7 @@ import { PasswordService } from './password/password.service';
import { JwtStrategy } from './strategies/jwt.strategy';
import { AuthController } from './auth.controller';
import { GoogleStrategy } from './strategies/google.strategy';
import { ShortJwtStrategy } from './strategies/jwt-short.strategy';
import { EventsJwtStrategy } from './strategies/events-jwt.strategy';
import { StripeService } from 'src/stripe/stripe.service';
import { UserService } from './user/user.service';
import { UserController } from './user/user.controller';
Expand All @@ -32,7 +32,7 @@ import { UserController } from './user/user.controller';
providers: [
AuthService,
JwtStrategy,
ShortJwtStrategy,
EventsJwtStrategy,
GoogleStrategy,
PasswordService,
StripeService,
Expand Down
5 changes: 3 additions & 2 deletions packages/altair-api/src/auth/auth.service.ts
Expand Up @@ -113,13 +113,14 @@ export class AuthService {
}

/**
* Generates a short-lived token for the purpose of event connection
* Generates a short-lived events token for the purpose of event connection
*/
getShortLivedToken(userId: string): string {
getShortLivedEventsToken(userId: string): string {
const securityConfig = this.configService.get<SecurityConfig>('security');
return this.jwtService.sign(
{ userId },
{
secret: this.configService.get<string>('EVENTS_JWT_ACCESS_SECRET'),
expiresIn: securityConfig.shortExpiresIn,
}
);
Expand Down
Expand Up @@ -2,4 +2,4 @@ import { Injectable } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';

@Injectable()
export class ShortJwtAuthGuard extends AuthGuard('short-jwt') {}
export class EventsJwtAuthGuard extends AuthGuard('events-jwt') {}
Expand Up @@ -7,14 +7,17 @@ import { AuthService } from '../auth.service';
import { JwtDto } from '../models/jwt.dto';

@Injectable()
export class ShortJwtStrategy extends PassportStrategy(Strategy, 'short-jwt') {
export class EventsJwtStrategy extends PassportStrategy(
Strategy,
'events-jwt'
) {
constructor(
private readonly authService: AuthService,
readonly configService: ConfigService
) {
super({
jwtFromRequest: ExtractJwt.fromUrlQueryParameter('slt'),
secretOrKey: configService.get('JWT_ACCESS_SECRET'),
secretOrKey: configService.get('EVENTS_JWT_ACCESS_SECRET'),
});
}

Expand Down