Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support Basic Auth in the catalog-ref URL (#1886) #1887

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
39 changes: 30 additions & 9 deletions src/main/java/dev/jbang/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.net.*;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -46,6 +41,9 @@
import javax.annotation.Nonnull;
import javax.swing.*;

import org.apache.commons.text.StringSubstitutor;
import org.apache.commons.text.lookup.DefaultStringLookup;
import org.apache.commons.text.lookup.StringLookupFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

Expand All @@ -63,6 +61,10 @@
import dev.jbang.dependencies.ModularClassPath;
import dev.jbang.source.Source;

import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toUnmodifiableMap;
import static org.apache.commons.text.lookup.DefaultStringLookup.*;

public class Util {

public static final String JBANG_JDK_VENDOR = "JBANG_JDK_VENDOR";
Expand Down Expand Up @@ -99,6 +101,16 @@ public class Util {
private static Boolean downloadSources;
private static Instant startTime = Instant.now();

private static final StringSubstitutor urlSubstitutor = createUrlSubstitutor();
bartoszpop marked this conversation as resolved.
Show resolved Hide resolved

private static StringSubstitutor createUrlSubstitutor() {
return new StringSubstitutor(StringLookupFactory.INSTANCE.interpolatorStringLookup(
asList(ENVIRONMENT, SYSTEM_PROPERTIES, PROPERTIES).stream().collect(
toUnmodifiableMap(DefaultStringLookup::getKey, DefaultStringLookup::getStringLookup)),
null, false
));
}

public static void setVerbose(boolean verbose) {
Util.verbose = verbose;
if (verbose) {
Expand Down Expand Up @@ -824,7 +836,7 @@ private static Path connect(String fileURL, ConnectionConfigurator configurator,
}
}

private interface ConnectionConfigurator {
interface ConnectionConfigurator {

void configure(URLConnection conn) throws IOException;

Expand Down Expand Up @@ -1123,11 +1135,20 @@ private static HttpURLConnection handleRedirects(HttpURLConnection httpConn, Con

private static void addAuthHeaderIfNeeded(URLConnection urlConnection) {
String auth = null;
URL url = urlConnection.getURL();
if (urlConnection.getURL().getHost().endsWith("github.com") && System.getenv().containsKey("GITHUB_TOKEN")) {
auth = "token " + System.getenv("GITHUB_TOKEN");
} else {
String username = System.getenv(JBANG_AUTH_BASIC_USERNAME);
String password = System.getenv(JBANG_AUTH_BASIC_PASSWORD);
String username;
String password;
if (url.getUserInfo() != null) {
String[] credentials = url.getUserInfo().split(":", 2);
username = urlSubstitutor.replace(credentials[0]);
password = credentials.length > 1 ? urlSubstitutor.replace(credentials[1]) : "";
} else {
username = System.getenv(JBANG_AUTH_BASIC_USERNAME);
password = System.getenv(JBANG_AUTH_BASIC_PASSWORD);
}
if (username != null && password != null) {
String id = username + ":" + password;
String encodedId = Base64.getEncoder().encodeToString(id.getBytes(StandardCharsets.UTF_8));
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/dev/jbang/util/NoOpUrlConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.jbang.util;

import java.net.URL;
import java.net.URLConnection;

public class NoOpUrlConnection extends URLConnection {

protected NoOpUrlConnection(URL url) {
super(url);
}

@Override
public void connect() {
}
}
13 changes: 12 additions & 1 deletion src/test/java/dev/jbang/util/TestUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.jbang.util;

import static dev.jbang.util.Util.ConnectionConfigurator.authentication;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
Expand All @@ -8,9 +9,10 @@
import static org.junit.jupiter.api.Assertions.*;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Path;
import java.util.List;

import org.junit.jupiter.api.Test;

import dev.jbang.BaseTest;
Expand Down Expand Up @@ -158,4 +160,13 @@ void testDispostionFilename() {
// assertThat(Util.getDispositionFilename("inline;
// filename*=iso-fake-1''dummy"), equalTo(""));
}

@Test
void testUrlBasicAuth() throws IOException {
URLConnection connection = new NoOpUrlConnection(new URL("https://SomeUser:${properties:src\\test\\resources\\url.properties::password}@example.com"));
bartoszpop marked this conversation as resolved.
Show resolved Hide resolved

authentication().configure(connection);

assertThat(connection.getRequestProperty("Authorization"), equalTo("Basic U29tZVVzZXI6VmVyeVN0cm9uZ1Bhc3N3b3JkMQ=="));
}
}
1 change: 1 addition & 0 deletions src/test/resources/url.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
password=VeryStrongPassword1