Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
Merge branch 'master' into feat/mfa
Browse files Browse the repository at this point in the history
  • Loading branch information
JumpLink committed Nov 3, 2023
2 parents 88def62 + 210948a commit 02524e6
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/api/controllers/ApiKeyController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ export class ApiKeyController {
await getRepository(ApiKey).save({
id,
secretHash,
creator,
description: data.description,
creator
expires: data.expires
});

return { token };
Expand Down
4 changes: 2 additions & 2 deletions src/api/data/ApiKeyData/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export function convertApiKeyToData(apiKey: ApiKey): GetApiKeyData {
return {
id: apiKey.id,
description: apiKey.description,
expires: apiKey.expires,
creator: convertContactToData(apiKey.creator),
createdAt: apiKey.createdAt,
secretHash: apiKey.secretHash
createdAt: apiKey.createdAt
};
}

Expand Down
11 changes: 8 additions & 3 deletions src/api/data/ApiKeyData/interface.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import { IsIn, IsString } from "class-validator";
import { IsDate, IsIn, IsOptional, IsString } from "class-validator";
import { GetContactData } from "../ContactData";
import { GetPaginatedQuery } from "../PaginatedData";
import { Type } from "class-transformer";

export class CreateApiKeyData {
@IsString()
description!: string;

@IsOptional()
@Type(() => Date)
@IsDate()
expires!: Date | null;
}

export interface GetApiKeyData extends CreateApiKeyData {
id: string;
creator: GetContactData;
createdAt: Date;
secretHash: string;
}

export class GetApiKeysQuery extends GetPaginatedQuery {
@IsIn(["createdAt"])
@IsIn(["createdAt", "expires"])
sort?: string;
}
2 changes: 1 addition & 1 deletion src/core/services/AuthService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async function isValidApiKey(key: string): Promise<boolean> {
const [_, secret] = key.split("_");
const secretHash = crypto.createHash("sha256").update(secret).digest("hex");
const apiKey = await getRepository(ApiKey).findOne({ secretHash });
return !!apiKey;
return !!apiKey && (!apiKey.expires || apiKey.expires > new Date());
}

class AuthService {
Expand Down
13 changes: 13 additions & 0 deletions src/migrations/1698938717666-AddApiKeyExpiryDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class AddApiKeyExpiryDate1698938717666 implements MigrationInterface {
name = "AddApiKeyExpiryDate1698938717666";

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "api_key" ADD "expires" TIMESTAMP`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "api_key" DROP COLUMN "expires"`);
}
}
3 changes: 3 additions & 0 deletions src/models/ApiKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export default class ApiKey {
@CreateDateColumn()
createdAt!: Date;

@Column({ type: Date, nullable: true })
expires!: Date | null;

@Column()
secretHash!: string;

Expand Down

0 comments on commit 02524e6

Please sign in to comment.