mirror of
https://github.com/CaramelFur/Picsur.git
synced 2025-11-18 01:00:38 +01:00
relocate auth module
This commit is contained in:
@@ -5,12 +5,12 @@ import { PicsurConfigModule } from './config/config.module';
|
|||||||
import { ServeStaticConfigService } from './config/servestatic.config.service';
|
import { ServeStaticConfigService } from './config/servestatic.config.service';
|
||||||
import { TypeOrmConfigService } from './config/typeorm.config.service';
|
import { TypeOrmConfigService } from './config/typeorm.config.service';
|
||||||
import { PicsurLoggerModule } from './logger/logger.module';
|
import { PicsurLoggerModule } from './logger/logger.module';
|
||||||
|
import { AuthManagerModule } from './managers/auth/auth.module';
|
||||||
import { DemoManagerModule } from './managers/demo/demomanager.module';
|
import { DemoManagerModule } from './managers/demo/demomanager.module';
|
||||||
import { AuthModule } from './routes/api/auth/auth.module';
|
import { AuthModule } from './routes/api/auth/auth.module';
|
||||||
import { PrefModule } from './routes/api/pref/pref.module';
|
import { PrefModule } from './routes/api/pref/pref.module';
|
||||||
import { ImageModule } from './routes/image/imageroute.module';
|
import { ImageModule } from './routes/image/imageroute.module';
|
||||||
|
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
TypeOrmModule.forRootAsync({
|
TypeOrmModule.forRootAsync({
|
||||||
@@ -21,6 +21,7 @@ import { ImageModule } from './routes/image/imageroute.module';
|
|||||||
useExisting: ServeStaticConfigService,
|
useExisting: ServeStaticConfigService,
|
||||||
imports: [PicsurConfigModule],
|
imports: [PicsurConfigModule],
|
||||||
}),
|
}),
|
||||||
|
AuthManagerModule,
|
||||||
AuthModule,
|
AuthModule,
|
||||||
ImageModule,
|
ImageModule,
|
||||||
DemoManagerModule,
|
DemoManagerModule,
|
||||||
|
|||||||
@@ -24,5 +24,6 @@ export class SysPreferenceDefaultsService {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
jwt_expires_in: () => this.jwtConfigService.getJwtExpiresIn() ?? '7d',
|
jwt_expires_in: () => this.jwtConfigService.getJwtExpiresIn() ?? '7d',
|
||||||
|
upload_require_auth: () => 'true',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
55
backend/src/managers/auth/auth.module.ts
Normal file
55
backend/src/managers/auth/auth.module.ts
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
import { Logger, Module, OnModuleInit } from '@nestjs/common';
|
||||||
|
import { JwtModule } from '@nestjs/jwt';
|
||||||
|
import { PassportModule } from '@nestjs/passport';
|
||||||
|
import { SysPreferenceModule } from '../../collections/syspreferencesdb/syspreferencedb.module';
|
||||||
|
import { UsersModule } from '../../collections/userdb/userdb.module';
|
||||||
|
import { AuthConfigService } from '../../config/auth.config.service';
|
||||||
|
import {
|
||||||
|
JwtConfigService,
|
||||||
|
JwtSecretProvider
|
||||||
|
} from '../../config/jwt.lateconfig.service';
|
||||||
|
import { PicsurLateConfigModule } from '../../config/lateconfig.module';
|
||||||
|
import { AuthManagerService } from './auth.service';
|
||||||
|
import { JwtStrategy } from './guards/jwt.strategy';
|
||||||
|
import { LocalAuthStrategy } from './guards/localauth.strategy';
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
imports: [
|
||||||
|
UsersModule,
|
||||||
|
PassportModule,
|
||||||
|
SysPreferenceModule,
|
||||||
|
PicsurLateConfigModule,
|
||||||
|
JwtModule.registerAsync({
|
||||||
|
useExisting: JwtConfigService,
|
||||||
|
imports: [PicsurLateConfigModule],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
providers: [
|
||||||
|
AuthManagerService,
|
||||||
|
LocalAuthStrategy,
|
||||||
|
JwtStrategy,
|
||||||
|
JwtSecretProvider,
|
||||||
|
],
|
||||||
|
exports: [AuthManagerService],
|
||||||
|
})
|
||||||
|
export class AuthManagerModule implements OnModuleInit {
|
||||||
|
private readonly logger = new Logger('AuthModule');
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private authService: AuthManagerService,
|
||||||
|
private authConfigService: AuthConfigService,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
async onModuleInit() {
|
||||||
|
await this.ensureAdminExists();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async ensureAdminExists() {
|
||||||
|
const username = this.authConfigService.getDefaultAdminUsername();
|
||||||
|
const password = this.authConfigService.getDefaultAdminPassword();
|
||||||
|
this.logger.debug(`Ensuring admin user "${username}" exists`);
|
||||||
|
|
||||||
|
await this.authService.createUser(username, password);
|
||||||
|
await this.authService.makeAdmin(username);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,11 +5,11 @@ import { instanceToPlain, plainToClass } from 'class-transformer';
|
|||||||
import { validate } from 'class-validator';
|
import { validate } from 'class-validator';
|
||||||
import { JwtDataDto } from 'picsur-shared/dist/dto/auth.dto';
|
import { JwtDataDto } from 'picsur-shared/dist/dto/auth.dto';
|
||||||
import { AsyncFailable, Fail, HasFailed } from 'picsur-shared/dist/types';
|
import { AsyncFailable, Fail, HasFailed } from 'picsur-shared/dist/types';
|
||||||
import { UsersService } from '../../../collections/userdb/userdb.service';
|
import { UsersService } from '../../collections/userdb/userdb.service';
|
||||||
import { EUserBackend } from '../../../models/entities/user.entity';
|
import { EUserBackend } from '../../models/entities/user.entity';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AuthService {
|
export class AuthManagerService {
|
||||||
private readonly logger = new Logger('AuthService');
|
private readonly logger = new Logger('AuthService');
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@@ -3,11 +3,11 @@ import { PassportStrategy } from '@nestjs/passport';
|
|||||||
import { Strategy } from 'passport-local';
|
import { Strategy } from 'passport-local';
|
||||||
import { AsyncFailable, HasFailed } from 'picsur-shared/dist/types';
|
import { AsyncFailable, HasFailed } from 'picsur-shared/dist/types';
|
||||||
import { EUserBackend } from '../../../models/entities/user.entity';
|
import { EUserBackend } from '../../../models/entities/user.entity';
|
||||||
import { AuthService } from './auth.service';
|
import { AuthManagerService } from '../auth.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class LocalAuthStrategy extends PassportStrategy(Strategy, 'local') {
|
export class LocalAuthStrategy extends PassportStrategy(Strategy, 'local') {
|
||||||
constructor(private authService: AuthService) {
|
constructor(private authService: AuthManagerService) {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import { FastifyRequest } from 'fastify';
|
import { FastifyRequest } from 'fastify';
|
||||||
import { EUserBackend } from '../../../models/entities/user.entity';
|
import { EUserBackend } from '../entities/user.entity';
|
||||||
|
|
||||||
export default interface AuthFasityRequest extends FastifyRequest {
|
export default interface AuthFasityRequest extends FastifyRequest {
|
||||||
user: EUserBackend;
|
user: EUserBackend;
|
||||||
@@ -8,15 +8,15 @@ import {
|
|||||||
AuthRegisterRequest
|
AuthRegisterRequest
|
||||||
} from 'picsur-shared/dist/dto/auth.dto';
|
} from 'picsur-shared/dist/dto/auth.dto';
|
||||||
import { HasFailed } from 'picsur-shared/dist/types';
|
import { HasFailed } from 'picsur-shared/dist/types';
|
||||||
import { AdminGuard } from './admin.guard';
|
import { AuthManagerService } from '../../../managers/auth/auth.service';
|
||||||
import { AuthService } from './auth.service';
|
import { AdminGuard } from '../../../managers/auth/guards/admin.guard';
|
||||||
import AuthFasityRequest from './authrequest';
|
import { JwtAuthGuard } from '../../../managers/auth/guards/jwt.guard';
|
||||||
import { JwtAuthGuard } from './jwt.guard';
|
import { LocalAuthGuard } from '../../../managers/auth/guards/localauth.guard';
|
||||||
import { LocalAuthGuard } from './localauth.guard';
|
import AuthFasityRequest from '../../../models/dto/authrequest.dto';
|
||||||
|
|
||||||
@Controller('api/auth')
|
@Controller('api/auth')
|
||||||
export class AuthController {
|
export class AuthController {
|
||||||
constructor(private authService: AuthService) {}
|
constructor(private authService: AuthManagerService) {}
|
||||||
|
|
||||||
@UseGuards(LocalAuthGuard)
|
@UseGuards(LocalAuthGuard)
|
||||||
@Post('login')
|
@Post('login')
|
||||||
|
|||||||
@@ -1,54 +1,9 @@
|
|||||||
import { Logger, Module, OnModuleInit } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { JwtModule } from '@nestjs/jwt';
|
import { AuthManagerModule } from '../../../managers/auth/auth.module';
|
||||||
import { PassportModule } from '@nestjs/passport';
|
|
||||||
import { SysPreferenceModule } from '../../../collections/syspreferencesdb/syspreferencedb.module';
|
|
||||||
import { UsersModule } from '../../../collections/userdb/userdb.module';
|
|
||||||
import { AuthConfigService } from '../../../config/auth.config.service';
|
|
||||||
import { JwtConfigService, JwtSecretProvider } from '../../../config/jwt.lateconfig.service';
|
|
||||||
import { PicsurLateConfigModule } from '../../../config/lateconfig.module';
|
|
||||||
import { AuthController } from './auth.controller';
|
import { AuthController } from './auth.controller';
|
||||||
import { AuthService } from './auth.service';
|
|
||||||
import { JwtStrategy } from './jwt.strategy';
|
|
||||||
import { LocalAuthStrategy } from './localauth.strategy';
|
|
||||||
|
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [AuthManagerModule],
|
||||||
UsersModule,
|
|
||||||
PassportModule,
|
|
||||||
SysPreferenceModule,
|
|
||||||
PicsurLateConfigModule,
|
|
||||||
JwtModule.registerAsync({
|
|
||||||
useExisting: JwtConfigService,
|
|
||||||
imports: [PicsurLateConfigModule],
|
|
||||||
}),
|
|
||||||
],
|
|
||||||
providers: [
|
|
||||||
AuthService,
|
|
||||||
LocalAuthStrategy,
|
|
||||||
JwtStrategy,
|
|
||||||
JwtSecretProvider,
|
|
||||||
],
|
|
||||||
controllers: [AuthController],
|
controllers: [AuthController],
|
||||||
})
|
})
|
||||||
export class AuthModule implements OnModuleInit {
|
export class AuthModule {}
|
||||||
private readonly logger = new Logger('AuthModule');
|
|
||||||
|
|
||||||
constructor(
|
|
||||||
private authService: AuthService,
|
|
||||||
private authConfigService: AuthConfigService,
|
|
||||||
) {}
|
|
||||||
|
|
||||||
async onModuleInit() {
|
|
||||||
await this.ensureAdminExists();
|
|
||||||
}
|
|
||||||
|
|
||||||
private async ensureAdminExists() {
|
|
||||||
const username = this.authConfigService.getDefaultAdminUsername();
|
|
||||||
const password = this.authConfigService.getDefaultAdminPassword();
|
|
||||||
this.logger.debug(`Ensuring admin user "${username}" exists`);
|
|
||||||
|
|
||||||
await this.authService.createUser(username, password);
|
|
||||||
await this.authService.makeAdmin(username);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ import {
|
|||||||
} from 'picsur-shared/dist/dto/syspreferences.dto';
|
} from 'picsur-shared/dist/dto/syspreferences.dto';
|
||||||
import { HasFailed } from 'picsur-shared/dist/types';
|
import { HasFailed } from 'picsur-shared/dist/types';
|
||||||
import { SysPreferenceService } from '../../../collections/syspreferencesdb/syspreferencedb.service';
|
import { SysPreferenceService } from '../../../collections/syspreferencesdb/syspreferencedb.service';
|
||||||
import { AdminGuard } from '../auth/admin.guard';
|
import { AdminGuard } from '../../../managers/auth/guards/admin.guard';
|
||||||
import { JwtAuthGuard } from '../auth/jwt.guard';
|
import { JwtAuthGuard } from '../../../managers/auth/guards/jwt.guard';
|
||||||
|
|
||||||
@UseGuards(JwtAuthGuard, AdminGuard)
|
@UseGuards(JwtAuthGuard, AdminGuard)
|
||||||
@Controller('api/pref')
|
@Controller('api/pref')
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
import { generateRandomString } from '../util/random';
|
|
||||||
import tuple from '../types/tuple';
|
|
||||||
import { randomBytes } from 'crypto';
|
|
||||||
import { IsNotEmpty } from 'class-validator';
|
import { IsNotEmpty } from 'class-validator';
|
||||||
|
import tuple from '../types/tuple';
|
||||||
|
|
||||||
const SysPreferencesTuple = tuple('jwt_secret', 'jwt_expires_in');
|
const SysPreferencesTuple = tuple(
|
||||||
|
'jwt_secret',
|
||||||
|
'jwt_expires_in',
|
||||||
|
'upload_require_auth',
|
||||||
|
);
|
||||||
|
|
||||||
export const SysPreferences: string[] = SysPreferencesTuple;
|
export const SysPreferences: string[] = SysPreferencesTuple;
|
||||||
export type SysPreferences = typeof SysPreferencesTuple[number];
|
export type SysPreferences = typeof SysPreferencesTuple[number];
|
||||||
|
|||||||
Reference in New Issue
Block a user