Skip to content

Commit

Permalink
use @navikt/oasis
Browse files Browse the repository at this point in the history
  • Loading branch information
andnorda authored and eirikv committed Mar 19, 2024
1 parent 22eb1a5 commit 9481691
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 219 deletions.
56 changes: 54 additions & 2 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
"dependencies": {
"@navikt/arbeidsgiver-notifikasjoner-brukerapi-mock": "^6.2.2",
"@navikt/nav-dekoratoren-moduler": "^2.1.5",
"@navikt/oasis": "^3.2.2",
"axios": "^1.6.7",
"cookie-parser": "^1.4.6",
"express-async-handler": "1.2.0",
"express-http-proxy": "^2.0.0",
"helmet": "^7.1.0",
"http-proxy-middleware": "^2.0.1",
"jsdom": "^24.0.0",
"openid-client": "^5.4.0"
"jsdom": "^24.0.0"
},
"devDependencies": {
"@types/cookie-parser": "^1.4.7",
Expand Down
60 changes: 0 additions & 60 deletions server/src/login/azure.ts

This file was deleted.

43 changes: 0 additions & 43 deletions server/src/login/loginProvider.ts

This file was deleted.

55 changes: 0 additions & 55 deletions server/src/login/tokenx.ts

This file was deleted.

64 changes: 21 additions & 43 deletions server/src/proxy/api-proxy.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,21 @@
import tokenx from '../login/tokenx';
import azure from '../login/azure';
import { requestOboToken } from '../auth';

Check failure on line 1 in server/src/proxy/api-proxy.ts

View workflow job for this annotation

GitHub Actions / Bygg branch

Cannot find module '../auth' or its corresponding type declarations.
import { Express } from 'express';
import { BaseClient } from 'openid-client';
import { Request } from 'express-serve-static-core';
import { createProxyMiddleware } from 'http-proxy-middleware';
import proxy from 'express-http-proxy';
import { ParsedQs } from 'qs';

const tokenxSetup = (app: Express, tokenxClient: BaseClient): void => {
const tokenxSetup = (app: Express): void => {
console.log('api-proxy setup for tokenx');

setupPath(app);

app.use(
'/tiltaksgjennomforing/api',
proxy(process.env.APIGW_URL as string, {
proxyReqPathResolver: (req: Request<{}, any, any, ParsedQs, Record<string, any>>) => {
return req.originalUrl.replace('/tiltaksgjennomforing/api', '/tiltaksgjennomforing-api');
},
proxyReqOptDecorator: async (options: any, req: Request<{}, any, any, ParsedQs, Record<string, any>>) => {
const accessToken = await tokenx.getTokenExchangeAccessToken(
tokenxClient,
process.env.API_AUDIENCE,
req,
);
options.headers.Authorization = `Bearer ${accessToken}`;
return options;
},
}),
);
setup(app, process.env.API_AUDIENCE!);
};

const azureSetup = (app: Express, azureClient: BaseClient, azureTokenEndpoint: any): void => {
const azureSetup = (app: Express): void => {
console.log('api-proxy setup for azure');

setupPath(app);

app.use(
'/tiltaksgjennomforing/api',
proxy(process.env.APIGW_URL as string, {
proxyReqPathResolver: (req: Request<{}, any, any, ParsedQs, Record<string, any>>) => {
return req.originalUrl.replace('/tiltaksgjennomforing/api', '/tiltaksgjennomforing-api');
},
proxyReqOptDecorator: async (options: any, req: Request<{}, any, any, ParsedQs, Record<string, any>>) => {
const accessToken = await azure.getOnBehalfOfAccessToken(azureClient, azureTokenEndpoint, req);
options.headers.Authorization = `Bearer ${accessToken}`;
return options;
},
}),
);
setup(app, process.env.API_SCOPE!);
};

function setupPath(app: Express) {
const setup = (app: Express, audience: string) => {
app.use('/tiltaksgjennomforing/api/internal', (req, res) => {
res.status(401).send();
});
Expand All @@ -74,6 +38,20 @@ function setupPath(app: Express) {
proxyTimeout: 10000,
}),
);
}

app.use(
'/tiltaksgjennomforing/api',
proxy(process.env.APIGW_URL as string, {
proxyReqPathResolver: (req: Request<{}, any, any, ParsedQs, Record<string, any>>) => {
return req.originalUrl.replace('/tiltaksgjennomforing/api', '/tiltaksgjennomforing-api');
},
proxyReqOptDecorator: async (options: any, req: Request<{}, any, any, ParsedQs, Record<string, any>>) => {
const accessToken = await requestOboToken(audience, req);
options.headers.Authorization = `Bearer ${accessToken}`;
return options;
},
}),
);
};

export default { tokenxSetup, azureSetup };
7 changes: 3 additions & 4 deletions server/src/proxy/decorator-intern-proxy.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import proxy from 'express-http-proxy';
import onbehalfof from '../login/azure';
import { BaseClient } from 'openid-client';
import { Express, Response } from 'express';
import { Request } from 'express-serve-static-core';
import { ParsedQs } from 'qs';
import { IncomingMessage, RequestOptions } from 'http';
import { requestOboToken } from '../auth';

Check failure on line 6 in server/src/proxy/decorator-intern-proxy.ts

View workflow job for this annotation

GitHub Actions / Bygg branch

Cannot find module '../auth' or its corresponding type declarations.

const setup = (app: Express, azureClient: BaseClient, azureTokenEndpoint: BaseClient) => {
const setup = (app: Express) => {
app.use(
'/modiacontextholder/api/decorator',
proxy(process.env.APIGW_URL as string, {
Expand All @@ -20,7 +19,7 @@ const setup = (app: Express, azureClient: BaseClient, azureTokenEndpoint: BaseCl
options: RequestOptions,
req: Request<{}, any, any, ParsedQs, Record<string, any>>,
) => {
const accessToken = await onbehalfof.getOnBehalfOfAccessToken(azureClient, azureTokenEndpoint, req);
const accessToken = await requestOboToken(process.env.API_SCOPE!, req);
if (options?.headers) {
options.headers.Authorization = `Bearer ${accessToken}`;
let cookies = options.headers.cookie;
Expand Down
Loading

0 comments on commit 9481691

Please sign in to comment.