Skip to content

Commit

Permalink
Resolve unhandled application crash when invalid auth response is use…
Browse files Browse the repository at this point in the history
…d (Issue #399) (#410)

* Add a http 400 response error handler
* If the response uri generates a 400 error, the JSON response will not contain the access_token. Request to re-authenticate
  • Loading branch information
abraunegg authored Mar 12, 2019
1 parent e849eb3 commit 4c3b959
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/onedrive.d
Original file line number Diff line number Diff line change
Expand Up @@ -384,13 +384,19 @@ final class OneDriveApi
private void acquireToken(const(char)[] postData)
{
JSONValue response = post(tokenUrl, postData);
accessToken = "bearer " ~ response["access_token"].str();
refreshToken = response["refresh_token"].str();
accessTokenExpiration = Clock.currTime() + dur!"seconds"(response["expires_in"].integer());
if (!.dryRun) {
std.file.write(cfg.refreshTokenFilePath, refreshToken);
if ("access_token" in response){
accessToken = "bearer " ~ response["access_token"].str();
refreshToken = response["refresh_token"].str();
accessTokenExpiration = Clock.currTime() + dur!"seconds"(response["expires_in"].integer());
if (!.dryRun) {
std.file.write(cfg.refreshTokenFilePath, refreshToken);
}
if (printAccessToken) writeln("New access token: ", accessToken);
} else {
log.error("\nInvalid authentication response from OneDrive. Please check the response uri\n");
// re-authorize
authorize();
}
if (printAccessToken) writeln("New access token: ", accessToken);
}

private void checkAccessTokenExpired()
Expand Down Expand Up @@ -720,6 +726,12 @@ final class OneDriveApi
{
switch(http.statusLine.code)
{
// 400 - Bad Request
case 400:
// Bad Request .. how should we act?
log.vlog("OneDrive returned a 'HTTP 400 - Bad Request' - gracefully handling error");
break;

// 412 - Precondition Failed
case 412:
log.vlog("OneDrive returned a 'HTTP 412 - Precondition Failed' - gracefully handling error");
Expand Down

0 comments on commit 4c3b959

Please sign in to comment.