-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
refactor(api-service): centralize auth into @novu/api and reduce DI complexity #7640
Changes from 1 commit
c609a2d
64e2241
e306af1
9fe21f7
b8a12ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { CanActivate, ExecutionContext, forwardRef, Inject, Injectable, UnauthorizedException } from '@nestjs/common'; | ||
import { AuthService } from '@novu/application-generic'; | ||
import { CanActivate, ExecutionContext, Injectable, UnauthorizedException } from '@nestjs/common'; | ||
import { AuthService } from '../services/auth.service'; | ||
|
||
@Injectable() | ||
export class RootEnvironmentGuard implements CanActivate { | ||
constructor(@Inject(forwardRef(() => AuthService)) private authService: AuthService) {} | ||
constructor(private authService: AuthService) {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to use this magic if it lives in the same package |
||
|
||
async canActivate(context: ExecutionContext) { | ||
const request = context.switchToHttp().getRequest(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,29 @@ | ||
import { UserAuthGuard } from '@novu/application-generic'; | ||
import { Injectable, ExecutionContext, Inject } from '@nestjs/common'; | ||
import { IAuthGuard } from '@nestjs/passport'; | ||
import { Observable } from 'rxjs'; | ||
import { UserSessionData } from '@novu/shared'; | ||
import { Instrument } from '@novu/application-generic'; | ||
|
||
export { UserAuthGuard }; | ||
@Injectable() | ||
export class UserAuthGuard { | ||
constructor(@Inject('USER_AUTH_GUARD') private authGuard: IAuthGuard) {} | ||
|
||
@Instrument() | ||
canActivate(context: ExecutionContext): boolean | Promise<boolean> | Observable<boolean> { | ||
return this.authGuard.canActivate(context); | ||
} | ||
|
||
getAuthenticateOptions(context: ExecutionContext) { | ||
return this.authGuard.getAuthenticateOptions(context); | ||
} | ||
|
||
handleRequest<TUser = UserSessionData>( | ||
err: any, | ||
user: UserSessionData | false, | ||
info: any, | ||
context: ExecutionContext, | ||
status?: any | ||
): TUser { | ||
return this.authGuard.handleRequest(err, user, info, context, status); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,7 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { MemberEntity, SubscriberEntity, UserEntity } from '@novu/dal'; | ||
import { | ||
AuthProviderEnum, | ||
AuthenticateContext, | ||
ISubscriberJwt, | ||
UserSessionData, | ||
} from '@novu/shared'; | ||
import { IAuthService } from './auth.service.interface'; | ||
import { AuthProviderEnum, AuthenticateContext, ISubscriberJwt, UserSessionData } from '@novu/shared'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need these to remain in shared? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some of these likely need to stay e.g. |
||
import { IAuthService } from '@novu/application-generic'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one interface needs to stay in app-generic because its reused in EE auth. We need to have common auth interface for private and public authentication so it makes sense. |
||
|
||
@Injectable() | ||
export class AuthService implements IAuthService { | ||
|
@@ -24,30 +19,17 @@ export class AuthService implements IAuthService { | |
id: string; | ||
}, | ||
distinctId: string, | ||
authContext: AuthenticateContext = {}, | ||
authContext: AuthenticateContext = {} | ||
): Promise<{ newUser: boolean; token: string }> { | ||
return this.authService.authenticate( | ||
authProvider, | ||
accessToken, | ||
refreshToken, | ||
profile, | ||
distinctId, | ||
authContext, | ||
); | ||
return this.authService.authenticate(authProvider, accessToken, refreshToken, profile, distinctId, authContext); | ||
} | ||
|
||
refreshToken(userId: string): Promise<string> { | ||
return this.authService.refreshToken(userId); | ||
} | ||
|
||
isAuthenticatedForOrganization( | ||
userId: string, | ||
organizationId: string, | ||
): Promise<boolean> { | ||
return this.authService.isAuthenticatedForOrganization( | ||
userId, | ||
organizationId, | ||
); | ||
isAuthenticatedForOrganization(userId: string, organizationId: string): Promise<boolean> { | ||
return this.authService.isAuthenticatedForOrganization(userId, organizationId); | ||
} | ||
|
||
getUserByApiKey(apiKey: string): Promise<UserSessionData> { | ||
|
@@ -66,21 +48,16 @@ export class AuthService implements IAuthService { | |
user: UserEntity, | ||
organizationId?: string, | ||
member?: MemberEntity, | ||
environmentId?: string, | ||
environmentId?: string | ||
): Promise<string> { | ||
return this.authService.getSignedToken( | ||
user, | ||
organizationId, | ||
member, | ||
environmentId, | ||
); | ||
return this.authService.getSignedToken(user, organizationId, member, environmentId); | ||
} | ||
|
||
validateUser(payload: UserSessionData): Promise<UserEntity> { | ||
return this.authService.validateUser(payload); | ||
} | ||
|
||
validateSubscriber(payload: ISubscriberJwt): Promise<SubscriberEntity> { | ||
validateSubscriber(payload: ISubscriberJwt): Promise<SubscriberEntity | null> { | ||
return this.authService.validateSubscriber(payload); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These services are used only in API, no reason to have them in app-generic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😍