using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using System.Net; using System.Threading; using System.Web; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using DotNetOpenAuth.Messaging; using DotNetOpenAuth.OAuth2; using DotNetOpenAuth.OAuth2.Messages; using Microsoft.Practices.ServiceLocation; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using Taloyhtio.GeneralSSO.Server.CodeFiles.Common; using Taloyhtio.GeneralSSO.Server.CodeFiles.Entities; using Taloyhtio.GeneralSSO.Server.CodeFiles.Infrastructure.OAuth; using Taloyhtio.GeneralSSO.Server.CodeFiles.Models; using Taloyhtio.GeneralSSO.Server.CodeFiles.Repositories; namespace Taloyhtio.GeneralSSO.Server.Clients { public partial class Details : LayoutsPageBase { private IClientRepository clientRepository; protected override bool AllowAnonymousAccess { get { return false; } } protected override bool RequireSiteAdministrator { get { return true; } } protected bool IsEditMode { get { return !string.IsNullOrEmpty(this.hdnId.Value); } } public Details() { this.clientRepository = ServiceLocator.Current.GetInstance(); } protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { string idStr = this.Request.QueryString["Id"]; if (string.IsNullOrEmpty(idStr)) { return; } this.lnkDeleteConfirm.Visible = true; int id = int.Parse(idStr); var client = this.clientRepository.GetById(id); if (client == null) { throw new Exception(string.Format("Client with id '{0}' not found", id)); } this.bind(client); } } private void bind(Client c) { if (c == null) { throw new Exception("Client is null"); } this.txtName.Text = c.Name; this.txtClientId.Text = c.ClientIdentifier; this.txtClientSecret.Text = c.ClientSecret; this.txtCallbackUrl.Text = c.Callback; this.hdnId.Value = c.Id.ToString(); } protected void lnkSave_Click(object sender, EventArgs e) { if (!this.IsValid) { return; } var client = this.IsEditMode ? this.clientRepository.GetById(int.Parse(this.hdnId.Value)) : new Client(); client.Name = this.txtName.Text; client.ClientIdentifier = this.txtClientId.Text; client.ClientSecret = this.txtClientSecret.Text; client.Callback = this.txtCallbackUrl.Text; client.ClientType = ClientType.Public; this.clientRepository.Save(client); this.Response.Redirect("~/_layouts/15/Taloyhtio/OAuth/admin/clients/list.aspx"); } protected void lnkDelete_Click(object sender, EventArgs e) { if (this.IsEditMode) { var client = this.clientRepository.GetById(int.Parse(this.hdnId.Value)); this.clientRepository.Delete(client); } this.Response.Redirect("~/_layouts/15/Taloyhtio/OAuth/admin/clients/list.aspx"); } protected void lnkCancel_Click(object sender, EventArgs e) { this.Response.Redirect("~/_layouts/15/Taloyhtio/OAuth/admin/clients/list.aspx"); } protected void validateName(object source, ServerValidateEventArgs args) { var client = this.clientRepository.GetByName(args.Value); args.IsValid = this.isUnique(client); } private bool isUnique(Client client) { if (client == null) { return true; } return this.IsEditMode ? client.Id == int.Parse(this.hdnId.Value) : false; } protected void validateClientId(object source, ServerValidateEventArgs args) { var client = this.clientRepository.GetByClientId(args.Value); args.IsValid = this.isUnique(client); } protected void validateClientSecret(object source, ServerValidateEventArgs args) { var client = this.clientRepository.GetByClientSecret(args.Value); args.IsValid = this.isUnique(client); } } }