-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
98e71e0
commit 3c284e9
Showing
15 changed files
with
351 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { hasher } from './hasher'; | ||
|
||
describe('hasher', () => { | ||
it('should hash the value', () => { | ||
const value1 = 'Une valeur spécifique'; | ||
const value2 = 'Une autre valeur moins spécifique'; | ||
|
||
const hash1 = hasher.hash(value1); | ||
const hash2 = hasher.hash(value2); | ||
|
||
expect(hash1.length).toBe(64); | ||
expect(hash2.length).toBe(64); | ||
expect(hash1).not.toBe(hash2); | ||
}); | ||
|
||
it('should verify if the hashed value matches', () => { | ||
const value = 'Une valeur spécifique'; | ||
|
||
const result = hasher.verify( | ||
value, | ||
'af85257f2800d6dfc7ed65d5566d7a159408e27080e5c81e491eb86783daac63', | ||
); | ||
|
||
expect(result).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import crypto from 'crypto'; | ||
import { config } from '../config'; | ||
|
||
const hasher = { hash, verify }; | ||
|
||
function hash(value: string) { | ||
const hashedValue = crypto.createHmac('sha256', config.HASH_SECRET).update(value).digest('hex'); | ||
return hashedValue; | ||
} | ||
|
||
function verify(value: string, hashedValue: string) { | ||
return hash(value) === hashedValue; | ||
} | ||
|
||
export { hasher }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { signer } from './signer'; | ||
|
||
describe('signer', () => { | ||
describe('sign', () => { | ||
it('should create a token', () => { | ||
const payload = { foo: 'bar' }; | ||
|
||
const token = signer.sign(payload); | ||
const [header, body, _] = token.split('.'); | ||
const value = JSON.parse(atob(body))['foo']; | ||
|
||
expect(header).toBe('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9'); | ||
expect(value).toBe('bar'); | ||
}); | ||
|
||
it('should verify a token', () => { | ||
const payload = { foo: 'bar' }; | ||
|
||
const token = signer.sign(payload); | ||
const value = (signer.verify(token) as Record<string, any>)['foo']; | ||
|
||
expect(value).toBe('bar'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import jwt from 'jsonwebtoken'; | ||
import { config } from '../config'; | ||
|
||
const SIX_MONTHS = 60 * 60 * 24 * 30 * 6; | ||
const ALGORITHM = 'HS256' as const; | ||
|
||
function sign(payload: Object) { | ||
const token = jwt.sign(payload, config.JWT_TOKEN_SECRET, { | ||
algorithm: ALGORITHM, | ||
expiresIn: SIX_MONTHS, | ||
}); | ||
|
||
return token; | ||
} | ||
|
||
function verify(token: string) { | ||
return jwt.verify(token, config.JWT_TOKEN_SECRET, { algorithms: [ALGORITHM] }); | ||
} | ||
|
||
const signer = { sign, verify }; | ||
|
||
export { signer }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class AddUser1733847009730 implements MigrationInterface { | ||
name = 'AddUser1733847009730'; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`CREATE TABLE "user" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "email" character varying NOT NULL, "hashedPassword" character varying NOT NULL, CONSTRAINT "UQ_e12875dfb3b1d92d7d7c5377e22" UNIQUE ("email"), CONSTRAINT "PK_cace4a159ff9f2512dd42373760" PRIMARY KEY ("id"))`, | ||
); | ||
const [insertedUser] = await queryRunner.query( | ||
`INSERT INTO "user" ("email", "hashedPassword") VALUES ('[email protected]', '7b155b65c3ecb88501347988ab889b021c4c891e547976b27e2419734117240b') RETURNING id`, | ||
); | ||
const userId = insertedUser.id; | ||
await queryRunner.query(`ALTER TABLE "system_pulse" ADD "userId" uuid`); | ||
await queryRunner.query(`ALTER TABLE "monitor" ADD "userId" uuid`); | ||
await queryRunner.query( | ||
`ALTER TABLE "system_pulse" ADD CONSTRAINT "FK_9c9997d98ccc4e43157c9803b0a" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE NO ACTION`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "monitor" ADD CONSTRAINT "FK_17b6081f05eee538f7db3de2f9c" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE NO ACTION`, | ||
); | ||
|
||
await queryRunner.query(`UPDATE "system_pulse" SET "userId"='${userId}'`); | ||
await queryRunner.query(`UPDATE "monitor" SET "userId"='${userId}'`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`ALTER TABLE "monitor" DROP CONSTRAINT "FK_17b6081f05eee538f7db3de2f9c"`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "system_pulse" DROP CONSTRAINT "FK_9c9997d98ccc4e43157c9803b0a"`, | ||
); | ||
await queryRunner.query(`ALTER TABLE "monitor" DROP COLUMN "userId"`); | ||
await queryRunner.query(`ALTER TABLE "system_pulse" DROP COLUMN "userId"`); | ||
await queryRunner.query(`DROP TABLE "user"`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'; | ||
|
||
@Entity() | ||
export class User { | ||
@PrimaryGeneratedColumn('uuid') | ||
id: string; | ||
|
||
@Column({ unique: true }) | ||
email: string; | ||
|
||
@Column() | ||
hashedPassword: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { User } from './User.entity'; | ||
import { buildUserController } from './user.controller'; | ||
|
||
export { User, buildUserController }; |
Oops, something went wrong.