Skip to content

Commit

Permalink
Fix git access token not being sent in header (#8)
Browse files Browse the repository at this point in the history
* 💡 Remove redundant comment

* ♻️ Optimize checks for unequal jar names

* ♻️ Refactor json string property getting

Consolidates a lot of repeated checks when accessing json object strings into a local method of its own

* 👷 Change run task's working directory

Change run tasks' working directory from project directory to <build_directory>/test-project

* 🔒️ Fix git authorization not being sent in header

Git only accepts auth tokens passed in the header

* Fix exception message
  • Loading branch information
fuzzyweapon authored Mar 17, 2023
1 parent 3cac268 commit cdad0fa
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 33 deletions.
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");
}
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);
}
}

}

0 comments on commit cdad0fa

Please sign in to comment.