Ejemplo n.º 1
0
        public async Task <AuthUserConfig> GetAuthUserConfigAsync()
        {
            try
            {
#if DEBUG
                if (SettingsStudio.Settings.UseLocalServer)
                {
                    httpClient.BaseAddress = new Uri(Keys.Azure.AuthUrl);
                }
#endif

                var keychain = new Keychain();

                var storedKeys = keychain.GetItemFromKeychain(AzureAppServiceUser.AuthenticationHeader);

                if (!string.IsNullOrEmpty(storedKeys.Account) && !string.IsNullOrEmpty(storedKeys.PrivateKey))
                {
                    httpClient.DefaultRequestHeaders.Remove(AzureAppServiceUser.AuthenticationHeader);

                    httpClient.DefaultRequestHeaders.Add(AzureAppServiceUser.AuthenticationHeader, storedKeys.PrivateKey);

                    var userConfigJson = await httpClient.GetStringAsync("api/user/config");

                    Log.Debug($"userConfigJson {userConfigJson}");

                    AuthUser = JsonConvert.DeserializeObject <AuthUserConfig> (userConfigJson);

                    return(AuthUser);
                }

                return(null);
            }
            catch (HttpRequestException reEx)
            {
                if (reEx.Message.Contains("401"))
                {
                    new Keychain().RemoveItemFromKeychain(AzureAppServiceUser.AuthenticationHeader);

                    return(null);
                }

                Log.Error(reEx.Message);
                throw;
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                throw;
            }
#if DEBUG
            finally
            {
                if (SettingsStudio.Settings.UseLocalServer)
                {
                    httpClient.BaseAddress = new Uri(Keys.Azure.ServiceUrl);
                }
            }
#endif
        }
Ejemplo n.º 2
0
        public async Task AuthenticateUser(string providerToken, string providerAuthCode)
        {
            try
            {
                if (string.IsNullOrEmpty(providerToken))
                {
                    throw new ArgumentNullException(nameof(providerToken));
                }
                if (string.IsNullOrEmpty(providerAuthCode))
                {
                    throw new ArgumentNullException(nameof(providerAuthCode));
                }

                ResetUser();

                var auth = JObject.Parse($"{{'id_token':'{providerToken}','authorization_code':'{providerAuthCode}'}}").ToString();

                NetworkIndicator.ToggleVisibility(true);

                var authResponse = await HttpClient.PostAsync(Routes.LoginGoogle, new StringContent (auth, Encoding.UTF8, Routes.Json));

                if (authResponse.IsSuccessStatusCode)
                {
                    var azureUser = JsonConvert.DeserializeObject <AzureAppServiceUser> (await authResponse.Content.ReadAsStringAsync());

                    Keychain.SaveItemToKeychain(AzureAppServiceUser.AuthenticationHeader, "azure", azureUser.AuthenticationToken);

                    _httpClient = null;

                    var userConfigJson = await HttpClient.GetStringAsync(Routes.AuthenticateUser);

                    authUser = JsonConvert.DeserializeObject <AuthUserConfig> (userConfigJson);

                    authUser.SaveToKeychain(Keychain);

                    Log.Debug(authUser.ToString());

                    _user = null;

                    CurrentUserChanged?.Invoke(this, User);
                }
                else
                {
                    Log.Error(auth);
                    Log.Error(authResponse?.ToString());
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                throw;
            }
            finally
            {
                NetworkIndicator.ToggleVisibility(false);
            }
        }
Ejemplo n.º 3
0
 public User(ClientAuthDetails providerDetails, AuthUserConfig serverDetails)
 {
     Id         = serverDetails.Id;
     Alias      = providerDetails.Email ?? serverDetails.Email;
     Email      = providerDetails.Email ?? serverDetails.Email;
     Username   = providerDetails.Email ?? serverDetails.Email;
     Name       = providerDetails.Name;
     GivenName  = providerDetails.GivenName ?? serverDetails.GivenName;
     FamilyName = providerDetails.FamilyName ?? serverDetails.SurName;
     AvatarUrl  = providerDetails.AvatarUrl ?? serverDetails.Picture;
     UserRole   = serverDetails.UserRole;
     Locale     = serverDetails.Locale;
     Token      = providerDetails.Token;
     AuthCode   = providerDetails.AuthCode;
 }
Ejemplo n.º 4
0
        public void ResetUser()
        {
            _user = null;

            Keychain.RemoveItemFromKeychain(AzureAppServiceUser.AuthenticationHeader);

            AuthUserConfig.RemoveFromKeychain(Keychain);

            Settings.SetContentToken <AvContent> (string.Empty);

            _httpClient = null;

            ContentClient.Shared.ResetClient();

            CurrentUserChanged?.Invoke(this, null);
        }
Ejemplo n.º 5
0
        public async Task <AuthUserConfig> GetAuthUserConfigAsync(string providerToken, string providerAuthCode)
        {
            try
            {
#if DEBUG
                if (SettingsStudio.Settings.UseLocalServer)
                {
                    httpClient.BaseAddress = new Uri(Keys.Azure.AuthUrl);
                }
#endif

                if (!string.IsNullOrEmpty(providerToken) && !string.IsNullOrEmpty(providerAuthCode))
                {
                    var auth = JObject.Parse($"{{'id_token':'{providerToken}','authorization_code':'{providerAuthCode}'}}").ToString();

                    var authResponse = await httpClient.PostAsync(".auth/login/google", new StringContent (auth, Encoding.UTF8, "application/json"));

                    if (authResponse.IsSuccessStatusCode)
                    {
                        var azureUserJson = await authResponse.Content.ReadAsStringAsync();

                        Log.Debug($"azureUserJson: {azureUserJson}");

                        var azureUser = JsonConvert.DeserializeObject <AzureAppServiceUser> (azureUserJson);


                        Log.Debug($"azureUser.AuthenticationToken {azureUser.AuthenticationToken}");

                        httpClient.DefaultRequestHeaders.Remove(AzureAppServiceUser.AuthenticationHeader);

                        httpClient.DefaultRequestHeaders.Add(AzureAppServiceUser.AuthenticationHeader, azureUser.AuthenticationToken);

                        var keychain = new Keychain();

                        keychain.SaveItemToKeychain(AzureAppServiceUser.AuthenticationHeader, "azure", azureUser.AuthenticationToken);

                        var userConfigJson = await httpClient.GetStringAsync("api/user/config");

                        Log.Debug($"userConfigJson {userConfigJson}");

                        AuthUser = JsonConvert.DeserializeObject <AuthUserConfig> (userConfigJson);

                        return(AuthUser);
                    }
                }

                return(null);
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                throw;
            }
#if DEBUG
            finally
            {
                if (SettingsStudio.Settings.UseLocalServer)
                {
                    httpClient.BaseAddress = new Uri(Keys.Azure.ServiceUrl);
                }
            }
#endif
        }