/// <summary> /// Request a new bearer token and store it on the local Account. /// </summary> /// <param name="username"></param> /// <param name="password"></param> /// <returns>Returns success state</returns> public static async Task <bool> RequestToken(string username, string password) { var authenticator = new OAuth2PasswordCredentialsAuthenticator(TokenEndpoint); authenticator.SetCredentials(username, password); try { var account = await authenticator.SignInAsync(); if (account == null) { return(false); } account.Properties.Add("password", password); account.Properties.Add("date", DateTime.Now.ToString()); await AccountStore.SaveAsync(account, App.Settings.ServiceId); AuthAccount = account; OnTokenUpdated?.Invoke(null, (string)account.Properties["access_token"]); return(true); } catch (Exception) { return(false); } }
public void AuthenticatorConstructorWithCredentialsGeneratesFields() { var tokenEndpoint = new Uri("https://companistawebtesting.azurewebsites.net/Token"); var auth = new OAuth2PasswordCredentialsAuthenticator(tokenEndpoint); auth.SetCredentials("Bill", "Abc_123"); Assert.IsTrue(auth.Fields.Count > 1); }
public void BasicFailedAuthenticationFlow() { var auth = new OAuth2PasswordCredentialsAuthenticator(new Uri("http://www.google.com")); auth.SetCredentials("Bob", "strongedpassword"); var errorFired = false; auth.Error += (obj, args) => errorFired = true; auth.Completed += (obj, args) => Assert.Fail("Complete should not be rised on failure"); var result = auth.SignInAsync().Result; Assert.IsFalse(auth.HasCompleted); Assert.IsTrue(errorFired); }
public void BasicValidAuthenticationFlow() { var completeFired = false; var tokenEndpoint = new Uri("https://companistawebtesting.azurewebsites.net/Token"); var auth = new OAuth2PasswordCredentialsAuthenticator(tokenEndpoint); auth.SetCredentials("Bill", "Abc_123"); auth.Error += (obj, args) => Assert.Fail("Error should not be rised on success"); auth.Completed += (obj, args) => completeFired = args.IsAuthenticated; var account = auth.SignInAsync().Result; Assert.AreEqual(tokenEndpoint, auth.AccessTokenUrl); Assert.IsTrue(auth.HasCompleted); Assert.IsTrue(completeFired); Assert.IsNotNull(account); }
public void ExceptionMessageIsNotNull() { var auth = new OAuth2PasswordCredentialsAuthenticator(new Uri("https://Google.com")); auth.SetCredentials("Bill", "Abc_123"); var completeFired = false; var message = string.Empty; auth.Error += (obj, args) => { completeFired = true; message = args.Message; }; var account = auth.SignInAsync().Result; Assert.IsTrue(completeFired); Assert.IsNotNull(message); }
public AuthDialogPage(OAuth2PasswordCredentialsAuthenticator authenticator, AuthPageConfiguration authPageConfiguration = null) { if (authenticator == null) { throw new ArgumentNullException(nameof(authenticator), "You must provide an anthenticator"); } if (authPageConfiguration == null) { authPageConfiguration = new AuthPageConfiguration(); } _authPageConfiguration = authPageConfiguration; _authenticator = authenticator; _authenticator.Completed += AuthenticatorOnCompleted; _authenticator.Error += AuthenticatorOnError; InitializeComponent(); AuthFieldsToEntries(); ConfigurePage(); _authenticationBehaviour = ControllerBag.GetBehaviour <AuthenticationBehaviour>(); }
public void ThrowsExeptionOnNullUri() { var auth = new OAuth2PasswordCredentialsAuthenticator(null); }