@@ -6,7 +6,6 @@ import { Response } from 'express';
|
|||||||
export class AppController {
|
export class AppController {
|
||||||
@Get('*')
|
@Get('*')
|
||||||
handleClientRoutes(@Res() res: Response) {
|
handleClientRoutes(@Res() res: Response) {
|
||||||
console.log("handle")
|
|
||||||
res.sendFile(join(__dirname, '..', 'client', 'index.html'));
|
res.sendFile(join(__dirname, '..', 'client', 'index.html'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,24 +1,18 @@
|
|||||||
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
|
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
|
||||||
import { AppService } from './app.service';
|
|
||||||
import { AuthModule } from './auth/auth.module';
|
|
||||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
import { ConfigModule } from '@nestjs/config';
|
import { ConfigModule } from '@nestjs/config';
|
||||||
import { ServeStaticModule } from '@nestjs/serve-static';
|
import { ServeStaticModule } from '@nestjs/serve-static';
|
||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
import { LoggerModule } from './core/logger.module';
|
|
||||||
import { SessionMiddleware } from './core/session.middleware';
|
import { SessionMiddleware } from './core/session.middleware';
|
||||||
import { ClientModule } from './client/client.module';
|
|
||||||
import { ApplicationModule } from './application/application.module';
|
import { ApplicationModule } from './application/application.module';
|
||||||
import { MailModule } from './application/mail/mail.module';
|
import { MailModule } from './application/mail/mail.module';
|
||||||
|
import { IDPModule } from './idp/idp.module';
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
AuthModule,
|
|
||||||
ClientModule,
|
|
||||||
ConfigModule.forRoot({
|
ConfigModule.forRoot({
|
||||||
envFilePath: ['.env'],
|
envFilePath: ['.env'],
|
||||||
isGlobal: true,
|
isGlobal: true,
|
||||||
}),
|
}),
|
||||||
LoggerModule,
|
|
||||||
ServeStaticModule.forRoot({
|
ServeStaticModule.forRoot({
|
||||||
rootPath: join(__dirname, '../client'),
|
rootPath: join(__dirname, '../client'),
|
||||||
exclude: ['*/api*'],
|
exclude: ['*/api*'],
|
||||||
@@ -39,19 +33,10 @@ import { MailModule } from './application/mail/mail.module';
|
|||||||
}),
|
}),
|
||||||
ApplicationModule,
|
ApplicationModule,
|
||||||
MailModule,
|
MailModule,
|
||||||
// TypeOrmModule.forRoot({
|
IDPModule,
|
||||||
// type: 'mysql',
|
|
||||||
// host: '85.215.137.185', // MySQL Hostname
|
|
||||||
// port: 3306, // MySQL Port (Standard ist 3306)
|
|
||||||
// username: 'root', // Dein MySQL-Benutzername
|
|
||||||
// password: 'Battlefield123', // Dein MySQL-Passwort
|
|
||||||
// database: 'global_users', // Name der Datenbank
|
|
||||||
// entities: [User, Client, RedirectUri, AuthorizationCode], // Hier werden deine Entitäten aufgelistet
|
|
||||||
// synchronize: true, // Setze dies auf `false` in der Produktion
|
|
||||||
// }),
|
|
||||||
],
|
],
|
||||||
controllers: [],
|
controllers: [],
|
||||||
providers: [AppService],
|
providers: [],
|
||||||
exports: [],
|
exports: [],
|
||||||
})
|
})
|
||||||
export class AppModule implements NestModule {
|
export class AppModule implements NestModule {
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
|
||||||
|
|
||||||
@Injectable()
|
|
||||||
export class AppService {
|
|
||||||
getHello(): string {
|
|
||||||
return 'Hello World!';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
|
||||||
import { ApplicationController } from './application.controller';
|
|
||||||
|
|
||||||
describe('ApplicationController', () => {
|
|
||||||
let controller: ApplicationController;
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
|
||||||
controllers: [ApplicationController],
|
|
||||||
}).compile();
|
|
||||||
|
|
||||||
controller = module.get<ApplicationController>(ApplicationController);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should be defined', () => {
|
|
||||||
expect(controller).toBeDefined();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import { Body, Controller, Get, Post } from '@nestjs/common';
|
import { Body, Controller, Get, Post } from '@nestjs/common';
|
||||||
import { User } from 'src/model';
|
import { User } from 'src/model';
|
||||||
import { LoginUserDto } from 'src/model/dto';
|
import { LoginUserDto } from 'src/model/dto';
|
||||||
import { UsersService } from 'src/users/users.service';
|
import { UsersService } from 'src/shared/users.service';
|
||||||
|
|
||||||
@Controller('app')
|
@Controller('app')
|
||||||
export class ApplicationController {
|
export class ApplicationController {
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { ApplicationController } from './application.controller';
|
import { ApplicationController } from './application.controller';
|
||||||
import { UserModule } from './user/user.module';
|
import { UserModule } from './user/user.module';
|
||||||
import { LoggerModule } from 'src/core/logger.module';
|
|
||||||
import { SecureModule } from 'src/core/secure/secure.module';
|
|
||||||
import { MailModule } from './mail/mail.module';
|
import { MailModule } from './mail/mail.module';
|
||||||
|
import { SharedModule } from 'src/shared/shared.module';
|
||||||
|
import { SecureModule } from 'src/shared/secure/secure.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
controllers: [ApplicationController],
|
controllers: [ApplicationController],
|
||||||
providers: [],
|
providers: [],
|
||||||
imports: [LoggerModule, UserModule, SecureModule, MailModule],
|
imports: [UserModule, SecureModule, MailModule, SharedModule],
|
||||||
})
|
})
|
||||||
export class ApplicationModule {}
|
export class ApplicationModule {}
|
||||||
|
|||||||
60
idp/src/application/application.service.ts
Normal file
60
idp/src/application/application.service.ts
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { ClientRepository, LogRepository, User, Client } from 'src/model';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class ApplicationService {
|
||||||
|
constructor(
|
||||||
|
private clientRepository: ClientRepository,
|
||||||
|
private logRepository: LogRepository,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
getUserClients(user: User): Promise<Client[]> {
|
||||||
|
return this.clientRepository.find({
|
||||||
|
where: { admins: { id: user.id } },
|
||||||
|
relations: ['admins'],
|
||||||
|
order: { createdAt: 'ASC' },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async getUserLogins() {
|
||||||
|
const logs = await this.logRepository.find({
|
||||||
|
where: [{ type: 'login' }, { type: 'systemlogin' }],
|
||||||
|
order: { timestamp: 'asc' },
|
||||||
|
});
|
||||||
|
|
||||||
|
const first = new Date(logs[0].timestamp.toISOString());
|
||||||
|
const last = logs[logs.length - 1].timestamp.toISOString().substring(0, 10);
|
||||||
|
|
||||||
|
const res = {};
|
||||||
|
|
||||||
|
res[first.toISOString().substring(0, 10)] = {
|
||||||
|
logins: 0,
|
||||||
|
systemLogins: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (logs.length > 1) {
|
||||||
|
const current = first;
|
||||||
|
|
||||||
|
while (current.toISOString().substring(0, 10) <= last) {
|
||||||
|
res[current.toISOString().substring(0, 10)] = {
|
||||||
|
logins: 0,
|
||||||
|
systemLogins: 0,
|
||||||
|
};
|
||||||
|
current.setDate(current.getDate() + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
logs.forEach((l) => {
|
||||||
|
if (l.type == 'login') {
|
||||||
|
res[l.timestamp.toISOString().substring(0, 10)].logins += 1;
|
||||||
|
} else if (l.type == 'systemlogin') {
|
||||||
|
res[l.timestamp.toISOString().substring(0, 10)].systemLogins += 1;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return Object.entries(res).map(([date, count]) => ({
|
||||||
|
date: new Date(date),
|
||||||
|
count,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,14 +1,14 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { DatabaseModule } from 'src/core/database/database.module';
|
|
||||||
import { MailerModule } from '@nestjs-modules/mailer';
|
import { MailerModule } from '@nestjs-modules/mailer';
|
||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||||
import { HandlebarsAdapter } from '@nestjs-modules/mailer/dist/adapters/handlebars.adapter';
|
import { HandlebarsAdapter } from '@nestjs-modules/mailer/dist/adapters/handlebars.adapter';
|
||||||
import { MailService } from './mail.service';
|
import { MailService } from './mail.service';
|
||||||
|
import { SharedModule } from 'src/shared/shared.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
DatabaseModule,
|
SharedModule,
|
||||||
MailerModule.forRootAsync({
|
MailerModule.forRootAsync({
|
||||||
imports: [ConfigModule],
|
imports: [ConfigModule],
|
||||||
inject: [ConfigService],
|
inject: [ConfigService],
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
|
||||||
import { MailService } from './mail.service';
|
|
||||||
|
|
||||||
describe('MailService', () => {
|
|
||||||
let service: MailService;
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
|
||||||
providers: [MailService],
|
|
||||||
}).compile();
|
|
||||||
|
|
||||||
service = module.get<MailService>(MailService);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should be defined', () => {
|
|
||||||
expect(service).toBeDefined();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
|
||||||
import { UserController } from './user.controller';
|
|
||||||
|
|
||||||
describe('UserController', () => {
|
|
||||||
let controller: UserController;
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
|
||||||
controllers: [UserController],
|
|
||||||
}).compile();
|
|
||||||
|
|
||||||
controller = module.get<UserController>(UserController);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should be defined', () => {
|
|
||||||
expect(controller).toBeDefined();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -8,17 +8,17 @@ import {
|
|||||||
Req,
|
Req,
|
||||||
UseGuards,
|
UseGuards,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { UserService } from './user.service';
|
import { APPUserService } from './user.service';
|
||||||
import { AuthGuard, Roles, RolesGuard } from 'src/core/secure/guards';
|
import { AuthGuard, Roles, RolesGuard } from 'src/shared/secure/guards';
|
||||||
import { AuthenticatedRequest, Client, RedirectUri } from 'src/model';
|
import { IAuthenticatedRequest, Client, RedirectUri } from 'src/model';
|
||||||
import { CreateClientDto } from 'src/model/dto/create-client.dto';
|
import { CreateClientDto } from 'src/model/dto/create-client.dto';
|
||||||
import { ClientService } from 'src/client/client.service';
|
import { ClientService } from 'src/idp/client/client.service';
|
||||||
|
|
||||||
@UseGuards(AuthGuard, RolesGuard)
|
@UseGuards(AuthGuard, RolesGuard)
|
||||||
@Controller('app/user')
|
@Controller('app/user')
|
||||||
export class UserController {
|
export class UserController {
|
||||||
constructor(
|
constructor(
|
||||||
private userService: UserService,
|
private userService: APPUserService,
|
||||||
private clientService: ClientService,
|
private clientService: ClientService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@@ -29,13 +29,13 @@ export class UserController {
|
|||||||
|
|
||||||
@Roles('admin')
|
@Roles('admin')
|
||||||
@Get('clients')
|
@Get('clients')
|
||||||
getClients(@Req() req: AuthenticatedRequest): Promise<Client[]> {
|
getClients(@Req() req: IAuthenticatedRequest): Promise<Client[]> {
|
||||||
return this.userService.getUserClients(req.user);
|
return this.userService.getUserClients(req.user);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Roles('admin')
|
@Roles('admin')
|
||||||
@Post('client')
|
@Post('client')
|
||||||
createClient(@Req() req: AuthenticatedRequest, @Body() b: CreateClientDto) {
|
createClient(@Req() req: IAuthenticatedRequest, @Body() b: CreateClientDto) {
|
||||||
return this.clientService.createClient(
|
return this.clientService.createClient(
|
||||||
req.user,
|
req.user,
|
||||||
b.clientName,
|
b.clientName,
|
||||||
@@ -47,13 +47,13 @@ export class UserController {
|
|||||||
|
|
||||||
@Roles('admin')
|
@Roles('admin')
|
||||||
@Delete('client/:id')
|
@Delete('client/:id')
|
||||||
deleteClient(@Req() req: AuthenticatedRequest, @Param('id') id) {
|
deleteClient(@Req() req: IAuthenticatedRequest, @Param('id') id) {
|
||||||
return this.clientService.deleteClient(req.user, id);
|
return this.clientService.deleteClient(req.user, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post('clients/:id/redirect')
|
@Post('clients/:id/redirect')
|
||||||
saveUris(
|
saveUris(
|
||||||
@Req() req: AuthenticatedRequest,
|
@Req() req: IAuthenticatedRequest,
|
||||||
@Param('id') id,
|
@Param('id') id,
|
||||||
@Body() uris: RedirectUri[],
|
@Body() uris: RedirectUri[],
|
||||||
): Promise<RedirectUri[]> {
|
): Promise<RedirectUri[]> {
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { UserController } from './user.controller';
|
import { UserController } from './user.controller';
|
||||||
import { SecureModule } from 'src/core/secure/secure.module';
|
import { APPUserService } from './user.service';
|
||||||
import { UserService } from './user.service';
|
import { SharedModule } from 'src/shared/shared.module';
|
||||||
import { ClientService } from 'src/client/client.service';
|
import { SecureModule } from 'src/shared/secure/secure.module';
|
||||||
import { DatabaseModule } from 'src/core/database/database.module';
|
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
controllers: [UserController],
|
controllers: [UserController],
|
||||||
imports: [SecureModule, DatabaseModule],
|
imports: [SecureModule, SharedModule],
|
||||||
providers: [UserService, ClientService],
|
providers: [APPUserService],
|
||||||
})
|
})
|
||||||
export class UserModule {}
|
export class UserModule {}
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
|
||||||
import { UserService } from './user.service';
|
|
||||||
|
|
||||||
describe('UserService', () => {
|
|
||||||
let service: UserService;
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
|
||||||
providers: [UserService],
|
|
||||||
}).compile();
|
|
||||||
|
|
||||||
service = module.get<UserService>(UserService);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should be defined', () => {
|
|
||||||
expect(service).toBeDefined();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';
|
|||||||
import { ClientRepository, LogRepository, User, Client } from 'src/model';
|
import { ClientRepository, LogRepository, User, Client } from 'src/model';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class UserService {
|
export class APPUserService {
|
||||||
constructor(
|
constructor(
|
||||||
private clientRepository: ClientRepository,
|
private clientRepository: ClientRepository,
|
||||||
private logRepository: LogRepository,
|
private logRepository: LogRepository,
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
|
||||||
import { AuthController } from './auth.controller';
|
|
||||||
|
|
||||||
describe('AuthController', () => {
|
|
||||||
let controller: AuthController;
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
|
||||||
controllers: [AuthController],
|
|
||||||
}).compile();
|
|
||||||
|
|
||||||
controller = module.get<AuthController>(AuthController);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should be defined', () => {
|
|
||||||
expect(controller).toBeDefined();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
import { Module } from '@nestjs/common';
|
|
||||||
import { AuthController } from './auth.controller';
|
|
||||||
import { ClientService } from 'src/client/client.service';
|
|
||||||
import { JwtModule } from '@nestjs/jwt';
|
|
||||||
import { NestjsFormDataModule } from 'nestjs-form-data';
|
|
||||||
import { LoggerModule } from 'src/core/logger.module';
|
|
||||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
|
||||||
import { DatabaseModule } from 'src/core/database/database.module';
|
|
||||||
import { MailModule } from 'src/application/mail/mail.module';
|
|
||||||
import { SecureModule } from 'src/core/secure/secure.module';
|
|
||||||
|
|
||||||
@Module({
|
|
||||||
providers: [ClientService],
|
|
||||||
controllers: [AuthController],
|
|
||||||
imports: [
|
|
||||||
JwtModule.registerAsync({
|
|
||||||
imports: [ConfigModule],
|
|
||||||
inject: [ConfigService],
|
|
||||||
useFactory: async (config: ConfigService) => ({
|
|
||||||
secret: config.get('JWT_SECRET'),
|
|
||||||
signOptions: { expiresIn: config.get('JWT_EXPIRES_IN') },
|
|
||||||
}),
|
|
||||||
}),
|
|
||||||
NestjsFormDataModule,
|
|
||||||
LoggerModule,
|
|
||||||
DatabaseModule,
|
|
||||||
MailModule,
|
|
||||||
SecureModule,
|
|
||||||
],
|
|
||||||
})
|
|
||||||
export class AuthModule {}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
|
||||||
import { ClientService } from './client.service';
|
|
||||||
|
|
||||||
describe('ClientService', () => {
|
|
||||||
let service: ClientService;
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
|
||||||
providers: [ClientService],
|
|
||||||
}).compile();
|
|
||||||
|
|
||||||
service = module.get<ClientService>(ClientService);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should be defined', () => {
|
|
||||||
expect(service).toBeDefined();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
import { Module } from '@nestjs/common';
|
|
||||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
|
||||||
import { JwtModule } from '@nestjs/jwt';
|
|
||||||
import { NestjsFormDataModule } from 'nestjs-form-data';
|
|
||||||
import { ClientService } from 'src/client/client.service';
|
|
||||||
import { UsersService } from 'src/users/users.service';
|
|
||||||
import { LoggerModule } from '../logger.module';
|
|
||||||
import { AuthGuard } from './guards/auth.guard';
|
|
||||||
import { DatabaseModule } from '../database/database.module';
|
|
||||||
import { RolesGuard } from './guards/roles.guard';
|
|
||||||
import { MailModule } from 'src/application/mail/mail.module';
|
|
||||||
|
|
||||||
@Module({
|
|
||||||
imports: [
|
|
||||||
JwtModule.registerAsync({
|
|
||||||
imports: [ConfigModule],
|
|
||||||
inject: [ConfigService],
|
|
||||||
useFactory: async (config: ConfigService) => ({
|
|
||||||
secret: config.get('JWT_SECRET'),
|
|
||||||
signOptions: { expiresIn: config.get('JWT_EXPIRES_IN') },
|
|
||||||
}),
|
|
||||||
}),
|
|
||||||
NestjsFormDataModule,
|
|
||||||
DatabaseModule,
|
|
||||||
LoggerModule,
|
|
||||||
MailModule,
|
|
||||||
],
|
|
||||||
providers: [UsersService, ClientService, AuthGuard, RolesGuard],
|
|
||||||
exports: [JwtModule, UsersService, AuthGuard, RolesGuard],
|
|
||||||
})
|
|
||||||
export class SecureModule {}
|
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
import { Body, Controller, Get, Post, Query } from '@nestjs/common';
|
import { Body, Controller, Get, Post, Query } from '@nestjs/common';
|
||||||
import { UsersService } from 'src/users/users.service';
|
import { ClientService } from 'src/idp/client/client.service';
|
||||||
import { ClientService } from 'src/client/client.service';
|
|
||||||
import { FormDataRequest } from 'nestjs-form-data';
|
import { FormDataRequest } from 'nestjs-form-data';
|
||||||
import { CustomLogger } from 'src/core/custom.logger';
|
|
||||||
import { CreateUserDto } from 'src/model/dto/create-user.dto';
|
import { CreateUserDto } from 'src/model/dto/create-user.dto';
|
||||||
import { RequestResetPwDto, ResetPWDto } from 'src/model/dto';
|
import { RequestResetPwDto, ResetPWDto } from 'src/model/dto';
|
||||||
import { Client } from 'src/model';
|
import { Client } from 'src/model';
|
||||||
|
import { UsersService } from 'src/shared/users.service';
|
||||||
|
import { CustomLogger } from 'src/shared/logger/custom.logger';
|
||||||
|
|
||||||
@Controller('auth')
|
@Controller('auth')
|
||||||
export class AuthController {
|
export class AuthController {
|
||||||
14
idp/src/idp/auth/auth.module.ts
Normal file
14
idp/src/idp/auth/auth.module.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import { Module } from '@nestjs/common';
|
||||||
|
import { AuthController } from './auth.controller';
|
||||||
|
import { ClientService } from 'src/idp/client/client.service';
|
||||||
|
import { NestjsFormDataModule } from 'nestjs-form-data';
|
||||||
|
import { MailModule } from 'src/application/mail/mail.module';
|
||||||
|
import { SharedModule } from 'src/shared/shared.module';
|
||||||
|
import { SecureModule } from 'src/shared/secure/secure.module';
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
providers: [ClientService],
|
||||||
|
controllers: [AuthController],
|
||||||
|
imports: [NestjsFormDataModule, SharedModule, MailModule, SecureModule],
|
||||||
|
})
|
||||||
|
export class AuthModule {}
|
||||||
@@ -1,12 +1,11 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { NestjsFormDataModule } from 'nestjs-form-data';
|
import { NestjsFormDataModule } from 'nestjs-form-data';
|
||||||
import { LoggerModule } from 'src/core/logger.module';
|
|
||||||
import { ClientController } from './client.controller';
|
import { ClientController } from './client.controller';
|
||||||
import { DatabaseModule } from 'src/core/database/database.module';
|
import { SharedModule } from 'src/shared/shared.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
providers: [],
|
providers: [],
|
||||||
controllers: [ClientController],
|
controllers: [ClientController],
|
||||||
imports: [NestjsFormDataModule, DatabaseModule, LoggerModule],
|
imports: [NestjsFormDataModule, SharedModule],
|
||||||
})
|
})
|
||||||
export class ClientModule {}
|
export class ClientModule {}
|
||||||
40
idp/src/idp/client/client.service.ts
Normal file
40
idp/src/idp/client/client.service.ts
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||||
|
import {
|
||||||
|
ClientRepository,
|
||||||
|
RedirectRepository,
|
||||||
|
Client,
|
||||||
|
RedirectUri,
|
||||||
|
} from 'src/model';
|
||||||
|
import { ClientBaseService } from 'src/shared/client/client.base.service';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class ClientService extends ClientBaseService {
|
||||||
|
constructor(clientRepo: ClientRepository, uriRepo: RedirectRepository) {
|
||||||
|
super(clientRepo, uriRepo);
|
||||||
|
}
|
||||||
|
|
||||||
|
async getClient(
|
||||||
|
clientId: string,
|
||||||
|
responseType: string,
|
||||||
|
redirectUri: string,
|
||||||
|
scope: string,
|
||||||
|
): Promise<Client> {
|
||||||
|
if (responseType !== 'code') {
|
||||||
|
throw new HttpException('Invalid client', HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
const client = await this.clientRepo.findById(clientId);
|
||||||
|
// console.log(client);
|
||||||
|
if (!client) {
|
||||||
|
throw new HttpException('Invalid client', HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!client.redirectUris.some((u: RedirectUri) => u.uri === redirectUri)) {
|
||||||
|
throw new HttpException('Invalid client', HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!scope) {
|
||||||
|
throw new HttpException('Invalid client', HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
}
|
||||||
11
idp/src/idp/idp.module.ts
Normal file
11
idp/src/idp/idp.module.ts
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
import { Module } from '@nestjs/common';
|
||||||
|
import { SharedModule } from 'src/shared/shared.module';
|
||||||
|
import { AuthModule } from './auth/auth.module';
|
||||||
|
import { ClientModule } from './client/client.module';
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
providers: [],
|
||||||
|
controllers: [],
|
||||||
|
imports: [SharedModule, AuthModule, ClientModule],
|
||||||
|
})
|
||||||
|
export class IDPModule {}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import { User } from '../entity';
|
import { User } from '../entity';
|
||||||
|
|
||||||
export interface AuthenticatedRequest extends Request {
|
export interface IAuthenticatedRequest extends Request {
|
||||||
user: User;
|
user: User;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +1,2 @@
|
|||||||
export * from './authenticated.request';
|
export * from './authenticated.request';
|
||||||
|
export * from './logger.interface';
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
export interface CustomLogger {
|
export interface ICustomLogger {
|
||||||
log(message: string, context?: string): void;
|
log(message: string, context?: string): void;
|
||||||
error(message: string, trace?: string, context?: string): void;
|
error(message: string, trace?: string, context?: string): void;
|
||||||
warn(message: string, context?: string): void;
|
warn(message: string, context?: string): void;
|
||||||
|
|||||||
@@ -1,18 +1,17 @@
|
|||||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
import { HttpException, HttpStatus } from '@nestjs/common';
|
||||||
import {
|
import {
|
||||||
|
Client,
|
||||||
ClientRepository,
|
ClientRepository,
|
||||||
RedirectRepository,
|
RedirectRepository,
|
||||||
User,
|
|
||||||
Client,
|
|
||||||
RedirectUri,
|
RedirectUri,
|
||||||
|
User,
|
||||||
} from 'src/model';
|
} from 'src/model';
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
|
|
||||||
@Injectable()
|
export class ClientBaseService {
|
||||||
export class ClientService {
|
|
||||||
constructor(
|
constructor(
|
||||||
private clientRepo: ClientRepository,
|
protected clientRepo: ClientRepository,
|
||||||
private uriRepo: RedirectRepository,
|
protected uriRepo: RedirectRepository,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async createClient(
|
async createClient(
|
||||||
@@ -43,31 +42,6 @@ export class ClientService {
|
|||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getClient(
|
|
||||||
clientId: string,
|
|
||||||
responseType: string,
|
|
||||||
redirectUri: string,
|
|
||||||
scope: string,
|
|
||||||
): Promise<Client> {
|
|
||||||
if (responseType !== 'code') {
|
|
||||||
throw new HttpException('Invalid client', HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
|
||||||
const client = await this.clientRepo.findById(clientId);
|
|
||||||
// console.log(client);
|
|
||||||
if (!client) {
|
|
||||||
throw new HttpException('Invalid client', HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!client.redirectUris.some((u: RedirectUri) => u.uri === redirectUri)) {
|
|
||||||
throw new HttpException('Invalid client', HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!scope) {
|
|
||||||
throw new HttpException('Invalid client', HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
|
||||||
return client;
|
|
||||||
}
|
|
||||||
|
|
||||||
getClientById(clientId: string): Promise<Client> {
|
getClientById(clientId: string): Promise<Client> {
|
||||||
return this.clientRepo.findById(clientId);
|
return this.clientRepo.findById(clientId);
|
||||||
}
|
}
|
||||||
2
idp/src/shared/index.ts
Normal file
2
idp/src/shared/index.ts
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
export * from './logger/logger.module';
|
||||||
|
export * from './database/database.module';
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { CustomLogger } from './custom.logger';
|
import { CustomLogger } from './custom.logger';
|
||||||
import { DatabaseModule } from './database/database.module';
|
import { DatabaseModule } from '../database/database.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
providers: [CustomLogger],
|
providers: [CustomLogger],
|
||||||
@@ -6,7 +6,7 @@ import {
|
|||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { Reflector } from '@nestjs/core';
|
import { Reflector } from '@nestjs/core';
|
||||||
import { JwtService } from '@nestjs/jwt';
|
import { JwtService } from '@nestjs/jwt';
|
||||||
import { UsersService } from 'src/users/users.service';
|
import { UsersService } from 'src/shared/users.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AuthGuard implements CanActivate {
|
export class AuthGuard implements CanActivate {
|
||||||
14
idp/src/shared/secure/secure.module.ts
Normal file
14
idp/src/shared/secure/secure.module.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import { Module } from '@nestjs/common';
|
||||||
|
import { NestjsFormDataModule } from 'nestjs-form-data';
|
||||||
|
import { ClientService } from 'src/idp/client/client.service';
|
||||||
|
import { AuthGuard } from './guards/auth.guard';
|
||||||
|
import { RolesGuard } from './guards/roles.guard';
|
||||||
|
import { MailModule } from 'src/application/mail/mail.module';
|
||||||
|
import { SharedModule } from 'src/shared/shared.module';
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
imports: [NestjsFormDataModule, SharedModule, MailModule, SharedModule],
|
||||||
|
providers: [ClientService, AuthGuard, RolesGuard],
|
||||||
|
exports: [AuthGuard, RolesGuard],
|
||||||
|
})
|
||||||
|
export class SecureModule {}
|
||||||
29
idp/src/shared/shared.module.ts
Normal file
29
idp/src/shared/shared.module.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import { Module } from '@nestjs/common';
|
||||||
|
import { DatabaseModule } from './database/database.module';
|
||||||
|
import { UsersService } from './users.service';
|
||||||
|
import { MailService } from 'src/application/mail/mail.service';
|
||||||
|
import { ClientService } from 'src/idp/client/client.service';
|
||||||
|
import { LoggerModule } from './logger/logger.module';
|
||||||
|
import { JwtModule } from '@nestjs/jwt';
|
||||||
|
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||||
|
|
||||||
|
const MODULES = [DatabaseModule, LoggerModule];
|
||||||
|
const SERVICES = [UsersService, MailService, ClientService];
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
imports: [
|
||||||
|
...MODULES,
|
||||||
|
JwtModule.registerAsync({
|
||||||
|
imports: [ConfigModule],
|
||||||
|
inject: [ConfigService],
|
||||||
|
useFactory: async (config: ConfigService) => ({
|
||||||
|
secret: config.get('JWT_SECRET'),
|
||||||
|
signOptions: { expiresIn: config.get('JWT_EXPIRES_IN') },
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
controllers: [],
|
||||||
|
providers: [...SERVICES],
|
||||||
|
exports: [...MODULES, ...SERVICES, JwtModule],
|
||||||
|
})
|
||||||
|
export class SharedModule {}
|
||||||
@@ -1,9 +1,8 @@
|
|||||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
import * as bcrypt from 'bcrypt';
|
import * as bcrypt from 'bcrypt';
|
||||||
import { ClientService } from 'src/client/client.service';
|
import { ClientService } from 'src/idp/client/client.service';
|
||||||
import { JwtService } from '@nestjs/jwt';
|
import { JwtService } from '@nestjs/jwt';
|
||||||
import { CustomLogger } from 'src/core/custom.logger';
|
|
||||||
import { CreateUserDto } from 'src/model/dto/create-user.dto';
|
import { CreateUserDto } from 'src/model/dto/create-user.dto';
|
||||||
import { LoginUserDto, RequestResetPwDto, ResetPWDto } from 'src/model/dto';
|
import { LoginUserDto, RequestResetPwDto, ResetPWDto } from 'src/model/dto';
|
||||||
import { MailService } from 'src/application/mail/mail.service';
|
import { MailService } from 'src/application/mail/mail.service';
|
||||||
@@ -17,6 +16,7 @@ import {
|
|||||||
AuthorizationCode,
|
AuthorizationCode,
|
||||||
ActivityLogRepository,
|
ActivityLogRepository,
|
||||||
} from 'src/model';
|
} from 'src/model';
|
||||||
|
import { CustomLogger } from './logger/custom.logger';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class UsersService {
|
export class UsersService {
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
|
||||||
import { UsersService } from './users.service';
|
|
||||||
|
|
||||||
describe('UsersService', () => {
|
|
||||||
let service: UsersService;
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
|
||||||
providers: [UsersService],
|
|
||||||
}).compile();
|
|
||||||
|
|
||||||
service = module.get<UsersService>(UsersService);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should be defined', () => {
|
|
||||||
expect(service).toBeDefined();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
Reference in New Issue
Block a user