//******************************************************************************************************
// ServiceBusSecurityPolicy.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:
// ----------------------------------------------------------------------------------------------------
// 10/18/2010 - Pinal C. Patel
// Generated original version of source code.
// 12/20/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
using System.IdentityModel.Policy;
using System.ServiceModel;
using System.Text.RegularExpressions;
using GSF.ServiceModel;
namespace GSF.ServiceBus
{
///
/// Represents an authorization policy that can be used for enabling role-based security on .
///
///
/// This example shows the required config file entries when securing :
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
/// ]]>
///
///
///
public class ServiceBusSecurityPolicy : SecurityPolicy
{
#region [ Members ]
// Constants
private const string NameRegex = @"<(?'Name'b:MessageName)\b[^>]*>(?'Value'.*?)\1>";
#endregion
#region [ Methods ]
///
/// Gets the name of resource being accessed.
///
///
/// . property value for operations,
/// otherwise property value for all other operations.
///
protected override string GetResourceName()
{
System.ServiceModel.Channels.Message message = OperationContext.Current.RequestContext.RequestMessage;
if (message.Headers.Action.EndsWith("Register"))
{
// For register operation use the topic and queue name for applying security.
MatchCollection matches = Regex.Matches(message.ToString(), NameRegex);
if (matches.Count > 0)
return matches[0].Groups["Value"].Value;
}
// For all other operations simply use the operation name for applying security.
return OperationContext.Current.RequestContext.RequestMessage.Headers.Action;
}
#endregion
}
}