View Javadoc

1   /*
2    * $Id: NtlmMessageFactory.java 22241 2011-06-22 14:47:53Z pablo.kraan $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.http.ntlm;
12  
13  import java.io.IOException;
14  
15  import jcifs.ntlmssp.Type1Message;
16  import jcifs.ntlmssp.Type2Message;
17  import jcifs.ntlmssp.Type3Message;
18  import jcifs.util.Base64;
19  import org.apache.commons.httpclient.NTCredentials;
20  
21  public class NtlmMessageFactory
22  {
23  
24      // Defines the default flags value set in the Type3Message. These flags must be set:
25      // NEGOTIATE_VERSION
26      // NEGOTIATE_TARGET_INFO
27      // NEGOTIATE_EXTENDED_SECURITY
28      // TARGET_TYPE_SERVER
29      // NEGOTIATE_ALWAYS_SIGN
30      // NEGOTIATE_NTLM_KEY
31      // REQUEST_TARGET
32      // NEGOTIATE_UNICODE
33      public static final int DEFAULT_TYPE_3_MESSAGE_FLAGS = 0X88205;
34  
35      // Defines flags value to use in the Type1Message. These flags must be set:
36      // NEGOTIATE_EXTENDED_SECURITY
37      // NEGOTIATE_ALWAYS_SIGN
38      // NEGOTIATE_NTLM_KEY
39      // REQUEST_TARGET
40      // NEGOTIATE_OEM
41      // NEGOTIATE_UNICODE
42      public static final int DEFAULT_TYPE_1_MESSAGE_FLAGS = 0X88207;
43  
44      /**
45       * Creates a {@link Type1Message} for NTLM authentication.
46       *
47       * @param host the client host
48       * @param domain the client domain
49       * @return a {@link Type1Message} to initiate the authentication process.
50       */
51      public Type1Message createType1Message(String host, String domain)
52      {
53          Type1Message message = new Type1Message(DEFAULT_TYPE_1_MESSAGE_FLAGS, domain, host);
54  
55          // Type1Message constructor sets a default workstation name when host == null, so it
56          // requires an override of that value in order to make it work
57          if (host == null)
58          {
59              message.setSuppliedWorkstation(null);
60          }
61  
62          return message;
63      }
64  
65      /**
66       * Creates a {@link Type2Message} for NTLM authentication from a challenge
67       * received from the NTLM server.
68       *
69       * @param challenge the challenge received from the server in response to a
70       *        {@link Type1Message} message previously sent.
71       * @return a {@link Type2Message} to continue the authentication process.
72       */
73      public Type2Message createType2Message(String challenge)
74      {
75          try
76          {
77              return new Type2Message(Base64.decode(challenge));
78          }
79          catch (IOException e)
80          {
81              throw new RuntimeException("Invalid Type2 message", e);
82          }
83      }
84  
85      /**
86       * Creates a {@link Type3Message} for NTLM authentication.
87       *
88       * @param ntCredentials the credentials used for the authentication
89       * @param type2Message the {@link Type2Message} received from the server
90       *        in response to a {@link Type1Message} message previously sent.
91       * @return a {@link Type3Message} to continue the authentication process.
92       */
93      public Type3Message createType3Message(NTCredentials ntCredentials, Type2Message type2Message)
94      {
95          return new Type3Message(type2Message, ntCredentials.getPassword(), type2Message.getTarget(),
96                                  ntCredentials.getUserName(), ntCredentials.getHost(), DEFAULT_TYPE_3_MESSAGE_FLAGS);
97      }
98  }