private void refreshToken(final ApiKey apiKey) throws Exception, UpdateFailedException {
// Check to see if we are running on a mirrored test instance
// and should therefore refrain from swapping tokens lest we
// invalidate an existing token instance
String disableTokenSwap = env.get("disableTokenSwap");
Connector connector = Connector.getConnector("moves");
if(disableTokenSwap!=null && disableTokenSwap.equals("true")) {
String msg = "**** Skipping refreshToken for moves connector instance because disableTokenSwap is set on this server";
;
StringBuilder sb2 = new StringBuilder("module=MovesController component=MovesController action=refreshToken apiKeyId=" + apiKey.getId())
.append(" message=\"").append(msg).append("\"");
logger.info(sb2.toString());
System.out.println(msg);
// Notify the user that the tokens need to be manually renewed
notificationsService.addNamedNotification(apiKey.getGuestId(), Notification.Type.WARNING, connector.statusNotificationName(),
"Heads Up. This server cannot automatically refresh your Moves authentication tokens.<br>" +
"Please head to <a href=\"javascript:App.manageConnectors()\">Manage Connectors</a>,<br>" +
"scroll to the Moves connector, delete the connector, and re-add<br>" +
"<p>We apologize for the inconvenience</p>");
// Record permanent failure since this connector won't work again until
// it is reauthenticated
guestService.setApiKeyStatus(apiKey.getId(), ApiKey.Status.STATUS_PERMANENT_FAILURE, null, ApiKey.PermanentFailReason.NEEDS_REAUTH);
throw new UpdateFailedException("requires token reauthorization", true, ApiKey.PermanentFailReason.NEEDS_REAUTH);
}
// We're not on a mirrored test server. Try to swap the expired
// access token for a fresh one. Typically moves access tokens are good for
// 180 days from time of issue.
String swapTokenUrl = "https://api.moves-app.com/oauth/v1/access_token";
final String refreshToken = guestService.getApiKeyAttribute(apiKey, "refreshToken");
Map<String,String> params = new HashMap<String,String>();
params.put("refresh_token", refreshToken);
params.put("client_id", guestService.getApiKeyAttribute(apiKey, "moves.client.id"));
params.put("client_secret", guestService.getApiKeyAttribute(apiKey, "moves.client.secret"));
params.put("grant_type", "refresh_token");
String fetched;
try {
fetched = HttpUtils.fetch(swapTokenUrl, params);
// Record that this connector is now up
guestService.setApiKeyStatus(apiKey.getId(), ApiKey.Status.STATUS_UP, null, null);
} catch (Exception e) {
// Notify the user that the tokens need to be manually renewed
notificationsService.addNamedNotification(apiKey.getGuestId(), Notification.Type.WARNING, connector.statusNotificationName(),
"Heads Up. We failed in our attempt to automatically refresh your Moves authentication tokens.<br>" +
"Please head to <a href=\"javascript:App.manageConnectors()\">Manage Connectors</a>,<br>" +
"scroll to the Moves connector, delete the connector, and re-add<br>" +
"<p>We apologize for the inconvenience</p>");