Skip to content

Commit 49be5ed

Browse files
authored
v1.1.0
2 parents ba8712c + fc5d4d1 commit 49be5ed

File tree

16 files changed

+390
-58
lines changed

16 files changed

+390
-58
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
# MinecraftAuth/lib
22
This is a Java library for querying the [minecraftauth.me](https://minecraftauth.me) service.
33

4-
This project (nor MinecraftAuthentication as a whole) is associated or endorsed by Minecraft/Mojang.
4+
This project (nor MinecraftAuthentication as a whole) is __not__ associated with or endorsed by Minecraft/Mojang.
55

66
# Maven [![Latest release](https://img.shields.io/github/release/MinecraftAuthentication/lib.svg)](https://github.com/MinecraftAuthentication/lib/releases/latest)
77
```xml
88
<repositories>
9-
<repority>
9+
<repository>
1010
<id>scarsz</id>
1111
<url>https://nexus.scarsz.me/content/groups/public/</url>
12-
</repority>
12+
</repository>
1313
</repositories>
1414
```
1515
```xml
1616
<dependencies>
1717
<dependency>
1818
<groupId>me.minecraftauth</groupId>
1919
<artifactId>lib</artifactId>
20-
<version>LATEST</version>
20+
<version>1.1.0</version>
2121
</dependency>
2222
</dependencies>
23-
```
23+
```

deploy.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<settings>
2+
<servers>
3+
<server>
4+
<id>scarsz</id>
5+
<username>${env.NEXUS_USER}</username>
6+
<password>${env.NEXUS_PASS}</password>
7+
</server>
8+
<server>
9+
<id>scarsz-snapshots</id>
10+
<username>${env.NEXUS_USER}</username>
11+
<password>${env.NEXUS_PASS}</password>
12+
</server>
13+
</servers>
14+
</settings>

pom.xml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>me.minecraftauth</groupId>
88
<artifactId>lib</artifactId>
9-
<version>1.0.1</version>
9+
<version>1.1.0</version>
1010

1111
<distributionManagement>
1212
<repository>
@@ -47,6 +47,17 @@
4747
<configuration>
4848
<createDependencyReducedPom>false</createDependencyReducedPom>
4949
<minimizeJar>true</minimizeJar>
50+
51+
<filters>
52+
<filter>
53+
<artifact>github.scarsz:configuralize</artifact>
54+
<excludes>
55+
<!-- snakeyaml and json simple is already inside craftbukkit -->
56+
<exclude>org/yaml/snakeyaml/**</exclude>
57+
<exclude>org/json/simple/**</exclude>
58+
</excludes>
59+
</filter>
60+
</filters>
5061
</configuration>
5162
</execution>
5263
</executions>
@@ -88,6 +99,7 @@
8899
<groupId>org.jetbrains</groupId>
89100
<artifactId>annotations</artifactId>
90101
<version>16.0.1</version>
102+
<scope>provided</scope>
91103
</dependency>
92104
<dependency>
93105
<groupId>com.googlecode.json-simple</groupId>
@@ -96,4 +108,4 @@
96108
</dependency>
97109
</dependencies>
98110

99-
</project>
111+
</project>

src/main/java/me/minecraftauth/lib/AuthService.java

Lines changed: 182 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,39 @@
66
import me.minecraftauth.lib.account.AccountType;
77
import me.minecraftauth.lib.account.Identity;
88
import me.minecraftauth.lib.exception.LookupException;
9+
import me.minecraftauth.lib.account.platform.twitch.SubTier;
910
import org.json.simple.parser.JSONParser;
1011
import org.json.simple.parser.ParseException;
1112

12-
import java.util.Arrays;
13-
import java.util.HashMap;
14-
import java.util.Map;
15-
import java.util.Optional;
13+
import java.util.*;
14+
import java.util.function.Predicate;
1615

1716
public class AuthService {
1817

1918
private static final JSONParser JSON_PARSER = new JSONParser();
2019

20+
// prevent instantiation
21+
private AuthService() {}
22+
23+
private static boolean expectResponseBody(HttpRequest request, Predicate<Dynamic> predicate) throws LookupException {
24+
int status = request.code();
25+
String body = request.body();
26+
27+
if (status / 100 == 2) {
28+
try {
29+
Dynamic response = Dynamic.from(JSON_PARSER.parse(body));
30+
return predicate.test(response);
31+
} catch (HttpRequest.HttpRequestException | ParseException e) {
32+
throw new LookupException("Failed to parse API response", e);
33+
}
34+
} else {
35+
throw new LookupException("MinecraftAuth server returned bad response: " + status + " / " + body);
36+
}
37+
}
38+
private static boolean expectTrue(HttpRequest request) throws LookupException {
39+
return expectResponseBody(request, dynamic -> dynamic.dget("result").convert().intoString().equals("true"));
40+
}
41+
2142
/**
2243
* Look up all of an identifier's linked accounts
2344
* @param from the account type to query by
@@ -28,23 +49,24 @@ public class AuthService {
2849
public static Optional<Identity> lookup(AccountType from, Object identifier) throws LookupException {
2950
HttpRequest request = HttpRequest.get("https://minecraftauth.me/api/lookup?" + from.name().toLowerCase() + "=" + identifier)
3051
.userAgent("MinecraftAuthLib");
52+
String body = request.body();
3153

32-
if (request.code() >= 200 && request.code() <= 299) {
54+
if (request.code() / 100 == 2) {
3355
try {
34-
Dynamic response = Dynamic.from(JSON_PARSER.parse(request.body()));
56+
Dynamic response = Dynamic.from(JSON_PARSER.parse(body));
3557

3658
Map<AccountType, Account> identifiers = new HashMap<>();
3759
response.children().forEach(dynamic -> Arrays.stream(AccountType.values())
3860
.filter(accountType -> accountType.name().equalsIgnoreCase(dynamic.key().convert().intoString()))
3961
.findFirst().ifPresent(type -> identifiers.put(type, Account.from(type, dynamic.dget("identifier").convert().intoString()))));
4062
return Optional.of(new Identity(identifiers));
41-
} catch (ParseException e) {
63+
} catch (HttpRequest.HttpRequestException | ParseException e) {
4264
throw new LookupException("Failed to parse API response", e);
4365
}
4466
} else if (request.code() == 404) {
4567
return Optional.empty();
4668
} else {
47-
throw new LookupException("MinecraftAuth server returned bad code: " + request.code());
69+
throw new LookupException("MinecraftAuth server returned bad code: " + request.code() + " / " + body);
4870
}
4971
}
5072

@@ -59,20 +81,169 @@ public static Optional<Identity> lookup(AccountType from, Object identifier) thr
5981
public static Optional<Account> lookup(AccountType from, Object identifier, AccountType to) throws LookupException {
6082
HttpRequest request = HttpRequest.get("https://minecraftauth.me/api/lookup/" + to.name().toLowerCase() + "?" + from.name().toLowerCase() + "=" + identifier)
6183
.userAgent("MinecraftAuthLib");
84+
String body = request.body();
6285

6386
if (request.code() / 100 == 2) {
6487
try {
65-
Dynamic response = Dynamic.from(JSON_PARSER.parse(request.body()));
88+
Dynamic response = Dynamic.from(JSON_PARSER.parse(body));
6689
String id = response.dget(to.name().toLowerCase() + ".identifier").convert().intoString();
6790
return Optional.of(Account.from(to, id));
68-
} catch (ParseException e) {
91+
} catch (HttpRequest.HttpRequestException | ParseException e) {
6992
throw new LookupException("Failed to parse API response", e);
7093
}
7194
} else if (request.code() == 404) {
7295
return Optional.empty();
7396
} else {
74-
throw new LookupException("MinecraftAuth server returned bad code: " + request.code());
97+
throw new LookupException("MinecraftAuth server returned bad response: " + request.code() + " / " + body);
7598
}
7699
}
77100

101+
private static boolean isFollowing(String serverToken, AccountType platform, UUID minecraftUuid) throws LookupException {
102+
HttpRequest request = HttpRequest.get("https://minecraftauth.me/api/following?platform=" + platform.name().toLowerCase() + "&minecraft=" + minecraftUuid)
103+
.userAgent("MinecraftAuthLib")
104+
.authorization("Basic " + serverToken);
105+
return expectTrue(request);
106+
}
107+
108+
private static boolean isSubscribed(String serverToken, AccountType platform, UUID minecraftUuid, Object data) throws LookupException {
109+
HttpRequest request = HttpRequest.get("https://minecraftauth.me/api/subscribed?platform=" + platform.name().toLowerCase() + "&minecraft=" + minecraftUuid + (data != null ? "&data=" + data : ""))
110+
.userAgent("MinecraftAuthLib")
111+
.authorization("Basic " + serverToken);
112+
return expectTrue(request);
113+
}
114+
115+
/**
116+
* Query if the given Discord user ID is in the given Discord server
117+
* @param serverToken the server authentication token to query data for
118+
* @param minecraftUuid the Minecraft player UUID to query
119+
* @param serverId the Discord server ID to query
120+
* @return if the given Discord user (and the Minecraft Authentication bot) is in the given server
121+
* @throws LookupException if the API returns abnormal error code
122+
*/
123+
public static boolean isDiscordMemberPresent(String serverToken, UUID minecraftUuid, String serverId) throws LookupException {
124+
HttpRequest request = HttpRequest.get("https://minecraftauth.me/api/discord/present?minecraft=" + minecraftUuid + "&server=" + serverId)
125+
.userAgent("MinecraftAuthLib")
126+
.authorization("Basic " + serverToken);
127+
return expectTrue(request);
128+
}
129+
/**
130+
* Query if the given Discord user ID has the given Discord role
131+
* @param serverToken the server authentication token to query data for
132+
* @param minecraftUuid the Minecraft player UUID to query
133+
* @param roleId the Discord role ID to query
134+
* @return if the given Discord user has the given role
135+
* @throws LookupException if the API returns abnormal error code
136+
*/
137+
public static boolean isDiscordRolePresent(String serverToken, UUID minecraftUuid, String roleId) throws LookupException {
138+
return isSubscribed(serverToken, AccountType.DISCORD, minecraftUuid, roleId);
139+
}
140+
141+
/**
142+
* Query if the given Patreon uid is a patron of the server token's Patreon campaign
143+
* @param serverToken the server authentication token to query data for
144+
* @param minecraftUuid the Minecraft player UUID to query
145+
* @return if the given Patreon uid is a patron of the server token's Patreon campaign
146+
* @throws LookupException if the API returns abnormal error code
147+
*/
148+
public static boolean isSubscribedPatreon(String serverToken, UUID minecraftUuid) throws LookupException {
149+
return isSubscribed(serverToken, AccountType.PATREON, minecraftUuid, null);
150+
}
151+
/**
152+
* Query if the given Patreon uid is a patron of the server token's Patreon campaign
153+
* @param serverToken the server authentication token to query data for
154+
* @param minecraftUuid the Minecraft player UUID to query
155+
* @param tierTitle the title of the requested Patreon tier
156+
* @return if the given Patreon uid is a patron of the server token's Patreon campaign
157+
* @throws LookupException if the API returns abnormal error code
158+
*/
159+
public static boolean isSubscribedPatreon(String serverToken, UUID minecraftUuid, String tierTitle) throws LookupException {
160+
return isSubscribed(serverToken, AccountType.PATREON, minecraftUuid, tierTitle);
161+
}
162+
163+
/**
164+
* Query if the given Glimpse user is a sponsor of the server token's Glimpse user
165+
* @param serverToken the server authentication token to query data for
166+
* @param minecraftUuid the Minecraft player UUID to query
167+
* @return if the given Glimpse user is a sponsor of the server token's Glimpse user
168+
* @throws LookupException if the API returns abnormal error code
169+
*/
170+
public static boolean isSubscribedGlimpse(String serverToken, UUID minecraftUuid) throws LookupException {
171+
return isSubscribed(serverToken, AccountType.GLIMPSE, minecraftUuid, null);
172+
}
173+
/**
174+
* Query if the given Glimpse user is a sponsor of the server token's Glimpse user
175+
* @param serverToken the server authentication token to query data for
176+
* @param minecraftUuid the Minecraft player UUID to query
177+
* @param levelName the name of the requested Glimpse level
178+
* @return if the given Glimpse user is a sponsor of the server token's Glimpse user
179+
* @throws LookupException if the API returns abnormal error code
180+
*/
181+
public static boolean isSubscribedGlimpse(String serverToken, UUID minecraftUuid, String levelName) throws LookupException {
182+
return isSubscribed(serverToken, AccountType.GLIMPSE, minecraftUuid, levelName);
183+
}
184+
185+
/**
186+
* Query if the given Twitch uid is following the server token's Twitch channel
187+
* @param serverToken the server authentication token to query data for
188+
* @param minecraftUuid the Minecraft player UUID to query
189+
* @return if the given Twitch uid is following the server token's Twitch channel
190+
* @throws LookupException if the API returns abnormal error code
191+
*/
192+
public static boolean isFollowingTwitch(String serverToken, UUID minecraftUuid) throws LookupException {
193+
return isFollowing(serverToken, AccountType.TWITCH, minecraftUuid);
194+
}
195+
/**
196+
* Query if the given Twitch uid is subbed to the server token's Twitch channel
197+
* @param serverToken the server authentication token to query data for
198+
* @param minecraftUuid the Minecraft player UUID to query
199+
* @return if the given Twitch uid is subbed to the server token's Twitch channel
200+
* @throws LookupException if the API returns abnormal error code
201+
*/
202+
public static boolean isSubscribedTwitch(String serverToken, UUID minecraftUuid) throws LookupException {
203+
return isSubscribed(serverToken, AccountType.TWITCH, minecraftUuid, null);
204+
}
205+
/**
206+
* Query if the given Twitch uid is subbed to the server token's Twitch channel
207+
* @param serverToken the server authentication token to query data for
208+
* @param minecraftUuid the Minecraft player UUID to query
209+
* @param tier the required tier level to qualify as being subscribed
210+
* @return if the given Twitch uid is subbed to the server token's Twitch channel
211+
* @throws LookupException if the API returns abnormal error code
212+
*/
213+
public static boolean isSubscribedTwitch(String serverToken, UUID minecraftUuid, SubTier tier) throws LookupException {
214+
return isSubscribed(serverToken, AccountType.TWITCH, minecraftUuid, tier.getValue());
215+
}
216+
217+
/**
218+
* Query if the player's YouTube account is subscribed to the server token's YouTube channel
219+
* @param serverToken the server authentication token to query data for
220+
* @param minecraftUuid the Minecraft player UUID to query
221+
* @return if the player's YouTube account is subscribed to the server token's YouTube channel
222+
* @throws LookupException if the API returns abnormal error code
223+
*/
224+
public static boolean isSubscribedYouTube(String serverToken, UUID minecraftUuid) throws LookupException {
225+
return isFollowing(serverToken, AccountType.GOOGLE, minecraftUuid);
226+
}
227+
/**
228+
* Query if the player's YouTube account is a paid member of the server token's YouTube channel
229+
* @param serverToken the server authentication token to query data for
230+
* @param minecraftUuid the Minecraft player UUID to query
231+
* @return if the player's YouTube account is a paid member of the server token's YouTube channel
232+
* @throws LookupException if the API returns abnormal error code
233+
*/
234+
public static boolean isMemberYouTube(String serverToken, UUID minecraftUuid) throws LookupException {
235+
return isSubscribed(serverToken, AccountType.GOOGLE, minecraftUuid, null);
236+
}
237+
/**
238+
* Query if the player's YouTube account is a paid member of the server token's YouTube channel
239+
* @param serverToken the server authentication token to query data for
240+
* @param minecraftUuid the Minecraft player UUID to query
241+
* @param tier the required tier level to qualify as being a member
242+
* @return if the player's YouTube account is a paid member of the server token's YouTube channel
243+
* @throws LookupException if the API returns abnormal error code
244+
*/
245+
public static boolean isMemberYouTube(String serverToken, UUID minecraftUuid, String tier) throws LookupException {
246+
return isSubscribed(serverToken, AccountType.GOOGLE, minecraftUuid, tier);
247+
}
248+
78249
}

src/main/java/me/minecraftauth/lib/account/Account.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package me.minecraftauth.lib.account;
22

33
import me.minecraftauth.lib.AuthService;
4+
import me.minecraftauth.lib.account.platform.discord.DiscordAccount;
5+
import me.minecraftauth.lib.account.platform.glimpse.GlimpseAccount;
6+
import me.minecraftauth.lib.account.platform.google.GoogleAccount;
7+
import me.minecraftauth.lib.account.platform.minecraft.MinecraftAccount;
8+
import me.minecraftauth.lib.account.platform.patreon.PatreonAccount;
9+
import me.minecraftauth.lib.account.platform.twitch.TwitchAccount;
410
import me.minecraftauth.lib.exception.LookupException;
511

612
import java.util.Optional;
@@ -18,6 +24,10 @@ public static <T extends Account> T from(AccountType type, String identifier) {
1824
return (T) new MinecraftAccount(UUID.fromString(identifier));
1925
case PATREON:
2026
return (T) new PatreonAccount(Integer.parseInt(identifier));
27+
case GLIMPSE:
28+
return (T) new GlimpseAccount(identifier);
29+
case GOOGLE:
30+
return (T) new GoogleAccount(identifier);
2131
case TWITCH:
2232
return (T) new TwitchAccount(Integer.parseInt(identifier));
2333
default:

src/main/java/me/minecraftauth/lib/account/AccountType.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ public enum AccountType {
77
DISCORD,
88
MINECRAFT,
99
PATREON,
10-
TWITCH;
10+
TWITCH,
11+
GLIMPSE,
12+
GOOGLE;
1113

1214
@Override
1315
public String toString() {

0 commit comments

Comments
 (0)