-
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.
👽 Tar i bruk oasis for innsending av søknad og mellomlagring
- Loading branch information
Showing
10 changed files
with
238 additions
and
82 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/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,33 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import { getAccessTokenFromRequest } from 'auth/accessToken'; | ||
import { beskyttetApi } from 'auth/beskyttetApi'; | ||
import { logError, tokenXApiProxy } from '@navikt/aap-felles-utils'; | ||
import { beskyttetApi } from '@navikt/aap-felles-utils'; | ||
import { lagreCache } from 'mock/mellomlagringsCache'; | ||
import { isFunctionalTest, isMock } from 'utils/environments'; | ||
import metrics from 'utils/metrics'; | ||
|
||
import { StepType } from 'components/StepWizard/Step'; | ||
import { hentMellomlagring } from 'pages/api/mellomlagring/les'; | ||
import { getOnBefalfOfToken } from 'lib/api/simpleTokenXProxy'; | ||
import { proxyApiRouteRequest } from '@navikt/next-api-proxy'; | ||
|
||
const handler = beskyttetApi(async (req: NextApiRequest, res: NextApiResponse) => { | ||
const accessToken = getAccessTokenFromRequest(req); | ||
const handler = beskyttetApi(async (req, res) => { | ||
if (isFunctionalTest()) return; | ||
if (isMock()) return await lagreCache(JSON.stringify(req.body)); | ||
|
||
const eksisterendeSøknad = await hentMellomlagring(accessToken); | ||
if ( | ||
eksisterendeSøknad && | ||
eksisterendeSøknad.søknad && | ||
Object.keys(eksisterendeSøknad.søknad).length > 0 && | ||
Object.keys(req.body.søknad).length === 0 | ||
) { | ||
const activeStepIndex = eksisterendeSøknad?.lagretStepList?.find( | ||
(e: StepType) => e.active, | ||
)?.stepIndex; | ||
const url = `/mellomlagring/søknad`; | ||
const onBehalfOfToken = await getOnBefalfOfToken(process.env.INNSENDING_AUDIENCE!, url, req); | ||
|
||
logError( | ||
`Overskriver eksisterende søknad med en tom søknad på side ${activeStepIndex ?? 'ukjent'}`, | ||
); | ||
} | ||
await lagreBucket(req.body, accessToken); | ||
res.status(201).json({}); | ||
return await proxyApiRouteRequest({ | ||
hostname: 'innsending', | ||
path: url, | ||
req: req, | ||
res: res, | ||
bearerToken: onBehalfOfToken, | ||
https: false, | ||
}); | ||
}); | ||
|
||
export const lagreBucket = async (data: string, accessToken?: string) => { | ||
if (isFunctionalTest()) return; | ||
if (isMock()) return await lagreCache(JSON.stringify(data)); | ||
await tokenXApiProxy({ | ||
url: `${process.env.INNSENDING_URL}/mellomlagring/søknad`, | ||
prometheusPath: `mellomlagring`, | ||
method: 'POST', | ||
data: JSON.stringify(data), | ||
audience: process.env.INNSENDING_AUDIENCE!, | ||
noResponse: true, | ||
bearerToken: accessToken, | ||
metricsStatusCodeCounter: metrics.backendApiStatusCodeCounter, | ||
metricsTimer: metrics.backendApiDurationHistogram, | ||
}); | ||
return; | ||
export const config = { | ||
api: { | ||
responseLimit: '50mb', | ||
bodyParser: false, | ||
externalResolver: true, | ||
}, | ||
}; | ||
|
||
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
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,36 @@ | ||
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/api/simpleTokenXProxy'; | ||
|
||
const handler = beskyttetApi(async (req: NextApiRequest, res: NextApiResponse) => { | ||
const accessToken = getAccessTokenFromRequest(req); | ||
await slettBucket(accessToken); | ||
await slettInnsending(req); | ||
res.status(204).json({}); | ||
}); | ||
|
||
export const slettBucket = async (accessToken?: string) => { | ||
export const slettInnsending = async (req: NextApiRequest) => { | ||
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 { | ||
const response = await simpleTokenXProxy({ | ||
url: `${process.env.INNSENDING_URL}/mellomlagring/søknad`, | ||
audience: process.env.INNSENDING_AUDIENCE!, | ||
method: 'DELETE', | ||
req, | ||
}); | ||
return response; | ||
} catch (error) { | ||
logError('Error sending slettInnsending', error); | ||
throw new Error('Error sending slettInnsending'); | ||
} | ||
}; | ||
|
||
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.