Skip to content

Commit

Permalink
Merge 2e11a20 into 5ab8df4
Browse files Browse the repository at this point in the history
  • Loading branch information
sthang committed Oct 21, 2019
2 parents 5ab8df4 + 2e11a20 commit 6efdd8a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 10 deletions.
14 changes: 9 additions & 5 deletions README.md
Expand Up @@ -73,19 +73,23 @@ Follow the instructions below to use the library :
clientId: '<Enter your clientId>',
clientSecret: '<Enter your clientSecret>',
environment: 'sandbox' || 'production',
redirectUri: '<Enter your callback URL>'
redirectUri: '<Enter your callback URL>',
autoRefresh: true,
autoRefreshInterval: 55 * 60
});
```

### Options

* `clientId` - clientID for your app. Required
* `clientSecret` - clientSecret fpor your app. Required
* `clientSecret` - clientSecret for your app. Required
* `environment` - environment for the client. Required
* `sandbox` - for authorizing in sandbox.
* `production` - for authorizing in production.
* `redirectUri` - redirectUri on your app to get the `authorizationCode` from Intuit Servers. Required
* `logging` - by default, logging is disabled i.e `false`. To enable provide`true`.
* `redirectUri` - redirectUri on your app to get the `authorizationCode` from Intuit Servers. Required
* `autoRefresh` - by default, autoRefresh is disabled i.e `false`. To enable provide `true`. If set to true, authclient would refresh the tokens every `autoRefreshInterval` specified so clients do not have to manually call refresh after the first `createToken` call. Optional.
* `autoRefreshInterval` - only if `autoRefresh` is set to true, config will use `autoRefreshInterval`. It is set in seconds. If user does not specify a value but has set `autoRefresh` as true, it would default to 55 minutes. Use `stopAutoRefresh` method on OAuthClient anytime to stop auto Refresh. Optional.
* `logging` - by default, logging is disabled i.e `false`. To enable provide`true`. Optional.



Expand Down Expand Up @@ -557,4 +561,4 @@ See the changelog [here](https://github.com/intuit/oauth-jsclient/blob/master/CH

Intuit `oauth-jsclient` is licensed under the [Apache License, Version 2.0](https://github.com/intuit/oauth-jsclient/blob/master/LICENSE)

[ss1]: https://help.developer.intuit.com/s/SDKFeedback?cid=1120
[ss1]: https://help.developer.intuit.com/s/SDKFeedback?cid=1120
16 changes: 14 additions & 2 deletions sample/app.js
Expand Up @@ -9,7 +9,8 @@ require('dotenv').config();
const express = require('express');
const app = express();
const path = require('path');
const OAuthClient = require('intuit-oauth');
// const OAuthClient = require('intuit-oauth');
const OAuthClient = require('/Users/sthangavel/work/hacktober/oauth-jsclient');
const bodyParser = require('body-parser');
const ngrok = (process.env.NGROK_ENABLED==="true") ? require('ngrok'):null;

Expand Down Expand Up @@ -58,7 +59,10 @@ app.get('/authUri', urlencodedParser, function(req,res) {
clientId: req.query.json.clientId,
clientSecret: req.query.json.clientSecret,
environment: req.query.json.environment,
redirectUri: req.query.json.redirectUri
redirectUri: req.query.json.redirectUri,
autoRefresh: req.query.json.autoRefresh,
autoRefreshInterval: 30,
logging: true
});

const authUri = oauthClient.authorizeUri({scope:[OAuthClient.scopes.Accounting],state:'intuit-test'});
Expand Down Expand Up @@ -91,6 +95,14 @@ app.get('/retrieveToken', function(req, res) {
});


/**
* Stop auto refresh of tokens if it `autoRefresh` is set to true while creating the oauth client
*/
app.get('/stopAutoRefresh', function(req, res) {
oauthClient.stopAutoRefresh();
res.send('');
});

/**
* Refresh the access-token
*/
Expand Down
24 changes: 21 additions & 3 deletions sample/public/index.html
Expand Up @@ -28,11 +28,15 @@ <h2>OAuth2.0</h2><h4>( Please enter the client credentials below )</h4><br>
<form>
<div class="form-group">
<label for="clientId">ClientID</label>
<input type="text" class="form-control" placeholder="enter the clientId" id="clientId" />
<input type="text" class="form-control" placeholder="enter the clientId" id="clientId" value="ABYb8fbCIUDX79Iu79rKsRAPXf0UDcHGY4RCASUYfLXiq7Z0lH"/>
</div>
<div class="form-group">
<label for="clientSecret">ClientSecret</label>
<input type="text" class="form-control" placeholder="enter the clientSecret" id="clientSecret" />
<input type="text" class="form-control" placeholder="enter the clientSecret" id="clientSecret" value="rQlQNh6mX678PqLrcaWmp7RSs2RmaDeZCoqJNYPI" />
</div>
<div class="form-group">
<label for="autoRefresh">AutoRefresh</label>
<input type="checkbox" id="autoRefresh" name="autoRefreshSelected" checked/>
</div>
<div class="form-group">
<label for="environment">Environment</label>
Expand All @@ -43,14 +47,15 @@ <h2>OAuth2.0</h2><h4>( Please enter the client credentials below )</h4><br>
</div>
<div class="form-group">
<label for="redirectUri">Redirect URI</label>
<input type="text" class="form-control" placeholder="enter the redirectUri" id="redirectUri" /><br>
<input type="text" class="form-control" placeholder="enter the redirectUri" id="redirectUri" value="http://localhost:8909/callback"/><br>
</div>
</form>
<p>Now click the <b>Connect to QuickBooks</b> button below.</p>
<pre id="accessToken"></pre>
<a class="imgLink" href="#" id="authorizeUri" ><img src="./images/C2QB_green_btn_lg_default.png" width="178" /></a>
<button type="button" id="retrieveToken" class="btn btn-success">Display Access Token</button>
<button type="button" id="refreshToken" class="btn btn-success">Refresh Token</button>
<button type="button" id="stopAutoRefresh" class="btn btn-success">Stop AutoRefresh</button>
<hr />

<h2>Make an API call</h2><h4>( Please refer to our <a target="_balnk" href="https://developer.intuit.com/v2/apiexplorer?apiname=V3QBO#?id=Account">API Explorer</a> )</h4>
Expand Down Expand Up @@ -84,6 +89,7 @@ <h2>Make an API call</h2><h4>( Please refer to our <a target="_balnk" href="http
jsonBody.clientSecret = $('#clientSecret').val();
jsonBody.environment = $('#environment').val();
jsonBody.redirectUri = $('#redirectUri').val();
jsonBody.autoRefresh = $('#autoRefresh').is(':checked')

$.get('/authUri', {json:jsonBody}, function (uri) {
console.log('The Auth Uris is :'+uri);
Expand Down Expand Up @@ -116,6 +122,13 @@ <h2>Make an API call</h2><h4>( Please refer to our <a target="_balnk" href="http
});
}

function stopAutoRefresh() {
// Stop AutoRefresh of the token
$.get('/stopAutoRefresh', function () {
$("#stopAutoRefresh").html();
});
}

function refreshToken() {

// Generate the authUri
Expand Down Expand Up @@ -143,6 +156,11 @@ <h2>Make an API call</h2><h4>( Please refer to our <a target="_balnk" href="http
retrieveToken();
});

document.getElementById('stopAutoRefresh').addEventListener('click', function response(e) {
e.preventDefault();
stopAutoRefresh();
});

document.getElementById('refreshToken').addEventListener('click', function response(e) {
e.preventDefault();
refreshToken();
Expand Down
19 changes: 19 additions & 0 deletions src/OAuthClient.js
Expand Up @@ -53,6 +53,9 @@ function OAuthClient(config) {
this.clientId = config.clientId;
this.clientSecret = config.clientSecret;
this.redirectUri = config.redirectUri;
this.autoRefresh = config.autoRefresh;
// If autoRefreshInterval is not choosen by user, defaults to 3300 seconds (55 minutes) since tokens expire after 60 minutes.
this.autoRefreshInterval = config.autoRefreshInterval || 3300;
this.token = new Token(config.token);
this.logging = !!(Object.prototype.hasOwnProperty.call(config, 'logging') && config.logging === true);
this.logger = null;
Expand Down Expand Up @@ -160,6 +163,11 @@ OAuthClient.prototype.createToken = function createToken(uri) {
const authResponse = res.json ? res : null;
const json = (authResponse && authResponse.getJson()) || res;
this.token.setToken(json);
if (this.autoRefresh) {
this.log('info', 'Setting AutoRefresh of tokens every: ', this.autoRefreshInterval + ' seconds');
// Set auto refresh call
this.autoRefreshHandle = setInterval(() => this.refresh(), this.autoRefreshInterval * 1000);
}
this.log('info', 'Create Token response is : ', JSON.stringify(authResponse, null, 2));
return authResponse;
}).catch((e) => {
Expand Down Expand Up @@ -650,6 +658,17 @@ OAuthClient.prototype.setToken = function setToken(params) {
return this.token;
};

/**
* Stops auto refresh of tokens
*/
OAuthClient.prototype.stopAutoRefresh = function () {
if (this.autoRefreshHandle) {
clearInterval(this.autoRefreshHandle);
} else {
this.log('info', 'AutoRefresh is not set so it cannot be cleared');
}
};


/**
* Get AuthHeader
Expand Down

0 comments on commit 6efdd8a

Please sign in to comment.