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

Fix git access token not being sent in header #8

Merged
merged 6 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
14 changes: 10 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ repositories {
jcenter()
}

mainClassName = 'link.infra.packwiz.installer.bootstrap.Main'
File testProjectDirectory = rootProject.getBuildDir().toPath().resolve("test-project").toFile()
testProjectDirectory.mkdirs()

application {
mainClassName = 'link.infra.packwiz.installer.bootstrap.Main'

// Change the working directory for the run task. Here is an example:
tasks.run.workingDir = testProjectDirectory
}

jar {
manifest {
attributes(
'Main-Class': 'link.infra.packwiz.installer.bootstrap.Main'
)
attributes('Main-Class': 'link.infra.packwiz.installer.bootstrap.Main')
}
}
60 changes: 31 additions & 29 deletions src/main/java/link/infra/packwiz/installer/bootstrap/Bootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,9 @@ private static Release requestRelease() throws IOException, GithubException {
Release rel = new Release();

URL url = new URL(updateURL);
if (accessToken != null) {
url = new URL(updateURL + "?access_token=" + accessToken);
}
URLConnection conn = url.openConnection();

addAuthorizationHeader(conn);
// 30 second read timeout
conn.setReadTimeout(30 * 1000);
InputStream in;
Expand All @@ -245,11 +244,7 @@ private static Release requestRelease() throws IOException, GithubException {
}
streamReader.close();

JsonValue tagName = object.get("tag_name");
if (tagName == null || !tagName.isString()) {
throw new GithubException("Tag name cannot be found");
}
rel.tagName = tagName.asString();
rel.tagName = getStringProperty("tag_name", object, "Tag name");

JsonValue assets = object.get("assets");
if (assets == null || !assets.isArray()) {
Expand All @@ -259,25 +254,16 @@ private static Release requestRelease() throws IOException, GithubException {
if (!assetValue.isObject()) {
throw new GithubException();
}

JsonObject asset = assetValue.asObject();
JsonValue name = asset.get("name");
if (name == null || !name.isString()) {
throw new GithubException("Asset name cannot be found");
}
if (!name.asString().equalsIgnoreCase(JAR_NAME)) {
String name = getStringProperty("name", asset, "Asset name");

if (!name.equalsIgnoreCase(JAR_NAME)) {
continue;
}
JsonValue downloadURL = asset.get("browser_download_url");
if (downloadURL == null || !downloadURL.isString()) {
throw new GithubException("Asset Download URL cannot be found");
}
rel.downloadURL = downloadURL.asString();

JsonValue assetURL = asset.get("url");
if (assetURL == null || !assetURL.isString()) {
throw new GithubException("Asset Download URL cannot be found");
}
rel.assetURL = assetURL.asString();

rel.downloadURL = getAssetUrl("browser_download_url", asset);
rel.assetURL = getAssetUrl("url", asset);
break;
}
if (rel.tagName == null) {
Expand All @@ -287,14 +273,23 @@ private static Release requestRelease() throws IOException, GithubException {
return rel;
}

private static String getAssetUrl(String property, JsonObject asset) throws GithubException {
return getStringProperty(property, asset, "Asset Download URL property");
}

private static String getStringProperty(String property, JsonObject obj, String displayName) throws GithubException {
JsonValue value = obj.get(property);
if (value == null || !value.isString()) {
throw new GithubException("$displayName (" + property + ") cannot be found");
comp500 marked this conversation as resolved.
Show resolved Hide resolved
}
return value.asString();
}

private static void downloadUpdate(String downloadURL, String assetURL, String path) throws IOException {
URL url = new URL(downloadURL);
if (accessToken != null) {
//url = new URL(downloadURL + "?access_token=" + accessToken);
// Authenticated downloads use the assetURL
url = new URL(assetURL + "?access_token=" + accessToken);
}
URLConnection conn = url.openConnection();

addAuthorizationHeader(conn);
conn.addRequestProperty("Accept", "application/octet-stream");
// 30 second read timeout
conn.setReadTimeout(30 * 1000);
Expand All @@ -308,4 +303,11 @@ private static void downloadUpdate(String downloadURL, String assetURL, String p
in.close();
}

private static void addAuthorizationHeader(URLConnection conn) {
if (accessToken != null) {
// Authenticated downloads use the assetURL
conn.addRequestProperty("Authorization", accessToken);
}
}

}