chore: moved auth utility functions to auth/helper.ts from src/utils.ts
This commit is contained in:
@@ -11,15 +11,15 @@ import {
|
|||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { AuthService } from './auth.service';
|
import { AuthService } from './auth.service';
|
||||||
import { SignInMagicDto } from './dto/signin-magic.dto';
|
import { SignInMagicDto } from './dto/signin-magic.dto';
|
||||||
import { verifyMagicDto } from './dto/verify-magic.dto';
|
import { VerifyMagicDto } from './dto/verify-magic.dto';
|
||||||
import { Response } from 'express';
|
import { Response } from 'express';
|
||||||
import * as E from 'fp-ts/Either';
|
import * as E from 'fp-ts/Either';
|
||||||
import { authCookieHandler, throwHTTPErr } from 'src/utils';
|
|
||||||
import { RTJwtAuthGuard } from './guards/rt-jwt-auth.guard';
|
import { RTJwtAuthGuard } from './guards/rt-jwt-auth.guard';
|
||||||
import { GqlUser } from 'src/decorators/gql-user.decorator';
|
import { GqlUser } from 'src/decorators/gql-user.decorator';
|
||||||
import { AuthUser } from 'src/types/AuthUser';
|
import { AuthUser } from 'src/types/AuthUser';
|
||||||
import { RTCookie } from 'src/decorators/rt-cookie.decorator';
|
import { RTCookie } from 'src/decorators/rt-cookie.decorator';
|
||||||
import { AuthGuard } from '@nestjs/passport';
|
import { AuthGuard } from '@nestjs/passport';
|
||||||
|
import { authCookieHandler, throwHTTPErr } from './helper';
|
||||||
|
|
||||||
@Controller('/v1/auth')
|
@Controller('/v1/auth')
|
||||||
export class AuthController {
|
export class AuthController {
|
||||||
@@ -41,7 +41,7 @@ export class AuthController {
|
|||||||
** Route to verify and sign in a valid user via magic-link
|
** Route to verify and sign in a valid user via magic-link
|
||||||
*/
|
*/
|
||||||
@Post('verify')
|
@Post('verify')
|
||||||
async verify(@Body() data: verifyMagicDto, @Res() res: Response) {
|
async verify(@Body() data: VerifyMagicDto, @Res() res: Response) {
|
||||||
const authTokens = await this.authService.verifyMagicLinkTokens(data);
|
const authTokens = await this.authService.verifyMagicLinkTokens(data);
|
||||||
if (E.isLeft(authTokens)) throwHTTPErr(authTokens.left);
|
if (E.isLeft(authTokens)) throwHTTPErr(authTokens.left);
|
||||||
authCookieHandler(res, authTokens.right, false);
|
authCookieHandler(res, authTokens.right, false);
|
||||||
|
|||||||
56
packages/hoppscotch-backend/src/auth/helper.ts
Normal file
56
packages/hoppscotch-backend/src/auth/helper.ts
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
import { HttpException, HttpStatus } from '@nestjs/common';
|
||||||
|
import { DateTime } from 'luxon';
|
||||||
|
import { AuthError } from 'src/types/AuthError';
|
||||||
|
import { AuthTokens } from 'src/types/AuthTokens';
|
||||||
|
import { Response } from 'express';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This function allows throw to be used as an expression
|
||||||
|
* @param errMessage Message present in the error message
|
||||||
|
*/
|
||||||
|
export function throwHTTPErr(errorData: AuthError): never {
|
||||||
|
const { message, statusCode } = errorData;
|
||||||
|
throw new HttpException(message, statusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets and returns the cookies in the response object on successful authentication
|
||||||
|
* @param res Express Response Object
|
||||||
|
* @param authTokens Object containing the access and refresh tokens
|
||||||
|
* @param redirect if true will redirect to provided URL else just send a 200 status code
|
||||||
|
*/
|
||||||
|
export const authCookieHandler = (
|
||||||
|
res: Response,
|
||||||
|
authTokens: AuthTokens,
|
||||||
|
redirect: boolean,
|
||||||
|
) => {
|
||||||
|
const currentTime = DateTime.now();
|
||||||
|
const accessTokenValidity = currentTime
|
||||||
|
.plus({
|
||||||
|
milliseconds: parseInt(process.env.ACCESS_TOKEN_VALIDITY),
|
||||||
|
})
|
||||||
|
.toMillis();
|
||||||
|
const refreshTokenValidity = currentTime
|
||||||
|
.plus({
|
||||||
|
milliseconds: parseInt(process.env.REFRESH_TOKEN_VALIDITY),
|
||||||
|
})
|
||||||
|
.toMillis();
|
||||||
|
|
||||||
|
res.cookie('access_token', authTokens.access_token, {
|
||||||
|
httpOnly: true,
|
||||||
|
secure: true,
|
||||||
|
sameSite: 'lax',
|
||||||
|
maxAge: accessTokenValidity,
|
||||||
|
signed: true,
|
||||||
|
});
|
||||||
|
res.cookie('refresh_token', authTokens.refresh_token, {
|
||||||
|
httpOnly: true,
|
||||||
|
secure: true,
|
||||||
|
sameSite: 'lax',
|
||||||
|
maxAge: refreshTokenValidity,
|
||||||
|
signed: true,
|
||||||
|
});
|
||||||
|
if (redirect) {
|
||||||
|
res.status(HttpStatus.OK).redirect(process.env.REDIRECT_URL);
|
||||||
|
} else res.status(HttpStatus.OK).send();
|
||||||
|
};
|
||||||
@@ -40,7 +40,8 @@ beforeEach(() => {
|
|||||||
mockPubSub.publish.mockClear();
|
mockPubSub.publish.mockClear();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('findUserByEmail', () => {
|
describe('UserService', () => {
|
||||||
|
describe('findUserByEmail', () => {
|
||||||
test('should successfully return a valid user given a valid email', async () => {
|
test('should successfully return a valid user given a valid email', async () => {
|
||||||
mockPrisma.user.findUniqueOrThrow.mockResolvedValueOnce(user);
|
mockPrisma.user.findUniqueOrThrow.mockResolvedValueOnce(user);
|
||||||
|
|
||||||
@@ -56,9 +57,9 @@ describe('findUserByEmail', () => {
|
|||||||
const result = await userService.findUserByEmail('jim@dundermifflin.com');
|
const result = await userService.findUserByEmail('jim@dundermifflin.com');
|
||||||
expect(result).resolves.toBeNone;
|
expect(result).resolves.toBeNone;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('findUserById', () => {
|
describe('findUserById', () => {
|
||||||
test('should successfully return a valid user given a valid user uid', async () => {
|
test('should successfully return a valid user given a valid user uid', async () => {
|
||||||
mockPrisma.user.findUniqueOrThrow.mockResolvedValueOnce(user);
|
mockPrisma.user.findUniqueOrThrow.mockResolvedValueOnce(user);
|
||||||
|
|
||||||
@@ -72,9 +73,9 @@ describe('findUserById', () => {
|
|||||||
const result = await userService.findUserById('sdcvbdbr');
|
const result = await userService.findUserById('sdcvbdbr');
|
||||||
expect(result).resolves.toBeNone;
|
expect(result).resolves.toBeNone;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('createUserViaMagicLink', () => {
|
describe('createUserViaMagicLink', () => {
|
||||||
test('should successfully create user and account for magic-link given valid inputs', async () => {
|
test('should successfully create user and account for magic-link given valid inputs', async () => {
|
||||||
mockPrisma.user.create.mockResolvedValueOnce(user);
|
mockPrisma.user.create.mockResolvedValueOnce(user);
|
||||||
|
|
||||||
@@ -83,9 +84,9 @@ describe('createUserViaMagicLink', () => {
|
|||||||
);
|
);
|
||||||
expect(result).toEqual(user);
|
expect(result).toEqual(user);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('createUserSSO', () => {
|
describe('createUserSSO', () => {
|
||||||
test('should successfully create user and account for SSO provider given valid inputs ', async () => {
|
test('should successfully create user and account for SSO provider given valid inputs ', async () => {
|
||||||
mockPrisma.user.create.mockResolvedValueOnce(user);
|
mockPrisma.user.create.mockResolvedValueOnce(user);
|
||||||
|
|
||||||
@@ -130,9 +131,9 @@ describe('createUserSSO', () => {
|
|||||||
photoURL: null,
|
photoURL: null,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('createProviderAccount', () => {
|
describe('createProviderAccount', () => {
|
||||||
test('should successfully create ProviderAccount for user given valid inputs ', async () => {
|
test('should successfully create ProviderAccount for user given valid inputs ', async () => {
|
||||||
mockPrisma.account.create.mockResolvedValueOnce({
|
mockPrisma.account.create.mockResolvedValueOnce({
|
||||||
id: '123dcdc',
|
id: '123dcdc',
|
||||||
@@ -222,9 +223,9 @@ describe('createProviderAccount', () => {
|
|||||||
loggedIn: currentTime,
|
loggedIn: currentTime,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('updateUserSessions', () => {
|
describe('updateUserSessions', () => {
|
||||||
test('Should resolve right and update users GQL session', async () => {
|
test('Should resolve right and update users GQL session', async () => {
|
||||||
const sessionData = user.currentGQLSession;
|
const sessionData = user.currentGQLSession;
|
||||||
|
|
||||||
@@ -310,4 +311,5 @@ describe('updateUserSessions', () => {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { ExecutionContext, HttpException, HttpStatus } from '@nestjs/common';
|
import { ExecutionContext } from '@nestjs/common';
|
||||||
import { GqlExecutionContext } from '@nestjs/graphql';
|
import { GqlExecutionContext } from '@nestjs/graphql';
|
||||||
import { pipe } from 'fp-ts/lib/function';
|
import { pipe } from 'fp-ts/lib/function';
|
||||||
import * as O from 'fp-ts/Option';
|
import * as O from 'fp-ts/Option';
|
||||||
@@ -7,10 +7,6 @@ import * as T from 'fp-ts/Task';
|
|||||||
import * as E from 'fp-ts/Either';
|
import * as E from 'fp-ts/Either';
|
||||||
import { User } from './user/user.model';
|
import { User } from './user/user.model';
|
||||||
import * as A from 'fp-ts/Array';
|
import * as A from 'fp-ts/Array';
|
||||||
import { AuthError } from './types/AuthError';
|
|
||||||
import { AuthTokens } from './types/AuthTokens';
|
|
||||||
import { Response } from 'express';
|
|
||||||
import { DateTime } from 'luxon';
|
|
||||||
import { JSON_INVALID } from './errors';
|
import { JSON_INVALID } from './errors';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -23,15 +19,6 @@ export function throwErr(errMessage: string): never {
|
|||||||
throw new Error(errMessage);
|
throw new Error(errMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* This function allows throw to be used as an expression
|
|
||||||
* @param errMessage Message present in the error message
|
|
||||||
*/
|
|
||||||
export function throwHTTPErr(errorData: AuthError): never {
|
|
||||||
const { message, statusCode } = errorData;
|
|
||||||
throw new HttpException(message, statusCode);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prints the given value to log and returns the same value.
|
* Prints the given value to log and returns the same value.
|
||||||
* Used for debugging functional pipelines.
|
* Used for debugging functional pipelines.
|
||||||
@@ -136,48 +123,6 @@ export const validateEmail = (email: string) => {
|
|||||||
).test(email);
|
).test(email);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets and returns the cookies in the response object on successful authentication
|
|
||||||
* @param res Express Response Object
|
|
||||||
* @param authTokens Object containing the access and refresh tokens
|
|
||||||
* @param redirect if true will redirect to provided URL else just send a 200 status code
|
|
||||||
*/
|
|
||||||
export const authCookieHandler = (
|
|
||||||
res: Response,
|
|
||||||
authTokens: AuthTokens,
|
|
||||||
redirect: boolean,
|
|
||||||
) => {
|
|
||||||
const currentTime = DateTime.now();
|
|
||||||
const accessTokenValidity = currentTime
|
|
||||||
.plus({
|
|
||||||
milliseconds: parseInt(process.env.ACCESS_TOKEN_VALIDITY),
|
|
||||||
})
|
|
||||||
.toMillis();
|
|
||||||
const refreshTokenValidity = currentTime
|
|
||||||
.plus({
|
|
||||||
milliseconds: parseInt(process.env.REFRESH_TOKEN_VALIDITY),
|
|
||||||
})
|
|
||||||
.toMillis();
|
|
||||||
|
|
||||||
res.cookie('access_token', authTokens.access_token, {
|
|
||||||
httpOnly: true,
|
|
||||||
secure: true,
|
|
||||||
sameSite: 'lax',
|
|
||||||
maxAge: accessTokenValidity,
|
|
||||||
signed: true,
|
|
||||||
});
|
|
||||||
res.cookie('refresh_token', authTokens.refresh_token, {
|
|
||||||
httpOnly: true,
|
|
||||||
secure: true,
|
|
||||||
sameSite: 'lax',
|
|
||||||
maxAge: refreshTokenValidity,
|
|
||||||
signed: true,
|
|
||||||
});
|
|
||||||
if (redirect) {
|
|
||||||
res.status(HttpStatus.OK).redirect(process.env.REDIRECT_URL);
|
|
||||||
} else res.status(HttpStatus.OK).send();
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* String to JSON parser
|
* String to JSON parser
|
||||||
* @param {str} str The string to parse
|
* @param {str} str The string to parse
|
||||||
|
|||||||
Reference in New Issue
Block a user