Skip to content

Commit

Permalink
Update token validation to make more sense for the new system
Browse files Browse the repository at this point in the history
  • Loading branch information
triphora committed Sep 10, 2023
1 parent 5e29523 commit ca56a4a
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 24 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,3 @@ jobs:
- name: Test with Gradle
run: ./gradlew fg:modrinth loom:modrinth
if: ${{ !contains(github.event.head_commit.message, 'skip test') }}
env:
MODRINTH_TOKEN: dummy_token_for_CI
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ A Gradle plugin for interfacing directly with Modrinth, through uploading build

Want to use a GitHub Action instead of a Gradle plugin? Check out [Kir-Antipov/mc-publish](https://github.com/Kir-Antipov/mc-publish), but note that Modrinth does not give support for `mc-publish` where we do for Minotaur.

Still using Minotaur v1? You should switch as soon as possible, because it uses the deprecated API v1. Migration instructions can be found [on the Modrinth documentation page](https://docs.modrinth.com/docs/migrations/v1-to-v2/#minotaur-v1-to-v2).

## Usage Guide

To use this plugin you must add it to your Gradle build script. After that, you can use the `modrinth` task to upload the version to Modrinth.

Minotaur requires a personal access token with the following scopes:
- `USER_READ`
- `CREATE_VERSION` (if running the `modrinth` task)
- `PROJECT_WRITE` (if running the `modrinthSyncBody` task)

Expand Down
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.2.0'
}

version = '2.8.3'
version = '2.8.4'
group = 'com.modrinth.minotaur'
archivesBaseName = 'Minotaur'
description = 'Modrinth plugin for publishing builds to the website!'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
import java.util.Objects;
import java.util.regex.Pattern;

import static com.modrinth.minotaur.Util.api;
import static com.modrinth.minotaur.Util.ext;
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
23 changes: 7 additions & 16 deletions src/main/java/com/modrinth/minotaur/Util.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.modrinth.minotaur;

import masecla.modrinth4j.client.agent.UserAgent;
import masecla.modrinth4j.exception.EndpointException;
import masecla.modrinth4j.main.ModrinthAPI;
import masecla.modrinth4j.model.user.ModrinthUser;
import org.gradle.api.Project;
import org.gradle.api.file.RegularFile;
import org.gradle.api.provider.Provider;
Expand All @@ -13,7 +11,6 @@
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.util.Objects;

/**
* Internal utility methods to make things easier and deduplicated
Expand All @@ -23,9 +20,8 @@ class Util {
/**
* @param project Gradle project for getting various info from
* @return A valid {@link ModrinthAPI} instance
* @throws EndpointException when the request to validate the token fails
*/
static ModrinthAPI api(Project project) throws EndpointException, NullPointerException {
static ModrinthAPI api(Project project) {
ModrinthExtension ext = ext(project);
String url = ext.getApiUrl().get();
if (url.endsWith("/")) {
Expand All @@ -39,19 +35,14 @@ static ModrinthAPI api(Project project) throws EndpointException, NullPointerExc
.contact(ext.getProjectId().get() + "/" + resolveVersionNumber(project))
.build();

String token = ext(project).getToken().get();
ModrinthAPI api = ModrinthAPI.rateLimited(agent, url, token);

// Ensure validity of token unless in Minotaur CI
if (token.equals("dummy_token_for_CI")) {
project.getLogger().info("Skipping token validation (GitHub repo {})", System.getenv("GITHUB_REPOSITORY"));
} else {
ModrinthUser user = api.users().getSelf().join();
String username = Objects.requireNonNull(user.getUsername(), "Failed to resolve username from token");
project.getLogger().debug("Signed in as user {}", username);
String token = ext.getToken().get();
if (token.startsWith("mra")) {
throw new RuntimeException("Token must be a personal-access token, not a session token!");
} else if (!token.startsWith("mrp")) {
project.getLogger().warn("Using GitHub tokens for authentication is deprecated. Please begin to use personal-access tokens.");
}

return api;
return ModrinthAPI.rateLimited(agent, url, token);
}

/**
Expand Down

0 comments on commit ca56a4a

Please sign in to comment.