//****************************************************************************************************** // 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.ServiceModel.Activation; 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)] public class SecurityService : SelfHostingService, ISecurityService { /// /// Returns information about the current user. /// /// An object of the user if user's security context has been initialized, otherwise null. public UserData GetUserData() { SecurityProviderCache.ValidateCurrentProvider(); return SecurityProviderCache.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() { SecurityProviderCache.ValidateCurrentProvider(); if (SecurityProviderCache.CurrentProvider.CanRefreshData) SecurityProviderCache.CurrentProvider.RefreshData(); return SecurityProviderCache.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 provider = SecurityProviderUtility.CreateProvider(username); if (provider.Authenticate(password)) SecurityProviderCache.CurrentProvider = provider; return provider.UserData; } /// /// Resets user password. /// /// Answer to user's security question. /// true if password is reset, otherwise false. public bool ResetPassword(string securityAnswer) { SecurityProviderCache.ValidateCurrentProvider(); if (!SecurityProviderCache.CurrentProvider.CanResetPassword) return false; return SecurityProviderCache.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) { SecurityProviderCache.ValidateCurrentProvider(); if (!SecurityProviderCache.CurrentProvider.CanChangePassword) return false; return SecurityProviderCache.CurrentProvider.ChangePassword(oldPassword, newPassword); } } }