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 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
15 changes: 12 additions & 3 deletions src/main/java/dev/jbang/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,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 +1123,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 = PropertiesValueResolver.replaceProperties(credentials[0]);
password = credentials.length > 1 ? PropertiesValueResolver.replaceProperties(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() {
}
}
15 changes: 15 additions & 0 deletions 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,8 +9,11 @@
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 java.util.Objects;

import org.junit.jupiter.api.Test;

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

@Test
void testUrlBasicAuth() throws IOException {
System.setProperty("SOME_USERNAME", "JohnDoe");
System.setProperty("SOME_PASSWORD", "VeryStrongPassword1");
URLConnection connection = new NoOpUrlConnection(new URL("https://${SOME_USERNAME}:${SOME_PASSWORD}@example.com"));

authentication().configure(connection);

assertThat(connection.getRequestProperty("Authorization"), equalTo("Basic Sm9obkRvZTpWZXJ5U3Ryb25nUGFzc3dvcmQx"));
}
}