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

Commit

Permalink
✨ Revoke JWT on update/delete
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Jul 18, 2019
1 parent 2f8c29a commit a0440ba
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 49 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "staart-manager",
"version": "1.0.81",
"version": "1.0.82",
"main": "index.js",
"repository": "[email protected]:AnandChowdhary/staart.git",
"author": "Anand Chowdhary <[email protected]>",
Expand Down
2 changes: 1 addition & 1 deletion setup/internal/staart-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.81
1.0.82
5 changes: 4 additions & 1 deletion src/crud/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { cachedQuery, deleteItemFromCache } from "../helpers/cache";
import { CacheCategories, ErrorCode } from "../interfaces/enum";
import { ApiKey } from "../interfaces/tables/organization";
import { getPaginatedData } from "./data";
import { apiKeyToken } from "../helpers/jwt";
import { apiKeyToken, invalidateToken } from "../helpers/jwt";
import { TOKEN_EXPIRY_API_KEY_MAX } from "../config";

/*
Expand Down Expand Up @@ -164,6 +164,7 @@ export const updateApiKey = async (
data.updatedAt = new Date();
data = removeReadOnlyValues(data);
const apiKey = await getApiKey(organizationId, apiKeyId);
if (apiKey.jwtApiKey) await invalidateToken(apiKey.jwtApiKey);
data.jwtApiKey = await apiKeyToken({ ...apiKey, ...data });
deleteItemFromCache(CacheCategories.API_KEY, apiKeyId);
deleteItemFromCache(
Expand All @@ -190,6 +191,8 @@ export const deleteApiKey = async (
CacheCategories.API_KEY_ORG,
`${organizationId}_${apiKeyId}`
);
const currentApiKey = await getApiKey(organizationId, apiKeyId);
if (currentApiKey.jwtApiKey) await invalidateToken(currentApiKey.jwtApiKey);
return await query(
`DELETE FROM ${tableName(
"api-keys"
Expand Down
33 changes: 17 additions & 16 deletions src/helpers/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const authHandler = async (
next: NextFunction
) => {
try {
let userJwt = req.get("Authorization") as string;
let userJwt = req.get("Authorization");
if (userJwt) {
if (userJwt.startsWith("Bearer "))
userJwt = userJwt.replace("Bearer ", "");
Expand All @@ -108,31 +108,32 @@ export const authHandler = async (
if (userToken) res.locals.token = userToken;
}

let apiKeyJwt = req.get("X-Api-Key") as string;
let apiKeyJwt = req.get("X-Api-Key");
if (apiKeyJwt) {
if (apiKeyJwt.startsWith("Bearer "))
apiKeyJwt = apiKeyJwt.replace("Bearer ", "");
const apiKeyToken = (await verifyToken(
apiKeyJwt,
Tokens.API_KEY
)) as ApiKeyResponse;
const referrerDomain = new URL(req.get("Origin") as string).hostname;
await checkInvalidatedToken(apiKeyJwt);
checkIpRestrictions(apiKeyToken, res.locals);
checkReferrerRestrictions(apiKeyToken, referrerDomain);
if (apiKeyToken.referrerRestrictions) {
if (
includesDomainInCommaList(
apiKeyToken.referrerRestrictions,
referrerDomain
)
) {
res.setHeader("Access-Control-Allow-Origin", req.get(
"Origin"
) as string);
const origin = req.get("Origin");
if (origin) {
const referrerDomain = new URL(origin).hostname;
checkReferrerRestrictions(apiKeyToken, referrerDomain);
if (apiKeyToken.referrerRestrictions) {
if (
includesDomainInCommaList(
apiKeyToken.referrerRestrictions,
referrerDomain
)
) {
res.setHeader("Access-Control-Allow-Origin", origin);
}
} else {
res.setHeader("Access-Control-Allow-Origin", "*");
}
} else {
res.setHeader("Access-Control-Allow-Origin", "*");
}
if (apiKeyToken && !res.locals.token) res.locals.token = apiKeyToken;
}
Expand Down
8 changes: 4 additions & 4 deletions src/rest/membership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import {
updateMembership
} from "../crud/membership";
import { User } from "../interfaces/tables/user";
import { ApiKey } from "../interfaces/tables/organization";
import { register } from "./auth";
import { can } from "../helpers/authorization";
import { Locals, KeyValue } from "../interfaces/general";
import { ApiKeyResponse } from "../helpers/jwt";

export const getMembershipDetailsForUser = async (
userId: number,
Expand All @@ -25,7 +25,7 @@ export const getMembershipDetailsForUser = async (
};

export const inviteMemberToOrganization = async (
userId: number | ApiKey,
userId: number | ApiKeyResponse,
organizationId: number,
newMemberName: string,
newMemberEmail: string,
Expand Down Expand Up @@ -72,7 +72,7 @@ export const inviteMemberToOrganization = async (
};

export const deleteMembershipForUser = async (
tokenUserId: number | ApiKey,
tokenUserId: number | ApiKeyResponse,
membershipId: number,
locals: Locals
) => {
Expand All @@ -97,7 +97,7 @@ export const deleteMembershipForUser = async (
};

export const updateMembershipForUser = async (
userId: number | ApiKey,
userId: number | ApiKeyResponse,
membershipId: number,
data: KeyValue,
locals: Locals
Expand Down
52 changes: 26 additions & 26 deletions src/rest/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ import {
createStripeSubscription
} from "../crud/billing";
import { getUser } from "../crud/user";
import { ApiKey } from "../interfaces/tables/organization";
import { getUserPrimaryEmail } from "../crud/email";
import { ApiKeyResponse } from "../helpers/jwt";

export const getOrganizationForUser = async (
userId: number | ApiKey,
userId: number | ApiKeyResponse,
organizationId: number
) => {
if (await can(userId, Authorizations.READ, "organization", organizationId))
Expand Down Expand Up @@ -95,7 +95,7 @@ export const newOrganizationForUser = async (
};

export const updateOrganizationForUser = async (
userId: number | ApiKey,
userId: number | ApiKeyResponse,
organizationId: number,
data: Organization,
locals: Locals
Expand All @@ -110,7 +110,7 @@ export const updateOrganizationForUser = async (
};

export const deleteOrganizationForUser = async (
userId: number | ApiKey,
userId: number | ApiKeyResponse,
organizationId: number,
locals: Locals
) => {
Expand All @@ -128,7 +128,7 @@ export const deleteOrganizationForUser = async (
};

export const getOrganizationBillingForUser = async (
userId: number | ApiKey,
userId: number | ApiKeyResponse,
organizationId: number
) => {
if (await can(userId, Authorizations.READ, "organization", organizationId)) {
Expand All @@ -141,7 +141,7 @@ export const getOrganizationBillingForUser = async (
};

export const updateOrganizationBillingForUser = async (
userId: number | ApiKey,
userId: number | ApiKeyResponse,
organizationId: number,
data: any,
locals: Locals
Expand All @@ -160,7 +160,7 @@ export const updateOrganizationBillingForUser = async (
};

export const getOrganizationInvoicesForUser = async (
userId: number | ApiKey,
userId: number | ApiKeyResponse,
organizationId: number,
params: KeyValue
) => {
Expand All @@ -174,7 +174,7 @@ export const getOrganizationInvoicesForUser = async (
};

export const getOrganizationInvoiceForUser = async (
userId: number | ApiKey,
userId: number | ApiKeyResponse,
organizationId: number,
invoiceId: string
) => {
Expand All @@ -188,7 +188,7 @@ export const getOrganizationInvoiceForUser = async (
};

export const getOrganizationSourcesForUser = async (
userId: number | ApiKey,
userId: number | ApiKeyResponse,
organizationId: number,
params: KeyValue
) => {
Expand All @@ -202,7 +202,7 @@ export const getOrganizationSourcesForUser = async (
};

export const getOrganizationSourceForUser = async (
userId: number | ApiKey,
userId: number | ApiKeyResponse,
organizationId: number,
sourceId: string
) => {
Expand All @@ -216,7 +216,7 @@ export const getOrganizationSourceForUser = async (
};

export const getOrganizationSubscriptionsForUser = async (
userId: number | ApiKey,
userId: number | ApiKeyResponse,
organizationId: number,
params: KeyValue
) => {
Expand All @@ -233,7 +233,7 @@ export const getOrganizationSubscriptionsForUser = async (
};

export const getOrganizationSubscriptionForUser = async (
userId: number | ApiKey,
userId: number | ApiKeyResponse,
organizationId: number,
subscriptionId: string
) => {
Expand All @@ -250,7 +250,7 @@ export const getOrganizationSubscriptionForUser = async (
};

export const updateOrganizationSubscriptionForUser = async (
userId: number | ApiKey,
userId: number | ApiKeyResponse,
organizationId: number,
subscriptionId: string,
data: KeyValue
Expand All @@ -269,7 +269,7 @@ export const updateOrganizationSubscriptionForUser = async (
};

export const createOrganizationSubscriptionForUser = async (
userId: number | ApiKey,
userId: number | ApiKeyResponse,
organizationId: number,
params: { plan: string; [index: string]: any }
) => {
Expand All @@ -286,7 +286,7 @@ export const createOrganizationSubscriptionForUser = async (
};

export const getOrganizationPricingPlansForUser = async (
userId: number | ApiKey,
userId: number | ApiKeyResponse,
organizationId: number,
productId: string
) => {
Expand All @@ -296,7 +296,7 @@ export const getOrganizationPricingPlansForUser = async (
};

export const deleteOrganizationSourceForUser = async (
userId: number | ApiKey,
userId: number | ApiKeyResponse,
organizationId: number,
sourceId: string
) => {
Expand All @@ -310,7 +310,7 @@ export const deleteOrganizationSourceForUser = async (
};

export const updateOrganizationSourceForUser = async (
userId: number | ApiKey,
userId: number | ApiKeyResponse,
organizationId: number,
sourceId: string,
data: any
Expand All @@ -331,7 +331,7 @@ export const updateOrganizationSourceForUser = async (
};

export const createOrganizationSourceForUser = async (
userId: number | ApiKey,
userId: number | ApiKeyResponse,
organizationId: number,
card: any
) => {
Expand All @@ -347,7 +347,7 @@ export const createOrganizationSourceForUser = async (
};

export const getAllOrganizationDataForUser = async (
userId: number | ApiKey,
userId: number | ApiKeyResponse,
organizationId: number
) => {
if (
Expand Down Expand Up @@ -388,7 +388,7 @@ export const getAllOrganizationDataForUser = async (
};

export const getOrganizationRecentEventsForUser = async (
userId: number | ApiKey,
userId: number | ApiKeyResponse,
organizationId: number
) => {
if (await can(userId, Authorizations.READ, "organization", organizationId))
Expand All @@ -397,7 +397,7 @@ export const getOrganizationRecentEventsForUser = async (
};

export const getOrganizationMembershipsForUser = async (
userId: number | ApiKey,
userId: number | ApiKeyResponse,
organizationId: number,
query?: KeyValue
) => {
Expand All @@ -407,7 +407,7 @@ export const getOrganizationMembershipsForUser = async (
};

export const getOrganizationApiKeysForUser = async (
userId: number | ApiKey,
userId: number | ApiKeyResponse,
organizationId: number,
query: KeyValue
) => {
Expand All @@ -424,7 +424,7 @@ export const getOrganizationApiKeysForUser = async (
};

export const getOrganizationApiKeyForUser = async (
userId: number | ApiKey,
userId: number | ApiKeyResponse,
organizationId: number,
apiKeyId: number
) => {
Expand All @@ -441,7 +441,7 @@ export const getOrganizationApiKeyForUser = async (
};

export const updateApiKeyForUser = async (
userId: number | ApiKey,
userId: number | ApiKeyResponse,
organizationId: number,
apiKeyId: number,
data: KeyValue,
Expand All @@ -462,7 +462,7 @@ export const updateApiKeyForUser = async (
};

export const createApiKeyForUser = async (
userId: number | ApiKey,
userId: number | ApiKeyResponse,
organizationId: number,
apiKey: KeyValue,
locals: Locals
Expand All @@ -482,7 +482,7 @@ export const createApiKeyForUser = async (
};

export const deleteApiKeyForUser = async (
userId: number | ApiKey,
userId: number | ApiKeyResponse,
organizationId: number,
apiKeyId: number,
locals: Locals
Expand Down

0 comments on commit a0440ba

Please sign in to comment.