Skip to content

Commit

Permalink
refactor isSignatureValid
Browse files Browse the repository at this point in the history
  • Loading branch information
vh13294 committed Sep 8, 2021
1 parent a6d07ad commit 4a7adce
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 32 deletions.
23 changes: 8 additions & 15 deletions lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ import { PATH_METADATA } from '@nestjs/common/constants';
import { Controller } from '@nestjs/common/interfaces/controllers/controller.interface';

import { BadRequestException } from '@nestjs/common';
import { ControllerMethod } from './interfaces';
import { ControllerMethod, Query, Params } from './interfaces';

export function generateUrl(
appUrl: string,
prefix: string,
relativePath: string,
query: any = {},
params: any = {},
query?: Query,
params?: Params,
): string {
relativePath = putParamsInUrl(relativePath, params);
const path = joinRoutes(appUrl, prefix, relativePath);
const queryString = stringifyQuery(query);
const fullPath = appendQueryParams(path, queryString);
const fullPath = appendQuery(path, queryString);
return fullPath;
}

export function stringifyQuery(query: Record<string, unknown>): string {
export function stringifyQuery(query?: Query): string {
return qsStringify(query);
}

Expand Down Expand Up @@ -53,18 +53,11 @@ export function signatureHasExpired(expirationDate: Date): boolean {
return currentDate > expirationDate;
}

export function isObjectEmpty(obj = {}): boolean {
return Object.keys(obj).length == 0;
}

function isRouteNotEmpty(route: string): boolean {
return !!route && route !== '/';
}

function isParamsNameInUrl(
route: string,
params: Record<string, string>,
): boolean {
function isParamsNameInUrl(route: string, params: Params): boolean {
const routeParts = route
.split('/')
.filter((path) => path[0] === ':')
Expand All @@ -79,14 +72,14 @@ function joinRoutes(...routes: string[]): string {
return routes.filter((route) => isRouteNotEmpty(route)).join('/');
}

function appendQueryParams(route: string, query: string): string {
export function appendQuery(route: string, query: string): string {
if (query) {
return `${route}?${query}`;
}
return route;
}

function putParamsInUrl(route: string, params: Record<string, string>): string {
function putParamsInUrl(route: string, params?: Params): string {
if (params) {
if (isParamsNameInUrl(route, params)) {
for (const [key, value] of Object.entries(params)) {
Expand Down
4 changes: 2 additions & 2 deletions lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { Controller } from '@nestjs/common/interfaces/controllers/controller.int

export type ControllerMethod = (...args: any[]) => Promise<any> | any;

interface Query {
export interface Query {
[key: string]: any;
}
interface Params {
export interface Params {
[key: string]: string;
}

Expand Down
21 changes: 6 additions & 15 deletions lib/url-generator.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { ApplicationConfig } from '@nestjs/core';
import {
Inject,
Injectable,
Logger,
ForbiddenException,
ConflictException,
} from '@nestjs/common';
import { Inject, Injectable, Logger, ForbiddenException } from '@nestjs/common';

import { UrlGeneratorModuleOptions } from './url-generator-options.interface';
import { URL_GENERATOR_MODULE_OPTIONS } from './url-generator.constants';
Expand All @@ -17,7 +11,7 @@ import {
isSignatureEqual,
stringifyQuery,
generateUrl,
isObjectEmpty,
appendQuery,
} from './helpers';

import {
Expand Down Expand Up @@ -108,7 +102,7 @@ export class UrlGeneratorService {
public signUrl({
relativePath,
expirationDate,
query,
query = {}, // empty object avoid undefined error
params,
}: SignUrlArgs): string {
const mappedQuery = query as ReservedQuery;
Expand Down Expand Up @@ -147,14 +141,11 @@ export class UrlGeneratorService {
}: IsSignatureValidArgs): boolean {
const { signed, ...restQuery } = query;

const path = `${protocol}://${host}${routePath}`;
const queryString = stringifyQuery(restQuery);
const fullPath = appendQuery(path, queryString);

let fullUrl = `${protocol}://${host}${routePath}`;
if (!isObjectEmpty(restQuery)) {
fullUrl += `?${queryString}`;
}

const hmac = generateHmac(fullUrl, this.urlGeneratorModuleOptions.secret);
const hmac = generateHmac(fullPath, this.urlGeneratorModuleOptions.secret);

if (!signed || !hmac || signed.length != hmac.length) {
throw new ForbiddenException('Invalid Url');
Expand Down

0 comments on commit 4a7adce

Please sign in to comment.