From 3dac9b91afac4a2e32f0d009dfd246b99ae134c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sindre=20S=C3=A6grov?= Date: Fri, 29 Nov 2024 08:49:34 +0100 Subject: [PATCH] Legg til mock ident for local utvikling --- app/models/saksbehandler.server.ts | 28 +++++++++++++++++++++++----- mocks/mock-azure.ts | 3 +++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/app/models/saksbehandler.server.ts b/app/models/saksbehandler.server.ts index aaecceca..c65d5b12 100644 --- a/app/models/saksbehandler.server.ts +++ b/app/models/saksbehandler.server.ts @@ -2,9 +2,12 @@ import { getToken, parseAzureUserToken, validateToken } from "@navikt/oasis"; import { LRUCache } from "lru-cache"; import { getMicrosoftOboToken } from "~/utils/auth.utils.server"; +import { getEnv } from "~/utils/env.utils"; import { getHeaders } from "~/utils/fetch.utils"; import { logger } from "~/utils/logger.utils"; +import { mockSaksbehandler } from "../../mocks/data/mock-saksbehandler"; + export interface ISaksbehandler { onPremisesSamAccountName: string; // Dette er saksbehandlerIdent givenName: string; @@ -20,9 +23,13 @@ const cache = new LRUCache({ export async function getSaksbehandler(request: Request): Promise { try { const navIdent = await getNavIdent(request); + if (navIdent === null) { + throw new Error("Mangler NAV ident"); + } - if (navIdent == null) throw new Response("Unauthorized", { status: 401 }); - if (cache.has(navIdent)) return cache.get(navIdent)!; + if (cache.has(navIdent)) { + return cache.get(navIdent)!; + } const oboToken = await getMicrosoftOboToken(request); @@ -45,14 +52,25 @@ export async function getSaksbehandler(request: Request): Promise { + if (getEnv("USE_MSW")) { + return mockSaksbehandler.onPremisesSamAccountName; + } + const token = getToken(request); - if (token == null) return null; + + if (token === null) { + return null; + } const validation = await validateToken(token); - if (!validation.ok) return null; + if (!validation.ok) { + return null; + } const parsed = parseAzureUserToken(token); - if (!parsed.ok) return null; + if (!parsed.ok) { + return null; + } return parsed.NAVident; } diff --git a/mocks/mock-azure.ts b/mocks/mock-azure.ts index 8bc780ef..da019b6e 100644 --- a/mocks/mock-azure.ts +++ b/mocks/mock-azure.ts @@ -1,10 +1,13 @@ import { http, HttpResponse } from "msw"; +import { logger } from "~/utils/logger.utils"; + import { mockSaksbehandler } from "./data/mock-saksbehandler"; export const mockAzure = [ // Hent saksbehandler http.get("https://graph.microsoft.com/v1.0/me/", () => { + logger.info(`[MSW]-GET https://graph.microsoft.com/v1.0/me/`); return HttpResponse.json(mockSaksbehandler); }), ];