2022-08-30 19:58:35 +00:00
|
|
|
import { IdentityProvider } from "@prisma/client";
|
2022-02-15 20:30:52 +00:00
|
|
|
import { compare, hash } from "bcryptjs";
|
2022-08-09 09:21:15 +00:00
|
|
|
import type { NextApiRequest } from "next";
|
2022-08-03 19:18:26 +00:00
|
|
|
import type { Session } from "next-auth";
|
2022-07-22 17:27:06 +00:00
|
|
|
import { getSession as getSessionInner, GetSessionParams } from "next-auth/react";
|
2022-02-15 20:30:52 +00:00
|
|
|
|
2022-08-09 09:21:15 +00:00
|
|
|
import { HttpError } from "@calcom/lib/http-error";
|
|
|
|
|
2022-02-15 20:30:52 +00:00
|
|
|
export async function hashPassword(password: string) {
|
|
|
|
const hashedPassword = await hash(password, 12);
|
|
|
|
return hashedPassword;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function verifyPassword(password: string, hashedPassword: string) {
|
|
|
|
const isValid = await compare(password, hashedPassword);
|
|
|
|
return isValid;
|
|
|
|
}
|
2022-07-22 17:27:06 +00:00
|
|
|
|
2022-08-30 19:46:52 +00:00
|
|
|
export function validPassword(password: string) {
|
|
|
|
if (password.length < 7) return false;
|
|
|
|
|
|
|
|
if (!/[A-Z]/.test(password) || !/[a-z]/.test(password)) return false;
|
|
|
|
|
|
|
|
if (!/\d+/.test(password)) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-22 17:27:06 +00:00
|
|
|
export async function getSession(options: GetSessionParams): Promise<Session | null> {
|
|
|
|
const session = await getSessionInner(options);
|
|
|
|
|
|
|
|
// that these are equal are ensured in `[...nextauth]`'s callback
|
|
|
|
return session as Session | null;
|
|
|
|
}
|
2022-07-27 23:28:21 +00:00
|
|
|
|
2022-08-02 05:52:25 +00:00
|
|
|
export function isPasswordValid(password: string): boolean;
|
|
|
|
export function isPasswordValid(
|
|
|
|
password: string,
|
2022-11-09 16:23:39 +00:00
|
|
|
breakdown: boolean,
|
|
|
|
strict?: boolean
|
2022-11-21 08:03:45 +00:00
|
|
|
): { caplow: boolean; num: boolean; min: boolean; admin_min: boolean };
|
2022-11-09 16:23:39 +00:00
|
|
|
export function isPasswordValid(password: string, breakdown?: boolean, strict?: boolean) {
|
2022-07-29 13:23:54 +00:00
|
|
|
let cap = false, // Has uppercase characters
|
|
|
|
low = false, // Has lowercase characters
|
|
|
|
num = false, // At least one number
|
2022-11-21 08:03:45 +00:00
|
|
|
min = false, // Eight characters, or fifteen in strict mode.
|
|
|
|
admin_min = false;
|
2022-11-09 16:23:39 +00:00
|
|
|
if (password.length > 7 && (!strict || password.length > 14)) min = true;
|
2022-11-21 08:03:45 +00:00
|
|
|
if (strict && password.length > 14) admin_min = true;
|
2022-07-27 23:28:21 +00:00
|
|
|
for (let i = 0; i < password.length; i++) {
|
|
|
|
if (!isNaN(parseInt(password[i]))) num = true;
|
|
|
|
else {
|
|
|
|
if (password[i] === password[i].toUpperCase()) cap = true;
|
|
|
|
if (password[i] === password[i].toLowerCase()) low = true;
|
|
|
|
}
|
|
|
|
}
|
2022-11-28 14:57:02 +00:00
|
|
|
|
|
|
|
if (!breakdown) return cap && low && num && min && (strict ? admin_min : true);
|
|
|
|
|
|
|
|
let errors: Record<string, boolean> = { caplow: cap && low, num, min };
|
|
|
|
// Only return the admin key if strict mode is enabled.
|
|
|
|
if (strict) errors = { ...errors, admin_min };
|
|
|
|
|
|
|
|
return errors;
|
2022-08-02 05:52:25 +00:00
|
|
|
}
|
2022-08-09 09:21:15 +00:00
|
|
|
|
|
|
|
type CtxOrReq = { req: NextApiRequest; ctx?: never } | { ctx: { req: NextApiRequest }; req?: never };
|
|
|
|
|
|
|
|
export const ensureSession = async (ctxOrReq: CtxOrReq) => {
|
|
|
|
const session = await getSession(ctxOrReq);
|
|
|
|
if (!session?.user.id) throw new HttpError({ statusCode: 401, message: "Unauthorized" });
|
|
|
|
return session;
|
|
|
|
};
|
2022-08-30 19:58:35 +00:00
|
|
|
|
|
|
|
export enum ErrorCode {
|
|
|
|
UserNotFound = "user-not-found",
|
|
|
|
IncorrectPassword = "incorrect-password",
|
|
|
|
UserMissingPassword = "missing-password",
|
|
|
|
TwoFactorDisabled = "two-factor-disabled",
|
|
|
|
TwoFactorAlreadyEnabled = "two-factor-already-enabled",
|
|
|
|
TwoFactorSetupRequired = "two-factor-setup-required",
|
|
|
|
SecondFactorRequired = "second-factor-required",
|
|
|
|
IncorrectTwoFactorCode = "incorrect-two-factor-code",
|
|
|
|
InternalServerError = "internal-server-error",
|
|
|
|
NewPasswordMatchesOld = "new-password-matches-old",
|
|
|
|
ThirdPartyIdentityProviderEnabled = "third-party-identity-provider-enabled",
|
|
|
|
RateLimitExceeded = "rate-limit-exceeded",
|
2022-10-25 00:32:00 +00:00
|
|
|
SocialIdentityProviderRequired = "social-identity-provider-required",
|
2022-08-30 19:58:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const identityProviderNameMap: { [key in IdentityProvider]: string } = {
|
|
|
|
[IdentityProvider.CAL]: "Cal",
|
|
|
|
[IdentityProvider.GOOGLE]: "Google",
|
|
|
|
[IdentityProvider.SAML]: "SAML",
|
|
|
|
};
|