2024-03-20 19:32:04 +01:00
|
|
|
import { Body, Controller, Get, HttpStatus, Post, Redirect, Req, Res } from '@nestjs/common';
|
|
|
|
|
import { ApiTags } from '@nestjs/swagger';
|
|
|
|
|
import { Request, Response } from 'express';
|
2024-04-19 11:19:23 -04:00
|
|
|
import { AuthType } from 'src/constants';
|
2023-01-23 23:13:42 -05:00
|
|
|
import {
|
2023-12-09 23:34:12 -05:00
|
|
|
AuthDto,
|
2024-04-19 11:19:23 -04:00
|
|
|
ImmichCookie,
|
2023-01-23 23:13:42 -05:00
|
|
|
LoginResponseDto,
|
2023-09-01 07:08:42 -04:00
|
|
|
OAuthAuthorizeResponseDto,
|
2023-01-23 23:13:42 -05:00
|
|
|
OAuthCallbackDto,
|
|
|
|
|
OAuthConfigDto,
|
2024-03-20 23:53:07 +01:00
|
|
|
} from 'src/dtos/auth.dto';
|
2024-05-26 18:15:52 -04:00
|
|
|
import { UserAdminResponseDto } from 'src/dtos/user.dto';
|
2024-05-09 13:58:44 -04:00
|
|
|
import { Auth, Authenticated, GetLoginDetails } from 'src/middleware/auth.guard';
|
2024-03-21 00:07:30 +01:00
|
|
|
import { AuthService, LoginDetails } from 'src/services/auth.service';
|
2024-04-19 11:19:23 -04:00
|
|
|
import { respondWithCookie } from 'src/utils/response';
|
2022-11-14 21:24:25 -05:00
|
|
|
|
|
|
|
|
@ApiTags('OAuth')
|
|
|
|
|
@Controller('oauth')
|
|
|
|
|
export class OAuthController {
|
2023-07-15 00:03:56 -04:00
|
|
|
constructor(private service: AuthService) {}
|
2022-11-14 21:24:25 -05:00
|
|
|
|
2022-12-29 15:47:30 -05:00
|
|
|
@Get('mobile-redirect')
|
|
|
|
|
@Redirect()
|
2024-02-02 04:18:00 +01:00
|
|
|
redirectOAuthToMobile(@Req() request: Request) {
|
2023-03-24 00:53:56 -04:00
|
|
|
return {
|
2024-02-02 04:18:00 +01:00
|
|
|
url: this.service.getMobileRedirect(request.url),
|
2023-03-24 00:53:56 -04:00
|
|
|
statusCode: HttpStatus.TEMPORARY_REDIRECT,
|
|
|
|
|
};
|
2022-12-29 15:47:30 -05:00
|
|
|
}
|
|
|
|
|
|
2023-09-01 07:08:42 -04:00
|
|
|
@Post('authorize')
|
2023-11-03 21:33:15 -04:00
|
|
|
startOAuth(@Body() dto: OAuthConfigDto): Promise<OAuthAuthorizeResponseDto> {
|
2023-09-01 07:08:42 -04:00
|
|
|
return this.service.authorize(dto);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 15:47:30 -05:00
|
|
|
@Post('callback')
|
2023-11-03 21:33:15 -04:00
|
|
|
async finishOAuth(
|
2023-01-23 23:13:42 -05:00
|
|
|
@Res({ passthrough: true }) res: Response,
|
2023-03-31 17:14:01 +02:00
|
|
|
@Body() dto: OAuthCallbackDto,
|
2023-04-25 22:19:23 -04:00
|
|
|
@GetLoginDetails() loginDetails: LoginDetails,
|
2022-11-20 11:43:10 -06:00
|
|
|
): Promise<LoginResponseDto> {
|
2024-04-19 11:19:23 -04:00
|
|
|
const body = await this.service.callback(dto, loginDetails);
|
|
|
|
|
return respondWithCookie(res, body, {
|
|
|
|
|
isSecure: loginDetails.isSecure,
|
|
|
|
|
values: [
|
|
|
|
|
{ key: ImmichCookie.ACCESS_TOKEN, value: body.accessToken },
|
|
|
|
|
{ key: ImmichCookie.AUTH_TYPE, value: AuthType.OAUTH },
|
|
|
|
|
{ key: ImmichCookie.IS_AUTHENTICATED, value: 'true' },
|
|
|
|
|
],
|
|
|
|
|
});
|
2022-11-14 21:24:25 -05:00
|
|
|
}
|
2022-12-26 10:35:52 -05:00
|
|
|
|
|
|
|
|
@Post('link')
|
2024-05-09 13:58:44 -04:00
|
|
|
@Authenticated()
|
2024-05-26 18:15:52 -04:00
|
|
|
linkOAuthAccount(@Auth() auth: AuthDto, @Body() dto: OAuthCallbackDto): Promise<UserAdminResponseDto> {
|
2023-12-09 23:34:12 -05:00
|
|
|
return this.service.link(auth, dto);
|
2022-12-26 10:35:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post('unlink')
|
2024-05-09 13:58:44 -04:00
|
|
|
@Authenticated()
|
2024-05-26 18:15:52 -04:00
|
|
|
unlinkOAuthAccount(@Auth() auth: AuthDto): Promise<UserAdminResponseDto> {
|
2023-12-09 23:34:12 -05:00
|
|
|
return this.service.unlink(auth);
|
2022-12-26 10:35:52 -05:00
|
|
|
}
|
2022-11-14 21:24:25 -05:00
|
|
|
}
|