CRAMMD5HashedSaslClientFactory.java
01 /*
02  *  Licensed to the Apache Software Foundation (ASF) under one
03  *  or more contributor license agreements.  See the NOTICE file
04  *  distributed with this work for additional information
05  *  regarding copyright ownership.  The ASF licenses this file
06  *  to you under the Apache License, Version 2.0 (the
07  *  "License"); you may not use this file except in compliance
08  *  with the License.  You may obtain a copy of the License at
09  *
10  *    http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing,
13  *  software distributed under the License is distributed on an
14  *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  *  KIND, either express or implied.  See the License for the
16  *  specific language governing permissions and limitations
17  *  under the License.    
18  *
19  
20  */
21 package org.apache.qpid.client.security.crammd5hashed;
22 
23 import org.apache.qpid.client.security.amqplain.AmqPlainSaslClient;
24 
25 import javax.security.sasl.SaslClientFactory;
26 import javax.security.sasl.SaslClient;
27 import javax.security.sasl.SaslException;
28 import javax.security.sasl.Sasl;
29 import javax.security.auth.callback.CallbackHandler;
30 import java.util.Map;
31 import java.security.Security;
32 
33 public class CRAMMD5HashedSaslClientFactory implements SaslClientFactory
34 {
35     /** The name of this mechanism */
36     public static final String MECHANISM = "CRAM-MD5-HASHED";
37 
38 
39     public SaslClient createSaslClient(String[] mechanisms, String authorizationId, String protocol, String serverName, Map<String, ?> props, CallbackHandler cbhthrows SaslException
40     {
41         for (int i = 0; i < mechanisms.length; i++)
42         {
43             if (mechanisms[i].equals(MECHANISM))
44             {
45                 if (cbh == null)
46                 {
47                     throw new SaslException("CallbackHandler must not be null");
48                 }
49 
50                 String[] mechs = {"CRAM-MD5"};
51                 return Sasl.createSaslClient(mechs, authorizationId, protocol, serverName, props, cbh);
52             }
53         }
54         return null;
55     }
56 
57     public String[] getMechanismNames(Map props)
58     {
59         if (props != null)
60         {
61             if (props.containsKey(Sasl.POLICY_NOPLAINTEXT||
62                 props.containsKey(Sasl.POLICY_NODICTIONARY||
63                 props.containsKey(Sasl.POLICY_NOACTIVE))
64             {
65                 // returned array must be non null according to interface documentation
66                 return new String[0];
67             }
68         }
69 
70         return new String[]{MECHANISM};
71     }
72 }