Update various SSO / SAML auth methods to support username

This commit is contained in:
Tuan Dang
2024-02-24 22:17:51 -08:00
parent bfee74ff4e
commit 8c31566e17
14 changed files with 37 additions and 33 deletions
+3 -2
View File
@@ -5,13 +5,14 @@ import { BadRequestError, UnauthorizedError } from "@app/lib/errors";
import { AuthModeProviderJwtTokenPayload, AuthModeProviderSignUpTokenPayload, AuthTokenType } from "./auth-type";
export const validateProviderAuthToken = (providerToken: string, email?: string) => {
export const validateProviderAuthToken = (providerToken: string, username?: string) => {
if (!providerToken) throw new UnauthorizedError();
const appCfg = getConfig();
const decodedToken = jwt.verify(providerToken, appCfg.AUTH_SECRET) as AuthModeProviderJwtTokenPayload;
if (decodedToken.authTokenType !== AuthTokenType.PROVIDER_TOKEN) throw new UnauthorizedError();
if (decodedToken.email !== email) throw new Error("Invalid auth credentials");
if (decodedToken.username !== username) throw new Error("Invalid auth credentials");
if (decodedToken.organizationId) {
return { orgId: decodedToken.organizationId };
@@ -133,7 +133,7 @@ export const authLoginServiceFactory = ({ userDAL, tokenService, smtpService }:
providerAuthToken,
clientPublicKey
}: TLoginGenServerPublicKeyDTO) => {
const userEnc = await userDAL.findUserEncKeyByEmail(email);
const userEnc = await userDAL.findUserEncKeyByUsername(email);
if (!userEnc || (userEnc && !userEnc.isAccepted)) {
throw new Error("Failed to find user");
}
@@ -160,7 +160,7 @@ export const authLoginServiceFactory = ({ userDAL, tokenService, smtpService }:
ip,
userAgent
}: TLoginClientProofDTO) => {
const userEnc = await userDAL.findUserEncKeyByEmail(email);
const userEnc = await userDAL.findUserEncKeyByUsername(email);
if (!userEnc) throw new Error("Failed to find user");
const cfg = getConfig();
@@ -301,7 +301,8 @@ export const authLoginServiceFactory = ({ userDAL, tokenService, smtpService }:
{
authTokenType: AuthTokenType.PROVIDER_TOKEN,
userId: user.id,
email: user.email,
// email: user.email,
username: user.username,
firstName: user.firstName,
lastName: user.lastName,
authMethod,
@@ -115,14 +115,14 @@ export const authSignupServiceFactory = ({
userAgent,
authorization
}: TCompleteAccountSignupDTO) => {
const user = await userDAL.findUserByEmail(email);
const user = await userDAL.findOne({ username: email });
if (!user || (user && user.isAccepted)) {
throw new Error("Failed to complete account for complete user");
}
let organizationId;
if (providerAuthToken) {
const { orgId } = validateProviderAuthToken(providerAuthToken, user.email as string);
const { orgId } = validateProviderAuthToken(providerAuthToken, user.username);
organizationId = orgId;
} else {
validateSignUpAuthorization(authorization, user.id);
@@ -152,7 +152,7 @@ export const authSignupServiceFactory = ({
if (!organizationId) {
await orgService.createOrganization({
userId: user.id,
userEmail: user.email,
userEmail: user.email ?? user.username ?? "", // TODO: look into
orgName: organizationName
});
}
+1 -1
View File
@@ -62,7 +62,7 @@ export type AuthModeRefreshJwtTokenPayload = {
export type AuthModeProviderJwtTokenPayload = {
authTokenType: AuthTokenType.PROVIDER_TOKEN;
email: string;
username: string;
organizationId?: string;
};
+3 -3
View File
@@ -20,10 +20,10 @@ export const userDALFactory = (db: TDbClient) => {
// USER ENCRYPTION FUNCTIONS
// -------------------------
const findUserEncKeyByEmail = async (email: string) => {
const findUserEncKeyByUsername = async (username: string) => {
try {
return await db(TableName.Users)
.where({ email, isGhost: false })
.where({ username, isGhost: false })
.join(TableName.UserEncryptionKey, `${TableName.Users}.id`, `${TableName.UserEncryptionKey}.userId`)
.first();
} catch (error) {
@@ -119,7 +119,7 @@ export const userDALFactory = (db: TDbClient) => {
return {
...userOrm,
findUserByEmail,
findUserEncKeyByEmail,
findUserEncKeyByUsername,
findUserEncKeyByUserId,
updateUserEncryptionByUserId,
findUserByProjectMembershipId,