Add automatic access token refresh and minor ui bug fixes

This commit is contained in:
Claus Vium 2019-01-22 18:41:54 +01:00
parent 4bbf4be451
commit 79b91824a3
5 changed files with 48 additions and 15 deletions

View File

@ -1,10 +1,6 @@
namespace Trakt.Api.DataContracts
{
// TODO DELETE
public class TraktUserToken
public class TraktUserAccessToken
{
public string access_token { get; set; }
public string token_type { get; set; }

View File

@ -3,10 +3,9 @@
namespace Trakt.Api.DataContracts
{
public class TraktUserTokenRequest
public class TraktUserRefreshTokenRequest
{
public string refresh_token { get; set; }
public string code { get; set; }
public string client_id { get; set; }
public string client_secret { get; set; }
public string redirect_uri { get; set; }

View File

@ -918,6 +918,48 @@ namespace Trakt.Api
return false;
}
public async Task RefreshUserAccessToken(TraktUser traktUser)
{
if (string.IsNullOrWhiteSpace(traktUser.RefreshToken))
{
_logger.LogError("Tried to reauthenticate with Trakt, but no refreshToken was available");
return;
}
var data = new TraktUserRefreshTokenRequest
{
client_id = TraktUris.ClientId,
client_secret = TraktUris.ClientSecret,
redirect_uri = "urn:ietf:wg:oauth:2.0:oob",
refresh_token = traktUser.RefreshToken,
grant_type = "refresh_token"
};
TraktUserAccessToken userAccessToken;
try
{
using (var response = await PostToTrakt(TraktUris.AccessToken, data).ConfigureAwait(false))
{
userAccessToken = _jsonSerializer.DeserializeFromStream<TraktUserAccessToken>(response.Content);
}
}
catch (HttpException ex)
{
_logger.LogError(ex, "An error occurred during token refresh");
return;
}
if (userAccessToken != null)
{
traktUser.AccessToken = userAccessToken.access_token;
traktUser.RefreshToken = userAccessToken.refresh_token;
traktUser.AccessTokenExpiration = DateTimeOffset.Now.AddMonths(2);
Plugin.Instance.SaveConfiguration();
_logger.LogInformation("Successfully refreshed the access token for user {UserId}", traktUser.LinkedMbUserId);
}
}
private Task<Stream> GetFromTrakt(string url, TraktUser traktUser)
{
return GetFromTrakt(url, CancellationToken.None, traktUser);
@ -1040,21 +1082,15 @@ namespace Trakt.Api
private async Task SetRequestHeaders(HttpRequestOptions options, TraktUser traktUser)
{
if (DateTimeOffset.Now > traktUser.AccessTokenExpiration)
{
traktUser.AccessToken = "";
await RefreshUserAccessToken(traktUser).ConfigureAwait(false);
}
// TODO remove?
// if (string.IsNullOrEmpty(traktUser.AccessToken) || !string.IsNullOrEmpty(traktUser.PIN))
// {
// await RefreshUserAuth(traktUser).ConfigureAwait(false);
// }
if (!string.IsNullOrEmpty(traktUser.AccessToken))
{
options.RequestHeaders.Add("Authorization", "Bearer " + traktUser.AccessToken);
}
}
}
}

View File

@ -10,6 +10,7 @@
public static readonly string DeviceCode = $@"{BaseUrl}/oauth/device/code";
public static readonly string DeviceToken = $@"{BaseUrl}/oauth/device/token";
public static readonly string AccessToken = $@"{BaseUrl}/oauth/token";
public static readonly string SyncCollectionAdd = $@"{BaseUrl}/sync/collection";
public static readonly string SyncCollectionRemove = $@"{BaseUrl}/sync/collection/remove";

View File

@ -102,6 +102,7 @@
}
// Set the auth button
$('#authorizeDevice', page).text(buttonText);
$('#authorizeDevice', page).removeClass('hide');
}
var TraktConfigurationPage =
{
@ -249,7 +250,7 @@
console.log('User is authorized: ' + result.isAuthorized);
$('#userCode').text('');
TraktConfigurationPage.loadConfiguration(currentUserId, page);
}).catch(error);
}).catch(handleError);
}).catch(handleError);
});
});