Skip to content

Commit

Permalink
Legg til mock ident for local utvikling
Browse files Browse the repository at this point in the history
  • Loading branch information
ssaegrov committed Nov 29, 2024
1 parent 29e0564 commit 3dac9b9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
28 changes: 23 additions & 5 deletions app/models/saksbehandler.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,9 +23,13 @@ const cache = new LRUCache<string, ISaksbehandler>({
export async function getSaksbehandler(request: Request): Promise<ISaksbehandler> {
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);

Expand All @@ -45,14 +52,25 @@ export async function getSaksbehandler(request: Request): Promise<ISaksbehandler
}

async function getNavIdent(request: Request): Promise<string | null> {
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;
}
3 changes: 3 additions & 0 deletions mocks/mock-azure.ts
Original file line number Diff line number Diff line change
@@ -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);
}),
];

0 comments on commit 3dac9b9

Please sign in to comment.