Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions apache-maven/src/conf/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ under the License.
| values (values used when the setting is not specified) are provided.
|
|-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
<settings xmlns="http://maven.apache.org/SETTINGS/1.4.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.4.0 https://maven.apache.org/xsd/settings-1.4.0.xsd">
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
Expand Down Expand Up @@ -131,6 +131,27 @@ under the License.
<passphrase>optional; leave empty if not used.</passphrase>
</server>
-->

<!-- Another sample, using tokens to authenticate (HTTP transports only, not Wagon).
| The token is sent as "Authorization: Bearer <token>", and username and password are ignored.
| Each tokenHeader is sent as is, for example Private-Token, Job-Token or Deploy-Token for GitLab,
| or CF-Access-Client-Id and CF-Access-Client-Secret for Cloudflare Access.
|
<server>
<id>bearerTokenServer</id>
<token>token</token>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why making it specific, we already have

                  <property>
                        <name>Authorization</name>
                        <value>Bearer xxxx</value>
                    </property>

in httpHeaders which avoids to have two inconsistent cases using token (bearer and a do what you want as schema case)

so literally we are just aliasing what is already there in terms of configuration so I would remove that

</server>

<server>
<id>tokenHeaderServer</id>
<tokenHeaders>
<tokenHeader>
<name>Private-Token</name>
<value>token</value>
</tokenHeader>
</tokenHeaders>
</server>
-->
</servers>

<!-- mirrors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/
package org.apache.maven.artifact.repository;

import java.util.LinkedHashMap;
import java.util.Map;

