Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade axios version #33

Merged
merged 11 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/clean-lobsters-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@ordermentum/express-asap": major
"@ordermentum/axios-asap": major
"@ordermentum/asap-core": major
"@ordermentum/hapi-asap": major
---

Upgrade axios version to 1.0
2 changes: 1 addition & 1 deletion packages/asap-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"typecheck": "yarn tsc --noEmit"
},
"dependencies": {
"axios": "^0.26.1",
"axios": "^1.7.7",
"express": "^4.17.3",
"jsonwebtoken": "^8.5.1",
"node-cache": "^5.1.2"
Expand Down
2 changes: 1 addition & 1 deletion packages/asap-core/src/authenticator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export function createAsapAuthenticator({
});
}

const verifyOptions = {
const verifyOptions: jsonWebToken.VerifyOptions = {
algorithms: ALLOWED_ALGORITHMS,
clockTolerance: CLOCK_TOLERANCE_SECONDS,
audience: resourceServerAudience,
Expand Down
7 changes: 5 additions & 2 deletions packages/asap-core/test/file_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { describe, afterEach, beforeEach } from 'mocha';
import { expect } from 'chai';
import sinon from 'sinon';
import { createPublicKeyFetcher, createTestPublicKeyFetcher } from '../src/fetchers/file';
import {
createPublicKeyFetcher,
createTestPublicKeyFetcher,
} from '../src/fetchers/file';

describe('file', () => {
let time;
Expand Down Expand Up @@ -39,6 +42,6 @@ describe('file', () => {
const key2 = await fetcher('test/service_2');
expect(key1).to.eqls(key2);
});
})
});
});
});
2 changes: 1 addition & 1 deletion packages/asap-core/test/generate_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe('createAuthHeaderGenerator', () => {
jwtConfig.privateKey = 'this is not a valid key';

expect(() => createAuthHeaderGenerator(jwtConfig)()).to.throw(
/PEM.*no start line/
/DECODER routines::unsupported/
);
});

Expand Down
5 changes: 2 additions & 3 deletions packages/axios-asap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"@types/express": "4.17.13",
"@types/jsonwebtoken": "8.5.8",
"@types/mocha": "9.1.0",
"@types/moxios": "0.4.15",
"@types/node": "17.0.21",
"@types/sinon": "10.0.11",
"chai": "4.3.6",
Expand All @@ -21,7 +20,7 @@
"lint-staged": "12.3.5",
"mocha": "9.2.2",
"mocha-sinon": "2.1.2",
"moxios": "0.4.0",
"nock": "13.5.5",
"sinon": "13.0.1",
"ts-node": "10.7.0",
"ts-node-dev": "1.1.8",
Expand All @@ -35,6 +34,6 @@
},
"dependencies": {
"@ordermentum/asap-core": "*",
"axios": "^0.26.1"
"axios": "^1.7.7"
}
}
14 changes: 9 additions & 5 deletions packages/axios-asap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
createAuthHeaderGenerator,
AuthHeaderConfig,
} from '@ordermentum/asap-core';
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
import axios, { AxiosInstance, AxiosRequestConfig, AxiosHeaders } from 'axios';
import http from 'http';
import https from 'https';

Expand All @@ -12,9 +12,13 @@ export const createAsapInterceptor = (authConfig: AuthHeaderConfig) => {
});
return (config: AxiosRequestConfig) => {
const header = headerGenerator();
const headers = config.headers ?? {};
headers.Authorization = header;
return { ...config, headers };
// @ts-expect-error RawAxiosHeaders is not accessible for type defs
const headers: AxiosHeaders = new AxiosHeaders(config.headers ?? {});
headers.set('Authorization', header);
return {
...config,
headers,
};
};
};

Expand All @@ -30,7 +34,7 @@ const getDefaultAxiosConfig = (): AxiosRequestConfig => {
if (!httpsAgent) {
httpsAgent = new https.Agent({ keepAlive: true });
}
return { httpAgent, httpsAgent }
return { httpAgent, httpsAgent };
};

export type Options = {
Expand Down
20 changes: 6 additions & 14 deletions packages/axios-asap/test/index_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, beforeEach } from 'mocha';
import axios from 'axios';
import moxios from 'moxios';
import nock from 'nock';
import { expect } from 'chai';
import { privateKeyPem } from '@ordermentum/asap-test-helpers';
import { createAsapInterceptor, createClient } from '../src';
Expand All @@ -19,14 +19,10 @@ describe('createAuthHeaderGenerator', () => {

it('sets the header', async () => {
const client = axios.create();
moxios.install(client);
client.interceptors.request.use(createAsapInterceptor(jwtConfig));
moxios.stubRequest('/say/hello', {
status: 200,
responseText: 'hello',
});
nock('http://localhost').get('/say/hello').reply(200, 'hello');

const res = await client.get('/say/hello');
const res = await client.get('http://localhost/say/hello');
expect(res.status).to.equal(200);
expect(Object.keys(res?.config?.headers ?? {})).to.include('Authorization');
expect(res?.config?.headers?.Authorization).to.include('Bearer');
Expand All @@ -49,13 +45,9 @@ describe('createAuthHeaderGenerator', () => {
}
);

moxios.install(client);
moxios.stubRequest('/say/hello', {
status: 200,
responseText: 'hello',
});
nock('http://localhost').get('/say/hello').reply(200, 'hello');

const res = await client.get('/say/hello');
const res = await client.get('http://localhost/say/hello');
expect(res.status).to.equal(200);
expect(Object.keys(res?.config?.headers ?? {})).to.include('Authorization');
expect(res?.config?.headers?.Authorization).to.include('Bearer');
Expand Down Expand Up @@ -151,5 +143,5 @@ describe('createAuthHeaderGenerator', () => {
expect(client1).to.equal(client2);
expect(client1.defaults.headers['x-custom-header']).to.equal('1');
expect(client2.defaults.headers['x-custom-header']).to.equal('1');
})
});
});
1 change: 0 additions & 1 deletion packages/express-asap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
},
"dependencies": {
"@ordermentum/asap-core": "*",
"axios": "^0.26.1",
"express": "^4.17.3",
"jsonwebtoken": "^8.5.1"
}
Expand Down
29 changes: 19 additions & 10 deletions packages/express-asap/test/index_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,22 @@ import createAsapAuthenticationMiddleware from '../src/middleware';
import createAsapIssuerWhitelistMiddleware from '../src/whitelist_middleware';

