mirror of
https://github.com/stonith404/pingvin-share.git
synced 2025-02-19 01:55:48 -05:00
refactor: move guard checks to service
This commit is contained in:
parent
233c26e5cf
commit
cd9d828686
3 changed files with 16 additions and 19 deletions
|
@ -34,12 +34,10 @@ export class ShareSecurityGuard implements CanActivate {
|
||||||
include: { security: true },
|
include: { security: true },
|
||||||
});
|
});
|
||||||
|
|
||||||
if (
|
const isExpired =
|
||||||
!share ||
|
moment().isAfter(share.expiration) && !moment(share.expiration).isSame(0);
|
||||||
(moment().isAfter(share.expiration) &&
|
|
||||||
moment(share.expiration).unix() !== 0)
|
if (!share || isExpired) throw new NotFoundException("Share not found");
|
||||||
)
|
|
||||||
throw new NotFoundException("Share not found");
|
|
||||||
|
|
||||||
if (share.security?.password && !shareToken)
|
if (share.security?.password && !shareToken)
|
||||||
throw new ForbiddenException(
|
throw new ForbiddenException(
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import {
|
import {
|
||||||
CanActivate,
|
CanActivate,
|
||||||
ExecutionContext,
|
ExecutionContext,
|
||||||
ForbiddenException,
|
|
||||||
Injectable,
|
Injectable,
|
||||||
NotFoundException,
|
NotFoundException,
|
||||||
} from "@nestjs/common";
|
} from "@nestjs/common";
|
||||||
|
@ -27,18 +26,10 @@ export class ShareTokenSecurity implements CanActivate {
|
||||||
include: { security: true },
|
include: { security: true },
|
||||||
});
|
});
|
||||||
|
|
||||||
if (
|
const isExpired =
|
||||||
!share ||
|
moment().isAfter(share.expiration) && !moment(share.expiration).isSame(0);
|
||||||
(moment().isAfter(share.expiration) &&
|
|
||||||
!moment(share.expiration).isSame(0))
|
|
||||||
)
|
|
||||||
throw new NotFoundException("Share not found");
|
|
||||||
|
|
||||||
if (share.security?.maxViews && share.security.maxViews <= share.views)
|
if (!share || isExpired) throw new NotFoundException("Share not found");
|
||||||
throw new ForbiddenException(
|
|
||||||
"Maximum views exceeded",
|
|
||||||
"share_max_views_exceeded"
|
|
||||||
);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -273,8 +273,16 @@ export class ShareService {
|
||||||
if (
|
if (
|
||||||
share?.security?.password &&
|
share?.security?.password &&
|
||||||
!(await argon.verify(share.security.password, password))
|
!(await argon.verify(share.security.password, password))
|
||||||
)
|
) {
|
||||||
throw new ForbiddenException("Wrong password");
|
throw new ForbiddenException("Wrong password");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (share.security?.maxViews && share.security.maxViews <= share.views) {
|
||||||
|
throw new ForbiddenException(
|
||||||
|
"Maximum views exceeded",
|
||||||
|
"share_max_views_exceeded"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const token = await this.generateShareToken(shareId);
|
const token = await this.generateShareToken(shareId);
|
||||||
await this.increaseViewCount(share);
|
await this.increaseViewCount(share);
|
||||||
|
|
Loading…
Add table
Reference in a new issue