//****************************************************************************************************** // SecurityService.svc.cs - Gbtc // // Copyright © 2012, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may // not use this file except in compliance with the License. You may obtain a copy of the License at: // // http://www.opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 05/18/2010 - Pinal C. Patel // Generated original version of source code. // 05/24/2010 - Pinal C. Patel // Modified the service so it could be hosted in ASP.NET compatibility mode. // Modified Login() to initialize the provider once and cache it for subsequent uses. // 12/09/2010 - Pinal C. Patel // Renamed Login operation to Authenticate and added GetUserData and RefreshUserData operations. // 01/14/2011 - Pinal C. Patel // Added ResetPassword() and ChangePassword() methods. // 12/20/2012 - Starlynn Danyelle Gilliam // Modified Header. // //****************************************************************************************************** using System.Diagnostics.CodeAnalysis; using System.ServiceModel.Activation; using System.Threading; using GSF.Security; using GSF.ServiceModel; namespace GSF.Web.Embedded { /// /// Embedded WCF service that can be used for securing applications using role-based security. /// /// /// This list shows the endpoints exposed by : /// /// /// URI /// Protocol /// /// /// ~/SecurityService.svc/soap /// SOAP 1.1 /// /// /// ~/SecurityService.svc/rest /// REST /// /// /// /// /// This example shows how to consume the REST endpoint of using jQuery: /// /// /// /// ]]> /// /// [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] [SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly")] public class SecurityService : SelfHostingService, ISecurityService { private ISecurityProvider CurrentProvider { get { return (Thread.CurrentPrincipal as SecurityPrincipal)?.Identity.Provider; } } private void ValidateCurrentProvider() { if (CurrentProvider == null) { ISecurityProvider securityProvider = SecurityProviderCache.CreateProvider(Thread.CurrentPrincipal.Identity.Name); securityProvider.PassthroughPrincipal = Thread.CurrentPrincipal; securityProvider.Authenticate(); SecurityIdentity securityIdentity = new SecurityIdentity(securityProvider); Thread.CurrentPrincipal = new SecurityPrincipal(securityIdentity); } } /// /// Returns information about the current user. /// /// An object of the user if user's security context has been initialized, otherwise null. public UserData GetUserData() { ValidateCurrentProvider(); return CurrentProvider.UserData; } /// /// Refreshes and returns information about the current user. /// /// An object of the user if user's security context has been initialized, otherwise null. public UserData RefreshUserData() { ValidateCurrentProvider(); if (CurrentProvider.CanRefreshData) CurrentProvider.RefreshData(); return CurrentProvider.UserData; } /// /// Authenticates a user and caches the security context upon successful authentication for subsequent use. /// /// Username of the user. /// Password of the user. /// An object of the user. public UserData Authenticate(string username, string password) { ISecurityProvider securityProvider = SecurityProviderUtility.CreateProvider(username); securityProvider.Password = password; if (securityProvider.Authenticate()) { SecurityIdentity securityIdentity = new SecurityIdentity(securityProvider); Thread.CurrentPrincipal = new SecurityPrincipal(securityIdentity); } return securityProvider.UserData; } /// /// Resets user password. /// /// Answer to user's security question. /// true if password is reset, otherwise false. public bool ResetPassword(string securityAnswer) { ValidateCurrentProvider(); if (!CurrentProvider.CanResetPassword) return false; return CurrentProvider.ResetPassword(securityAnswer); } /// /// Changes user password. /// /// User's current password. /// User's new password. /// true if the password is changed, otherwise false. public bool ChangePassword(string oldPassword, string newPassword) { ValidateCurrentProvider(); if (!CurrentProvider.CanChangePassword) return false; return CurrentProvider.ChangePassword(oldPassword, newPassword); } } }