Resolve unhandled application crash when invalid auth response is used (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
This commit is contained in:
abraunegg 2019-03-12 11:50:21 +11:00 committed by GitHub
parent e849eb3de4
commit 4c3b959bf6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -384,6 +384,7 @@ final class OneDriveApi
private void acquireToken(const(char)[] postData) private void acquireToken(const(char)[] postData)
{ {
JSONValue response = post(tokenUrl, postData); JSONValue response = post(tokenUrl, postData);
if ("access_token" in response){
accessToken = "bearer " ~ response["access_token"].str(); accessToken = "bearer " ~ response["access_token"].str();
refreshToken = response["refresh_token"].str(); refreshToken = response["refresh_token"].str();
accessTokenExpiration = Clock.currTime() + dur!"seconds"(response["expires_in"].integer()); accessTokenExpiration = Clock.currTime() + dur!"seconds"(response["expires_in"].integer());
@ -391,6 +392,11 @@ final class OneDriveApi
std.file.write(cfg.refreshTokenFilePath, refreshToken); std.file.write(cfg.refreshTokenFilePath, refreshToken);
} }
if (printAccessToken) writeln("New access token: ", accessToken); if (printAccessToken) writeln("New access token: ", accessToken);
} else {
log.error("\nInvalid authentication response from OneDrive. Please check the response uri\n");
// re-authorize
authorize();
}
} }
private void checkAccessTokenExpired() private void checkAccessTokenExpired()
@ -720,6 +726,12 @@ final class OneDriveApi
{ {
switch(http.statusLine.code) 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 // 412 - Precondition Failed
case 412: case 412:
log.vlog("OneDrive returned a 'HTTP 412 - Precondition Failed' - gracefully handling error"); log.vlog("OneDrive returned a 'HTTP 412 - Precondition Failed' - gracefully handling error");