/// <summary> /// Calls <see cref="Modules.TraktSyncModule.GetLastActivitiesAsync()" /> to check, /// whether the given access token is still valid and was not revoked by the user. /// </summary> /// <param name="accessToken">The access token, which will be checked.</param> /// <returns>True, if the given access token was revoked and / or is not valid anymore, otherwise false.</returns> /// <exception cref="ArgumentException">Thrown, if the given access token is null, empty or contains spaces.</exception> /// <exception cref="TraktException">Thrown, if the request <see cref="Modules.TraktSyncModule.GetLastActivitiesAsync()" /> fails.</exception> public async Task <bool> CheckIfAccessTokenWasRevokedOrIsNotValidAsync(string accessToken) { if (string.IsNullOrEmpty(accessToken) || accessToken.ContainsSpace()) { throw new ArgumentException("access token must not be null, empty or contain any spaces", nameof(accessToken)); } var currentAuthorization = Authorization; Authorization = TraktAuthorization.CreateWith(accessToken); try { await Client.Sync.GetLastActivitiesAsync(); return(false); } catch (TraktAuthorizationException) { return(true); } finally { Authorization = currentAuthorization; } }
/// <summary> /// Revokes the given access token. If, successful, the given access token will be invalid /// and the user has to be re-authenticated, e.g. by calling <see cref="TraktOAuth.GetAuthorizationAsync() "/>. /// <para> /// See <a href="http://docs.trakt.apiary.io/#reference/authentication-oauth/get-token/revoke-an-access_token">"Trakt API Doc - OAuth: Revoke Token"</a> for more information. /// </para> /// <para> /// See also <seealso cref="RevokeAuthorizationAsync()" />, <seealso cref="RevokeAuthorizationAsync(string)" />. /// See also <seealso cref="Authorization" />. /// </para> /// </summary> /// <param name="accessToken">The access token, which will be revoked. See also <seealso cref="TraktAuthorization.AccessToken" />.</param> /// <param name="clientId">The Trakt Client ID, which will be used for the request. See also <seealso cref="ClientId" />.</param> /// <exception cref="TraktAuthorizationException"> /// Thrown, if the current <see cref="TraktClient" /> instance is not authorized and the given access token is null, /// empty or contains spaces. /// </exception> /// <exception cref="TraktAuthenticationException">Thrown, if revoking the given access token fails with unknown error.</exception> /// <exception cref="TraktException">Thrown, if the request fails.</exception> /// <exception cref="ArgumentException"> /// Thrown, if the given access token is null, empty or contains spaces. /// Thrown, if the given client id is null, empty or contains spaces. /// </exception> public async Task RevokeAuthorizationAsync(string accessToken, string clientId) { if (!IsAuthorized && (string.IsNullOrEmpty(accessToken) || accessToken.ContainsSpace())) { throw new TraktAuthorizationException("not authorized"); } if (string.IsNullOrEmpty(accessToken) || accessToken.ContainsSpace()) { throw new ArgumentException("access token not valid", nameof(accessToken)); } if (string.IsNullOrEmpty(clientId) || clientId.ContainsSpace()) { throw new ArgumentException("client id not valid", nameof(clientId)); } var postContent = $"token={accessToken}"; var httpClient = TraktConfiguration.HTTP_CLIENT; if (httpClient == null) { httpClient = new HttpClient(); } SetDefaultRequestHeaders(httpClient); SetAuthorizationRequestHeaders(httpClient, accessToken, clientId); var tokenUrl = $"{Client.Configuration.BaseUrl}{TraktConstants.OAuthRevokeUri}"; var content = new StringContent(postContent, Encoding.UTF8, "application/x-www-form-urlencoded"); var response = await httpClient.PostAsync(tokenUrl, content).ConfigureAwait(false); if (!response.IsSuccessStatusCode) { if (response.StatusCode == HttpStatusCode.Unauthorized) { var responseContent = response.Content != null ? await response.Content.ReadAsStringAsync() : string.Empty; throw new TraktAuthenticationException("error on revoking access token") { RequestUrl = tokenUrl, RequestBody = postContent, Response = responseContent, ServerReasonPhrase = response.ReasonPhrase }; } await ErrorHandling(response, tokenUrl, postContent); } else { Client.Authorization = TraktAuthorization.CreateWith(string.Empty, string.Empty); } }
/// <summary> /// Calls <see cref="Modules.TraktSyncModule.GetLastActivitiesAsync()" /> to check, /// whether the given <see cref="TraktAuthorization" /> is not expired yet and was not revoked by the user. /// </summary> /// <param name="authorization">The authorization information, which will be checked.</param> /// <param name="autoRefresh"> /// Indicates, whether the current <see cref="Authorization" /> should be refreshed, if it was revoked. /// If this is set to true, <see cref="RefreshAuthorizationAsync()" /> will be called.<para /> /// See also <seealso cref="RefreshAuthorizationAsync()" />. /// </param> /// <returns> /// Returns an <see cref="Pair{T, U}" /> instance with <see cref="Pair{T, U}.First" /> set to true, /// if the current <see cref="Authorization" /> is expired or was revoked, otherwise set to false. /// If <paramref name="autoRefresh" /> is set to true, the returned <see cref="Pair{T, U}.Second" /> contains the new /// <see cref="TraktAuthorization" /> information, otherwise the returned <see cref="Pair{T, U}.Second" /> will be null. /// </returns> /// <exception cref="ArgumentNullException">Thrown, if the given <paramref name="authorization" /> is null.</exception> /// <exception cref="TraktException">Thrown, if the request <see cref="RefreshAuthorizationAsync()" /> fails.</exception> public async Task <Pair <bool, TraktAuthorization> > CheckIfAuthorizationIsExpiredOrWasRevokedAsync(TraktAuthorization authorization, bool autoRefresh = false) { if (authorization == null) { throw new ArgumentNullException(nameof(authorization)); } var currentAuthorization = Authorization; Authorization = authorization; var result = new Pair <bool, TraktAuthorization>(true, null); try { result = await CheckIfAuthorizationIsExpiredOrWasRevokedAsync(autoRefresh); } finally { Authorization = currentAuthorization; } return(result); }