const app = express();
app.use(
createAsapAuthenticationMiddleware({
keyLoader: (_key: string) => Promise.resolve(publicKey),
resourceServerAudience: 'test',
maxLifeTimeSeconds: 60,
})
);
app
.use(
createAsapAuthenticationMiddleware({
keyLoader: (_key: string) => Promise.resolve(publicKey),
resourceServerAudience: 'test',
maxLifeTimeSeconds: 60,
})
)
.use((req, res, next) => {
// @ts-ignore
if (req.headers.authorization && req.locals?.asapClaims === undefined) {
res.status(401).send('Did not authenticate');
} else {
next();
}
});

app.get('/', (_req, res) => {
res.status(200);
Expand Down Expand Up @@ -43,7 +52,7 @@ describe('middleware', () => {
tokenExpiryMs: 60 * 1000,
};

const authHeader = createAuthHeaderGenerator(jwtConfig)();
const authHeader = createAuthHeaderGenerator(jwtConfig)({ admin: true });
const res = await agent.get('/').set('Authorization', authHeader);
expect(res.status).to.equal(200);
expect(res.text).to.equal('OK');
Expand All @@ -58,7 +67,7 @@ describe('middleware', () => {
tokenExpiryMs: 60 * 1000,
};

const authHeader = createAuthHeaderGenerator(jwtConfig)();
const authHeader = createAuthHeaderGenerator(jwtConfig)({ admin: true });
const res = await agent.get('/').set('Authorization', authHeader);
expect(res.status).to.equal(401);
});
Expand All @@ -77,7 +86,7 @@ describe('middleware', () => {
return res.send('OK');
});

const authHeader = createAuthHeaderGenerator(jwtConfig)();
const authHeader = createAuthHeaderGenerator(jwtConfig)({ admin: true });
const res = await agent.get('/protected').set('Authorization', authHeader);
expect(res.status).to.equal(401);
});
Expand Down
1 change: 0 additions & 1 deletion packages/hapi-asap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"@hapi/boom": "^10.0.0",
"@hapi/hapi": "^20.2.2",
"@ordermentum/asap-core": "*",
"axios": "^0.26.1",
"express": "^4.17.3",
"jsonwebtoken": "^8.5.1"
}
Expand Down
11 changes: 2 additions & 9 deletions packages/hapi-asap/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ const implementation = (_server: Hapi.Server, options?: any) => {
* @param opts AuthenticatorOptions
*
*/
const register = (
server: Hapi.Server,
options: AuthenticatorOptions
) => {
const register = (server: Hapi.Server, options: AuthenticatorOptions) => {
server.auth.scheme('hapi-asap', implementation);
server.auth.strategy('hapi-asap', 'hapi-asap', options);
};
Expand All @@ -60,10 +57,6 @@ const plugin: Hapi.Plugin<AuthenticatorOptions> = {
pkg: { name: 'ASAP Authentication', version: '0.1.0' },
};

export {
register,
AuthenticatorOptions,
plugin
}
export { register, AuthenticatorOptions, plugin };

export default plugin;
16 changes: 13 additions & 3 deletions packages/hapi-asap/test/index_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@ import { expect } from 'chai';
import { createAuthHeaderGenerator } from '@ordermentum/asap-core';
import { publicKey, privateKeyPem } from '@ordermentum/asap-test-helpers';
import Hapi from '@hapi/hapi';
import registerPlugin from '../src/middleware';
import { plugin as hapiAsap } from '../src/middleware';

async function init() {
const server = Hapi.server({
port: 3000,
port: 10000,
host: 'localhost',
});

await server.register(registerPlugin);
await server.register({
plugin: hapiAsap,
options: {
keyLoader: (_key: string) => Promise.resolve(publicKey),
resourceServerAudience: 'test',
maxLifeTimeSeconds: 60,
},
});

// await server.register(registerPlugin);
server.auth.strategy('asap', 'hapi-asap', {
keyLoader: (_key: string) => Promise.resolve(publicKey),
resourceServerAudience: 'test',
Expand All @@ -35,6 +43,7 @@ async function init() {
return `Ok`;
}

// @ts-expect-error
return `${asapClaims?.aud}`;
},
});
Expand All @@ -50,6 +59,7 @@ async function init() {
path: '/required',
handler(request, h) {
const { asapClaims } = request.auth.artifacts ?? {};
// @ts-expect-error
return `test ${asapClaims?.aud}`;
},
});
Expand Down
Loading
Loading