async void handleApplicationTapped(object sender, ItemTappedEventArgs e) { try { if (!isBusy) { isBusy = true; if (e.Item == null) { return; } var selectedApplication = e.Item as Workbench.Client.Models.Application; //Reset the IsInitiator property App.ViewModel.IsUserInitiator = false; await App.ViewModel.SetCurrentApplication(selectedApplication.Id.ToString()); await App.ViewModel.RefreshWorkflowsForSelectedApplication(selectedApplication.Id.ToString()); } } catch (Exception ex) { AppCenterHelper.Report(ex); } finally { isBusy = false; } }
public async Task <bool> LoginAsync() { if (IsBusy) { return(false); } IsBusy = true; IsAnimationVisible = true; try { var response = await executeLoginAsync(); IsAnimationVisible = false; return(response); } catch (Exception e) { AppCenterHelper.Report(e); await App.Current.MainPage.DisplayAlert("Error", $"executeLoginAsync: {e.Message}", "OK"); } finally { IsBusy = false; IsAnimationVisible = false; } return(false); }
public async Task <LoginResponse> LoginAsync(string authority, string resource, string clientId, string returnUri, bool isRefresh = false) { var authContext = new AuthenticationContext(authority); if (authContext.TokenCache.ReadItems().Any()) { authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority); } try { AuthenticationResult authResult; if (isRefresh) { authResult = await authContext.AcquireTokenSilentAsync(resource, clientId); } else { var controller = ViewControllerHelpers.GetVisibleViewController(); var uri = new Uri(returnUri); var platformParams = new PlatformParameters(controller); authResult = await authContext.AcquireTokenAsync(resource, clientId, uri, platformParams); } var result = new LoginResponse { AuthHeader = authResult.CreateAuthorizationHeader(), AccessToken = authResult.AccessToken, AccessTokenType = authResult.AccessTokenType, ExpiresOn = authResult.ExpiresOn, ExtendedLifetimeToken = authResult.ExtendedLifeTimeToken, IdToken = authResult.IdToken, TenantId = authResult.TenantId }; result.Profile.DisplayableId = authResult.UserInfo.DisplayableId; result.Profile.FamilyName = authResult.UserInfo.FamilyName; result.Profile.GivenName = authResult.UserInfo.GivenName; result.Profile.UniqueId = authResult.UserInfo.UniqueId; return(result); } catch (Exception e) { var _controller = ViewControllerHelpers.GetVisibleViewController(); var alert = UIAlertController.Create("Error", e.Message, UIAlertControllerStyle.Alert); alert.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, null)); _controller.BeginInvokeOnMainThread(() => { _controller.PresentViewController(alert, true, null); }); AppCenterHelper.Report(e); clearTokenFromCache(authority); return(null); } }
public async Task <LoginResponse> LoginAsync(string authority, string resource, string clientId, string returnUri, bool isRefresh = false) { var authContext = new AuthenticationContext(authority); if (authContext.TokenCache.ReadItems().Any()) { authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority); } var uri = new Uri(returnUri); var platformParams = new PlatformParameters((Android.App.Activity)Xamarin.Forms.Forms.Context); try { AuthenticationResult authResult; if (isRefresh) { authResult = await authContext.AcquireTokenSilentAsync(resource, clientId); } else { authResult = await authContext.AcquireTokenAsync(resource, clientId, uri, platformParams).ConfigureAwait(false); } var result = new LoginResponse { AuthHeader = authResult.CreateAuthorizationHeader(), AccessToken = authResult.AccessToken, AccessTokenType = authResult.AccessTokenType, ExpiresOn = authResult.ExpiresOn, ExtendedLifetimeToken = authResult.ExtendedLifeTimeToken, IdToken = authResult.IdToken, TenantId = authResult.TenantId }; result.Profile.DisplayableId = authResult.UserInfo.DisplayableId; result.Profile.FamilyName = authResult.UserInfo.FamilyName; result.Profile.GivenName = authResult.UserInfo.GivenName; result.Profile.UniqueId = authResult.UserInfo.UniqueId; return(result); } catch (Exception e) { AppCenterHelper.Report(e); clearTokenFromCache(authority); return(null); } }
public async Task LoadContractInstanceAsync() { IsBusy = true; try { Blocks = new List <BlockModel>(); DisplayActions = false; var res = await GatewayApi.Instance.GetContractByIdAsync(ContractInstance.Id.ToString()); if (res != default(Contract) || res != null) { ContractInstance = res; assembleBlocks(); } assembleUsers(); var actions = await GatewayApi.Instance.GetWorkflowActionsAsync(ContractInstance.Id.ToString()); if (actions != null) { Actions = new List <WorkflowFunction>(actions); if (Actions?.Count > 0) { DisplayActions = true; } else { DisplayActions = false; } } else { DisplayActions = false; } } catch (Exception e) { AppCenterHelper.Report(e); } finally { IsBusy = false; } }
async Task configureFirstEnvironment() { try { addingNewEnvironment = true; var prompt = new PromptConfig { IsCancellable = true, Message = $"Please enter the URL that contains your environment configuration JSON", OkText = "Ok", Title = "Add Environment" }; var URLResults = await UserDialogs.Instance.PromptAsync(prompt); if (!string.IsNullOrEmpty(URLResults?.Value)) { using (var httpclient = new HttpClient()) { var results = await httpclient.GetStringAsync(URLResults?.Value); if (!string.IsNullOrEmpty(results)) { var environment = JsonConvert.DeserializeObject <BlockchainEnvironment>(results); await LocalDbHelper.Instance.SaveEnvironmentAsync(environment); Device.BeginInvokeOnMainThread(async() => { await LoginAsync(); }); } else { await UserDialogs.Instance.AlertAsync("Unable to get environment data, please ensure you entered a valid URL", "Error", "OK"); } } } } catch (Exception e) { AppCenterHelper.Report(e); await UserDialogs.Instance.AlertAsync("Unable to get environment data, please ensure you entered a valid URL", "Error", "OK"); } }
public void clearTokenFromCache(string authority) { try { Android.Webkit.CookieManager.Instance.RemoveAllCookies(null); var authContext = new AuthenticationContext(authority); authContext.TokenCache.Clear(); var editor = SharedPreferences.Edit(); editor.Remove(SharedPreferencesKey); } catch (Exception ex) { // log the error AppCenterHelper.Report(ex); System.Diagnostics.Debug.WriteLine(ex.Message); } }
public App() { FlowListView.Init(); GatewayApi.Instance.ExpiredAccessToken += async(object sender, EventArgs e) => { var localEnvironments = await LocalDbHelper.Instance.GetAllSavedEnvironmentsAsync(); BlockchainEnvironment environment = localEnvironments[0]; var authService = ServiceContainer.Resolve <IAuthentication>(); var loginResponse = await authService.LoginAsync( $"https://login.windows.net/{environment.TenantId}", environment.ResourceId, environment.ClientId, environment.ReturnUrl, isRefresh : true); if (loginResponse != null) { AppCenterHelper.TrackEvent("Refreshed Token", new Dictionary <string, string>() { { "username", loginResponse.Profile.DisplayableId } }); Settings.AccessToken = loginResponse.AccessToken; GatewayApi.Instance.SetAuthToken(loginResponse.AccessToken); #if DEBUG Settings.AccessTokenExpiration = new DateTimeOffset(DateTime.Now.AddMinutes(2)); GatewayApi.Instance.AccessTokenExpiration = new DateTimeOffset(DateTime.Now.AddMinutes(2)); #else Settings.AccessTokenExpiration = loginResponse.ExpiresOn; GatewayApi.Instance.AccessTokenExpiration = loginResponse.ExpiresOn; #endif } else { App.Logout(false); } }; GatewayApi.Instance.ExceptionThrown += async(object sender, Exception e) => { AppCenterHelper.Report(e, new Dictionary <string, string>() { { "baseurl", GatewayApi.SiteUrl } }); }; ViewModel = new AppViewModel(); backgroundPage = new BackgroundPage(); var navPage = new Xamarin.Forms.NavigationPage(backgroundPage); var detailNavPage = new Xamarin.Forms.NavigationPage(new ApplicationsPage()); ContractsPage = new WorklfowInstanceListPage(); navPage.BarBackgroundColor = Constants.NavBarBackgroundColor; navPage.BarTextColor = Constants.NavBarTextColor; detailNavPage.BarBackgroundColor = Constants.NavBarBackgroundColor; detailNavPage.BarTextColor = Constants.NavBarTextColor; Master = new MasterDetailPage { BindingContext = ViewModel }; Master.IsPresentedChanged += (object sender, EventArgs e) => { var mdp = sender as MasterDetailPage; if (mdp.IsPresented) { ((Xamarin.Forms.NavigationPage)mdp.Detail) .On <iOS>() .SetStatusBarTextColorMode(StatusBarTextColorMode.DoNotAdjust); } else { ((Xamarin.Forms.NavigationPage)mdp.Detail) .On <iOS>() .SetStatusBarTextColorMode(StatusBarTextColorMode.MatchNavigationBarTextLuminosity); } }; Master.Master = new MasterPage { BindingContext = ViewModel }; Master.Detail = detailNavPage; Master.SetBinding(MasterDetailPage.IsPresentedProperty, nameof(AppViewModel.MenuPresented), BindingMode.TwoWay); Xamarin.Forms.NavigationPage.SetHasNavigationBar(detailNavPage, false); try { MainPage = navPage; } catch (Exception e) { AppCenterHelper.Report(e); } }
void setupActionView() { foreach (var actionParam in contractAction?.Parameters) { var action = actionParam.Type.Name.ToLower(); //Add Labels var label = new Label { HorizontalOptions = LayoutOptions.FillAndExpand, Text = $"{actionParam.DisplayName}:" }; parameterViewStack.Children.Add(label); //Check if action parameter is within the application roles var currentAppRole = App.ViewModel.CurrentApplication.ApplicationRoles.FirstOrDefault(x => x.Name.ToLower().Equals(action)); if (currentAppRole != null) { var picker = new Picker { HorizontalOptions = LayoutOptions.FillAndExpand, Title = actionParam.DisplayName }; var listOfRoles = App.ViewModel.ApplicationRoleMappingsList.Where(X => X.ApplicationRoleId.Equals(currentAppRole.Id)); picker.ItemsSource = listOfRoles.Select(x => x.User).ToList().Select(Y => Y.DisplayName).ToList(); parameterViewStack.Children.Add(picker); } else { switch (action) { case ContractParameterType.String: case ContractParameterType.Address: case ContractParameterType.Contract: var stringEntry = new Entry { HorizontalOptions = LayoutOptions.FillAndExpand }; parameterViewStack.Children.Add(stringEntry); break; case ContractParameterType.Money: case ContractParameterType.Uint: case ContractParameterType.Int: case ContractParameterType.Time: var moneyEntry = new Entry { HorizontalOptions = LayoutOptions.FillAndExpand, Keyboard = Keyboard.Numeric }; parameterViewStack.Children.Add(moneyEntry); break; case ContractParameterType.Device: case ContractParameterType.User: var picker = new Picker { HorizontalOptions = LayoutOptions.FillAndExpand, Title = actionParam.DisplayName }; var listOfRoles = App.ViewModel.ApplicationRoleMappingsList.Where(X => X.ApplicationRoleId.Equals(currentAppRole.Id)); picker.ItemsSource = listOfRoles.Select(x => x.User).ToList().Select(Y => Y.DisplayName).ToList(); parameterViewStack.Children.Add(picker); break; case ContractParameterType.Enum: var enumPicker = new Picker { HorizontalOptions = LayoutOptions.FillAndExpand, Title = actionParam.DisplayName }; enumPicker.ItemsSource = actionParam.Type.EnumValues; parameterViewStack.Children.Add(enumPicker); break; case ContractParameterType.Array: var arrayEntry = new Entry { HorizontalOptions = LayoutOptions.FillAndExpand }; if (actionParam.Type.ElementType.Name.Equals("bool")) { arrayEntry.Keyboard = Keyboard.Default; arrayEntry.Placeholder = "Input a comma separated array of booleans"; } else { arrayEntry.Placeholder = "Input a comma separated array of integers"; arrayEntry.Keyboard = Keyboard.Numeric; } parameterViewStack.Children.Add(arrayEntry); break; case ContractParameterType.Bool: var boolPicker = new Picker { HorizontalOptions = LayoutOptions.FillAndExpand, Title = actionParam.DisplayName }; boolPicker.ItemsSource = new List <string>() { "True", "False" }; parameterViewStack.Children.Add(boolPicker); break; default: AppCenterHelper.Report(new Exception($"unsupported data type encountered: {action}")); break; } } } }