Skip to content

Commit

Permalink
Authentication basics (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
radoslavirha authored Oct 23, 2023
1 parent 78baf49 commit 6cb7781
Show file tree
Hide file tree
Showing 110 changed files with 1,313 additions and 654 deletions.
3 changes: 1 addition & 2 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ PACKAGE: packages/**
"Hiker's Book UI": ui/hikers-book/**
'@hikers-book/cli-tools': packages/cli-tools/**
'@hikers-book/config': packages/config/**
'@hikers-book/cryptography': packages/cryptography/**
'@hikers-book/eslint-config': packages/eslint/**
'@hikers-book/jest': packages/jest/**
'@hikers-book/tsed-swagger': packages/tsed-swagger/**
'@hikers-book/tsed-common': packages/tsed-common/**

ci: .github/**
deps: '**/package.json'
Expand Down
6 changes: 5 additions & 1 deletion api/authentication/.barrelsby.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
"directory": [
"./src/docs/controllers/pages",
"./src/v1/controllers",
"./src/v1/handlers/auth",
"./src/v1/models",
"./src/v1/types",
"./src/v1/types/enum"
"./src/v1/types/enum",
"./src/services",
"./src/utils"
],
"exclude": [
"__mock__",
Expand Down
2 changes: 2 additions & 0 deletions api/authentication/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
keys/jwt.pem
keys/jwt.pem.pub
Empty file.
11 changes: 7 additions & 4 deletions api/authentication/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"author": "radoslav.irha@gmail.com",
"license": "MIT",
"scripts": {
"bootstrap": "pnpm run config:all && pnpm run keys",
"build": "pnpm run barrels && tsc --project tsconfig.compile.json",
"barrels": "barrelsby --config .barrelsby.json",
"start": "pnpm run barrels && tsnd --inspect --exit-child --cls --ignore-watch node_modules --respawn --transpile-only -r tsconfig-paths/register src/index.ts",
Expand All @@ -18,11 +19,11 @@
"config:clear": "find ./config -name '*.json' -type f -delete",
"config:all": "pnpm run config:env && pnpm run config && pnpm run config:test",
"postinstall": "pnpm run config:all",
"cleanup": "rm -rf coverage dist"
"cleanup": "rm -rf coverage dist",
"keys": "openssl genrsa -out keys/jwt.pem 2048 && openssl rsa -in keys/jwt.pem -outform PEM -pubout -out keys/jwt.pem.pub"
},
"dependencies": {
"@hikers-book/cryptography": "workspace:*",
"@hikers-book/tsed-swagger": "workspace:*",
"@hikers-book/tsed-common": "workspace:*",
"@tsed/ajv": "^7.36.8",
"@tsed/common": "^7.36.8",
"@tsed/core": "^7.36.8",
Expand All @@ -43,7 +44,7 @@
"@tsed/platform-views": "^7.36.8",
"@tsed/schema": "^7.36.8",
"@tsed/swagger": "^7.36.8",
"ajv": "^8.12.0",
"argon2": "^0.31.1",
"barrelsby": "^2.8.1",
"body-parser": "^1.20.2",
"class-validator": "^0.13.2",
Expand All @@ -57,6 +58,7 @@
"dotenv-flow": "^3.3.0",
"express": "^4.18.2",
"helmet": "^7.0.0",
"jsonwebtoken": "^9.0.2",
"method-override": "^3.0.0",
"mongoose": "^7.5.0"
},
Expand All @@ -76,6 +78,7 @@
"@types/cors": "^2.8.14",
"@types/express": "^4.17.18",
"@types/jest": "^29.5.5",
"@types/jsonwebtoken": "^9.0.3",
"@types/method-override": "^0.0.33",
"@types/multer": "^1.4.8",
"@types/node": "^20.5.7",
Expand Down
61 changes: 18 additions & 43 deletions api/authentication/src/Server.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,41 @@
import { getHelmetDirectives, getSwaggerConfig } from '@hikers-book/tsed-swagger';
import { BaseServer } from '@hikers-book/tsed-common/server';
import { ConfigServiceBase } from '@hikers-book/tsed-common/services';
import { getHelmetDirectives, getSwaggerConfig } from '@hikers-book/tsed-common/swagger';
import '@tsed/ajv';
import { $log, PlatformApplication } from '@tsed/common';
import { Configuration, Inject } from '@tsed/di';
import { Configuration } from '@tsed/di';
import '@tsed/mongoose';
import '@tsed/platform-express'; // /!\ keep this import
import bodyParser from 'body-parser';
import compress from 'compression';
import cookieParser from 'cookie-parser';
import cors from 'cors';
import helmet from 'helmet';
import methodOverride from 'method-override';
import { join } from 'path';
import { config } from './config/index';
import * as docs from './docs/controllers/pages/index';
import './providers/ConfigProvider';
import * as v1 from './v1/controllers/index';

@Configuration({
...config,
acceptMimes: ['application/json'],
disableComponentsScan: true,
// @Configuration decorator from base class is not working
...ConfigServiceBase.getServerDefaults(),
mount: {
'/v1': [...Object.values(v1)],
'/': [...Object.values(docs)]
},
swagger: getSwaggerConfig(join(__dirname, '../package.json')),
swagger: getSwaggerConfig(),
views: {
root: join(process.cwd(), '../views'),
extensions: {
ejs: 'ejs'
}
},
exclude: ['**/*.spec.ts']
}
})
export class Server {
@Inject()
protected app!: PlatformApplication;

@Configuration()
protected settings!: Configuration;

export class Server extends BaseServer {
$beforeRoutesInit(): void {
this.app
.use(
helmet({
contentSecurityPolicy: {
directives: getHelmetDirectives()
}
})
)
.use(cors())
.use(cookieParser())
.use(compress({}))
.use(methodOverride())
.use(bodyParser.json())
.use(
bodyParser.urlencoded({
extended: true
})
);
}
this.registerMiddlewares();

$onReady(): void {
$log.info(`${this.settings.api} ${this.settings.version} is ready!`);
this.app.use(
helmet({
contentSecurityPolicy: {
directives: getHelmetDirectives()
}
})
);
}
}
9 changes: 0 additions & 9 deletions api/authentication/src/config/envs/index.ts

This file was deleted.

16 changes: 0 additions & 16 deletions api/authentication/src/config/index.ts

This file was deleted.

24 changes: 0 additions & 24 deletions api/authentication/src/config/logger/index.ts

This file was deleted.

10 changes: 0 additions & 10 deletions api/authentication/src/config/mongoose/index.ts

This file was deleted.

4 changes: 3 additions & 1 deletion api/authentication/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { $log } from '@tsed/common';
import { PlatformExpress } from '@tsed/platform-express';
import { Server } from './Server';
import { ConfigService } from './services/ConfigService';

async function bootstrap() {
try {
const platform = await PlatformExpress.bootstrap(Server);
const config = ConfigService.getServerConfig();
const platform = await PlatformExpress.bootstrap(Server, config);
await platform.listen();

process.on('SIGINT', () => {
Expand Down
28 changes: 28 additions & 0 deletions api/authentication/src/models/ConfigJWTModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Enum, Required } from '@tsed/schema';
import { Algorithm } from 'jsonwebtoken';

export class ConfigJWTModel {
@Required()
@Enum(
'HS256',
'HS384',
'HS512',
'RS256',
'RS384',
'RS512',
'ES256',
'ES384',
'ES512',
'PS256',
'PS384',
'PS512',
'none'
)
algorithm!: Algorithm;

@Required()
expiresIn!: string;

@Required()
expiresInRefresh!: string;
}
11 changes: 11 additions & 0 deletions api/authentication/src/models/ConfigModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { MongoDBConfigModel } from '@hikers-book/tsed-common/models';
import { Required } from '@tsed/schema';
import { ConfigJWTModel } from './ConfigJWTModel';

export class ConfigModel {
@Required()
jwt!: ConfigJWTModel;

@Required()
mongodb!: MongoDBConfigModel;
}
7 changes: 7 additions & 0 deletions api/authentication/src/providers/ConfigProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { registerProvider } from '@tsed/di';
import { ConfigService } from '../services/ConfigService';

registerProvider({
provide: ConfigService,
useClass: ConfigService
});
28 changes: 28 additions & 0 deletions api/authentication/src/services/ConfigService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ConfigServiceBase } from '@hikers-book/tsed-common/services';
import { Injectable } from '@tsed/di';
import { ConfigModel } from '../models/ConfigModel';

@Injectable()
export class ConfigService extends ConfigServiceBase<ConfigModel> {
static api = "Hiker's Book Authentication API";
static port = 5501;

constructor() {
super(ConfigService.api, ConfigService.port, ConfigModel);
}

static getServerConfig(): Partial<TsED.Configuration> {
const config = ConfigServiceBase.load(ConfigService.api, ConfigService.port, ConfigModel);

return {
...config.base,
mongoose: [
{
id: 'authentication',
url: config.file.mongodb.url,
connectionOptions: config.file.mongodb.connectionOptions
}
]
};
}
}
Loading

0 comments on commit 6cb7781

Please sign in to comment.