2023-01-10 11:30:39 +00:00
|
|
|
import {
|
|
|
|
|
Body,
|
|
|
|
|
Controller,
|
|
|
|
|
Get,
|
|
|
|
|
HttpException,
|
|
|
|
|
HttpStatus,
|
|
|
|
|
Post,
|
2023-01-11 13:59:33 +00:00
|
|
|
Request,
|
2023-01-10 11:30:39 +00:00
|
|
|
Res,
|
2023-01-11 13:59:33 +00:00
|
|
|
UseGuards,
|
2023-01-10 11:30:39 +00:00
|
|
|
} from '@nestjs/common';
|
2023-01-09 13:32:14 +00:00
|
|
|
import { AuthService } from './auth.service';
|
2023-02-01 12:22:33 +00:00
|
|
|
import { SignInMagicDto } from './dto/signin-magic.dto';
|
2023-02-01 13:17:11 +00:00
|
|
|
import { VerifyMagicDto } from './dto/verify-magic.dto';
|
2023-01-10 10:36:42 +00:00
|
|
|
import { Response } from 'express';
|
2023-01-10 11:30:39 +00:00
|
|
|
import * as E from 'fp-ts/Either';
|
2023-01-11 13:59:33 +00:00
|
|
|
import { RTJwtAuthGuard } from './guards/rt-jwt-auth.guard';
|
|
|
|
|
import { GqlUser } from 'src/decorators/gql-user.decorator';
|
|
|
|
|
import { AuthUser } from 'src/types/AuthUser';
|
|
|
|
|
import { RTCookie } from 'src/decorators/rt-cookie.decorator';
|
2023-01-12 19:22:29 +00:00
|
|
|
import { AuthGuard } from '@nestjs/passport';
|
2023-02-01 13:17:11 +00:00
|
|
|
import { authCookieHandler, throwHTTPErr } from './helper';
|
2023-01-09 13:32:14 +00:00
|
|
|
|
2023-02-01 13:49:39 +00:00
|
|
|
@Controller({ path: 'auth', version: '1' })
|
2023-01-09 13:32:14 +00:00
|
|
|
export class AuthController {
|
|
|
|
|
constructor(private authService: AuthService) {}
|
|
|
|
|
|
2023-01-30 13:25:53 +00:00
|
|
|
/**
|
|
|
|
|
** Route to initiate magic-link auth for a users email
|
|
|
|
|
*/
|
2023-01-09 13:32:14 +00:00
|
|
|
@Post('signin')
|
2023-02-01 12:22:33 +00:00
|
|
|
async signInMagicLink(@Body() authData: SignInMagicDto) {
|
2023-01-30 13:25:53 +00:00
|
|
|
const deviceIdToken = await this.authService.signInMagicLink(
|
|
|
|
|
authData.email,
|
|
|
|
|
);
|
|
|
|
|
if (E.isLeft(deviceIdToken)) throwHTTPErr(deviceIdToken.left);
|
|
|
|
|
return deviceIdToken.right;
|
2023-01-09 13:32:14 +00:00
|
|
|
}
|
2023-01-10 10:36:42 +00:00
|
|
|
|
2023-01-30 13:25:53 +00:00
|
|
|
/**
|
|
|
|
|
** Route to verify and sign in a valid user via magic-link
|
|
|
|
|
*/
|
2023-01-10 10:36:42 +00:00
|
|
|
@Post('verify')
|
2023-02-01 13:17:11 +00:00
|
|
|
async verify(@Body() data: VerifyMagicDto, @Res() res: Response) {
|
2023-01-30 13:25:53 +00:00
|
|
|
const authTokens = await this.authService.verifyMagicLinkTokens(data);
|
2023-01-10 11:30:39 +00:00
|
|
|
if (E.isLeft(authTokens)) throwHTTPErr(authTokens.left);
|
|
|
|
|
authCookieHandler(res, authTokens.right, false);
|
2023-01-10 10:36:42 +00:00
|
|
|
}
|
2023-01-11 13:59:33 +00:00
|
|
|
|
2023-01-30 13:25:53 +00:00
|
|
|
/**
|
|
|
|
|
** Route to refresh auth tokens with Refresh Token Rotation
|
|
|
|
|
* @see https://auth0.com/docs/secure/tokens/refresh-tokens/refresh-token-rotation
|
|
|
|
|
*/
|
2023-01-11 13:59:33 +00:00
|
|
|
@Get('refresh')
|
|
|
|
|
@UseGuards(RTJwtAuthGuard)
|
|
|
|
|
async refresh(
|
|
|
|
|
@GqlUser() user: AuthUser,
|
|
|
|
|
@RTCookie() refresh_token: string,
|
|
|
|
|
@Res() res,
|
|
|
|
|
) {
|
|
|
|
|
const newTokenPair = await this.authService.refreshAuthTokens(
|
|
|
|
|
refresh_token,
|
|
|
|
|
user,
|
|
|
|
|
);
|
|
|
|
|
if (E.isLeft(newTokenPair)) throwHTTPErr(newTokenPair.left);
|
|
|
|
|
authCookieHandler(res, newTokenPair.right, false);
|
|
|
|
|
}
|
2023-01-12 19:22:29 +00:00
|
|
|
|
2023-01-30 13:25:53 +00:00
|
|
|
/**
|
|
|
|
|
** Route to initiate SSO auth via Google
|
|
|
|
|
*/
|
2023-01-12 19:22:29 +00:00
|
|
|
@Get('google')
|
|
|
|
|
@UseGuards(AuthGuard('google'))
|
|
|
|
|
async googleAuth(@Request() req) {}
|
|
|
|
|
|
2023-01-30 13:25:53 +00:00
|
|
|
/**
|
|
|
|
|
** Callback URL for Google SSO
|
|
|
|
|
* @see https://auth0.com/docs/get-started/authentication-and-authorization-flow/authorization-code-flow#how-it-works
|
|
|
|
|
*/
|
2023-01-12 19:22:29 +00:00
|
|
|
@Get('google/callback')
|
|
|
|
|
@UseGuards(AuthGuard('google'))
|
|
|
|
|
async googleAuthRedirect(@Request() req, @Res() res) {
|
2023-01-19 09:43:55 +00:00
|
|
|
const authTokens = await this.authService.generateAuthTokens(req.user.uid);
|
2023-01-12 19:22:29 +00:00
|
|
|
if (E.isLeft(authTokens)) throwHTTPErr(authTokens.left);
|
|
|
|
|
authCookieHandler(res, authTokens.right, true);
|
|
|
|
|
}
|
2023-01-12 20:41:42 +00:00
|
|
|
|
2023-01-30 13:25:53 +00:00
|
|
|
/**
|
|
|
|
|
** Route to initiate SSO auth via Github
|
|
|
|
|
*/
|
2023-01-12 20:41:42 +00:00
|
|
|
@Get('github')
|
|
|
|
|
@UseGuards(AuthGuard('github'))
|
|
|
|
|
async githubAuth(@Request() req) {}
|
|
|
|
|
|
2023-01-30 13:25:53 +00:00
|
|
|
/**
|
|
|
|
|
** Callback URL for Github SSO
|
|
|
|
|
* @see https://auth0.com/docs/get-started/authentication-and-authorization-flow/authorization-code-flow#how-it-works
|
|
|
|
|
*/
|
2023-01-12 20:41:42 +00:00
|
|
|
@Get('github/callback')
|
|
|
|
|
@UseGuards(AuthGuard('github'))
|
|
|
|
|
async githubAuthRedirect(@Request() req, @Res() res) {
|
2023-01-19 09:43:55 +00:00
|
|
|
const authTokens = await this.authService.generateAuthTokens(req.user.uid);
|
2023-01-12 20:41:42 +00:00
|
|
|
if (E.isLeft(authTokens)) throwHTTPErr(authTokens.left);
|
|
|
|
|
authCookieHandler(res, authTokens.right, true);
|
|
|
|
|
}
|
2023-01-18 17:04:15 +00:00
|
|
|
|
2023-01-30 13:25:53 +00:00
|
|
|
/**
|
|
|
|
|
** Route to initiate SSO auth via Microsoft
|
|
|
|
|
*/
|
2023-01-18 17:04:15 +00:00
|
|
|
@Get('microsoft')
|
|
|
|
|
@UseGuards(AuthGuard('microsoft'))
|
|
|
|
|
async microsoftAuth(@Request() req) {}
|
|
|
|
|
|
2023-01-30 13:25:53 +00:00
|
|
|
/**
|
|
|
|
|
** Callback URL for Microsoft SSO
|
|
|
|
|
* @see https://auth0.com/docs/get-started/authentication-and-authorization-flow/authorization-code-flow#how-it-works
|
|
|
|
|
*/
|
2023-01-18 17:04:15 +00:00
|
|
|
@Get('microsoft/callback')
|
|
|
|
|
@UseGuards(AuthGuard('microsoft'))
|
|
|
|
|
async microsoftAuthRedirect(@Request() req, @Res() res) {
|
2023-01-19 09:43:55 +00:00
|
|
|
const authTokens = await this.authService.generateAuthTokens(req.user.uid);
|
2023-01-18 17:04:15 +00:00
|
|
|
if (E.isLeft(authTokens)) throwHTTPErr(authTokens.left);
|
|
|
|
|
authCookieHandler(res, authTokens.right, true);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-30 13:25:53 +00:00
|
|
|
/**
|
|
|
|
|
** Log user out by clearing cookies containing auth tokens
|
|
|
|
|
*/
|
2023-01-18 17:04:15 +00:00
|
|
|
@Get('logout')
|
|
|
|
|
async logout(@Res() res: Response) {
|
|
|
|
|
res.clearCookie('access_token');
|
|
|
|
|
res.clearCookie('refresh_token');
|
2023-01-19 09:43:55 +00:00
|
|
|
return res.status(200).send();
|
2023-01-18 17:04:15 +00:00
|
|
|
}
|
2023-01-09 13:32:14 +00:00
|
|
|
}
|