private OidcClientDto AddOidcClient(OidcClientDto oidcClientDto)
		{
			var serverDto = GetServerDto ();
			var auth = SnapInContext.Instance.AuthTokenManager.GetAuthToken(serverDto.ServerName);
			var tenantName = GetTenant();
			return SnapInContext.Instance.ServiceGateway.OidcClient.Create(serverDto, tenantName, oidcClientDto.OidcClientMetadataDTO, auth.Token);
		}
 public OidcClientDto Get(ServerDto serverDto, string tenant, OidcClientDto oidcClientDto, Token token)
 {
     tenant = Uri.EscapeDataString(tenant);
     var clientId = Uri.EscapeDataString(oidcClientDto.ClientId);
     var url = string.Format(ServiceConfigManager.OidcClientPostEndPoint, serverDto.Protocol, serverDto.ServerName, serverDto.Port, tenant, clientId);
     ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
     var requestConfig = new RequestSettings
     {
         Method = HttpMethod.Post,
     };
     var headers = ServiceHelper.AddHeaders(ServiceConfigManager.JsonContentType);
     var json = "access_token=" + token.AccessToken + "&token_type=" + token.TokenType.ToString().ToLower();
     var response = _webRequestManager.GetResponse(url, requestConfig, headers, null, json);
     return JsonConvert.Deserialize<OidcClientDto>(response);
 }
		public void DeleteOidc(OidcClientDto dto)
		{
			ActionHelper.Execute (delegate() {
				var serverDto = GetServerDto ();
				var tenant = GetTenant ();
				var auth = SnapInContext.Instance.AuthTokenManager.GetAuthToken (serverDto.ServerName);
				var success = SnapInContext.Instance.ServiceGateway.OidcClient.Delete (serverDto, tenant, dto, auth.Token);
				if (success) {
					UIErrorHelper.ShowAlert ("Relying party " + dto.ClientId + " deleted successfully", "Information");
				} else {

					UIErrorHelper.ShowAlert ("Failed to delete relying party " + dto.ClientId, "Information");
				}
				Refresh (this, EventArgs.Empty);
			});
		}
		public override void AwakeFromNib ()
		{
			base.AwakeFromNib ();
			OidcClientDto = new OidcClientDto { 
				OidcClientMetadataDTO = new OidcClientMetadataDto {
					RedirectUris=new List<string>(), 
					PostLogoutRedirectUris = new List<string>()
				} 
			};
			BtnSelectCertificate.Activated +=	(object sender, EventArgs e) => {
				var openPanel = new NSOpenPanel();
				openPanel.ReleasedWhenClosed = true;
				openPanel.Prompt = "Select file";

				var result = openPanel.RunModal();
				if (result == 1)
				{
					var filePath = openPanel.Url.AbsoluteString.Replace("file://",string.Empty);
					var cert = new X509Certificate2 ();
					ActionHelper.Execute (delegate() {
						cert.Import (filePath);
						TxtCertificateDN.StringValue = cert.Subject;
					});
				}
			};

			BtnAddRedirectUri.Activated += (object sender, EventArgs e) => {
				if(string.IsNullOrEmpty(TxtRedirectUri.StringValue))
				{
					UIErrorHelper.ShowAlert ("Redirect Uri cannot be empty", "Alert");
					return;
				} else if(!WebUtil.IsValidHttpUrl(TxtRedirectUri.StringValue))
				{
					UIErrorHelper.ShowAlert ("Redirect Uri is invalid", "Alert");
					return;
				}
				OidcClientDto.OidcClientMetadataDTO.RedirectUris.Add(TxtRedirectUri.StringValue);
				ReloadTableView(RedirectUriTableView, OidcClientDto.OidcClientMetadataDTO.RedirectUris);
				TxtRedirectUri.StringValue = (NSString)string.Empty;
			};

			BtnAddPostLogoutRedirectUri.Activated += (object sender, EventArgs e) => {
				if(string.IsNullOrEmpty(TxtPostLogoutRedirectUri.StringValue))
				{
					UIErrorHelper.ShowAlert ("Post logout redirect Uri cannot be empty", "Alert");
					return;
				} else if(!WebUtil.IsValidHttpUrl(TxtPostLogoutRedirectUri.StringValue))
				{
					UIErrorHelper.ShowAlert ("Post logout is invalid", "Alert");
					return;
				}
				OidcClientDto.OidcClientMetadataDTO.PostLogoutRedirectUris.Add(TxtPostLogoutRedirectUri.StringValue);
				ReloadTableView(PostLogoutUtiTableView, OidcClientDto.OidcClientMetadataDTO.PostLogoutRedirectUris);
				TxtPostLogoutRedirectUri.StringValue = (NSString)string.Empty;
			};

			BtnRemoveRedirectUri.Activated += (object sender, EventArgs e) => {
				if (RedirectUriTableView.SelectedRows.Count > 0) {
					foreach (var row in RedirectUriTableView.SelectedRows) {
						
						OidcClientDto.OidcClientMetadataDTO.RedirectUris.RemoveAt((int)row);
					}
					ReloadTableView(RedirectUriTableView, OidcClientDto.OidcClientMetadataDTO.RedirectUris);
				}
			};

			BtnRemovePostLogoutRedirectUri.Activated += (object sender, EventArgs e) => {
				if (PostLogoutUtiTableView.SelectedRows.Count > 0) {
					foreach (var row in PostLogoutUtiTableView.SelectedRows) {

						OidcClientDto.OidcClientMetadataDTO.PostLogoutRedirectUris.RemoveAt((int)row);
					}
					ReloadTableView(PostLogoutUtiTableView, OidcClientDto.OidcClientMetadataDTO.PostLogoutRedirectUris);
				}
			};

			BtnClose.Activated += (object sender, EventArgs e) => {
				OidcClientDto = null;
				this.Close ();
				NSApplication.SharedApplication.StopModalWithCode (0);
			};

			this.BtnSave.Activated += (object sender, EventArgs e) => {
				if (string.IsNullOrEmpty (TxtCertificateDN.StringValue)) {
					UIErrorHelper.ShowAlert ("Please choose a valid certificate", "Alert");
				} else if (string.IsNullOrEmpty (TxtLogoutUri.StringValue) || !WebUtil.IsValidHttpUrl(TxtLogoutUri.StringValue)) {
					UIErrorHelper.ShowAlert ("Please enter valid logout uri", "Alert");
				} else if (OidcClientDto.OidcClientMetadataDTO.RedirectUris.Count == 0) {
					UIErrorHelper.ShowAlert ("Please enter a valid redirect URI", "Alert");
				} else if (OidcClientDto.OidcClientMetadataDTO.PostLogoutRedirectUris.Count == 0) {
					UIErrorHelper.ShowAlert ("Please enter a valid post logout redirect URI", "Alert");
				}else {
					OidcClientDto.OidcClientMetadataDTO.LogoutUri = TxtLogoutUri.StringValue;
					OidcClientDto.OidcClientMetadataDTO.TokenEndpointAuthMethod = (NSString)CbAuthTokenMethod.SelectedValue;
					OidcClientDto.OidcClientMetadataDTO.CertSubjectDN = TxtCertificateDN.StringValue;
					this.Close ();
					NSApplication.SharedApplication.StopModalWithCode (1);
				}
			};

			CbAuthTokenMethod.SelectItem (0);
		}
		public override void AwakeFromNib ()
		{
			base.AwakeFromNib ();
			OidcClientDto = new OidcClientDto {
				ClientId = OidcClientDtoOriginal.ClientId,
				OidcClientMetadataDTO = new OidcClientMetadataDto {
					RedirectUris = OidcClientDtoOriginal.OidcClientMetadataDTO.RedirectUris, 
					PostLogoutRedirectUris = OidcClientDtoOriginal.OidcClientMetadataDTO.PostLogoutRedirectUris,
					CertSubjectDN = OidcClientDtoOriginal.OidcClientMetadataDTO.CertSubjectDN,
					LogoutUri = OidcClientDtoOriginal.OidcClientMetadataDTO.LogoutUri,
					TokenEndpointAuthMethod = OidcClientDtoOriginal.OidcClientMetadataDTO.TokenEndpointAuthMethod
				}
			};

			TxtName.StringValue = OidcClientDtoOriginal.ClientId;
			var authIndex = OidcClientDtoOriginal.OidcClientMetadataDTO.TokenEndpointAuthMethod == "none" ? 0 : 1;
			CbTokenAuthMethod.SelectItem (authIndex);
			TxtLogoutUrl.StringValue = string.IsNullOrEmpty (OidcClientDtoOriginal.OidcClientMetadataDTO.LogoutUri) ? string.Empty :
				OidcClientDtoOriginal.OidcClientMetadataDTO.LogoutUri;
			TxtCertificateDN.StringValue = string.IsNullOrEmpty (OidcClientDtoOriginal.OidcClientMetadataDTO.CertSubjectDN) ? 
				string.Empty : OidcClientDtoOriginal.OidcClientMetadataDTO.CertSubjectDN;
			ReloadTableView(RedirectUriTableView, OidcClientDto.OidcClientMetadataDTO.RedirectUris);
			ReloadTableView(PostLogoutRedirectUriTableView, OidcClientDto.OidcClientMetadataDTO.PostLogoutRedirectUris);

			BtnBrowseCertificate.Activated +=	(object sender, EventArgs e) => {
				var openPanel = new NSOpenPanel();
				openPanel.ReleasedWhenClosed = true;
				openPanel.Prompt = "Select file";

				var result = openPanel.RunModal();
				if (result == 1)
				{
					var filePath = openPanel.Url.AbsoluteString.Replace("file://",string.Empty);
					var cert = new X509Certificate2 ();
					ActionHelper.Execute (delegate() {
						cert.Import (filePath);
						TxtCertificateDN.StringValue = cert.Subject;
					});
				}
			};

			BtnAddRedirectUri.Activated += (object sender, EventArgs e) => {
				if(string.IsNullOrEmpty(TxtRedirectUri.StringValue))
				{
					UIErrorHelper.ShowAlert ("Redirect Uri cannot be empty", "Alert");
					return;
				} else if(!WebUtil.IsValidHttpUrl(TxtRedirectUri.StringValue))
				{
					UIErrorHelper.ShowAlert ("Redirect Uri is invalid", "Alert");
					return;
				}
				OidcClientDto.OidcClientMetadataDTO.RedirectUris.Add(TxtRedirectUri.StringValue);
				ReloadTableView(RedirectUriTableView, OidcClientDto.OidcClientMetadataDTO.RedirectUris);
				TxtRedirectUri.StringValue = (NSString)string.Empty;
			};

			BtnAddPostLogoutRedirectUri.Activated += (object sender, EventArgs e) => {
				if(string.IsNullOrEmpty(TxtPostLogoutRedirectUri.StringValue))
				{
					UIErrorHelper.ShowAlert ("Post logout redirect Uri cannot be empty", "Alert");
					return;
				} else if(!WebUtil.IsValidHttpUrl(TxtPostLogoutRedirectUri.StringValue))
				{
					UIErrorHelper.ShowAlert ("Post logout is invalid", "Alert");
					return;
				}
				OidcClientDto.OidcClientMetadataDTO.PostLogoutRedirectUris.Add(TxtPostLogoutRedirectUri.StringValue);
				ReloadTableView(PostLogoutRedirectUriTableView, OidcClientDto.OidcClientMetadataDTO.PostLogoutRedirectUris);
				TxtPostLogoutRedirectUri.StringValue = (NSString)string.Empty;
			};

			BtnRemoveRedirectUri.Activated += (object sender, EventArgs e) => {
				if (RedirectUriTableView.SelectedRows.Count > 0) {
					foreach (var row in RedirectUriTableView.SelectedRows) {

						OidcClientDto.OidcClientMetadataDTO.RedirectUris.RemoveAt((int)row);
					}
					ReloadTableView(RedirectUriTableView, OidcClientDto.OidcClientMetadataDTO.RedirectUris);
				}
			};

			BtnRemovePostLogoutRedirectUri.Activated += (object sender, EventArgs e) => {
				if (PostLogoutRedirectUriTableView.SelectedRows.Count > 0) {
					foreach (var row in PostLogoutRedirectUriTableView.SelectedRows) {

						OidcClientDto.OidcClientMetadataDTO.PostLogoutRedirectUris.RemoveAt((int)row);
					}
					ReloadTableView(PostLogoutRedirectUriTableView, OidcClientDto.OidcClientMetadataDTO.PostLogoutRedirectUris);
				}
			};

			this.BtnApply.Activated += (object sender, EventArgs e) => {

				ActionHelper.Execute (delegate() {
					if (string.IsNullOrEmpty (TxtCertificateDN.StringValue)) {
						UIErrorHelper.ShowAlert ("Please choose a valid certificate", "Alert");
					} else if (string.IsNullOrEmpty (TxtLogoutUrl.StringValue) || !WebUtil.IsValidHttpUrl(TxtLogoutUrl.StringValue)) {
						UIErrorHelper.ShowAlert ("Please enter valid logout uri", "Alert");
					} else if (OidcClientDto.OidcClientMetadataDTO.RedirectUris.Count == 0) {
						UIErrorHelper.ShowAlert ("Please enter a valid redirect URI", "Alert");
					} else if (OidcClientDto.OidcClientMetadataDTO.PostLogoutRedirectUris.Count == 0) {
						UIErrorHelper.ShowAlert ("Please enter a valid post logout redirect URI", "Alert");
					} else {
						OidcClientDto.OidcClientMetadataDTO.LogoutUri = TxtLogoutUrl.StringValue;
						OidcClientDto.OidcClientMetadataDTO.TokenEndpointAuthMethod = (NSString)CbTokenAuthMethod.SelectedValue;
						OidcClientDto.OidcClientMetadataDTO.CertSubjectDN = TxtCertificateDN.StringValue;
						var auth = SnapInContext.Instance.AuthTokenManager.GetAuthToken (ServerDto.ServerName);
						OidcClientDto = SnapInContext.Instance.ServiceGateway.OidcClient.Update (ServerDto, TenantName, OidcClientDto.ClientId, OidcClientDto.OidcClientMetadataDTO, auth.Token);
						NSNotificationCenter.DefaultCenter.PostNotificationName ("RefreshTableView", this);
					}
				});
			};
		}