small updates

This commit is contained in:
Evert Prants 2022-08-18 10:12:02 +03:00
parent ded40a2514
commit 545a71e57d
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
12 changed files with 72 additions and 41 deletions

View File

@ -46,6 +46,6 @@ async function bootstrap() {
app.setBaseViewsDir(join(__dirname, '..', 'views')); app.setBaseViewsDir(join(__dirname, '..', 'views'));
app.setViewEngine('pug'); app.setViewEngine('pug');
await app.listen(3000); await app.listen(3000, '0.0.0.0');
} }
bootstrap(); bootstrap();

View File

@ -1,3 +1,5 @@
import { DataSourceOptions } from 'typeorm';
export interface SMTPConfiguration { export interface SMTPConfiguration {
host: string; host: string;
port: number; port: number;
@ -29,4 +31,5 @@ export interface Configuration {
app: AppConfiguration; app: AppConfiguration;
email: EmailConfiguration; email: EmailConfiguration;
jwt: JWTConfiguration; jwt: JWTConfiguration;
database: DataSourceOptions;
} }

View File

@ -17,6 +17,7 @@ export const configProviders: Provider<any>[] = [
useValue: { useValue: {
app: { app: {
base_url: 'http://localhost:3000', base_url: 'http://localhost:3000',
// generate the following with crypto.randomBytes(256 / 8).toString('hex')
session_secret: 'change me!', session_secret: 'change me!',
challenge_secret: 'change me!', challenge_secret: 'change me!',
}, },
@ -37,23 +38,37 @@ export const configProviders: Provider<any>[] = [
issuer: 'localhost', issuer: 'localhost',
expiration: 3600, expiration: 3600,
}, },
database: {
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: 'root',
database: 'icyauth',
entities: [__dirname + '/../**/*.entity{.ts,.js}'],
synchronize: true,
migrations: ['migration/*.js'],
cli: {
migrationsDir: 'migration',
},
},
} as Configuration, } as Configuration,
}, },
{ {
provide: 'CONFIGURATION', provide: 'CONFIGURATION',
useFactory: async ( useFactory: async (
path: string, configPath: string,
def: Configuration, defaultConfig: Configuration,
): Promise<Configuration> => { ): Promise<Configuration> => {
try { try {
const file = await readFile(path, { encoding: 'utf-8' }); const file = await readFile(configPath, { encoding: 'utf-8' });
return { return {
...def, ...defaultConfig,
...JSON.parse(JSON.stringify(toml.parse(file))), ...JSON.parse(JSON.stringify(toml.parse(file))),
}; };
} catch (e: any) { } catch (e: any) {
console.error('Failed to load configuration:', e.message); console.error('Failed to load configuration:', e.message);
return def; return defaultConfig;
} }
}, },
inject: ['CONFIG_PATH', 'DEFAULT_CONFIG'], inject: ['CONFIG_PATH', 'DEFAULT_CONFIG'],

View File

@ -1,14 +1,16 @@
import { Provider } from '@nestjs/common'; import { FactoryProvider } from '@nestjs/common';
import { ConfigurationService } from 'src/modules/config/config.service'; import { ConfigurationService } from 'src/modules/config/config.service';
import { ConnectionOptions, createConnection } from 'typeorm'; import { DataSource, DataSourceOptions } from 'typeorm';
export const databaseProviders: Provider<any>[] = [ export const databaseProviders: FactoryProvider<Promise<DataSource>>[] = [
{ {
provide: 'DATABASE_CONNECTION', provide: 'DATA_SOURCE',
useFactory: async (config: ConfigurationService) => { useFactory: async (config: ConfigurationService) => {
return await createConnection({ const dataSource = new DataSource({
...config.get<ConnectionOptions>('database'), ...config.get<DataSourceOptions>('database'),
}); });
return dataSource.initialize();
}, },
inject: [ConfigurationService], inject: [ConfigurationService],
}, },

View File

@ -1,6 +1,6 @@
import { Provider } from '@nestjs/common'; import { Provider } from '@nestjs/common';
import { join } from 'path'; import { join } from 'path';
import { Connection } from 'typeorm'; import { DataSource } from 'typeorm';
import { Document } from './document.entity'; import { Document } from './document.entity';
@ -11,7 +11,7 @@ export const documentProviders: Provider<any>[] = [
}, },
{ {
provide: 'DOCUMENT_REPOSITORY', provide: 'DOCUMENT_REPOSITORY',
useFactory: (connection: Connection) => connection.getRepository(Document), useFactory: (dataSource: DataSource) => dataSource.getRepository(Document),
inject: ['DATABASE_CONNECTION'], inject: ['DATA_SOURCE'],
}, },
]; ];

View File

@ -1,5 +1,5 @@
import { Provider } from '@nestjs/common'; import { Provider } from '@nestjs/common';
import { Connection } from 'typeorm'; import { DataSource } from 'typeorm';
import { OAuth2ClientAuthorization } from './oauth2-client-authorization.entity'; import { OAuth2ClientAuthorization } from './oauth2-client-authorization.entity';
import { OAuth2ClientURL } from './oauth2-client-url.entity'; import { OAuth2ClientURL } from './oauth2-client-url.entity';
import { OAuth2Client } from './oauth2-client.entity'; import { OAuth2Client } from './oauth2-client.entity';
@ -7,20 +7,20 @@ import { OAuth2Client } from './oauth2-client.entity';
export const clientProviders: Provider<any>[] = [ export const clientProviders: Provider<any>[] = [
{ {
provide: 'CLIENT_REPOSITORY', provide: 'CLIENT_REPOSITORY',
useFactory: (connection: Connection) => useFactory: (dataSource: DataSource) =>
connection.getRepository(OAuth2Client), dataSource.getRepository(OAuth2Client),
inject: ['DATABASE_CONNECTION'], inject: ['DATA_SOURCE'],
}, },
{ {
provide: 'CLIENT_URL_REPOSITORY', provide: 'CLIENT_URL_REPOSITORY',
useFactory: (connection: Connection) => useFactory: (dataSource: DataSource) =>
connection.getRepository(OAuth2ClientURL), dataSource.getRepository(OAuth2ClientURL),
inject: ['DATABASE_CONNECTION'], inject: ['DATA_SOURCE'],
}, },
{ {
provide: 'CLIENT_AUTHORIZATION_REPOSITORY', provide: 'CLIENT_AUTHORIZATION_REPOSITORY',
useFactory: (connection: Connection) => useFactory: (dataSource: DataSource) =>
connection.getRepository(OAuth2ClientAuthorization), dataSource.getRepository(OAuth2ClientAuthorization),
inject: ['DATABASE_CONNECTION'], inject: ['DATA_SOURCE'],
}, },
]; ];

View File

@ -1,12 +1,12 @@
import { Provider } from '@nestjs/common'; import { Provider } from '@nestjs/common';
import { Connection } from 'typeorm'; import { DataSource } from 'typeorm';
import { OAuth2Token } from './oauth2-token.entity'; import { OAuth2Token } from './oauth2-token.entity';
export const tokenProviders: Provider<any>[] = [ export const tokenProviders: Provider<any>[] = [
{ {
provide: 'TOKEN_REPOSITORY', provide: 'TOKEN_REPOSITORY',
useFactory: (connection: Connection) => useFactory: (dataSource: DataSource) =>
connection.getRepository(OAuth2Token), dataSource.getRepository(OAuth2Token),
inject: ['DATABASE_CONNECTION'], inject: ['DATA_SOURCE'],
}, },
]; ];

View File

@ -1,11 +1,11 @@
import { Provider } from '@nestjs/common'; import { Provider } from '@nestjs/common';
import { Connection } from 'typeorm'; import { DataSource } from 'typeorm';
import { Privilege } from './privilege.entity'; import { Privilege } from './privilege.entity';
export const privilegeProviders: Provider<any>[] = [ export const privilegeProviders: Provider<any>[] = [
{ {
provide: 'PRIVILEGE_REPOSITORY', provide: 'PRIVILEGE_REPOSITORY',
useFactory: (connection: Connection) => connection.getRepository(Privilege), useFactory: (dataSource: DataSource) => dataSource.getRepository(Privilege),
inject: ['DATABASE_CONNECTION'], inject: ['DATA_SOURCE'],
}, },
]; ];

View File

@ -1,11 +1,11 @@
import { Provider } from '@nestjs/common'; import { Provider } from '@nestjs/common';
import { Connection } from 'typeorm'; import { DataSource } from 'typeorm';
import { Upload } from './upload.entity'; import { Upload } from './upload.entity';
export const uploadProviders: Provider<any>[] = [ export const uploadProviders: Provider<any>[] = [
{ {
provide: 'UPLOAD_REPOSITORY', provide: 'UPLOAD_REPOSITORY',
useFactory: (connection: Connection) => connection.getRepository(Upload), useFactory: (dataSource: DataSource) => dataSource.getRepository(Upload),
inject: ['DATABASE_CONNECTION'], inject: ['DATA_SOURCE'],
}, },
]; ];

View File

@ -1,11 +1,11 @@
import { Provider } from '@nestjs/common'; import { Provider } from '@nestjs/common';
import { Connection } from 'typeorm'; import { DataSource } from 'typeorm';
import { UserToken } from './user-token.entity'; import { UserToken } from './user-token.entity';
export const userTokenProviders: Provider<any>[] = [ export const userTokenProviders: Provider<any>[] = [
{ {
provide: 'USER_TOKEN_REPOSITORY', provide: 'USER_TOKEN_REPOSITORY',
useFactory: (connection: Connection) => connection.getRepository(UserToken), useFactory: (dataSource: DataSource) => dataSource.getRepository(UserToken),
inject: ['DATABASE_CONNECTION'], inject: ['DATA_SOURCE'],
}, },
]; ];

View File

@ -1,11 +1,11 @@
import { Provider } from '@nestjs/common'; import { Provider } from '@nestjs/common';
import { Connection } from 'typeorm'; import { DataSource } from 'typeorm';
import { User } from './user.entity'; import { User } from './user.entity';
export const userProviders: Provider<any>[] = [ export const userProviders: Provider<any>[] = [
{ {
provide: 'USER_REPOSITORY', provide: 'USER_REPOSITORY',
useFactory: (connection: Connection) => connection.getRepository(User), useFactory: (dataSource: DataSource) => dataSource.getRepository(User),
inject: ['DATABASE_CONNECTION'], inject: ['DATA_SOURCE'],
}, },
]; ];

View File

@ -8,6 +8,11 @@ export class FormUtilityService {
public passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d\w\W]{8,}$/; public passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d\w\W]{8,}$/;
public usernameRegex = /^[a-zA-Z0-9_\-.]{3,26}$/; public usernameRegex = /^[a-zA-Z0-9_\-.]{3,26}$/;
/**
* Merge array of objects into single object
* @param flash Array of objects
* @returns Merged object
*/
public mergeObjectArray(flash: Record<string, any>[]): Record<string, any> { public mergeObjectArray(flash: Record<string, any>[]): Record<string, any> {
return flash.reduce<Record<string, any>>( return flash.reduce<Record<string, any>>(
(obj, item) => ({ ...obj, ...item }), (obj, item) => ({ ...obj, ...item }),
@ -15,6 +20,12 @@ export class FormUtilityService {
); );
} }
/**
* Trim values of specified keys of an object
* @param entry Object
* @param fields Fields to strip spaces from
* @returns Object with trimmed values
*/
public trimmed<T>(entry: T, fields: string[]): T { public trimmed<T>(entry: T, fields: string[]): T {
return fields.reduce<T>( return fields.reduce<T>(
(object, key) => ({ ...object, [key]: object[key]?.trim() }), (object, key) => ({ ...object, [key]: object[key]?.trim() }),