Skip to content

Commit

Permalink
refactor: send reporter key in auth header
Browse files Browse the repository at this point in the history
  • Loading branch information
eranshmil committed Mar 15, 2019
1 parent fc232bf commit cb6aa8d
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 38 deletions.
22 changes: 18 additions & 4 deletions src/controllers/user-statuses-controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { Body, Controller, Query, Get, Post, Response, Route, Security, Tags } from 'tsoa';
import {
Body,
Controller,
Query,
Get,
Post,
Response,
Route,
Security,
Tags,
Header
} from 'tsoa';

import { getUserStatusMap, createNewReport } from '../data';
import { Platform, UserStatusMap, Status, Cache } from '../core';
import { Platform, UserStatusMap, Cache } from '../core';
import { UserStatus } from '../models';

const usersCache = new Cache(
Expand Down Expand Up @@ -53,7 +64,10 @@ export class UserStatusesController extends Controller {
@Response(401, 'Authentication fail')
@Security('reporterAuth')
@Post('report')
public async report(@Body() report: UserStatus): Promise<void> {
await createNewReport(report);
public async report(
@Body() report: UserStatus,
@Header('Authorization') reporterKey: string
): Promise<void> {
await createNewReport(report, reporterKey);
}
}
8 changes: 4 additions & 4 deletions src/data/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { UserStatus } from '../models';
* mark report as 'DUPLICATE'.
* @param report Report to create.
*/
export const createNewReport = async (report: UserStatus) => {
let initialStatus: Status = Status.REPORTED;
export const createNewReport = async (report: UserStatus, reporterKey: string) => {
let status: Status = Status.REPORTED;
const { platform, userId, postId, commentId, replyCommentId } = report;

const userStatusRepository = getConnection().getRepository(UserStatus);
Expand Down Expand Up @@ -42,10 +42,10 @@ export const createNewReport = async (report: UserStatus) => {
}

/** Mark the report as 'DUPLICATE' */
initialStatus = Status.DUPLICATE;
status = Status.DUPLICATE;
}

const userStatus = new UserStatus({ ...report, status: initialStatus });
const userStatus = new UserStatus({ ...report, status, reporterKey });

await userStatusRepository.save(userStatus);
};
4 changes: 4 additions & 0 deletions src/data/reporters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { getConnection } from '../core';
import { Reporter } from '../models';

export const checkReporterKey = async (reporterKey: string): Promise<boolean> => {
if (!reporterKey) {
return;
}

/** Get the record that reporterKey match *reporterKey* parameter. */
const reporterRepository = getConnection().getRepository(Reporter);
const reporter = await reporterRepository.findOne({ reporterKey });
Expand Down
16 changes: 0 additions & 16 deletions src/models/authenticated-request.model.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './authenticated-request.model';
export * from './user-status.model';
export * from './reporter.model';
10 changes: 5 additions & 5 deletions src/models/user-status.model.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { Entity, PrimaryGeneratedColumn, Column, Unique } from 'typeorm';

import { AuthenticatedRequest } from './authenticated-request.model';

import { Platform, Status, Reason } from '../core';

@Entity({ name: 'user_statuses' })
@Unique(['platform', 'userId', 'postId', 'commentId', 'replyCommentId'])
export class UserStatus extends AuthenticatedRequest {
export class UserStatus {
@PrimaryGeneratedColumn() private id: number;

@Column({ type: 'enum', enum: Platform, nullable: false })
Expand All @@ -33,9 +31,11 @@ export class UserStatus extends AuthenticatedRequest {
@Column({ type: 'varchar', length: 200, nullable: true })
public description?: string;

constructor(private userStatus?: Partial<UserStatus>) {
super(userStatus);
@Column({ name: 'reporter_key', type: 'varchar', length: 30, nullable: false })
// optional for the report route validations
public reporterKey?: string;

constructor(private userStatus?: Partial<UserStatus>) {
if (userStatus) {
Object.assign(this, userStatus);
}
Expand Down
13 changes: 5 additions & 8 deletions src/security/authentication.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import * as express from 'express';

import { AuthenticatedRequest } from '../models';
import { checkReporterKey } from '../data';
import { Cache, logger } from '../core';
import { logger } from '../core';

/**
* Cert Authentication middelwhere API.
* Cert Authentication middleware API.
* the key should be the 'reporterKey' property in the body.
*/
export const expressAuthentication = async (request: express.Request, scopes: string[]) => {
Expand All @@ -16,11 +15,9 @@ export const expressAuthentication = async (request: express.Request, scopes: st
}

/** Make sure that there is a body, and the body contains the API key. */
const authenticatedRequest: AuthenticatedRequest = request.body;
if (authenticatedRequest && authenticatedRequest.reporterKey) {
if (await checkReporterKey(authenticatedRequest.reporterKey)) {
return;
}
const reporterKey = request.header('Authorization');
if (await checkReporterKey(reporterKey)) {
return;
}

throw new Error('auth fail');
Expand Down

0 comments on commit cb6aa8d

Please sign in to comment.