-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add auth and proxy support for Search RR (#55)
Adds HTTP Auth and HTTP proxy support for Search API SMO and RR backends. This is just "basic" support, that can be improved with "real" config coming from session. For now keep it simple. Fixes #51
- Loading branch information
Showing
4 changed files
with
109 additions
and
12 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
...d/src/main/java/eu/maveniverse/maven/toolbox/shared/internal/Java11HttpClientFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package eu.maveniverse.maven.toolbox.shared.internal; | ||
|
||
import java.net.Authenticator; | ||
import java.net.InetSocketAddress; | ||
import java.net.PasswordAuthentication; | ||
import java.net.ProxySelector; | ||
import java.net.http.HttpClient; | ||
import java.time.Duration; | ||
import java.util.HashMap; | ||
import org.eclipse.aether.RepositorySystemSession; | ||
import org.eclipse.aether.repository.AuthenticationContext; | ||
import org.eclipse.aether.repository.RemoteRepository; | ||
|
||
/** | ||
* Java 11 {@link HttpClient} factory that creates pre-configured HTTP client based on Resolver {@link RemoteRepository}. | ||
*/ | ||
public final class Java11HttpClientFactory { | ||
public static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(10L); | ||
|
||
public static HttpClient buildHttpClient(RepositorySystemSession session, RemoteRepository repository) { | ||
return buildHttpClient(DEFAULT_TIMEOUT, session, repository); | ||
} | ||
|
||
private static HttpClient buildHttpClient( | ||
Duration timeout, RepositorySystemSession session, RemoteRepository repository) { | ||
|
||
HttpClient.Builder builder = | ||
HttpClient.newBuilder().connectTimeout(timeout).followRedirects(HttpClient.Redirect.NEVER); | ||
|
||
HashMap<Authenticator.RequestorType, PasswordAuthentication> authentications = new HashMap<>(); | ||
try (AuthenticationContext repoAuthContext = AuthenticationContext.forRepository(session, repository)) { | ||
if (repoAuthContext != null) { | ||
authentications.put( | ||
Authenticator.RequestorType.SERVER, | ||
new PasswordAuthentication( | ||
repoAuthContext.get(AuthenticationContext.USERNAME), | ||
repoAuthContext | ||
.get(AuthenticationContext.USERNAME) | ||
.toCharArray())); | ||
} | ||
} | ||
if (repository.getProxy() != null) { | ||
builder.proxy(ProxySelector.of(new InetSocketAddress( | ||
repository.getProxy().getHost(), repository.getProxy().getPort()))); | ||
try (AuthenticationContext proxyAuthContext = AuthenticationContext.forProxy(session, repository)) { | ||
if (proxyAuthContext != null) { | ||
authentications.put( | ||
Authenticator.RequestorType.PROXY, | ||
new PasswordAuthentication( | ||
proxyAuthContext.get(AuthenticationContext.USERNAME), | ||
proxyAuthContext | ||
.get(AuthenticationContext.PASSWORD) | ||
.toCharArray())); | ||
} | ||
} | ||
} | ||
if (!authentications.isEmpty()) { | ||
builder.authenticator(new Authenticator() { | ||
@Override | ||
protected PasswordAuthentication getPasswordAuthentication() { | ||
return authentications.get(getRequestorType()); | ||
} | ||
}); | ||
} | ||
|
||
return builder.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters