innogy-smarthome-lib
Advanced tools
Comparing version 0.1.17 to 0.1.18
@@ -91,2 +91,27 @@ const jsonfile = require('jsonfile'); | ||
var refreshToken = function (oAuthConfig, callback) { | ||
const oauth2 = oauthLib.create(oAuthConfig); | ||
var cachedAuthorization = getCachedAuthorization(); | ||
if (cachedAuthorization) { | ||
var token = oauth2.accessToken.create(cachedAuthorization); | ||
token.refresh(function (error, result) { | ||
if (result && result.token) { | ||
jsonfile.writeFile(currentAuthFile, result.token); | ||
if (callback) | ||
callback(error, { | ||
needsAuthorization: false, | ||
token: result.token | ||
}); | ||
} else if (callback) | ||
callback(error, { | ||
needsAuthorization: true, | ||
token: null | ||
}); | ||
}); | ||
} | ||
}; | ||
var getAuthorizationStatus = function (oAuthConfig, callback) { | ||
@@ -102,7 +127,7 @@ const oauth2 = oauthLib.create(oAuthConfig); | ||
var now = new Date(); | ||
var before30Mins = new Date(); | ||
before30Mins.setMinutes(now.getMinutes() - 30); | ||
// Refresh token 30 Mins before expiration date | ||
expirationDate.setMinutes(expirationDate.getMinutes() - 30); | ||
if (expirationDate < before30Mins) { | ||
if (expirationDate < now) { | ||
token.refresh(function (error, result) { | ||
@@ -150,4 +175,7 @@ if (result && result.token) { | ||
return getAuthorizationStartUri(config.oAuth2Credentials, config.oAuth2RedirectConfig, port); | ||
}, | ||
refreshToken: function (callback) { | ||
return refreshToken(config.oAuth2Credentials, callback); | ||
} | ||
} | ||
}; |
@@ -60,3 +60,16 @@ const rp = require("request-promise"); | ||
} else { | ||
$doRequest(resolve, reject, status.token, options); | ||
$doRequest(resolve, function (e) { | ||
if (e && e.error && e.error.errorcode) { | ||
// Token expired ... | ||
if (e.error.errorcode === 2007) { | ||
that._oAuth2.refreshToken(function () { | ||
$doRequest(resolve, reject, status.token, options); | ||
}) | ||
} else { | ||
reject(e); | ||
} | ||
} else { | ||
reject(e); | ||
} | ||
}, status.token, options); | ||
} | ||
@@ -63,0 +76,0 @@ }); |
{ | ||
"name": "innogy-smarthome-lib", | ||
"version": "0.1.17", | ||
"version": "0.1.18", | ||
"description": "JavaScript library for the Innogy SmartHome system", | ||
@@ -5,0 +5,0 @@ "main": "./lib/smarthome", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
57612
1263