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

refactor(oauth): replace got with isomorphic-fetch #11

Merged
merged 5 commits into from
Dec 14, 2023
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
12 changes: 7 additions & 5 deletions .github/workflows/commitlint.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
name: Commitlint
on: [push, pull_request]
on: [pull_request]

jobs:
lint-commits:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0 # Ensures commit history is fetched

- name: Use Node.js
uses: actions/setup-node@v2
uses: actions/setup-node@v3
with:
node-version: '14' # Specify a Node.js version
node-version: '18' # Specify a Node.js version

- name: Install dependencies
run: npm install
env:
GH_NPM_TOKEN: ${{ secrets.GH_NPM_TOKEN }}

- name: Lint last commit
run: npx commitlint --from=${{ github.event.pull_request.base.sha }} --to=${{ github.head_ref }} --verbose
run: npx commitlint --from=${{ github.event.pull_request.base.sha }} --to=${{ github.event.pull_request.head.sha }} --verbose
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"conventionalCommits.scopes": [
"repo"
"repo",
"oauth"
]
}
6 changes: 4 additions & 2 deletions package-lock.json

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

6 changes: 4 additions & 2 deletions packages/oauth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
},
"homepage": "https://github.com/camunda-community-hub/camunda-saas-oauth-nodejs#readme",
"dependencies": {
"@types/debug": "^4.1.12",
"debug": "^4.3.4",
"got": "^11.8.5",
"isomorphic-fetch": "^3.0.0",
"neon-env": "^0.1.1"
},
"devDependencies": {
"@types/debug": "^4.1.12"
}
}
4 changes: 2 additions & 2 deletions packages/oauth/src/__test__/OAuthImpl.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import http from 'http'
import { OAuthProviderImpl } from '../lib/OAuthProviderImpl'

jest.setTimeout(10000)
// Added test for https://github.com/camunda-community-hub/camunda-saas-oauth-nodejs/issues/8
// "Can not renew expired token"
// Updated test for https://github.com/camunda-community-hub/camunda-8-js-sdk/issues/3
Expand Down Expand Up @@ -47,7 +48,6 @@ test('In-memory cache is populated and evicted after timeout', done => {
await delay(1600)
const token3 = await o.getToken('CONSOLE')
expect(token3).toBe('1')
server.close()
done()
server.close(() => done())
})
})
68 changes: 36 additions & 32 deletions packages/oauth/src/lib/OAuthProviderImpl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as fs from 'fs';
import got from 'got';
import 'isomorphic-fetch'
import { Token, OAuthProviderConfig } from '../index';
import * as os from 'os'
import { debug } from 'debug'
Expand Down Expand Up @@ -102,33 +102,34 @@ export class OAuthProviderImpl {
}

private makeDebouncedTokenRequest(audience: TokenGrantAudiences) {
const form = {
audience: this.getAudience(audience),
client_id: this.clientId,
client_secret: this.clientSecret,
grant_type: 'client_credentials',
};

return got
.post(this.authServerUrl, {
form,
// const form = {
// audience: this.getAudience(audience),
// client_id: this.clientId,
// client_secret: this.clientSecret,
// grant_type: 'client_credentials',
// };

const body = `audience=${this.getAudience(audience)}&client_id=${this.clientId}&client_secret=${this.clientSecret}&grant_type=client_credentials`
return fetch(this.authServerUrl, {
method: 'POST',
body,
headers: {
'content-type': 'application/x-www-form-urlencoded',
'user-agent': this.userAgentString,
},
})
.then(res => this.safeJSONParse(res.body)
.then(t => {
const token = {...t, audience}
if (this.useFileCache) {
this.sendToFileCache({ audience, token });
}
this.sendToMemoryCache({ audience, token })
trace(`Got token from endpoint: \n${token.access_token}`)
trace(`Token expires in ${token.expires_in} seconds`)
return token.access_token;
})
);
.then(res => res.json())
// .then(res => this.safeJSONParse(res)
.then((t: any) => {
const token = {...t, audience}
if (this.useFileCache) {
this.sendToFileCache({ audience, token });
}
this.sendToMemoryCache({ audience, token })
trace(`Got token from endpoint: \n${token.access_token}`)
trace(`Token expires in ${token.expires_in} seconds`)
return token.access_token;
})
}

private sendToMemoryCache({ audience, token }: { audience: TokenGrantAudiences; token: Token; }) {
Expand All @@ -138,15 +139,18 @@ export class OAuthProviderImpl {
this.tokenCache[key] = token;
}

private safeJSONParse(thing: any): Promise<Token> {
return new Promise((resolve, reject) => {
try {
resolve(JSON.parse(thing));
} catch (e) {
reject(e);
}
});
}
// private safeJSONParse(thing: any): Promise<Token> {
// // tslint:disable-next-line: no-console
// console.log(thing); // @DEBUG

// return new Promise((resolve, reject) => {
// try {
// resolve(JSON.parse(thing));
// } catch (e) {
// reject(e);
// }
// });
// }

private retrieveFromFileCache(clientId: string, audience: TokenGrantAudiences) {
let token: Token;
Expand Down