summaryrefslogtreecommitdiff
path: root/dotnet/Qpid.Client/Client/Security/CallbackHandlerRegistry.cs
blob: 9ac0381850247535fc535108e02e454c2e8349ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

using System;
using System.Collections;
using System.Configuration;
using System.Text;
using Apache.Qpid.Sasl;
using Apache.Qpid.Sasl.Mechanisms;

using Apache.Qpid.Client.Configuration;

namespace Apache.Qpid.Client.Security
{

   /// <summary>
   /// Helper class to map SASL mechanisms to our 
   /// internal ISaslCallbackHandler implementations.
   /// </summary>
   /// <remarks>
   /// The set of configured callback handlers and their order
   /// controls the selection of the SASL mechanism used for authentication.
   /// <para>
   /// You can either replace the default handler for CRAM-MD5 and PLAIN
   /// authentication (the two default options) using the application 
   /// configuration file. Configuration is done by especifying the SASL
   /// mechanism name (e.g PLAIN) and the type implementing the callback handler
   /// used to provide any data required by the mechanism like username and password.
   /// </para>
   /// <para>
   /// Callback handler types should implement the IAMQCallbackHandler interface.
   /// </para>
   /// <para>
   /// New callbacks or authentication mechanisms can be configured like this:
   /// </para>
   /// <example><![CDATA[
   /// <configuration>
   ///   <configSections>
   ///      <sectionGroup name="qpid.client">
   ///         <section name="authentication" type="Apache.Qpid.Client.Configuration.AuthenticationConfigurationSectionHandler, Apache.Qpid.Client"/>
   ///      </sectionGroup>
   ///   </configSections>
   ///   <qpid.client>
   ///      <authentication>
   ///         <add key="TEST" value="Apache.Qpid.Client.Tests.Security.TestCallbackHandler, Apache.Qpid.Client.Tests"/>
   ///      </authentication>
   ///   </qpid.client>
   /// </configuration>
   /// ]]></example>
   /// </remarks>
   public sealed class CallbackHandlerRegistry
   {
      private static CallbackHandlerRegistry _instance = 
         new CallbackHandlerRegistry();
      private OrderedHashTable _mechanism2HandlerMap;
      private string[] _mechanisms;

      public static CallbackHandlerRegistry Instance
      {
         get { return _instance; }
      }

      public string[] Mechanisms
      {
         get { return _mechanisms; }
      }

      private CallbackHandlerRegistry()
      {
         _mechanism2HandlerMap = (OrderedHashTable)
            ConfigurationSettings.GetConfig("qpid.client/authentication");

         // configure default options if not available
         if ( _mechanism2HandlerMap == null )
            _mechanism2HandlerMap = new OrderedHashTable();

         if ( !_mechanism2HandlerMap.Contains(ExternalSaslClient.Mechanism) )
            _mechanism2HandlerMap.Add(ExternalSaslClient.Mechanism, typeof(UsernamePasswordCallbackHandler));
         if ( !_mechanism2HandlerMap.Contains(CramMD5SaslClient.Mechanism) )
            _mechanism2HandlerMap.Add(CramMD5SaslClient.Mechanism, typeof(UsernamePasswordCallbackHandler));
         if ( !_mechanism2HandlerMap.Contains(CramMD5HexSaslClient.Mechanism) )
            _mechanism2HandlerMap.Add(CramMD5HexSaslClient.Mechanism, typeof(UsernamePasswordCallbackHandler));
         if ( !_mechanism2HandlerMap.Contains(PlainSaslClient.Mechanism) )
            _mechanism2HandlerMap.Add(PlainSaslClient.Mechanism, typeof(UsernamePasswordCallbackHandler));

         _mechanisms = new string[_mechanism2HandlerMap.Count];
         _mechanism2HandlerMap.OrderedKeys.CopyTo(_mechanisms, 0);
      }

      public bool IsSupportedMechanism(string mechanism)
      {
         return _mechanism2HandlerMap.Contains(mechanism);
      }

      public string ChooseMechanism(string mechanisms)
      {
         IList mechs = mechanisms.Split(' ');
         foreach ( string supportedMech in _mechanisms )
         {
            if ( mechs.Contains(supportedMech) )
               return supportedMech;
         }
         return null;
      }

      public Type GetCallbackHandler(string mechanism)
      {
         return (Type)_mechanism2HandlerMap[mechanism];
      }
   }
}