Skip to content

Commit

Permalink
fix validate token
Browse files Browse the repository at this point in the history
  • Loading branch information
thanhdanh27600 committed Oct 13, 2023
1 parent e30df60 commit 5ddff0c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/components/atoms/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const Modal = (props: Props) => {
{...props.ModalProps}>
<div
data-te-modal-dialog-ref
className="pointer-events-none relative flex min-h-full w-auto translate-y-[-50px] items-center opacity-0 transition-all duration-300 ease-in-out min-[576px]:mx-auto min-[576px]:mt-7 min-[576px]:min-h-full min-[576px]:max-w-[500px]">
className="pointer-events-none relative flex min-h-full w-auto translate-y-[-50px] items-center opacity-0 transition-all duration-300 ease-in-out max-[576px]:mt-12 min-[576px]:mx-auto min-[576px]:min-h-full min-[576px]:max-w-[500px]">
<div className="min-[576px]:shadow-[0_0.5rem_1rem_rgba(#000, 0.15)] pointer-events-auto relative mx-auto flex w-[95%] flex-col rounded-md border-none bg-white bg-clip-padding text-current shadow-lg outline-none">
<div className="flex flex-shrink-0 items-center justify-between rounded-t-md border-b-2 border-neutral-100 border-opacity-100 p-4">
<h5 className="text-xl font-medium leading-normal text-neutral-800" id={`${props.id}Label`}>
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/forward.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const handler = api<Forward>(
const hashKey = getRedisKey(REDIS_KEY.MAP_SHORTEN_BY_HASH, hash);
const shortenedUrlCache = (await redis.hgetall(hashKey)) as any;
if (!isEmpty(shortenedUrlCache)) {
valid = await shortenService.verifyToken(shortenedUrlCache, token);
valid = shortenService.verifyToken(shortenedUrlCache, token);
if (!valid) return res.send({ errorCode: HttpStatusCode.UNAUTHORIZED, errorMessage: 'UNAUTHORIZED' });
// cache hit
sendMessageToQueue([{ subject: 'forward', body: data }]);
Expand All @@ -64,7 +64,7 @@ export const handler = api<Forward>(
if (!history) {
return badRequest(res);
}
valid = await shortenService.verifyToken(history, token);
valid = shortenService.verifyToken(history, token);
if (!valid) return res.send({ errorCode: HttpStatusCode.UNAUTHORIZED, errorMessage: 'UNAUTHORIZED' });
sendMessageToQueue([{ subject: 'forward', body: data }]);
shortenCacheService.postShortenHash(history);
Expand Down
8 changes: 6 additions & 2 deletions src/controllers/stats/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Prisma } from '@prisma/client';
import requestIp from 'request-ip';
import prisma from '../../services/db/prisma';
import { shortenService } from '../../services/shorten';
import { LIMIT_RECENT_HISTORY } from '../../types/constants';
import { Stats } from '../../types/stats';
import { api, errorHandler, successHandler } from '../../utils/axios';
import { decryptS } from '../../utils/crypto';
import { parseIntSafe } from '../../utils/number';
import { withQueryCursor } from '../../utils/requests';
import { validateStatsSchema } from '../../utils/validateMiddleware';
Expand Down Expand Up @@ -54,7 +54,11 @@ export const handler = api<Stats>(
},
});
if (history && history?.password) {
const valid = await shortenService.verifyToken(history, req.headers['X-Platform-Auth'.toLowerCase()] as string);
let valid = false;
const token = req.headers['X-Platform-Auth'.toLowerCase()] as string;
if (decryptS(token) === history.id.toString()) {
valid = true;
}
if (!valid) {
return errorHandler(res);
}
Expand Down
6 changes: 3 additions & 3 deletions src/services/shorten/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ export class ShortenService {
async verifyPassword(history: UrlShortenerHistory | null, password: string) {
if (!history) return false;
if (!history.password) return true;
if (!history.usePasswordForward) return true;
const decryptPassword = decryptS(history.password);
return decryptPassword === password;
}
async verifyToken(history: UrlShortenerHistory | null, token: string) {
verifyToken(history: UrlShortenerHistory | null, token: string) {
if (!history) return false;
if (!history.password) return true;
if (!history.usePasswordForward) return true;
if (!token || decryptS(token) !== history.id.toString()) {
if (!token) return false;
if (decryptS(token) !== history.id.toString()) {
return false;
}
return true;
Expand Down

0 comments on commit 5ddff0c

Please sign in to comment.