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

Commit

Permalink
♻️ Use number ID for user, validate number
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Aug 4, 2020
1 parent f883ec6 commit 5edf233
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 77 deletions.
11 changes: 7 additions & 4 deletions src/_staart/helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import { config } from "@anandchowdhary/cosmic";
import { users } from "@prisma/client";
import { Request, Response } from "@staart/server";
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 { Tokens } from "../interfaces/enum";
import { Locals } from "../interfaces/general";
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);
export const twtToId = (twt: string, userId?: number) =>
twt === "me" && userId
? userId
: parseInt(verify(twt, config("twtSecret")), 10);

/**
* Delete any sensitive information for a user like passwords and tokens
Expand Down
26 changes: 13 additions & 13 deletions src/controllers/users/_id/access-tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import {
export class UserAccessTokensController {
@Get()
async getUserAccessTokens(req: Request, res: Response) {
const id = twtToId(req.params.id);
joiValidate({ id: Joi.string().required() }, { id });
const id = twtToId(req.params.id, res.locals.token.id);
joiValidate({ id: Joi.number().required() }, { id });
return getUserAccessTokensForUser(res.locals.token.id, id, req.query);
}

Expand All @@ -47,8 +47,8 @@ export class UserAccessTokensController {
)
)
async putUserAccessTokens(req: Request, res: Response) {
const id = twtToId(req.params.id);
joiValidate({ id: Joi.string().required() }, { id });
const id = twtToId(req.params.id, res.locals.token.id);
joiValidate({ id: Joi.number().required() }, { id });
try {
const added = await createAccessTokenForUser(
res.locals.token.id,
Expand All @@ -65,12 +65,12 @@ export class UserAccessTokensController {

@Get(":accessTokenId")
async getUserAccessToken(req: Request, res: Response) {
const id = twtToId(req.params.id);
const id = twtToId(req.params.id, res.locals.token.id);
const accessTokenId = twtToId(req.params.accessTokenId);
joiValidate(
{
id: Joi.string().required(),
accessTokenId: Joi.string().required(),
id: Joi.number().required(),
accessTokenId: Joi.number().required(),
},
{ id, accessTokenId }
);
Expand All @@ -90,12 +90,12 @@ export class UserAccessTokensController {
)
)
async patchUserAccessToken(req: Request, res: Response) {
const id = twtToId(req.params.id);
const id = twtToId(req.params.id, res.locals.token.id);
const accessTokenId = twtToId(req.params.accessTokenId);
joiValidate(
{
id: Joi.string().required(),
accessTokenId: Joi.string().required(),
id: Joi.number().required(),
accessTokenId: Joi.number().required(),
},
{ id, accessTokenId }
);
Expand All @@ -111,12 +111,12 @@ export class UserAccessTokensController {

@Delete(":accessTokenId")
async deleteUserAccessToken(req: Request, res: Response) {
const id = twtToId(req.params.id);
const id = twtToId(req.params.id, res.locals.token.id);
const accessTokenId = twtToId(req.params.accessTokenId);
joiValidate(
{
id: Joi.string().required(),
accessTokenId: Joi.string().required(),
id: Joi.number().required(),
accessTokenId: Joi.number().required(),
},
{ id, accessTokenId }
);
Expand Down
26 changes: 13 additions & 13 deletions src/controllers/users/_id/emails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ import {
export class UserEmailsController {
@Get()
async getEmails(req: Request, res: Response) {
const id = twtToId(req.params.id);
joiValidate({ id: Joi.string().required() }, { id });
const id = twtToId(req.params.id, res.locals.token.id);
joiValidate({ id: Joi.number().required() }, { id });
return getAllEmailsForUser(res.locals.token.id, id, req.query);
}

@Put()
async putEmails(req: Request, res: Response) {
const id = twtToId(req.params.id);
const id = twtToId(req.params.id, res.locals.token.id);
const email = req.body.email;
joiValidate(
{
id: Joi.string().required(),
id: Joi.number().required(),
email: Joi.string().email().required(),
},
{ id, email }
Expand All @@ -55,12 +55,12 @@ export class UserEmailsController {

@Get(":emailId")
async getEmail(req: Request, res: Response) {
const id = twtToId(req.params.id);
const id = twtToId(req.params.id, res.locals.token.id);
const emailId = twtToId(req.params.emailId);
joiValidate(
{
id: Joi.string().required(),
emailId: Joi.string().required(),
id: Joi.number().required(),
emailId: Joi.number().required(),
},
{ id, emailId }
);
Expand All @@ -69,12 +69,12 @@ export class UserEmailsController {

@Post(":emailId/resend")
async postResend(req: Request, res: Response) {
const id = twtToId(req.params.id);
const id = twtToId(req.params.id, res.locals.token.id);
const emailId = twtToId(req.params.emailId);
joiValidate(
{
id: Joi.string().required(),
emailId: Joi.string().required(),
id: Joi.number().required(),
emailId: Joi.number().required(),
},
{ id, emailId }
);
Expand All @@ -84,12 +84,12 @@ export class UserEmailsController {

@Delete(":emailId")
async deleteEmail(req: Request, res: Response) {
const id = twtToId(req.params.id);
const id = twtToId(req.params.id, res.locals.token.id);
const emailId = twtToId(req.params.emailId);
joiValidate(
{
id: Joi.string().required(),
emailId: Joi.string().required(),
id: Joi.number().required(),
emailId: Joi.number().required(),
},
{ id, emailId }
);
Expand Down
20 changes: 10 additions & 10 deletions src/controllers/users/_id/identities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ import {
export class UserIdentitiesController {
@Get()
async getUserIdentities(req: Request, res: Response) {
const id = twtToId(req.params.id);
joiValidate({ id: Joi.string().required() }, { id });
const id = twtToId(req.params.id, res.locals.token.id);
joiValidate({ id: Joi.number().required() }, { id });
return getUserIdentitiesForUser(res.locals.token.id, id, req.query);
}

@Put()
async createUserIdentity(req: Request, res: Response) {
const id = twtToId(req.params.id);
joiValidate({ id: Joi.string().required() }, { id });
const id = twtToId(req.params.id, res.locals.token.id);
joiValidate({ id: Joi.number().required() }, { id });
const added = await createUserIdentityForUser(
res.locals.token.id,
id,
Expand All @@ -47,8 +47,8 @@ export class UserIdentitiesController {

@Post(":service")
async connectUserIdentity(req: Request, res: Response) {
const id = twtToId(req.params.id);
joiValidate({ id: Joi.string().required() }, { id });
const id = twtToId(req.params.id, res.locals.token.id);
joiValidate({ id: Joi.number().required() }, { id });
const service = req.params.service;
const url = req.body.url;
joiValidate(
Expand All @@ -61,12 +61,12 @@ export class UserIdentitiesController {

@Get(":identityId")
async getUserIdentity(req: Request, res: Response) {
const id = twtToId(req.params.id);
const id = twtToId(req.params.id, res.locals.token.id);
const identityId = twtToId(req.params.identityId);
joiValidate(
{
id: Joi.string().required(),
identityId: Joi.string().required(),
identityId: Joi.number().required(),
},
{ id, identityId }
);
Expand All @@ -75,12 +75,12 @@ export class UserIdentitiesController {

@Delete(":identityId")
async deleteUserIdentity(req: Request, res: Response) {
const id = twtToId(req.params.id);
const id = twtToId(req.params.id, res.locals.token.id);
const identityId = twtToId(req.params.identityId);
joiValidate(
{
id: Joi.string().required(),
identityId: Joi.string().required(),
identityId: Joi.number().required(),
},
{ id, identityId }
);
Expand Down
12 changes: 6 additions & 6 deletions src/controllers/users/_id/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import {
export class UserController {
@Get()
async get(req: Request, res: Response) {
const id = twtToId(req.params.id);
joiValidate({ id: Joi.string().required() }, { id });
const id = twtToId(req.params.id, res.locals.token.id);
joiValidate({ id: Joi.number().required() }, { id });
return getUserFromIdForUser(id, res.locals.token.id, req.query);
}

Expand Down Expand Up @@ -59,8 +59,8 @@ export class UserController {
)
)
async patch(req: Request, res: Response) {
const id = twtToId(req.params.id);
joiValidate({ id: Joi.string().required() }, { id });
const id = twtToId(req.params.id, res.locals.token.id);
joiValidate({ id: Joi.number().required() }, { id });
const updated = await updateUserForUser(
res.locals.token.id,
id,
Expand All @@ -72,8 +72,8 @@ export class UserController {

@Delete()
async delete(req: Request, res: Response) {
const id = twtToId(req.params.id);
joiValidate({ id: Joi.string().required() }, { id });
const id = twtToId(req.params.id, res.locals.token.id);
joiValidate({ id: Joi.number().required() }, { id });
await deleteUserForUser(res.locals.token.id, id, res.locals);
return respond(RESOURCE_DELETED);
}
Expand Down
22 changes: 11 additions & 11 deletions src/controllers/users/_id/memberships.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ import {
export class UserMembershipsController {
@Get()
async getMemberships(req: Request, res: Response) {
const id = twtToId(req.params.id);
joiValidate({ id: Joi.string().required() }, { id });
const id = twtToId(req.params.id, res.locals.token.id);
joiValidate({ id: Joi.number().required() }, { id });
return getMembershipsForUser(res.locals.token.id, id, req.query);
}

@Get(":membershipId")
async getMembership(req: Request, res: Response) {
const id = twtToId(req.params.id);
const id = twtToId(req.params.id, res.locals.token.id);
const membershipId = twtToId(req.params.membershipId);
joiValidate(
{
id: Joi.string().required(),
membershipId: Joi.string().required(),
id: Joi.number().required(),
membershipId: Joi.number().required(),
},
{ id, membershipId }
);
Expand All @@ -42,12 +42,12 @@ export class UserMembershipsController {

@Delete(":membershipId")
async deleteMembership(req: Request, res: Response) {
const id = twtToId(req.params.id);
const id = twtToId(req.params.id, res.locals.token.id);
const membershipId = twtToId(req.params.membershipId);
joiValidate(
{
id: Joi.string().required(),
membershipId: Joi.string().required(),
id: Joi.number().required(),
membershipId: Joi.number().required(),
},
{ id, membershipId }
);
Expand All @@ -57,12 +57,12 @@ export class UserMembershipsController {

@Patch(":membershipId")
async updateMembership(req: Request, res: Response) {
const id = twtToId(req.params.id);
const id = twtToId(req.params.id, res.locals.token.id);
const membershipId = twtToId(req.params.membershipId);
joiValidate(
{
id: Joi.string().required(),
membershipId: Joi.string().required(),
id: Joi.number().required(),
membershipId: Joi.number().required(),
},
{ id, membershipId }
);
Expand Down
24 changes: 12 additions & 12 deletions src/controllers/users/_id/security.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ export class UserSecurityController {
)
)
async updatePassword(req: Request, res: Response) {
const id = twtToId(req.params.id);
const id = twtToId(req.params.id, res.locals.token.id);
const oldPassword = req.body.oldPassword;
const newPassword = req.body.newPassword;
joiValidate(
{
id: Joi.string().required(),
id: Joi.number().required(),
},
{ id }
);
Expand All @@ -55,25 +55,25 @@ export class UserSecurityController {

@Get("data")
async getUserData(req: Request, res: Response) {
const id = twtToId(req.params.id);
joiValidate({ id: Joi.string().required() }, { id });
const id = twtToId(req.params.id, res.locals.token.id);
joiValidate({ id: Joi.number().required() }, { id });
return getAllDataForUser(res.locals.token.id, id);
}

@Get("2fa/enable")
async getEnable2FA(req: Request, res: Response) {
const id = twtToId(req.params.id);
joiValidate({ id: Joi.string().required() }, { id });
const id = twtToId(req.params.id, res.locals.token.id);
joiValidate({ id: Joi.number().required() }, { id });
return enable2FAForUser(res.locals.token.id, id);
}

@Post("2fa/verify")
async postVerify2FA(req: Request, res: Response) {
const id = twtToId(req.params.id);
const id = twtToId(req.params.id, res.locals.token.id);
const code = req.body.code;
joiValidate(
{
id: Joi.string().required(),
id: Joi.number().required(),
code: Joi.number().min(5).required(),
},
{ id, code }
Expand All @@ -84,16 +84,16 @@ export class UserSecurityController {

@Delete("2fa")
async delete2FA(req: Request, res: Response) {
const id = twtToId(req.params.id);
joiValidate({ id: Joi.string().required() }, { id });
const id = twtToId(req.params.id, res.locals.token.id);
joiValidate({ id: Joi.number().required() }, { id });
await disable2FAForUser(res.locals.token.id, id);
return respond(RESOURCE_SUCCESS);
}

@Get("backup-codes/regenerate")
async getRegenerateBackupCodes(req: Request, res: Response) {
const id = twtToId(req.params.id);
joiValidate({ id: Joi.string().required() }, { id });
const id = twtToId(req.params.id, res.locals.token.id);
joiValidate({ id: Joi.number().required() }, { id });
const backupCodes = await regenerateBackupCodesForUser(
res.locals.token.id,
id
Expand Down
Loading

0 comments on commit 5edf233

Please sign in to comment.