src/java.security.sasl/share/classes/com/sun/security/sasl/digest/FactoryImpl.java
author hb
Tue, 02 Jan 2018 15:03:52 +0530
branchjmx-rest-api
changeset 56005 90cff2ac77b8
parent 47216 71c04702a3d5
permissions -rw-r--r--
REST base url gets complete complete domain name
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.security.sasl.digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.security.sasl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.security.auth.callback.CallbackHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import com.sun.security.sasl.util.PolicyUtils;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
  * Client and server factory for DIGEST-MD5 SASL client/server mechanisms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
  * See DigestMD5Client and DigestMD5Server for input requirements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
  * @author Jonathan Bruce
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
  * @author Rosanna Lee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public final class FactoryImpl implements SaslClientFactory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
SaslServerFactory{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 25859
diff changeset
    47
    private static final String[] myMechs = { "DIGEST-MD5" };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static final int DIGEST_MD5 = 0;
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 25859
diff changeset
    49
    private static final int[] mechPolicies = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        PolicyUtils.NOPLAINTEXT|PolicyUtils.NOANONYMOUS};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
      * Empty constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public FactoryImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * Returns a new instance of the DIGEST-MD5 SASL client mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @throws SaslException If there is an error creating the DigestMD5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * SASL client.
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 31538
diff changeset
    63
     * @return a new SaslClient; otherwise null if unsuccessful.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public SaslClient createSaslClient(String[] mechs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
         String authorizationId, String protocol, String serverName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
         Map<String,?> props, CallbackHandler cbh)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
         throws SaslException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
         for (int i=0; i<mechs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            if (mechs[i].equals(myMechs[DIGEST_MD5]) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                PolicyUtils.checkPolicy(mechPolicies[DIGEST_MD5], props)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                if (cbh == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    throw new SaslException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                        "Callback handler with support for RealmChoiceCallback, " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                        "RealmCallback, NameCallback, and PasswordCallback " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                        "required");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                return new DigestMD5Client(authorizationId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    protocol, serverName, props, cbh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Returns a new instance of the DIGEST-MD5 SASL server mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @throws SaslException If there is an error creating the DigestMD5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * SASL server.
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 31538
diff changeset
    93
     * @return a new SaslServer; otherwise null if unsuccessful.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public SaslServer createSaslServer(String mech,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
         String protocol, String serverName, Map<String,?> props, CallbackHandler cbh)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
         throws SaslException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
         if (mech.equals(myMechs[DIGEST_MD5]) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
             PolicyUtils.checkPolicy(mechPolicies[DIGEST_MD5], props)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                if (cbh == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    throw new SaslException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                        "Callback handler with support for AuthorizeCallback, "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                        "RealmCallback, NameCallback, and PasswordCallback " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                        "required");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                return new DigestMD5Server(protocol, serverName, props, cbh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
         return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
      * Returns the authentication mechanisms that this factory can produce.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
      *
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 31538
diff changeset
   117
      * @return String[] {"DigestMD5"} if policies in env match those of this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
      * factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public String[] getMechanismNames(Map<String,?> env) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return PolicyUtils.filterMechs(myMechs, mechPolicies, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
}