.
This commit is contained in:
1 parent
90a7cf6a7e
commit
905bbcc6dd
9 files changed
+122
-67
No files matched your search
@@ -6,7 +6,7 @@ import { IdentityService } from './identity.service';
|
||||
import { JwtAuthGuard } from './jwt.guard';
|
||||
import { JwtService } from './jwt.service';
|
||||
import { RolesGuard } from './roles.guard';
|
||||
import { UserResolverInterceptor } from './user-resolver.interceptor';
|
||||
import { UserResolverGuard } from './user-resolver.guard';
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
@@ -16,17 +16,18 @@ import { UserResolverInterceptor } from './user-resolver.interceptor';
|
||||
IdentityService,
|
||||
JwtAuthGuard,
|
||||
IdentityResolverGuard,
|
||||
UserResolverGuard,
|
||||
RolesGuard,
|
||||
UserResolverInterceptor,
|
||||
{ provide: APP_GUARD, useClass: JwtAuthGuard },
|
||||
{ provide: APP_GUARD, useClass: IdentityResolverGuard },
|
||||
{ provide: APP_GUARD, useClass: UserResolverGuard },
|
||||
],
|
||||
exports: [
|
||||
AuthService,
|
||||
JwtService,
|
||||
IdentityService,
|
||||
UserResolverGuard,
|
||||
RolesGuard,
|
||||
UserResolverInterceptor,
|
||||
],
|
||||
})
|
||||
export class AuthModule {}
|
||||
@@ -0,0 +1,55 @@
|
||||
import { ExecutionContext } from '@nestjs/common';
|
||||
import mongoose from 'mongoose';
|
||||
import { ConnectionService } from '../global/database/connection.service';
|
||||
import { IdentityDocument, UserDocument } from '../global/database/schema';
|
||||
import { UserResolverGuard } from './user-resolver.guard';
|
||||
import './auth-request';
|
||||
|
||||
function createContext(request: Record<string, unknown>): ExecutionContext {
|
||||
return {
|
||||
switchToHttp: () => ({
|
||||
getRequest: () => request,
|
||||
}),
|
||||
} as ExecutionContext;
|
||||
}
|
||||
|
||||
describe('UserResolverGuard', () => {
|
||||
it('resolves the community membership before role guards run', async () => {
|
||||
const communityId = new mongoose.Types.ObjectId();
|
||||
const identityId = new mongoose.Types.ObjectId();
|
||||
const user = { _id: new mongoose.Types.ObjectId() } as UserDocument;
|
||||
const findOne = jest.fn().mockReturnValue({
|
||||
exec: jest.fn().mockResolvedValue(user),
|
||||
});
|
||||
const connectionService = {
|
||||
getModel: jest.fn().mockResolvedValue({ findOne }),
|
||||
} as unknown as ConnectionService;
|
||||
const request = {
|
||||
params: { communityId: communityId.toString() },
|
||||
identity: { _id: identityId } as IdentityDocument,
|
||||
};
|
||||
const guard = new UserResolverGuard(connectionService);
|
||||
|
||||
await expect(guard.canActivate(createContext(request))).resolves.toBe(true);
|
||||
|
||||
expect(findOne).toHaveBeenCalledWith({
|
||||
communityId,
|
||||
identityId,
|
||||
});
|
||||
expect(request.user).toBe(user);
|
||||
});
|
||||
|
||||
it('skips lookup when there is no resolved identity', async () => {
|
||||
const connectionService = {
|
||||
getModel: jest.fn(),
|
||||
} as unknown as ConnectionService;
|
||||
const request = {
|
||||
params: { communityId: new mongoose.Types.ObjectId().toString() },
|
||||
};
|
||||
const guard = new UserResolverGuard(connectionService);
|
||||
|
||||
await expect(guard.canActivate(createContext(request))).resolves.toBe(true);
|
||||
|
||||
expect(connectionService.getModel).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,33 @@
|
||||
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
|
||||
import type { Request } from 'express';
|
||||
import mongoose from 'mongoose';
|
||||
import { ConnectionService } from '../global/database/connection.service';
|
||||
import './auth-request';
|
||||
|
||||
@Injectable()
|
||||
export class UserResolverGuard implements CanActivate {
|
||||
constructor(private readonly connectionService: ConnectionService) {}
|
||||
|
||||
public async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
const request = context.switchToHttp().getRequest<Request>();
|
||||
const communityId = request.params?.communityId;
|
||||
|
||||
if (
|
||||
request.user ||
|
||||
!request.identity ||
|
||||
typeof communityId !== 'string' ||
|
||||
!mongoose.isValidObjectId(communityId)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const UserModel = await this.connectionService.getModel('user');
|
||||
const user = await UserModel.findOne({
|
||||
communityId: new mongoose.Types.ObjectId(communityId),
|
||||
identityId: request.identity._id,
|
||||
}).exec();
|
||||
|
||||
if (user) request.user = user;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
import {
|
||||
CallHandler,
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
NestInterceptor,
|
||||
} from '@nestjs/common';
|
||||
import type { Request } from 'express';
|
||||
import mongoose from 'mongoose';
|
||||
import { Observable } from 'rxjs';
|
||||
import { ConnectionService } from '../global/database/connection.service';
|
||||
import './auth-request';
|
||||
|
||||
@Injectable()
|
||||
export class UserResolverInterceptor implements NestInterceptor {
|
||||
constructor(private readonly connectionService: ConnectionService) {}
|
||||
|
||||
public async intercept(
|
||||
context: ExecutionContext,
|
||||
next: CallHandler,
|
||||
): Promise<Observable<unknown>> {
|
||||
const request = context.switchToHttp().getRequest<Request>();
|
||||
const communityId = request.params?.communityId;
|
||||
|
||||
if (
|
||||
!request.user &&
|
||||
request.identity &&
|
||||
typeof communityId === 'string' &&
|
||||
mongoose.isValidObjectId(communityId)
|
||||
) {
|
||||
const UserModel = await this.connectionService.getModel('user');
|
||||
const user = await UserModel.findOne({
|
||||
communityId: new mongoose.Types.ObjectId(communityId),
|
||||
identityId: request.identity._id,
|
||||
}).exec();
|
||||
if (user) request.user = user;
|
||||
}
|
||||
|
||||
return next.handle();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user