/**
* Authentication
*/
Expand All @@ -27,6 +30,8 @@ public class Authentication {

private String passphrase;

private Map<String, String> tokenHeaders = new LinkedHashMap<>();

public Authentication(String userName, String password) {
this.username = userName;
this.password = password;
Expand Down Expand Up @@ -114,4 +119,26 @@ public String getPrivateKey() {
public void setPrivateKey(final String privateKey) {
this.privateKey = privateKey;
}

/**
* Get the HTTP token headers used to access the repository, sent by HTTP transports (except Wagon): header name
* to full header value, for example {@code Authorization} to {@code Bearer <token>}, or {@code Private-Token} to
* the token.
*
* @return the token headers, never {@code null}
* @since 3.10.3
*/
public Map<String, String> getTokenHeaders() {
return tokenHeaders;
}

/**
* Set the HTTP token headers used to access the repository.
*
* @param tokenHeaders header name to full header value, may be {@code null}
* @since 3.10.3
*/
public void setTokenHeaders(final Map<String, String> tokenHeaders) {
this.tokenHeaders = tokenHeaders != null ? tokenHeaders : new LinkedHashMap<>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.apache.maven.repository.legacy.repository.ArtifactRepositoryFactory;
import org.apache.maven.settings.Mirror;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.TokenHeader;
import org.apache.maven.settings.building.SettingsProblem;
import org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest;
import org.apache.maven.settings.crypto.SettingsDecrypter;
Expand Down Expand Up @@ -514,6 +515,14 @@ public void injectAuthentication(List<ArtifactRepository> repositories, List<Ser
Authentication authentication = new Authentication(server.getUsername(), server.getPassword());
authentication.setPrivateKey(server.getPrivateKey());
authentication.setPassphrase(server.getPassphrase());
if (server.getToken() != null) {
authentication.getTokenHeaders().put("Authorization", "Bearer " + server.getToken());
}
for (TokenHeader tokenHeader : server.getTokenHeaders()) {
if (tokenHeader.getValue() != null) {
authentication.getTokenHeaders().put(tokenHeader.getName(), tokenHeader.getValue());
}
}

repository.setAuthentication(authentication);
} else {
Expand All @@ -539,6 +548,17 @@ private Authentication getAuthentication(RepositorySystemSession session, Artifa
authCtx.get(AuthenticationContext.PASSWORD));
result.setPrivateKey(authCtx.get(AuthenticationContext.PRIVATE_KEY_PATH));
result.setPassphrase(authCtx.get(AuthenticationContext.PRIVATE_KEY_PASSPHRASE));
String tokenHeaders = authCtx.get(AuthenticationContext.HTTP_TOKEN_HEADERS);
if (tokenHeaders != null) {
for (String name : tokenHeaders.split(",")) {
// spelled as listed, trimmed like HttpTransporterUtils.putTokenHeaders does
String header = name.trim();
String value = authCtx.get(AuthenticationContext.HTTP_TOKEN_VALUE_PREFIX + header);
if (value != null) {
result.getTokenHeaders().put(header, value);
}
}
}
return result;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@
import java.io.File;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
import org.apache.maven.artifact.repository.Authentication;
import org.apache.maven.repository.ArtifactTransferFailedException;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.TokenHeader;
import org.codehaus.plexus.testing.PlexusTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
Expand Down Expand Up @@ -62,6 +65,11 @@ public void testAuthenticationHandling() throws Exception {
server.setId("repository");
server.setUsername("jason");
server.setPassword("abc123");
server.setToken("tok3n");
TokenHeader tokenHeader = new TokenHeader();
tokenHeader.setName("Private-Token");
tokenHeader.setValue("t0ken");
server.addTokenHeader(tokenHeader);

ArtifactRepository repository =
repositorySystem.createArtifactRepository("repository", "http://foo", null, null, null);
Expand All @@ -70,6 +78,10 @@ public void testAuthenticationHandling() throws Exception {
assertNotNull(authentication);
assertEquals("jason", authentication.getUsername());
assertEquals("abc123", authentication.getPassword());
Map<String, String> tokenHeaders = new LinkedHashMap<>();
tokenHeaders.put("Authorization", "Bearer tok3n");
tokenHeaders.put("Private-Token", "t0ken");
assertEquals(tokenHeaders, authentication.getTokenHeaders());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ private static Authentication toAuthentication(org.apache.maven.artifact.reposit
AuthenticationBuilder authBuilder = new AuthenticationBuilder();
authBuilder.addUsername(auth.getUsername()).addPassword(auth.getPassword());
authBuilder.addPrivateKey(auth.getPrivateKey(), auth.getPassphrase());
// full header values: as read back from a session, a bearer token is Authorization with "Bearer <token>"
auth.getTokenHeaders().forEach(authBuilder::addTokenHeader);
result = authBuilder.build();
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,17 @@ private Authentication getAuthentication(RepositorySystemSession session, Artifa
authCtx.get(AuthenticationContext.PASSWORD));
result.setPrivateKey(authCtx.get(AuthenticationContext.PRIVATE_KEY_PATH));
result.setPassphrase(authCtx.get(AuthenticationContext.PRIVATE_KEY_PASSPHRASE));
String tokenHeaders = authCtx.get(AuthenticationContext.HTTP_TOKEN_HEADERS);
if (tokenHeaders != null) {
for (String name : tokenHeaders.split(",")) {
// spelled as listed, trimmed like HttpTransporterUtils.putTokenHeaders does
String header = name.trim();
String value = authCtx.get(AuthenticationContext.HTTP_TOKEN_VALUE_PREFIX + header);
if (value != null) {
result.getTokenHeaders().put(header, value);
}
}
}
return result;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.apache.maven.settings.Mirror;
import org.apache.maven.settings.Proxy;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.TokenHeader;
import org.apache.maven.settings.building.SettingsProblem;
import org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest;
import org.apache.maven.settings.crypto.SettingsDecrypter;
Expand All @@ -67,6 +68,7 @@
import org.eclipse.aether.repository.WorkspaceReader;
import org.eclipse.aether.resolution.ResolutionErrorPolicy;
import org.eclipse.aether.util.ConfigUtils;
import org.eclipse.aether.util.connector.transport.http.HttpTransporterUtils;
import org.eclipse.aether.util.graph.manager.TransitiveDependencyManager;
import org.eclipse.aether.util.listener.ChainedRepositoryListener;
import org.eclipse.aether.util.repository.AuthenticationBuilder;
Expand Down Expand Up @@ -224,6 +226,39 @@ public class DefaultRepositorySystemSessionFactory implements RepositorySystemSe

private final GenericVersionScheme versionScheme = new GenericVersionScheme();

/**
* Adds the token and token headers of the server, checked with the rules the resolver HTTP transports apply, so
* a header they would reject or silently drop fails the build up front. An empty value is not sent, as the
* settings validator warns. The messages never contain a token value, it is a secret.
*/
static void addTokens(AuthenticationBuilder authBuilder, Server server) {
String token = server.getToken();
if (token != null && !token.isEmpty()) {
if (!HttpTransporterUtils.isValidTokenValue(token)) {
throw new IllegalArgumentException("Invalid token for server " + server.getId() + ": it must not be"
+ " blank, and must contain only visible ASCII characters, spaces and tabs");
}
authBuilder.addBearerToken(token);
}
for (TokenHeader tokenHeader : server.getTokenHeaders()) {
String name = tokenHeader.getName();
if (!HttpTransporterUtils.isValidTokenHeaderName(name)) {
throw new IllegalArgumentException("Invalid token header name '" + name + "' for server "
+ server.getId() + ": it must be a valid HTTP header name, not one the HTTP transports"
+ " control or drop (such as Host, Proxy-* or Sec-*)");
}
String value = tokenHeader.getValue();
if (value != null && !value.isEmpty()) {
if (!HttpTransporterUtils.isValidTokenValue(value)) {
throw new IllegalArgumentException("Invalid value of token header " + name + " for server "
+ server.getId() + ": it must not be blank, and must contain only visible ASCII"
+ " characters, spaces and tabs");
}
authBuilder.addTokenHeader(name, value);
}
}
}

/**
* Warns when a value that has the shape of a Maven encrypted password was not decrypted, as it is then
* sent to the server as it is written in the settings. Decryption is attempted, and fails, for every value
Expand Down Expand Up @@ -321,6 +356,15 @@ public RepositorySystemSession.SessionBuilder newRepositorySessionBuilder(MavenE
logger, "password of server " + server.getId(), server.getPassword(), result.getPassword());
warnIfNotDecrypted(
logger, "passphrase of server " + server.getId(), server.getPassphrase(), result.getPassphrase());
warnIfNotDecrypted(logger, "token of server " + server.getId(), server.getToken(), result.getToken());
for (int j = 0; j < server.getTokenHeaders().size(); j++) {
TokenHeader tokenHeader = server.getTokenHeaders().get(j);
warnIfNotDecrypted(
logger,
"token header " + tokenHeader.getName() + " of server " + server.getId(),
tokenHeader.getValue(),
result.getTokenHeaders().get(j).getValue());
}
}
for (int i = 0; i < request.getProxies().size(); i++) {
Proxy proxy = request.getProxies().get(i);
Expand Down Expand Up @@ -384,6 +428,7 @@ public RepositorySystemSession.SessionBuilder newRepositorySessionBuilder(MavenE
AuthenticationBuilder authBuilder = new AuthenticationBuilder();
authBuilder.addUsername(server.getUsername()).addPassword(server.getPassword());
authBuilder.addPrivateKey(server.getPrivateKey(), server.getPassphrase());
addTokens(authBuilder, server);
authSelector.add(server.getId(), authBuilder.build());

// origins the operator bound to this id explicitly, added to the ones collected above from the
Expand Down
Loading
Loading