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,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()
@ -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");