feat: add ldap login to auth controller

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2022-04-02 22:34:54 +02:00
parent 82dd9f8885
commit b4e62c8425

View file

@ -8,6 +8,7 @@ import {
Body, Body,
Controller, Controller,
Delete, Delete,
Param,
Post, Post,
Put, Put,
Req, Req,
@ -17,6 +18,8 @@ import { ApiTags } from '@nestjs/swagger';
import { Session } from 'express-session'; import { Session } from 'express-session';
import { IdentityService } from '../../../identity/identity.service'; import { IdentityService } from '../../../identity/identity.service';
import { LdapLoginDto } from '../../../identity/ldap/ldap-login.dto';
import { LdapAuthGuard } from '../../../identity/ldap/ldap.strategy';
import { LocalAuthGuard } from '../../../identity/local/local.strategy'; import { LocalAuthGuard } from '../../../identity/local/local.strategy';
import { LoginDto } from '../../../identity/local/login.dto'; import { LoginDto } from '../../../identity/local/login.dto';
import { RegisterDto } from '../../../identity/local/register.dto'; import { RegisterDto } from '../../../identity/local/register.dto';
@ -30,6 +33,13 @@ import { OpenApi } from '../../utils/openapi.decorator';
import { RegistrationEnabledGuard } from '../../utils/registration-enabled.guard'; import { RegistrationEnabledGuard } from '../../utils/registration-enabled.guard';
import { RequestUser } from '../../utils/request-user.decorator'; import { RequestUser } from '../../utils/request-user.decorator';
type RequestWithSession = Request & {
session: {
authProvider: string;
user: string;
};
};
@ApiTags('auth') @ApiTags('auth')
@Controller('auth') @Controller('auth')
export class AuthController { export class AuthController {
@ -76,12 +86,7 @@ export class AuthController {
@OpenApi(201, 400, 401) @OpenApi(201, 400, 401)
login( login(
@Req() @Req()
request: Request & { request: RequestWithSession,
session: {
authProvider: string;
user: string;
};
},
@Body() loginDto: LoginDto, @Body() loginDto: LoginDto,
): void { ): void {
// There is no further testing needed as we only get to this point if LocalAuthGuard was successful // There is no further testing needed as we only get to this point if LocalAuthGuard was successful
@ -89,6 +94,20 @@ export class AuthController {
request.session.authProvider = 'local'; request.session.authProvider = 'local';
} }
@UseGuards(LdapAuthGuard)
@Post('ldap/:ldapIdentifier')
@OpenApi(201, 400, 401)
loginWithLdap(
@Req()
request: RequestWithSession,
@Param('ldapIdentifier') ldapIdentifier: string,
@Body() loginDto: LdapLoginDto,
): void {
// There is no further testing needed as we only get to this point if LocalAuthGuard was successful
request.session.user = loginDto.username;
request.session.authProvider = 'ldap';
}
@UseGuards(SessionGuard) @UseGuards(SessionGuard)
@Delete('logout') @Delete('logout')
@OpenApi(204, 400, 401) @OpenApi(204, 400, 401)