View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.impl.auth;
28  
29  import java.io.IOException;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.apache.http.Header;
34  import org.apache.http.HttpRequest;
35  import org.apache.http.auth.AuthenticationException;
36  import org.apache.http.auth.Credentials;
37  import org.apache.http.protocol.HttpContext;
38  import org.apache.http.util.Args;
39  import org.ietf.jgss.GSSException;
40  import org.ietf.jgss.Oid;
41  
42  /**
43   * SPNEGO (Simple and Protected GSSAPI Negotiation Mechanism) authentication
44   * scheme.
45   *
46   * @since 4.1
47   *
48   * @deprecated (4.2)  use {@link SPNegoScheme} or {@link KerberosScheme}.
49   */
50  @Deprecated
51  public class NegotiateScheme extends GGSSchemeBase {
52  
53      private final Log log = LogFactory.getLog(getClass());
54  
55      private static final String SPNEGO_OID       = "1.3.6.1.5.5.2";
56      private static final String KERBEROS_OID     = "1.2.840.113554.1.2.2";
57  
58      private final SpnegoTokenGenerator spengoGenerator;
59  
60      /**
61       * Default constructor for the Negotiate authentication scheme.
62       *
63       */
64      public NegotiateScheme(final SpnegoTokenGenerator spengoGenerator, final boolean stripPort) {
65          super(stripPort);
66          this.spengoGenerator = spengoGenerator;
67      }
68  
69      public NegotiateScheme(final SpnegoTokenGenerator spengoGenerator) {
70          this(spengoGenerator, false);
71      }
72  
73      public NegotiateScheme() {
74          this(null, false);
75      }
76  
77      /**
78       * Returns textual designation of the Negotiate authentication scheme.
79       *
80       * @return {@code Negotiate}
81       */
82      public String getSchemeName() {
83          return "Negotiate";
84      }
85  
86      @Override
87      public Header authenticate(
88              final Credentials credentials,
89              final HttpRequest request) throws AuthenticationException {
90          return authenticate(credentials, request, null);
91      }
92  
93      /**
94       * Produces Negotiate authorization Header based on token created by
95       * processChallenge.
96       *
97       * @param credentials Never used be the Negotiate scheme but must be provided to
98       * satisfy common-httpclient API. Credentials from JAAS will be used instead.
99       * @param request The request being authenticated
100      *
101      * @throws AuthenticationException if authorisation string cannot
102      *   be generated due to an authentication failure
103      *
104      * @return an Negotiate authorisation Header
105      */
106     @Override
107     public Header authenticate(
108             final Credentials credentials,
109             final HttpRequest request,
110             final HttpContext context) throws AuthenticationException {
111         return super.authenticate(credentials, request, context);
112     }
113 
114     @Override
115     protected byte[] generateToken(final byte[] input, final String authServer) throws GSSException {
116         return super.generateToken(input, authServer);
117     }
118 
119     @Override
120     protected byte[] generateToken(final byte[] input, final String authServer, final Credentials credentials) throws GSSException {
121         /* Using the SPNEGO OID is the correct method.
122          * Kerberos v5 works for IIS but not JBoss. Unwrapping
123          * the initial token when using SPNEGO OID looks like what is
124          * described here...
125          *
126          * http://msdn.microsoft.com/en-us/library/ms995330.aspx
127          *
128          * Another helpful URL...
129          *
130          * http://publib.boulder.ibm.com/infocenter/wasinfo/v7r0/index.jsp?topic=/com.ibm.websphere.express.doc/info/exp/ae/tsec_SPNEGO_token.html
131          *
132          * Unfortunately SPNEGO is JRE >=1.6.
133          */
134 
135         /** Try SPNEGO by default, fall back to Kerberos later if error */
136         Oid negotiationOid  = new Oid(SPNEGO_OID);
137 
138         byte[] token = input;
139         boolean tryKerberos = false;
140         try {
141             token = generateGSSToken(token, negotiationOid, authServer, credentials);
142         } catch (final GSSException ex){
143             // BAD MECH means we are likely to be using 1.5, fall back to Kerberos MECH.
144             // Rethrow any other exception.
145             if (ex.getMajor() == GSSException.BAD_MECH ){
146                 log.debug("GSSException BAD_MECH, retry with Kerberos MECH");
147                 tryKerberos = true;
148             } else {
149                 throw ex;
150             }
151 
152         }
153         if (tryKerberos){
154             /* Kerberos v5 GSS-API mechanism defined in RFC 1964.*/
155             log.debug("Using Kerberos MECH " + KERBEROS_OID);
156             negotiationOid  = new Oid(KERBEROS_OID);
157             token = generateGSSToken(token, negotiationOid, authServer, credentials);
158 
159             /*
160              * IIS accepts Kerberos and SPNEGO tokens. Some other servers Jboss, Glassfish?
161              * seem to only accept SPNEGO. Below wraps Kerberos into SPNEGO token.
162              */
163             if (token != null && spengoGenerator != null) {
164                 try {
165                     token = spengoGenerator.generateSpnegoDERObject(token);
166                 } catch (final IOException ex) {
167                     log.error(ex.getMessage(), ex);
168                 }
169             }
170         }
171         return token;
172     }
173 
174     /**
175      * Returns the authentication parameter with the given name, if available.
176      *
177      * <p>There are no valid parameters for Negotiate authentication so this
178      * method always returns {@code null}.</p>
179      *
180      * @param name The name of the parameter to be returned
181      *
182      * @return the parameter with the given name
183      */
184     public String getParameter(final String name) {
185         Args.notNull(name, "Parameter name");
186         return null;
187     }
188 
189     /**
190      * The concept of an authentication realm is not supported by the Negotiate
191      * authentication scheme. Always returns {@code null}.
192      *
193      * @return {@code null}
194      */
195     public String getRealm() {
196         return null;
197     }
198 
199     /**
200      * Returns {@code true}.
201      * Negotiate authentication scheme is connection based.
202      *
203      * @return {@code true}.
204      */
205     public boolean isConnectionBased() {
206         return true;
207     }
208 
209 }