Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
✨ Add Redis helper and handler
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Sep 30, 2019
1 parent 705b3ac commit fc4e2bc
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "staart-manager",
"version": "1.1.28",
"version": "1.1.29",
"main": "index.js",
"repository": "[email protected]:AnandChowdhary/staart.git",
"author": "Anand Chowdhary <[email protected]>",
Expand Down Expand Up @@ -144,5 +144,5 @@
"setup"
],
"snyk": true,
"staart-version": "1.1.28"
"staart-version": "1.1.29"
}
15 changes: 6 additions & 9 deletions src/helpers/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {
TOKEN_EXPIRY_LOGIN,
TOKEN_EXPIRY_REFRESH,
TOKEN_EXPIRY_APPROVE_LOCATION,
TOKEN_EXPIRY_API_KEY_MAX,
REDIS_URL
TOKEN_EXPIRY_API_KEY_MAX
} from "../config";
import { User, AccessToken } from "../interfaces/tables/user";
import { Tokens, ErrorCode, EventType, Templates } from "../interfaces/enum";
Expand All @@ -33,8 +32,8 @@ import { getGeolocationFromIp } from "./location";
import i18n from "../i18n";
import { ApiKey } from "../interfaces/tables/organization";
import cryptoRandomString from "crypto-random-string";
import { createHandyClient } from "handy-redis";
import ipRangeCheck from "ip-range-check";
import { redis } from "./redis";

/**
* Generate a new JWT
Expand Down Expand Up @@ -239,21 +238,18 @@ export const getLoginResponse = async (
return await postLoginTokens(user, locals);
};

const client = createHandyClient({
url: REDIS_URL
});

/**
* Check if a token is invalidated in Redis
* @param token - JWT
*/
export const checkInvalidatedToken = async (token: string) => {
if (!redis) return;
const details = decode(token);
if (
details &&
typeof details === "object" &&
details.jti &&
(await client.get(`${JWT_ISSUER}-revoke-${details.sub}-${details.jti}`))
(await redis.get(`${JWT_ISSUER}-revoke-${details.sub}-${details.jti}`))
)
throw new Error(ErrorCode.REVOKED_TOKEN);
};
Expand All @@ -263,9 +259,10 @@ export const checkInvalidatedToken = async (token: string) => {
* @param token - JWT
*/
export const invalidateToken = async (token: string) => {
if (!redis) return;
const details = decode(token);
if (details && typeof details === "object" && details.jti)
client.set(
redis.set(
`${JWT_ISSUER}-revoke-${details.sub}-${details.jti}`,
"1",
details.exp && [
Expand Down
6 changes: 6 additions & 0 deletions src/helpers/redis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { createHandyClient } from "handy-redis";
import { REDIS_URL } from "../config";

export const redis = createHandyClient({
url: REDIS_URL
});
3 changes: 2 additions & 1 deletion src/interfaces/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ export enum ErrorCode {
DOMAIN_MISSING_FILE = "400/domain-missing-file",
DOMAIN_MISSING_DNS = "400/domain-missing-dns",
DOMAIN_ALREADY_VERIFIED = "400/domain-already-verified",
CANNOT_INVITE_DOMAIN = "400/cannot-invite-domain"
CANNOT_INVITE_DOMAIN = "400/cannot-invite-domain",
UNABLE_TO_SEND_EMAIL = "500/unable-to-email"
}

export enum Templates {
Expand Down

0 comments on commit fc4e2bc

Please sign in to comment.