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

Commit

Permalink
♻️ Use TWT instead of username
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Aug 4, 2020
1 parent 22ce341 commit 91ac3cc
Show file tree
Hide file tree
Showing 18 changed files with 96 additions and 121 deletions.
5 changes: 5 additions & 0 deletions src/_staart/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ import { isMatch } from "@staart/text";
import { Joi, joiValidate } from "@staart/validate";
import dns from "dns";
import { Tokens } from "../interfaces/enum";
import { verify } from "twt";
import { ApiKeyResponse } from "./jwt";
import { config } from "@anandchowdhary/cosmic";

/**
* Make s single property optional
* @source https://stackoverflow.com/a/54178819/1656944
*/
export type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;

export const twtToId = (twt: string) =>
parseInt(verify(twt, config("twtSecret")), 10);

/**
* Delete any sensitive information for a user like passwords and tokens
*/
Expand Down
17 changes: 7 additions & 10 deletions src/controllers/organizations/_id/api-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ import {
} from "@staart/server";
import { Joi, joiValidate } from "@staart/validate";
import { authHandler, validator } from "../../../_staart/helpers/middleware";
import {
groupUsernameToId,
localsToTokenOrKey,
} from "../../../_staart/helpers/utils";
import { twtToId, localsToTokenOrKey } from "../../../_staart/helpers/utils";
import {
createApiKeyForUser,
deleteApiKeyForUser,
Expand All @@ -33,7 +30,7 @@ import {
export class GroupApiKeysController {
@Get()
async getUserApiKeys(req: Request, res: Response) {
const id = await groupUsernameToId(req.params.id);
const id = twtToId(req.params.id);
joiValidate({ id: Joi.string().required() }, { id });
return getGroupApiKeysForUser(localsToTokenOrKey(res), id, req.query);
}
Expand All @@ -52,7 +49,7 @@ export class GroupApiKeysController {
)
)
async putUserApiKeys(req: Request, res: Response) {
const id = await groupUsernameToId(req.params.id);
const id = twtToId(req.params.id);
joiValidate({ id: Joi.string().required() }, { id });
const added = await createApiKeyForUser(
localsToTokenOrKey(res),
Expand All @@ -65,7 +62,7 @@ export class GroupApiKeysController {

@Get(":apiKeyId")
async getUserApiKey(req: Request, res: Response) {
const id = await groupUsernameToId(req.params.id);
const id = twtToId(req.params.id);
const apiKeyId = req.params.apiKeyId;
joiValidate(
{
Expand All @@ -91,7 +88,7 @@ export class GroupApiKeysController {
)
)
async patchUserApiKey(req: Request, res: Response) {
const id = await groupUsernameToId(req.params.id);
const id = twtToId(req.params.id);
const apiKeyId = req.params.apiKeyId;
joiValidate(
{
Expand All @@ -112,7 +109,7 @@ export class GroupApiKeysController {

@Delete(":apiKeyId")
async deleteUserApiKey(req: Request, res: Response) {
const id = await groupUsernameToId(req.params.id);
const id = twtToId(req.params.id);
const apiKeyId = req.params.apiKeyId;
joiValidate(
{
Expand All @@ -132,7 +129,7 @@ export class GroupApiKeysController {

@Get(":apiKeyId/logs")
async getUserApiKeyLogs(req: Request, res: Response) {
const id = await groupUsernameToId(req.params.id);
const id = twtToId(req.params.id);
const apiKeyId = req.params.apiKeyId;
joiValidate(
{
Expand Down
11 changes: 4 additions & 7 deletions src/controllers/organizations/_id/billing.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { ClassMiddleware, Get, Patch, Request, Response } from "@staart/server";
import { Joi, joiValidate } from "@staart/validate";
import { authHandler } from "../../../_staart/helpers/middleware";
import {
groupUsernameToId,
localsToTokenOrKey,
} from "../../../_staart/helpers/utils";
import { twtToId, localsToTokenOrKey } from "../../../_staart/helpers/utils";
import {
getGroupBillingForUser,
getGroupPricingPlansForUser,
Expand All @@ -15,14 +12,14 @@ import {
export class GroupBillingController {
@Get()
async getBilling(req: Request, res: Response) {
const groupId = await groupUsernameToId(req.params.id);
const groupId = twtToId(req.params.id);
joiValidate({ groupId: Joi.string().required() }, { groupId });
return getGroupBillingForUser(localsToTokenOrKey(res), groupId);
}

@Patch()
async patchBilling(req: Request, res: Response) {
const groupId = await groupUsernameToId(req.params.id);
const groupId = twtToId(req.params.id);
joiValidate({ groupId: Joi.string().required() }, { groupId });
await updateGroupBillingForUser(
localsToTokenOrKey(res),
Expand All @@ -35,7 +32,7 @@ export class GroupBillingController {

@Get("pricing")
async getPlans(req: Request, res: Response) {
const groupId = await groupUsernameToId(req.params.id);
const groupId = twtToId(req.params.id);
joiValidate(
{
groupId: Joi.string().required(),
Expand Down
17 changes: 7 additions & 10 deletions src/controllers/organizations/_id/domains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ import {
} from "@staart/server";
import { Joi, joiValidate } from "@staart/validate";
import { authHandler, validator } from "../../../_staart/helpers/middleware";
import {
groupUsernameToId,
localsToTokenOrKey,
} from "../../../_staart/helpers/utils";
import { twtToId, localsToTokenOrKey } from "../../../_staart/helpers/utils";
import {
createDomainForUser,
deleteDomainForUser,
Expand All @@ -34,7 +31,7 @@ import {
export class GroupDomainsController {
@Get()
async getUserDomains(req: Request, res: Response) {
const id = await groupUsernameToId(req.params.id);
const id = twtToId(req.params.id);
joiValidate({ id: Joi.string().required() }, { id });
return getGroupDomainsForUser(localsToTokenOrKey(res), id, req.query);
}
Expand All @@ -49,7 +46,7 @@ export class GroupDomainsController {
)
)
async putUserDomains(req: Request, res: Response) {
const id = await groupUsernameToId(req.params.id);
const id = twtToId(req.params.id);
joiValidate({ id: Joi.string().required() }, { id });
const added = await createDomainForUser(
localsToTokenOrKey(res),
Expand All @@ -62,7 +59,7 @@ export class GroupDomainsController {

@Get(":domainId")
async getUserDomain(req: Request, res: Response) {
const id = await groupUsernameToId(req.params.id);
const id = twtToId(req.params.id);
const domainId = req.params.domainId;
joiValidate(
{
Expand All @@ -84,7 +81,7 @@ export class GroupDomainsController {
)
)
async patchUserDomain(req: Request, res: Response) {
const id = await groupUsernameToId(req.params.id);
const id = twtToId(req.params.id);
const domainId = req.params.domainId;
joiValidate(
{
Expand All @@ -105,7 +102,7 @@ export class GroupDomainsController {

@Delete(":domainId")
async deleteUserDomain(req: Request, res: Response) {
const id = await groupUsernameToId(req.params.id);
const id = twtToId(req.params.id);
const domainId = req.params.domainId;
joiValidate(
{
Expand All @@ -125,7 +122,7 @@ export class GroupDomainsController {

@Post(":domainId/verify")
async verifyGroupDomain(req: Request, res: Response) {
const id = await groupUsernameToId(req.params.id);
const id = twtToId(req.params.id);
const domainId = req.params.domainId;
const method = req.body.method || req.query.method;
joiValidate(
Expand Down
13 changes: 5 additions & 8 deletions src/controllers/organizations/_id/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import {
} from "@staart/server";
import { Joi, joiValidate } from "@staart/validate";
import { authHandler, validator } from "../../../_staart/helpers/middleware";
import {
groupUsernameToId,
localsToTokenOrKey,
} from "../../../_staart/helpers/utils";
import { twtToId, localsToTokenOrKey } from "../../../_staart/helpers/utils";
import {
deleteGroupForUser,
getAllGroupDataForUser,
Expand All @@ -25,7 +22,7 @@ import {
export class GroupController {
@Get()
async get(req: Request, res: Response) {
const id = await groupUsernameToId(req.params.id);
const id = twtToId(req.params.id);
joiValidate({ id: Joi.string().required() }, { id });
const group = await getGroupForUser(localsToTokenOrKey(res), id);
return group;
Expand All @@ -47,7 +44,7 @@ export class GroupController {
)
)
async patch(req: Request, res: Response) {
const id = await groupUsernameToId(req.params.id);
const id = twtToId(req.params.id);
joiValidate({ id: Joi.string().required() }, { id });
const updated = await updateGroupForUser(
localsToTokenOrKey(res),
Expand All @@ -60,15 +57,15 @@ export class GroupController {

@Delete()
async delete(req: Request, res: Response) {
const groupId = await groupUsernameToId(req.params.id);
const groupId = twtToId(req.params.id);
joiValidate({ groupId: Joi.string().required() }, { groupId });
await deleteGroupForUser(res.locals.token.id, groupId, res.locals);
return respond(RESOURCE_DELETED);
}

@Get("data")
async getData(req: Request, res: Response) {
const groupId = await groupUsernameToId(req.params.id);
const groupId = twtToId(req.params.id);
joiValidate({ groupId: Joi.string().required() }, { groupId });
return getAllGroupDataForUser(localsToTokenOrKey(res), groupId);
}
Expand Down
9 changes: 3 additions & 6 deletions src/controllers/organizations/_id/invoices.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { ClassMiddleware, Get, Request, Response } from "@staart/server";
import { Joi, joiValidate } from "@staart/validate";
import { authHandler } from "../../../_staart/helpers/middleware";
import {
groupUsernameToId,
localsToTokenOrKey,
} from "../../../_staart/helpers/utils";
import { twtToId, localsToTokenOrKey } from "../../../_staart/helpers/utils";
import {
getGroupInvoiceForUser,
getGroupInvoicesForUser,
Expand All @@ -14,7 +11,7 @@ import {
export class GroupInvoicesController {
@Get()
async getInvoices(req: Request, res: Response) {
const groupId = await groupUsernameToId(req.params.id);
const groupId = twtToId(req.params.id);
joiValidate({ groupId: Joi.string().required() }, { groupId });
const subscriptionParams = { ...req.query };
joiValidate(
Expand All @@ -36,7 +33,7 @@ export class GroupInvoicesController {

@Get(":invoiceId")
async getInvoice(req: Request, res: Response) {
const groupId = await groupUsernameToId(req.params.id);
const groupId = twtToId(req.params.id);
const invoiceId = req.params.invoiceId;
joiValidate(
{
Expand Down
15 changes: 6 additions & 9 deletions src/controllers/organizations/_id/memberships.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ import {
} from "@staart/server";
import { Joi, joiValidate } from "@staart/validate";
import { authHandler, validator } from "../../../_staart/helpers/middleware";
import {
groupUsernameToId,
localsToTokenOrKey,
} from "../../../_staart/helpers/utils";
import { twtToId, localsToTokenOrKey } from "../../../_staart/helpers/utils";
import {
deleteGroupMembershipForUser,
getGroupMembershipForUser,
Expand All @@ -33,7 +30,7 @@ import {
export class GroupMembershipsController {
@Get()
async getMemberships(req: Request, res: Response) {
const groupId = await groupUsernameToId(req.params.id);
const groupId = twtToId(req.params.id);
joiValidate({ groupId: Joi.string().required() }, { groupId });
return getGroupMembershipsForUser(
localsToTokenOrKey(res),
Expand All @@ -44,7 +41,7 @@ export class GroupMembershipsController {

@Put()
async putMemberships(req: Request, res: Response) {
const groupId = await groupUsernameToId(req.params.id);
const groupId = twtToId(req.params.id);
const newMemberName = req.body.name;
const newMemberEmail = req.body.email;
const role = req.body.role;
Expand Down Expand Up @@ -75,7 +72,7 @@ export class GroupMembershipsController {

@Get(":membershipId")
async getMembership(req: Request, res: Response) {
const groupId = await groupUsernameToId(req.params.id);
const groupId = twtToId(req.params.id);
const membershipId = req.params.membershipId;
joiValidate(
{
Expand All @@ -101,7 +98,7 @@ export class GroupMembershipsController {
)
)
async updateMembership(req: Request, res: Response) {
const groupId = await groupUsernameToId(req.params.id);
const groupId = twtToId(req.params.id);
const membershipId = req.params.membershipId;
joiValidate(
{
Expand All @@ -121,7 +118,7 @@ export class GroupMembershipsController {

@Delete(":membershipId")
async deleteMembership(req: Request, res: Response) {
const groupId = await groupUsernameToId(req.params.id);
const groupId = twtToId(req.params.id);
const membershipId = req.params.membershipId;
joiValidate(
{
Expand Down
15 changes: 6 additions & 9 deletions src/controllers/organizations/_id/sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ import {
} from "@staart/server";
import { Joi, joiValidate } from "@staart/validate";
import { authHandler } from "../../../_staart/helpers/middleware";
import {
groupUsernameToId,
localsToTokenOrKey,
} from "../../../_staart/helpers/utils";
import { twtToId, localsToTokenOrKey } from "../../../_staart/helpers/utils";
import {
createGroupSourceForUser,
deleteGroupSourceForUser,
Expand All @@ -31,7 +28,7 @@ import {
export class GroupSourcesController {
@Get()
async getSources(req: Request, res: Response) {
const groupId = await groupUsernameToId(req.params.id);
const groupId = twtToId(req.params.id);
joiValidate({ groupId: Joi.string().required() }, { groupId });
const subscriptionParams = { ...req.query };
joiValidate(
Expand All @@ -50,7 +47,7 @@ export class GroupSourcesController {

@Put()
async putSources(req: Request, res: Response) {
const groupId = await groupUsernameToId(req.params.id);
const groupId = twtToId(req.params.id);
joiValidate({ groupId: Joi.string().required() }, { groupId });
await createGroupSourceForUser(
localsToTokenOrKey(res),
Expand All @@ -63,7 +60,7 @@ export class GroupSourcesController {

@Get(":sourceId")
async getSource(req: Request, res: Response) {
const groupId = await groupUsernameToId(req.params.id);
const groupId = twtToId(req.params.id);
const sourceId = req.params.sourceId;
joiValidate(
{
Expand All @@ -78,7 +75,7 @@ export class GroupSourcesController {
@Patch(":sourceId")
async patchSource(req: Request, res: Response) {
const sourceId = req.params.sourceId;
const groupId = await groupUsernameToId(req.params.id);
const groupId = twtToId(req.params.id);
joiValidate(
{
groupId: Joi.string().required(),
Expand All @@ -99,7 +96,7 @@ export class GroupSourcesController {
@Delete(":sourceId")
async deleteSource(req: Request, res: Response) {
const sourceId = req.params.sourceId;
const groupId = await groupUsernameToId(req.params.id);
const groupId = twtToId(req.params.id);
joiValidate(
{
groupId: Joi.string().required(),
Expand Down
Loading

0 comments on commit 91ac3cc

Please sign in to comment.