/// <summary> /// Async method for refreshing the token, persisting the data in the encrypted settings and returning the updated /// account /// with the new access token. /// </summary> /// <param name="account"></param> /// <returns>Boolean based on if the refresh auth token succeeded or not</returns> public static async Task <Account> RefreshAuthTokenAsync(Account account) { LoggingService.Log("Atempting to refresh auth token", LoggingLevel.Verbose); if (account == null) { return(null); } try { var loginOptions = account.GetLoginOptions(); // args var argsStr = string.Format(OauthRefreshQueryString, loginOptions.ClientId, account.RefreshToken); // Refresh url var refreshUrl = loginOptions.LoginUrl + OauthRefreshPath; // Post var call = HttpCall.CreatePost(refreshUrl, argsStr); var response = await call.ExecuteAndDeserializeAsync <AuthResponse>(); account.AccessToken = response.AccessToken; account.IdentityUrl = response.IdentityUrl; await SDKServiceLocator.Get <IAuthHelper>().PersistCurrentAccountAsync(account); } catch (DeviceOfflineException ex) { LoggingService.Log("Failed to refresh the token because we were offline", LoggingLevel.Warning); LoggingService.Log(ex, LoggingLevel.Warning); throw; } catch (WebException ex) { LoggingService.Log("Exception occurred when refreshing token:", LoggingLevel.Critical); LoggingService.Log(ex, LoggingLevel.Critical); Debug.WriteLine("Error refreshing token"); throw new OAuthException(ex.Message, ex); } catch (Exception ex) { LoggingService.Log("Exception occurred when refreshing token:", LoggingLevel.Critical); LoggingService.Log(ex, LoggingLevel.Critical); Debug.WriteLine("Error refreshing token"); throw new OAuthException(ex.Message, ex); } return(account); }
/// <summary> /// Async method to revoke the user's refresh token (i.e. do a server-side logout for the authenticated user) /// </summary> /// <param name="loginOptions"></param> /// <param name="refreshToken"></param> /// <returns>true if successful</returns> public static async Task <bool> RevokeAuthToken(LoginOptions loginOptions, string refreshToken) { // Args string argsStr = string.Format(OAUTH_REVOKE_QUERY_STRING, new string[] { refreshToken }); // Revoke url string revokeUrl = loginOptions.LoginUrl + OAUTH_REVOKE_PATH; // Post HttpCall c = HttpCall.CreatePost(revokeUrl, argsStr); // Execute post return(await c.Execute().ContinueWith(t => t.Result.StatusCode == HttpStatusCode.OK)); }
/// <summary> /// Async method to get a new auth token by doing a refresh flow /// </summary> /// <param name="loginOptions"></param> /// <param name="refreshToken"></param> /// <returns></returns> public static async Task <AuthResponse> RefreshAuthToken(LoginOptions loginOptions, string refreshToken) { // Args string argsStr = string.Format(OAUTH_REFRESH_QUERY_STRING, new string[] { loginOptions.ClientId, refreshToken }); // Refresh url string refreshUrl = loginOptions.LoginUrl + OAUTH_REFRESH_PATH; // Post HttpCall c = HttpCall.CreatePost(refreshUrl, argsStr); // Execute post return(await c.ExecuteAndDeserialize <AuthResponse>()); }
/// <summary> /// Async method to make the request for a new auth token. Method returns an AuthResponse with the data returned back /// from /// Salesforce. /// </summary> /// <param name="loginOptions"></param> /// <param name="refreshToken"></param> /// <returns></returns> public static async Task <AuthResponse> RefreshAuthTokenRequest(LoginOptions loginOptions, string refreshToken) { PlatformAdapter.SendToCustomLogger("OAuth2.RefreshAuthTokenRequest - attempting to refresh auth token", LoggingLevel.Verbose); // Args string argsStr = string.Format(OauthRefreshQueryString, new[] { loginOptions.ClientId, refreshToken }); // Refresh url string refreshUrl = loginOptions.LoginUrl + OauthRefreshPath; // Post HttpCall c = HttpCall.CreatePost(refreshUrl, argsStr); // Execute post return(await c.ExecuteAndDeserialize <AuthResponse>()); }
/// <summary> /// Async method to revoke the user's refresh token (i.e. do a server-side logout for the authenticated user) /// </summary> /// <param name="loginOptions"></param> /// <param name="refreshToken"></param> /// <returns>true if successful</returns> public static async Task <bool> RevokeAuthTokenAsync(LoginOptions loginOptions, string refreshToken) { // Args string argsStr = string.Format(OauthRevokeQueryString, new[] { WebUtility.UrlEncode(refreshToken) }); // Revoke url string revokeUrl = loginOptions.LoginUrl + OauthRevokePath; // Post HttpCall c = HttpCall.CreatePost(revokeUrl, argsStr); // ExecuteAsync post HttpCall result = await c.ExecuteAsync().ConfigureAwait(false); LoggingService.Log($"result.StatusCode = {result.StatusCode}", LoggingLevel.Verbose); return(result.StatusCode == HttpStatusCode.OK); }
/// <summary> /// Async method to revoke the user's refresh token (i.e. do a server-side logout for the authenticated user) /// </summary> /// <param name="loginOptions"></param> /// <param name="refreshToken"></param> /// <returns>true if successful</returns> public static async Task <bool> RevokeAuthToken(LoginOptions loginOptions, string refreshToken) { // Args string argsStr = string.Format(OauthRevokeQueryString, new[] { WebUtility.UrlEncode(refreshToken) }); // Revoke url string revokeUrl = loginOptions.LoginUrl + OauthRevokePath; // Post HttpCall c = HttpCall.CreatePost(revokeUrl, argsStr); // Execute post HttpCall result = await c.Execute().ConfigureAwait(false); PlatformAdapter.SendToCustomLogger(string.Format("OAuth2.RevokeAuthToken - result.StatusCode = {0}", result.StatusCode), LoggingLevel.Verbose); return(result.StatusCode == HttpStatusCode.Ok); }