Skip to content

Commit

Permalink
chore(tsc): fix noPropertyAccessFromIndexSignature error
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasduteil committed Aug 27, 2024
1 parent 175d6ff commit 7b14a25
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 20 deletions.
22 changes: 20 additions & 2 deletions src/config/env.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
// load variable from .env file, only used in local dev env
import "dotenv/config";

declare global {
namespace NodeJS {
interface ProcessEnv {
CONSIDER_ALL_EMAIL_DOMAINS_AS_FREE: "True" | "False";
CONSIDER_ALL_EMAIL_DOMAINS_AS_NON_FREE: "True" | "False";
DISABLE_SECURITY_RESPONSE_HEADERS: "True" | "False";
DISPLAY_TEST_ENV_WARNING: "True" | "False";
DO_NOT_AUTHENTICATE_BROWSER: "True" | "False";
DO_NOT_CHECK_EMAIL_DELIVERABILITY: "True" | "False";
DO_NOT_RATE_LIMIT: "True" | "False";
DO_NOT_SEND_MAIL: "True" | "False";
DO_NOT_USE_ANNUAIRE_EMAILS: "True" | "False";
ENABLE_FIXED_ACR: "True" | "False";
SECURE_COOKIES: "true" | "false";
SYMMETRIC_ENCRYPTION_KEY: string;
}
}
}

export const {
NODE_ENV,
DEPLOY_ENV = "preview",
Expand Down Expand Up @@ -35,8 +54,7 @@ if (!process.env.SYMMETRIC_ENCRYPTION_KEY) {
"The SYMMETRIC_ENCRYPTION_KEY environment variable should be 32 bytes long! Use crypto.randomBytes(32).toString('base64') to generate one.",
);
}
export const SYMMETRIC_ENCRYPTION_KEY: string = process.env
.SYMMETRIC_ENCRYPTION_KEY as string;
export const SYMMETRIC_ENCRYPTION_KEY = process.env.SYMMETRIC_ENCRYPTION_KEY;

export const MONCOMPTEPRO_LABEL = "MonComptePro";
export const MONCOMPTEPRO_IDENTIFIER = new URL(MONCOMPTEPRO_HOST).hostname;
Expand Down
12 changes: 10 additions & 2 deletions src/config/oidc-provider-configuration.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { Request } from "express";
import { Configuration } from "oidc-provider";
import { Configuration, type UnknownObject } from "oidc-provider";
import { destroyAuthenticatedSession } from "../managers/session/authenticated";
import epochTime from "../services/epoch-time";
import { findAccount } from "../services/oidc-account-adapter";
import policy from "../services/oidc-policy";
import { renderWithEjsLayout } from "../services/renderer";

//

export interface OIDCContextParams extends UnknownObject {
scope: string;
prompt: "select_organization" | "update_userinfo";
}

