001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 *
020 */
021 package org.apache.qpid.server.security.auth.sasl.crammd5;
022
023 import javax.security.sasl.SaslServer;
024 import javax.security.sasl.SaslException;
025 import javax.security.sasl.Sasl;
026 import javax.security.sasl.SaslServerFactory;
027 import javax.security.auth.callback.CallbackHandler;
028 import java.util.Enumeration;
029 import java.util.Map;
030
031 public class CRAMMD5HashedSaslServer implements SaslServer
032 {
033 public static final String MECHANISM = "CRAM-MD5-HASHED";
034
035 private SaslServer _realServer;
036
037 public CRAMMD5HashedSaslServer(String mechanism, String protocol, String serverName, Map<String, ?> props,
038 CallbackHandler cbh) throws SaslException
039 {
040 Enumeration factories = Sasl.getSaslServerFactories();
041
042 while (factories.hasMoreElements())
043 {
044 SaslServerFactory factory = (SaslServerFactory) factories.nextElement();
045
046 if (factory instanceof CRAMMD5HashedServerFactory)
047 {
048 continue;
049 }
050
051 String[] mechs = factory.getMechanismNames(props);
052
053 for (String mech : mechs)
054 {
055 if (mech.equals("CRAM-MD5"))
056 {
057 _realServer = factory.createSaslServer("CRAM-MD5", protocol, serverName, props, cbh);
058 return;
059 }
060 }
061 }
062
063 throw new RuntimeException("No default SaslServer found for mechanism:" + "CRAM-MD5");
064 }
065
066 public String getMechanismName()
067 {
068 return MECHANISM;
069 }
070
071 public byte[] evaluateResponse(byte[] response) throws SaslException
072 {
073 return _realServer.evaluateResponse(response);
074 }
075
076 public boolean isComplete()
077 {
078 return _realServer.isComplete();
079 }
080
081 public String getAuthorizationID()
082 {
083 return _realServer.getAuthorizationID();
084 }
085
086 public byte[] unwrap(byte[] incoming, int offset, int len) throws SaslException
087 {
088 return _realServer.unwrap(incoming, offset, len);
089 }
090
091 public byte[] wrap(byte[] outgoing, int offset, int len) throws SaslException
092 {
093 return _realServer.wrap(outgoing, offset, len);
094 }
095
096 public Object getNegotiatedProperty(String propName)
097 {
098 return _realServer.getNegotiatedProperty(propName);
099 }
100
101 public void dispose() throws SaslException
102 {
103 _realServer.dispose();
104 }
105 }
|