//****************************************************************************************************** // AntiForgeryWorker.cs - Gbtc // // Copyright © 2018, 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://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: // ---------------------------------------------------------------------------------------------------- // 02/15/2018 - J. Ritchie Carroll // Generated original version of source code. // //****************************************************************************************************** #region [ Contributor License Agreements ] // Derived from AspNetWebStack (https://github.com/aspnet/AspNetWebStack) // Copyright (c) .NET Foundation. All rights reserved. // See NOTICE.txt file in Source folder for more information. #endregion using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Net.Http; using System.Security.Principal; using System.Web.Mvc; using GSF.Security; namespace GSF.Web.Security.AntiCsrf { internal sealed class AntiForgeryWorker { private readonly ReadOnlyAntiForgeryConfig m_config = new ReadOnlyAntiForgeryConfig(); private readonly AntiForgeryTokenStore m_tokenStore = new AntiForgeryTokenStore(); private readonly TokenValidator m_validator = new TokenValidator(); private void CheckSSLConfig(HttpRequestMessage request) { if (m_config.RequireSSL && !request.RequestUri.Scheme.Equals("https")) throw new InvalidOperationException("The anti-forgery system has the configuration value AntiForgeryConfig.RequireSsl = true, but the current request is not an SSL request."); } private AntiForgeryToken DeserializeToken(string serializedToken) { return !string.IsNullOrEmpty(serializedToken) ? AntiForgeryTokenSerializer.Deserialize(serializedToken) : null; } [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Caller will just regenerate token in case of failure.")] private AntiForgeryToken DeserializeTokenNoThrow(string serializedToken) { try { return DeserializeToken(serializedToken); } catch { // ignore failures since we'll just generate a new token return null; } } private static IIdentity ExtractIdentity(HttpRequestMessage request) { SecurityPrincipal securityPrincipal = request.GetRequestContext().Principal as SecurityPrincipal; return securityPrincipal?.Identity; } [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Caller will just regenerate token in case of failure.")] private AntiForgeryToken GetCookieTokenNoThrow(HttpRequestMessage request) { try { return m_tokenStore.GetCookieToken(request); } catch { // ignore failures since we'll just generate a new token return null; } } // [ ENTRY POINT ] // Generates an anti-XSRF token pair for the current user. The return // value is the hidden input form element that should be rendered in // the