Skip to content

Commit

Permalink
Validate token before uploading anything to Modrinth
Browse files Browse the repository at this point in the history
Closes #38
  • Loading branch information
triphora committed Jan 20, 2023
1 parent 8b0e988 commit 7eb2a26
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ plugins {
id 'com.gradle.plugin-publish' version '1.1.0'
}

version = '2.6.0'
version = '2.7.0'
group = 'com.modrinth.minotaur'
archivesBaseName = 'Minotaur'
description = 'Modrinth plugin for publishing builds to the website!'
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/modrinth/minotaur/TaskModrinthSyncBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@

import java.util.regex.Pattern;

import static com.modrinth.minotaur.Util.getExtension;
import static com.modrinth.minotaur.Util.getUploadEndpoint;
import static com.modrinth.minotaur.Util.resolveId;
import static com.modrinth.minotaur.Util.*;

/**
* A task used to communicate with Modrinth for the purpose of syncing project body with, for example, a README.
Expand Down Expand Up @@ -57,6 +55,7 @@ public void apply() {
final HttpClient client = Util.createHttpClient();
final HttpPatch patch = new HttpPatch(getUploadEndpoint(this.getProject()) + "project/" + resolveId(this.getProject(), extension.getProjectId().get()));

validateToken(this.getProject());
patch.addHeader("Authorization", extension.getToken().get());

JsonObject data = new JsonObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ public void upload(List<File> files) throws IOException {
final HttpClient client = createHttpClient();
final HttpPost post = new HttpPost(getUploadEndpoint(this.getProject()) + "version");

validateToken(this.getProject());
post.addHeader("Authorization", extension.getToken().get());

final MultipartEntityBuilder form = MultipartEntityBuilder.create();
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/modrinth/minotaur/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,29 @@ static File resolveFile(Project project, Object in) {
// None of the previous checks worked. Fall back to Gradle's built-in file resolution mechanics.
return project.file(in);
}

/**
* Validates that the token provided in the extension is valid.
*
* @param project Gradle project
*/
static void validateToken(Project project) throws IOException {
if (System.getenv("GITHUB_ACTIONS") != null) {

This comment has been minimized.

Copy link
@necauqua

necauqua Jan 20, 2023

oof, this was specifically my case, a dry run in the manual github action :)

Could be a more generic CI if this is against CI, and if not then why is this needed?

This comment has been minimized.

Copy link
@triphora

triphora Jan 20, 2023

Author Contributor

Ah that was to make the actions on this repo not fail. Let me make it specific to this repo only

return;
}

HttpClient client = createHttpClient();
Gson gson = createGsonInstance();
HttpGet get = new HttpGet(String.format("%suser", getUploadEndpoint(project)));
get.addHeader("Authorization", getExtension(project).getToken().get());
HttpResponse response = client.execute(get);

int code = response.getStatusLine().getStatusCode();
if (code != 200) {
ResponseError errorInfo = gson.fromJson(EntityUtils.toString(response.getEntity()), ResponseError.class);
String error = String.format("Failed to validate Modrinth token! Status: %s Error: %s Reason: %s", code, errorInfo.getError(), errorInfo.getDescription());
project.getLogger().error(error);
throw new GradleException(error);
}
}
}

0 comments on commit 7eb2a26

Please sign in to comment.