mirror of
https://github.com/swissmakers/swiss-datashare.git
synced 2026-04-11 10:27:01 +02:00
feat: allow unauthenticated uploads
This commit is contained in:
@@ -1,7 +1,14 @@
|
||||
import { ExecutionContext } from "@nestjs/common";
|
||||
import { AuthGuard } from "@nestjs/passport";
|
||||
import { Observable } from "rxjs";
|
||||
|
||||
export class JwtGuard extends AuthGuard("jwt") {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
canActivate(
|
||||
context: ExecutionContext
|
||||
): boolean | Promise<boolean> | Observable<boolean> {
|
||||
return process.env.ALLOW_UNAUTHENTICATED_SHARES == "true" ? true : super.canActivate(context);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
|
||||
super({
|
||||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
||||
secretOrKey: config.get("JWT_SECRET"),
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,8 @@ export class ShareOwnerGuard implements CanActivate {
|
||||
|
||||
if (!share) throw new NotFoundException("Share not found");
|
||||
|
||||
if(!share.creatorId) return true;
|
||||
|
||||
return share.creatorId == (request.user as User).id;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ export class ShareService {
|
||||
private jwtService: JwtService
|
||||
) {}
|
||||
|
||||
async create(share: CreateShareDTO, user: User) {
|
||||
async create(share: CreateShareDTO, user?: User) {
|
||||
if (!(await this.isShareIdAvailable(share.id)).isAvailable)
|
||||
throw new BadRequestException("Share id already in use");
|
||||
|
||||
@@ -58,7 +58,7 @@ export class ShareService {
|
||||
data: {
|
||||
...share,
|
||||
expiration: expirationDate,
|
||||
creator: { connect: { id: user.id } },
|
||||
creator: { connect: user ? { id: user.id } : undefined },
|
||||
security: { create: share.security },
|
||||
},
|
||||
});
|
||||
@@ -154,6 +154,8 @@ export class ShareService {
|
||||
});
|
||||
|
||||
if (!share) throw new NotFoundException("Share not found");
|
||||
if (!share.creatorId)
|
||||
throw new ForbiddenException("Anonymous shares can't be deleted");
|
||||
|
||||
await this.fileService.deleteAllFiles(shareId);
|
||||
await this.prisma.share.delete({ where: { id: shareId } });
|
||||
|
||||
Reference in New Issue
Block a user