001: /*
002: * Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.security.sasl.digest;
027:
028: import java.util.Map;
029:
030: import javax.security.sasl.*;
031: import javax.security.auth.callback.CallbackHandler;
032:
033: import com.sun.security.sasl.util.PolicyUtils;
034:
035: /**
036: * Client and server factory for DIGEST-MD5 SASL client/server mechanisms.
037: * See DigestMD5Client and DigestMD5Server for input requirements.
038: *
039: * @author Jonathan Bruce
040: * @author Rosanna Lee
041: */
042:
043: public final class FactoryImpl implements SaslClientFactory,
044: SaslServerFactory {
045:
046: private static final String myMechs[] = { "DIGEST-MD5" };
047: private static final int DIGEST_MD5 = 0;
048: private static final int mechPolicies[] = { PolicyUtils.NOPLAINTEXT
049: | PolicyUtils.NOANONYMOUS };
050:
051: /**
052: * Empty constructor.
053: */
054: public FactoryImpl() {
055: }
056:
057: /**
058: * Returns a new instance of the DIGEST-MD5 SASL client mechanism.
059: *
060: * @throws SaslException If there is an error creating the DigestMD5
061: * SASL client.
062: * @returns a new SaslClient ; otherwise null if unsuccessful.
063: */
064: public SaslClient createSaslClient(String[] mechs,
065: String authorizationId, String protocol, String serverName,
066: Map<String, ?> props, CallbackHandler cbh)
067: throws SaslException {
068:
069: for (int i = 0; i < mechs.length; i++) {
070: if (mechs[i].equals(myMechs[DIGEST_MD5])
071: && PolicyUtils.checkPolicy(
072: mechPolicies[DIGEST_MD5], props)) {
073:
074: if (cbh == null) {
075: throw new SaslException(
076: "Callback handler with support for RealmChoiceCallback, "
077: + "RealmCallback, NameCallback, and PasswordCallback "
078: + "required");
079: }
080:
081: return new DigestMD5Client(authorizationId, protocol,
082: serverName, props, cbh);
083: }
084: }
085: return null;
086: }
087:
088: /**
089: * Returns a new instance of the DIGEST-MD5 SASL server mechanism.
090: *
091: * @throws SaslException If there is an error creating the DigestMD5
092: * SASL server.
093: * @returns a new SaslServer ; otherwise null if unsuccessful.
094: */
095: public SaslServer createSaslServer(String mech, String protocol,
096: String serverName, Map<String, ?> props, CallbackHandler cbh)
097: throws SaslException {
098:
099: if (mech.equals(myMechs[DIGEST_MD5])
100: && PolicyUtils.checkPolicy(mechPolicies[DIGEST_MD5],
101: props)) {
102:
103: if (cbh == null) {
104: throw new SaslException(
105: "Callback handler with support for AuthorizeCallback, "
106: + "RealmCallback, NameCallback, and PasswordCallback "
107: + "required");
108: }
109:
110: return new DigestMD5Server(protocol, serverName, props, cbh);
111: }
112: return null;
113: }
114:
115: /**
116: * Returns the authentication mechanisms that this factory can produce.
117: *
118: * @returns String[] {"DigestMD5"} if policies in env match those of this
119: * factory.
120: */
121: public String[] getMechanismNames(Map<String, ?> env) {
122: return PolicyUtils.filterMechs(myMechs, mechPolicies, env);
123: }
124: }
|