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