/// <summary> /// Persist oauth credentials via the AccountManager and navigate back to previous screen /// </summary> /// <param name="loginOptions"></param> /// <param name="authResponse"></param> public async void EndLoginFlow(LoginOptions loginOptions, AuthResponse authResponse) { await AccountManager.CreateNewAccount(loginOptions, authResponse); Frame frame = Window.Current.Content as Frame; frame.Navigate(SalesforceApplication.RootApplicationPage); SalesforcePhoneApplication.ContinuationManager.MarkAsStale(); }
public static async Task TestSetup(TestContext context) { SFApplicationHelper.RegisterServices(); SDKServiceLocator.RegisterService<ILoggingService, Hybrid.Logging.Logger>(); var settings = new EncryptionSettings(new HmacSHA256KeyGenerator()); Encryptor.init(settings); var options = new LoginOptions(TestCredentials.LoginUrl, TestCredentials.ClientId, TestCallbackUrl, "mobile", TestScopes); var response = new AuthResponse { RefreshToken = TestCredentials.RefreshToken, AccessToken = TestAuthToken, InstanceUrl = TestCredentials.InstanceUrl, IdentityUrl = TestCredentials.IdentityUrl, Scopes = TestScopes, }; Account account = await AccountManager.CreateNewAccount(options, response); account.UserId = TestCredentials.UserId; account.UserName = TestCredentials.Username; await OAuth2.RefreshAuthTokenAsync(account); _smartStore = SmartStore.GetSmartStore(); _smartStore.ResetDatabase(); _syncManager = SyncManager.GetInstance(); _restClient = new RestClient(account.InstanceUrl, account.AccessToken, async () => { account = AccountManager.GetAccount(); account = await OAuth2.RefreshAuthTokenAsync(account); return account.AccessToken; } ); CreateAccountsSoup(); _idToNames = await CreateTestAccountsOnServer(CountTestAccounts); }
/// <summary> /// Persist oauth credentials via the AccountManager /// </summary> /// <param name="loginOptions"></param> /// <param name="authResponse"></param> public async void EndLoginFlow(LoginOptions loginOptions, AuthResponse authResponse) { if (await AccountManager.CreateNewAccount(loginOptions, authResponse)) { Frame frame = Window.Current.Content as Frame; frame.Navigate(SalesforceApplication.RootApplicationPage); } }
/// <summary> /// This should be called when login is complete. A new account is created /// in the AccountManager and the pincode screen is shown if needeed /// </summary> /// <param name="loginOptions"></param> /// <param name="authResponse"></param> public async Task OnLoginCompleteAsync(LoginOptions loginOptions, AuthResponse authResponse) { var frame = Window.Current.Content as Frame; var account = await AccountManager.CreateNewAccount(loginOptions, authResponse); if (account.Policy != null && (!PincodeManager.IsPincodeSet() || AuthStorageHelper.IsPincodeRequired())) { PincodeManager.LaunchPincodeScreen(); } else { SDKServiceLocator.Get<ILoggingService>().Log($"Navigating to {SDKManager.RootApplicationPage}", LoggingLevel.Information); frame?.Navigate(SDKManager.RootApplicationPage); } }
public void TestCallIdentityService() { // Get auth token and identity url (through refresh) var loginOptions = new LoginOptions(TestCredentials.LOGIN_URL, TestCredentials.CLIENT_ID, null, null); AuthResponse refreshResponse = OAuth2.RefreshAuthTokenRequest(loginOptions, TestCredentials.REFRESH_TOKEN).Result; // Call the identity service IdentityResponse identityResponse = OAuth2.CallIdentityService(refreshResponse.IdentityUrl, refreshResponse.AccessToken).Result; // Check username Assert.AreEqual("*****@*****.**", identityResponse.UserName); }
/// <summary> /// This should be called when login is complete. A new account is created /// in the AccountManager and the pincode screen is shown if needeed /// </summary> /// <param name="loginOptions"></param> /// <param name="authResponse"></param> public async Task OnLoginCompleteAsync(LoginOptions loginOptions, AuthResponse authResponse) { var frame = Window.Current.Content as Frame; var account = await AccountManager.CreateNewAccount(loginOptions, authResponse); if (account.Policy != null && (!PincodeManager.IsPincodeSet() || AuthStorageHelper.IsPincodeRequired())) { PincodeManager.LaunchPincodeScreen(); } else { SDKServiceLocator.Get <ILoggingService>().Log($"Navigating to {SDKManager.RootApplicationPage}", LoggingLevel.Information); frame?.Navigate(SDKManager.RootApplicationPage); } }
/// <summary> /// Persist oauth credentials via the AccountManager /// </summary> /// <param name="loginOptions"></param> /// <param name="authResponse"></param> public async void EndLoginFlow(LoginOptions loginOptions, AuthResponse authResponse) { var frame = Window.Current.Content as Frame; Account account = await AccountManager.CreateNewAccount(loginOptions, authResponse); if (account.Policy != null && (!PincodeManager.IsPincodeSet() || AuthStorageHelper.IsPincodeRequired())) { PincodeManager.LaunchPincodeScreen(); } else { PlatformAdapter.SendToCustomLogger(string.Format("AuthHelper.EndLoginFlow - Navigating to {0}", SDKManager.RootApplicationPage), LoggingLevel.Information); frame.Navigate(SDKManager.RootApplicationPage); } }
/// <summary> /// Persist oauth credentials via the AccountManager /// </summary> /// <param name="loginOptions"></param> /// <param name="authResponse"></param> public async void EndLoginFlow(LoginOptions loginOptions, AuthResponse authResponse) { var frame = Window.Current.Content as Frame; Account account = await AccountManager.CreateNewAccount(loginOptions, authResponse); if (account.Policy != null && (!PincodeManager.IsPincodeSet() || AuthStorageHelper.IsPincodeRequired())) { PincodeManager.LaunchPincodeScreen(); } else { PlatformAdapter.SendToCustomLogger(string.Format("AuthHelper.EndLoginFlow - Navigating to {0}", SDKManager.RootApplicationPage), LoggingLevel.Information); frame.Navigate(SDKManager.RootApplicationPage); } }
/// <summary> /// Create and persist Account for newly authenticated user /// </summary> /// <param name="loginOptions"></param> /// <param name="authResponse"></param> public async static Task <bool> CreateNewAccount(LoginOptions loginOptions, AuthResponse authResponse) { Account account = new Account(loginOptions.LoginUrl, loginOptions.ClientId, loginOptions.CallbackUrl, loginOptions.Scopes, authResponse.InstanceUrl, authResponse.IdentityUrl, authResponse.AccessToken, authResponse.RefreshToken); var cm = new ClientManager(); var client = cm.PeekRestClient(); IdentityResponse identity = await OAuth2.CallIdentityService(authResponse.IdentityUrl, authResponse.AccessToken); if (identity != null) { account.UserId = identity.UserId; account.UserName = identity.UserName; AuthStorage.PersistCredentials(account); return(true); } return(false); }
/// <summary> /// Create and persist Account for newly authenticated user /// </summary> /// <param name="loginOptions"></param> /// <param name="authResponse"></param> public static async Task<Account> CreateNewAccount(LoginOptions loginOptions, AuthResponse authResponse) { PlatformAdapter.SendToCustomLogger("AccountManager.CreateNewAccount - create account object", LoggingLevel.Verbose); var account = new Account(loginOptions.LoginUrl, loginOptions.ClientId, loginOptions.CallbackUrl, loginOptions.Scopes, authResponse.InstanceUrl, authResponse.IdentityUrl, authResponse.AccessToken, authResponse.RefreshToken); account.CommunityId = authResponse.CommunityId; account.CommunityUrl = authResponse.CommunityUrl; var cm = new ClientManager(); cm.PeekRestClient(); IdentityResponse identity = null; try { identity = await OAuth2.CallIdentityService(authResponse.IdentityUrl, authResponse.AccessToken); } catch (JsonException ex) { PlatformAdapter.SendToCustomLogger( "AccountManager.CreateNewAccount - Exception occurred when retrieving account identity:", LoggingLevel.Critical); PlatformAdapter.SendToCustomLogger(ex, LoggingLevel.Critical); Debug.WriteLine("Error retrieving account identity"); } if (identity != null) { account.UserId = identity.UserId; account.UserName = identity.UserName; account.Policy = identity.MobilePolicy; AuthStorageHelper.GetAuthStorageHelper().PersistCredentials(account); } PlatformAdapter.SendToCustomLogger("AccountManager.CreateNewAccount - done", LoggingLevel.Verbose); return account; }
/// <summary> /// Persist oauth credentials via the AccountManager and navigate back to previous screen /// </summary> /// <param name="loginOptions"></param> /// <param name="authResponse"></param> public void EndLoginFlow(LoginOptions loginOptions, AuthResponse authResponse) { AccountManager.CreateNewAccount(loginOptions, authResponse); ((PhoneApplicationFrame) Application.Current.RootVisual).GoBack(); }
/// <summary> /// Create and persist Account for newly authenticated user /// </summary> /// <param name="loginOptions"></param> /// <param name="authResponse"></param> public static async Task<Account> CreateNewAccount(LoginOptions loginOptions, AuthResponse authResponse) { LoggingService.Log("Create account object", LoggingLevel.Verbose); var account = new Account( loginOptions.LoginUrl, loginOptions.ClientId, loginOptions.CallbackUrl, loginOptions.Scopes, authResponse.InstanceUrl, authResponse.IdentityUrl, authResponse.AccessToken, authResponse.RefreshToken) { CommunityId = authResponse.CommunityId, CommunityUrl = authResponse.CommunityUrl }; var cm = new ClientManager(); cm.PeekRestClient(); IdentityResponse identity = null; try { identity = await OAuth2.CallIdentityServiceAsync(authResponse.IdentityUrl, authResponse.AccessToken); } catch (JsonException ex) { LoggingService.Log("Exception occurred when retrieving account identity:", LoggingLevel.Critical); LoggingService.Log(ex, LoggingLevel.Critical); Debug.WriteLine("Error retrieving account identity"); } if (identity != null) { account.UserId = identity.UserId; account.UserName = identity.UserName; account.Policy = identity.MobilePolicy; account.OrganizationId = identity.OrganizationId; await AuthStorageHelper.PersistCurrentAccountAsync(account); LoggedInAccount = account; } LoggingService.Log("Finished creating account", LoggingLevel.Verbose); return account; }
/// <summary> /// Create and persist Account for newly authenticated user /// </summary> /// <param name="loginOptions"></param> /// <param name="authResponse"></param> public static async Task <Account> CreateNewAccount(LoginOptions loginOptions, AuthResponse authResponse) { PlatformAdapter.SendToCustomLogger("AccountManager.CreateNewAccount - create account object", LoggingLevel.Verbose); var account = new Account(loginOptions.LoginUrl, loginOptions.ClientId, loginOptions.CallbackUrl, loginOptions.Scopes, authResponse.InstanceUrl, authResponse.IdentityUrl, authResponse.AccessToken, authResponse.RefreshToken); account.CommunityId = authResponse.CommunityId; account.CommunityUrl = authResponse.CommunityUrl; IdentityResponse identity = null; try { var cm = new ClientManager(); cm.PeekRestClient(); identity = await OAuth2.CallIdentityService(authResponse.IdentityUrl, authResponse.AccessToken); } catch (JsonException ex) { PlatformAdapter.SendToCustomLogger( "AccountManager.CreateNewAccount - Exception occurred when retrieving account identity:", LoggingLevel.Critical); PlatformAdapter.SendToCustomLogger(ex, LoggingLevel.Critical); Debug.WriteLine("Error retrieving account identity"); } if (identity != null) { account.UserId = identity.UserId; account.UserName = identity.UserName; account.Policy = identity.MobilePolicy; AuthStorageHelper.GetAuthStorageHelper().PersistCredentials(account); } PlatformAdapter.SendToCustomLogger("AccountManager.CreateNewAccount - done", LoggingLevel.Verbose); return(account); }
/// <summary> /// Create and persist Account for newly authenticated user /// </summary> /// <param name="loginOptions"></param> /// <param name="authResponse"></param> public async static Task<bool> CreateNewAccount(LoginOptions loginOptions, AuthResponse authResponse) { Account account = new Account(loginOptions.LoginUrl, loginOptions.ClientId, loginOptions.CallbackUrl, loginOptions.Scopes, authResponse.InstanceUrl, authResponse.IdentityUrl, authResponse.AccessToken, authResponse.RefreshToken); var cm = new ClientManager(); var client = cm.PeekRestClient(); IdentityResponse identity = await OAuth2.CallIdentityService(authResponse.IdentityUrl, authResponse.AccessToken); if (identity != null) { account.UserId = identity.UserId; account.UserName = identity.UserName; AuthStorage.PersistCredentials(account); return true; } return false; }
/// <summary> /// Create and persist Account for newly authenticated user /// </summary> /// <param name="loginOptions"></param> /// <param name="authResponse"></param> public static void CreateNewAccount(LoginOptions loginOptions, AuthResponse authResponse) { Account account = new Account(loginOptions.LoginUrl, loginOptions.ClientId, loginOptions.CallbackUrl, loginOptions.Scopes, authResponse.InstanceUrl, authResponse.AccessToken, authResponse.RefreshToken); PlatformAdapter.Resolve<IAuthStorageHelper>().PersistCredentials(account); }
/// <summary> /// Create and persist Account for newly authenticated user /// </summary> /// <param name="loginOptions"></param> /// <param name="authResponse"></param> public static async Task <Account> CreateNewAccount(LoginOptions loginOptions, AuthResponse authResponse) { LoggingService.Log("Create account object", LoggingLevel.Verbose); var account = new Account( loginOptions.LoginUrl, loginOptions.ClientId, loginOptions.CallbackUrl, loginOptions.Scopes, authResponse.InstanceUrl, authResponse.IdentityUrl, authResponse.AccessToken, authResponse.RefreshToken) { CommunityId = authResponse.CommunityId, CommunityUrl = authResponse.CommunityUrl }; var cm = new ClientManager(); cm.PeekRestClient(); IdentityResponse identity = null; try { identity = await OAuth2.CallIdentityServiceAsync(authResponse.IdentityUrl, authResponse.AccessToken); } catch (JsonException ex) { LoggingService.Log("Exception occurred when retrieving account identity:", LoggingLevel.Critical); LoggingService.Log(ex, LoggingLevel.Critical); Debug.WriteLine("Error retrieving account identity"); } if (identity != null) { account.UserId = identity.UserId; account.UserName = identity.UserName; account.Policy = identity.MobilePolicy; account.OrganizationId = identity.OrganizationId; await AuthStorageHelper.PersistCurrentAccountAsync(account); LoggedInAccount = account; } LoggingService.Log("Finished creating account", LoggingLevel.Verbose); return(account); }
/// <summary> /// Extract the authentication data from the fragment portion of a URL /// </summary> /// <param name="fragmentstring"></param> /// <returns></returns> public static AuthResponse ParseFragment(string fragmentstring) { var res = new AuthResponse(); string[] parameters = fragmentstring.Split('&'); foreach (string parameter in parameters) { string[] parts = parameter.Split('='); string name = Uri.UnescapeDataString(parts[0]); string value = parts.Length > 1 ? Uri.UnescapeDataString(parts[1]) : ""; switch (name) { case "id": res.IdentityUrl = value; break; case "instance_url": res.InstanceUrl = value; break; case "access_token": res.AccessToken = value; break; case "refresh_token": res.RefreshToken = value; break; case "issued_at": res.IssuedAt = value; break; case "scope": res.Scopes = value.Split('+'); break; case "sfdc_community_id": res.CommunityId = value; break; case "sfdc_community_url": res.CommunityUrl = value; break; default: Debug.WriteLine("Parameter not recognized {0}", name); break; } } return res; }
/// <summary> /// Persist oauth credentials via the AccountManager /// </summary> /// <param name="loginOptions"></param> /// <param name="authResponse"></param> public void EndLoginFlow(LoginOptions loginOptions, AuthResponse authResponse) { AccountManager.CreateNewAccount(loginOptions, authResponse); }