export const oidcProviderConfiguration = ({
sessionTtlInSeconds = 14 * 24 * 60 * 60,
shortTokenTtlInSeconds = 10 * 60,
Expand Down Expand Up @@ -85,6 +92,7 @@ export const oidcProviderConfiguration = ({
if (!ctx.oidc.session || !ctx.oidc.client || !ctx.oidc.params) {
return undefined;
}
const oidcContextParams = ctx.oidc.params as OIDCContextParams;
const grantId = ctx.oidc.session.grantIdFor(ctx.oidc.client.clientId);

let grant;
Expand All @@ -111,7 +119,7 @@ export const oidcProviderConfiguration = ({

// event existing grant should be updated, as requested scopes might
// be different
grant.addOIDCScope(ctx.oidc.params.scope as string);
grant.addOIDCScope(oidcContextParams.scope);
await grant.save();
return grant;
},
Expand Down
5 changes: 3 additions & 2 deletions src/controllers/user/official-contact-email-verification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,13 @@ export const postOfficialContactEmailVerificationMiddleware = async (

return next();
} catch (error) {
const { organization_id } = req.params;
if (
req.params?.organization_id &&
organization_id &&
(error instanceof InvalidTokenError || error instanceof ZodError)
) {
return res.redirect(
`/users/official-contact-email-verification/${req.params.organization_id}?notification=invalid_verify_email_code`,
`/users/official-contact-email-verification/${organization_id}?notification=invalid_verify_email_code`,
);
}

Expand Down
3 changes: 2 additions & 1 deletion src/managers/oidc-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Sentry from "@sentry/node";
import { isEmpty, isString } from "lodash-es";
import { KoaContextWithOIDC } from "oidc-provider";
import { NotFoundError } from "../config/errors";
import type { OIDCContextParams } from "../config/oidc-provider-configuration";
import {
addConnection,
findByClientId,
Expand All @@ -25,7 +26,7 @@ export const recordNewConnection = async ({
accountId: string;
// tricky way to get the non exported Client type
client: NonNullable<KoaContextWithOIDC["oidc"]["client"]>;
params: KoaContextWithOIDC["oidc"]["params"];
params: OIDCContextParams;
}): Promise<Connection> => {
const user_id = parseInt(accountId, 10);

Expand Down
3 changes: 2 additions & 1 deletion src/middlewares/connection-count.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as Sentry from "@sentry/node";
import { NextFunction } from "express";
import { KoaContextWithOIDC } from "oidc-provider";
import type { OIDCContextParams } from "../config/oidc-provider-configuration";
import { recordNewConnection } from "../managers/oidc-client";
import { logger } from "../services/log";

Expand Down Expand Up @@ -39,7 +40,7 @@ export const connectionCountMiddleware = async (
await recordNewConnection({
accountId: ctx.oidc.session.accountId,
client: ctx.oidc.client,
params: ctx.oidc.params,
params: ctx.oidc.params as OIDCContextParams,
});
} else {
// This is unexpected, we log it in sentry
Expand Down
2 changes: 1 addition & 1 deletion src/routers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const apiRouter = () => {

return res
.status(statusCode)
.json({ message: err.message || err.statusMessage });
.json({ message: err.message || err["statusMessage"] });
},
);

Expand Down
29 changes: 18 additions & 11 deletions src/services/oidc-policy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { interactionPolicy } from "oidc-provider";
import { interactionPolicy, type InteractionResults } from "oidc-provider";
import type { OIDCContextParams } from "../config/oidc-provider-configuration";
import { getSelectedOrganizationId } from "../repositories/redis/selected-organization";
import { mustReturnOneOrganizationInPayload } from "./must-return-one-organization-in-payload";

//

interface OidcInteractionResults extends InteractionResults {
select_organization?: boolean;
update_userinfo?: boolean;
}
//

const { Prompt, Check, base } = interactionPolicy;

const policy = base();
Expand All @@ -26,11 +35,9 @@ policy.add(
) &&
!selectedOrganizationId
) {
// @ts-ignore
return Check.REQUEST_PROMPT;
}

// @ts-ignore
return Check.NO_NEED_TO_PROMPT;
},
),
Expand All @@ -47,15 +54,15 @@ policy.add(
"interaction_required",
async (ctx) => {
const { oidc } = ctx;
const oidcContextParams = ctx.oidc.params as OIDCContextParams;
const oidcContextResult = oidc.result as OidcInteractionResults;
if (
ctx.params.prompt === "select_organization" &&
!oidc.result?.select_organization
oidcContextParams.prompt === "select_organization" &&
!oidcContextResult?.select_organization
) {
// @ts-ignore
return Check.REQUEST_PROMPT;
}

// @ts-ignore
return Check.NO_NEED_TO_PROMPT;
},
),
Expand All @@ -72,15 +79,15 @@ policy.add(
"interaction_required",
async (ctx) => {
const { oidc } = ctx;
const oidcContextParams = oidc.params as OIDCContextParams;
const oidcContextResult = oidc.result as OidcInteractionResults;
if (
ctx.params.prompt === "update_userinfo" &&
!oidc.result?.update_userinfo
oidcContextParams.prompt === "update_userinfo" &&
!oidcContextResult?.update_userinfo
) {
// @ts-ignore
return Check.REQUEST_PROMPT;
}

// @ts-ignore
return Check.NO_NEED_TO_PROMPT;
},
),
Expand Down

0 comments on commit 7b14a25

Please sign in to comment.