View Javadoc
1   package org.metricshub.winrm.service.client.auth.ntlm;
2   
3   /*-
4    * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5    * WinRM Java Client
6    * ჻჻჻჻჻჻
7    * Copyright 2023 - 2024 Metricshub
8    * ჻჻჻჻჻჻
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
21   */
22  
23  import java.util.function.LongUnaryOperator;
24  import org.apache.http.Header;
25  import org.apache.http.HttpRequest;
26  import org.apache.http.auth.AuthenticationException;
27  import org.apache.http.auth.Credentials;
28  import org.apache.http.client.config.AuthSchemes;
29  import org.apache.http.message.BasicHeader;
30  
31  /**
32   * Code from io.cloudsoft.winrm4j.client.ntlm.NtlmMasqAsSpnegoScheme
33   * release 0.12.3 @link https://github.com/cloudsoft/winrm4j
34   */
35  public class NtlmMasqAsSpnegoScheme extends NTLMScheme {
36  
37  	private static final LongUnaryOperator FLAG_MODIFIER = flags ->
38  		flags |
39  		NTLMEngineUtils.NTLMSSP_NEGOTIATE_SIGN |
40  		NTLMEngineUtils.NTLMSSP_NEGOTIATE_SEAL |
41  		NTLMEngineUtils.NTLMSSP_NEGOTIATE_KEY_EXCH;
42  
43  	public NtlmMasqAsSpnegoScheme() {
44  		super(newDefaultNtlmEngine());
45  	}
46  
47  	private static NTLMEngine newDefaultNtlmEngine() {
48  		return new NTLMEngineImpl() {
49  			@Override
50  			public Integer getDefaultFlags() {
51  				final Long flags = (long) Type1Message.getDefaultFlags();
52  				return (int) FLAG_MODIFIER.applyAsLong(flags);
53  			}
54  		};
55  	}
56  
57  	@Override
58  	public String getSchemeName() {
59  		return AuthSchemes.SPNEGO;
60  	}
61  
62  	@Override
63  	public Header authenticate(final Credentials credentials, final HttpRequest httpRequest)
64  		throws AuthenticationException {
65  		final Header header = super.authenticate(credentials, httpRequest);
66  
67  		// code from winrm4j implementation: https://github.com/cloudsoft/winrm4j
68  		return new BasicHeader(header.getName(), header.getValue().replace("NTLM", getSchemeName()));
69  	}
70  }