Skip to content

Jan develop fix m2m #133

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

Merged
merged 14 commits into from
Feb 25, 2020
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ jobs:
- attach_workspace:
at: .
- run: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
- run: npm publish
- run: npm publish --tag test-release
# dont change anything
workflows:
version: 2
8 changes: 8 additions & 0 deletions config/test.js
Original file line number Diff line number Diff line change
@@ -4,4 +4,12 @@ module.exports = {
V3: 'https://api.topcoder-dev.com/v3',
},
dummyConfigKey: 'Dummy config value',
SECRET: {
TC_M2M: {
AUTH0_URL: '',
AUTH0_AUDIENCE: '',
TOKEN_CACHE_TIME: '',
AUTH0_PROXY_SERVER_URL: '',
},
},
};
17 changes: 10 additions & 7 deletions config/webpack/default.js
Original file line number Diff line number Diff line change
@@ -2,10 +2,11 @@
const webpack = require('webpack');

module.exports = {
plugins: [
// eslint-disable-next-line global-require
new webpack.DefinePlugin({ CONFIG: JSON.stringify(require('config')) }),
],
node: {
tls: 'empty',
fs: 'empty',
net: 'empty',
},
// Don't include the dependencies to keep built bundle small,
// they will be provided by the app using this lib
externals: [
@@ -24,8 +25,10 @@ module.exports = {
'tc-accounts',
'to-capital-case',
'topcoder-react-utils',
'tc-core-library-js',
],
plugins: [
// eslint-disable-next-line global-require
new webpack.DefinePlugin({ CONFIG: JSON.stringify(require('config')) }),
],
node: {
fs: 'empty',
},
};
1,445 changes: 0 additions & 1,445 deletions dist/dev/index.js

This file was deleted.

18,275 changes: 8,615 additions & 9,660 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@
"lint:js": "./node_modules/.bin/eslint --ext .js,.jsx .",
"test": "npm run lint && npm run jest"
},
"version": "0.11.0",
"version": "1000.10.0",
"dependencies": {
"auth0-js": "^6.8.4",
"config": "^3.2.0",
@@ -48,6 +48,7 @@
"redux": "^3.7.2",
"redux-actions": "^2.4.0",
"tc-accounts": "https://github.com/appirio-tech/accounts-app.git#dev",
"tc-core-library-js": "appirio-tech/tc-core-library-js.git#v2.6",
"to-capital-case": "^1.0.0",
"topcoder-react-utils": "0.7.5"
},
31 changes: 9 additions & 22 deletions src/services/api.js
Original file line number Diff line number Diff line change
@@ -6,12 +6,17 @@
import _ from 'lodash';
import fetch from 'isomorphic-fetch';
import { config, isomorphy } from 'topcoder-react-utils';
import { auth } from 'tc-core-library-js';
import { delay } from '../utils/time';
import {
setErrorIcon,
ERROR_ICON_TYPES,
} from '../utils/errors';

const { m2m: m2mAuth } = auth;

const m2m = m2mAuth(_.pick(config.AUTH_CONFIG, ['AUTH0_URL', 'AUTH0_AUDIENCE', 'TOKEN_CACHE_TIME', 'AUTH0_PROXY_SERVER_URL']));

/* The minimal delay [ms] between API calls. To avoid problems with the requests
* rate limits configured in Topcoder APIs, we throttle requests rate at the
* client side, and at server-side, in dev mode (which is meant to be used for
@@ -279,27 +284,9 @@ export async function getTcM2mToken() {
if (!isomorphy.isServerSide()) {
throw new Error('getTcM2mToken() called outside the server');
}
const now = Date.now();
const { cached } = getTcM2mToken;

const { TC_M2M } = config.SECRET;
if (!cached || cached.expires < now + getTcM2mToken.MIN_LIFETIME) {
let res = await fetch(`https://${config.AUTH0.DOMAIN}/oauth/token`, {
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
client_id: TC_M2M.CLIENT_ID,
client_secret: TC_M2M.CLIENT_SECRET,
audience: TC_M2M.AUDIENCE,
grant_type: TC_M2M.GRANT_TYPE,
}),
method: 'POST',
});
res = await res.json();
getTcM2mToken.cached = {
expires: now + 1000 * res.expires_in, // [ms]
token: res.access_token,
};
}
return getTcM2mToken.cached.token;
}

getTcM2mToken.MIN_LIFETIME = 30 * 1000; // [ms]
const token = await m2m.getMachineToken(TC_M2M.CLIENT_ID, TC_M2M.CLIENT_SECRET);
return token;
}