-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
👽 Setter opp oasis for innsending av søknad og mellomlagring
- Loading branch information
Showing
10 changed files
with
235 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// TODO: Errorcode og developerMessage vil returneres fra innsending og oppslag etterhvert. | ||
// Her må vi støtte begge deler parallelt i en periode. | ||
|
||
export class ErrorMedStatus extends Error { | ||
status: number; | ||
developerMessage?: string; | ||
errorCode?: string; | ||
navCallId?: string; | ||
constructor( | ||
message: string, | ||
status: number, | ||
navCallId = '', | ||
developerMessage = '', | ||
errorCode = '', | ||
) { | ||
super(message); | ||
this.status = status; | ||
this.developerMessage = developerMessage; | ||
this.errorCode = errorCode; | ||
this.navCallId = navCallId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { validateToken, requestOboToken, getToken } from '@navikt/oasis'; | ||
import { logError, logInfo } from '@navikt/aap-felles-utils'; | ||
import { randomUUID } from 'crypto'; | ||
import { IncomingMessage } from 'http'; | ||
import { ErrorMedStatus } from 'lib/utils/api/ErrorMedStatus'; | ||
|
||
export const getOnBefalfOfToken = async ( | ||
audience: string, | ||
url: string, | ||
req: IncomingMessage, | ||
): Promise<string> => { | ||
const token = getToken(req); | ||
if (!token) { | ||
logError(`Token for ${url} er undefined`); | ||
throw new Error('Token for simpleTokenXProxy is undefined'); | ||
} | ||
|
||
const validation = await validateToken(token); | ||
if (!validation.ok) { | ||
logError(`Token for ${url} validerte ikke`); | ||
throw new Error('Token for simpleTokenXProxy didnt validate'); | ||
} | ||
|
||
const onBehalfOf = await requestOboToken(token, audience); | ||
if (!onBehalfOf.ok) { | ||
logError(`Henting av oboToken for ${url} feilet`); | ||
throw new Error('Request oboToken for simpleTokenXProxy failed'); | ||
} | ||
|
||
return onBehalfOf.token; | ||
}; | ||
|
||
interface Opts { | ||
url: string; | ||
method?: 'GET' | 'POST' | 'DELETE'; | ||
audience: string; | ||
body?: object; | ||
req?: IncomingMessage; | ||
} | ||
|
||
export const simpleTokenXProxy = async <T>({ | ||
url, | ||
audience, | ||
req, | ||
method = 'GET', | ||
body, | ||
}: Opts): Promise<T> => { | ||
if (!req) { | ||
logError(`Request for ${url} er undefined`); | ||
throw new Error('Request for simpleTokenXProxy is undefined'); | ||
} | ||
const onBehalfOfToken = await getOnBefalfOfToken(audience, url, req); | ||
const navCallId = randomUUID(); | ||
|
||
logInfo(`${req.method} ${url}, callId ${navCallId}`); | ||
|
||
const response = await fetch(url, { | ||
method: method, | ||
headers: { | ||
Authorization: `Bearer ${onBehalfOfToken}`, | ||
'Content-Type': 'application/json', | ||
'Nav-CallId': navCallId, | ||
}, | ||
body: method === 'POST' ? JSON.stringify(body) : undefined, | ||
}); | ||
|
||
try { | ||
if (response.ok) { | ||
logInfo(`OK ${url}, status ${response.status}, callId ${navCallId}`); | ||
const headers = response.headers.get('content-type'); | ||
const isJson = headers?.includes('application/json'); | ||
|
||
// TODO: Midlertidig, til innsending returnerer json på alle OK-responser | ||
if (!isJson) { | ||
return (await response.text()) as T; | ||
} | ||
return await response.json(); | ||
} | ||
} catch (error) { | ||
logError(`Unable to parse response for ${url}`, error); | ||
} | ||
logError( | ||
`Error fetching simpleTokenXProxy. Fikk responskode ${response.status} fra ${url} med navCallId: ${navCallId}`, | ||
); | ||
throw new ErrorMedStatus('Error fetching simpleTokenXProxy', response.status, navCallId); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,37 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import { getAccessTokenFromRequest } from 'auth/accessToken'; | ||
import { beskyttetApi } from 'auth/beskyttetApi'; | ||
import { tokenXApiProxy } from '@navikt/aap-felles-utils'; | ||
import { logError, tokenXApiProxy } from '@navikt/aap-felles-utils'; | ||
import metrics from 'utils/metrics'; | ||
import { deleteCache } from 'mock/mellomlagringsCache'; | ||
import { isMock } from 'utils/environments'; | ||
import { simpleTokenXProxy } from 'lib/utils/api/simpleTokenXProxy'; | ||
import { IncomingMessage } from 'http'; | ||
import { ca } from 'date-fns/locale'; | ||
|
||
const handler = beskyttetApi(async (req: NextApiRequest, res: NextApiResponse) => { | ||
const accessToken = getAccessTokenFromRequest(req); | ||
await slettBucket(accessToken); | ||
await slettBucket(req); | ||
res.status(204).json({}); | ||
}); | ||
|
||
export const slettBucket = async (accessToken?: string) => { | ||
export const slettBucket = async (req: IncomingMessage) => { | ||
if (isMock()) { | ||
await deleteCache(); | ||
return; | ||
} | ||
|
||
await tokenXApiProxy({ | ||
url: `${process.env.INNSENDING_URL}/mellomlagring/søknad`, | ||
prometheusPath: `mellomlagring`, | ||
method: 'DELETE', | ||
noResponse: true, | ||
audience: process.env.INNSENDING_AUDIENCE!, | ||
bearerToken: accessToken, | ||
metricsStatusCodeCounter: metrics.backendApiStatusCodeCounter, | ||
metricsTimer: metrics.backendApiDurationHistogram, | ||
}); | ||
try { | ||
await simpleTokenXProxy({ | ||
url: `${process.env.INNSENDING_URL}/mellomlagring/søknad`, | ||
method: 'DELETE', | ||
audience: process.env.INNSENDING_AUDIENCE!, | ||
req, | ||
}); | ||
return; | ||
} catch (error) { | ||
logError('Noe gikk galt ved sletting av søknad', error); | ||
throw new Error('Error deleting søknad via aap-innsending'); | ||
} | ||
}; | ||
|
||
export default handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.