Skip to content

Commit

Permalink
move flattenObject into util functions file
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-trajanovski committed Oct 18, 2023
1 parent 15bacf1 commit 7fa5615
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
21 changes: 2 additions & 19 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { User } from "src/users/schemas/user.schema";
import { UsersService } from "../users/users.service";
import { Request } from "express";
import { OidcConfig } from "src/config/configuration";
import { parseBoolean } from "src/common/utils";
import { flattenObject, parseBoolean } from "src/common/utils";
import { Issuer } from "openid-client";

@Injectable()
Expand Down Expand Up @@ -75,23 +75,6 @@ export class AuthService {
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
flattenObject = (obj: any) => {
const result: Record<string, unknown> = {};

for (const i in obj) {
if (typeof obj[i] === "object" && !Array.isArray(obj[i])) {
const temp = this.flattenObject(obj[i]);
for (const j in temp) {
result[i + "." + j] = temp[j];
}
} else {
result[i] = obj[i];
}
}
return result;
};

async additionalLogoutTasks(req: Request, logoutURL: string) {
const user = req.user as Omit<User, "password">;
if (user?.authStrategy === "oidc") {
Expand All @@ -111,7 +94,7 @@ export class AuthService {
`${oidcConfig?.issuer}/.well-known/openid-configuration`,
);
// Flatten the object in case the end_session url is nested.
const flattenTrustIssuer = this.flattenObject(trustIssuer);
const flattenTrustIssuer = flattenObject(trustIssuer);

// Note search for "end_session" key into the flatten object
const endSessionEndpointKey = Object.keys(flattenTrustIssuer).find(
Expand Down
16 changes: 16 additions & 0 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,22 @@ export const extractMetadataKeys = <T>(
return Array.from(keys);
};

export const flattenObject = <T>(obj: T) => {
const result: Record<string, unknown> = {};

for (const i in obj) {
if (typeof obj[i] === "object" && !Array.isArray(obj[i])) {
const temp = flattenObject(obj[i]);
for (const j in temp) {
result[i + "." + j] = temp[j];
}
} else {
result[i] = obj[i];
}
}
return result;
};

export const handleAxiosRequestError = (
err: unknown,
context?: string,
Expand Down

0 comments on commit 7fa5615

Please sign in to comment.