Skip to content

Commit 0bd9510

Browse files
authored
Merge pull request #2 from MuShare/develop/pluto-master
Support pluto 1.4+
2 parents 55b2038 + 8cd2cd2 commit 0bd9510

File tree

5 files changed

+38
-87
lines changed

5 files changed

+38
-87
lines changed

build.gradle

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,7 @@ repositories {
1919
}
2020

2121
dependencies {
22-
// This dependency is exported to consumers, that is to say found on their compile classpath.
23-
api 'org.apache.commons:commons-math3:3.6.1'
24-
25-
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
26-
implementation 'com.google.guava:guava:28.1-jre'
27-
22+
implementation 'com.auth0:java-jwt:3.10.3'
2823
compile 'com.github.kevinsawicki:http-request:6.0'
2924
compile 'net.sf.json-lib:json-lib:2.4:jdk15'
3025
// Use JUnit test framework
@@ -45,7 +40,7 @@ publishing {
4540

4641
publications {
4742
maven(MavenPublication) {
48-
version '0.1.6'
43+
version '0.2'
4944
group 'org.mushare'
5045
from components.java
5146
}

src/main/java/org/mushare/pluto/Pluto.java

Lines changed: 23 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
package org.mushare.pluto;
22

3+
import com.auth0.jwt.JWT;
4+
import com.auth0.jwt.JWTVerifier;
5+
import com.auth0.jwt.algorithms.Algorithm;
6+
import com.auth0.jwt.interfaces.Claim;
7+
import com.auth0.jwt.interfaces.DecodedJWT;
38
import com.github.kevinsawicki.http.HttpRequest;
49
import net.sf.json.JSONArray;
510
import net.sf.json.JSONObject;
611
import org.mushare.pluto.exception.PlutoErrorCode;
712
import org.mushare.pluto.exception.PlutoException;
813

914
import java.io.UnsupportedEncodingException;
15+
import java.nio.charset.StandardCharsets;
1016
import java.security.Signature;
11-
import java.util.ArrayList;
12-
import java.util.Base64;
13-
import java.util.List;
17+
import java.security.interfaces.RSAPublicKey;
18+
import java.util.*;
19+
import java.util.stream.Collector;
1420
import java.util.stream.Collectors;
1521
import java.util.stream.IntStream;
1622

@@ -33,64 +39,29 @@ public static void setup(String server, String appId) {
3339
}
3440

3541
public static PlutoUser auth(String token) throws PlutoException {
36-
if (token == null) {
37-
throw new PlutoException(PlutoErrorCode.jwtFormatError);
38-
}
39-
String[] parts = token.split("\\.");
40-
if (parts.length != 3) {
41-
throw new PlutoException(PlutoErrorCode.jwtFormatError);
42-
}
43-
44-
String header = null;
45-
JSONObject payload = null;
46-
try {
47-
header = new String(Base64.getDecoder().decode(parts[0]), "utf-8");
48-
payload = JSONObject.fromObject(new String(Base64.getDecoder().decode(parts[1]), "utf-8"));
49-
} catch (UnsupportedEncodingException e) {
50-
e.printStackTrace();
51-
throw new PlutoException(PlutoErrorCode.other);
52-
}
53-
// Verify the appId of the jwt token.
54-
if (!payload.getString("appId").equals(shared.appId)) {
55-
throw new PlutoException(PlutoErrorCode.appIdError);
56-
}
57-
Long expire = payload.getLong("expire_time") * 1000;
58-
if (expire < System.currentTimeMillis()) {
59-
throw new PlutoException(PlutoErrorCode.expired);
60-
}
61-
62-
boolean verified = false;
63-
try {
64-
Signature signature = Signature.getInstance("SHA256withRSA");
65-
signature.initVerify(shared.keyManager.getPublicKey());
66-
signature.update((header + payload).getBytes());
67-
verified = signature.verify(Base64.getDecoder().decode(parts[2]));
68-
} catch (Exception e) {
69-
e.printStackTrace();
70-
throw new PlutoException(PlutoErrorCode.other);
71-
}
72-
73-
if (!verified) {
74-
throw new PlutoException(PlutoErrorCode.notVerified);
75-
}
76-
return new PlutoUser(payload);
42+
JWTVerifier verifier = JWT.require(Algorithm.RSA256((RSAPublicKey) shared.keyManager.getPublicKey()))
43+
.withIssuer(shared.appId)
44+
.build();
45+
DecodedJWT jwt = verifier.verify(token);
46+
return new PlutoUser(jwt.getClaims());
7747
}
7848

7949
public static List<PlutoUserInfo> fetUserInfos(List<Long> userIds) {
8050
if (userIds == null || userIds.size() == 0) {
8151
return new ArrayList<>();
8252
}
8353
String ids = userIds.stream()
84-
.map(userId -> userId + "-")
54+
.map(userId -> "ids=" + userId)
8555
.reduce("", (s1, s2) -> {
86-
return s1 + s2;
56+
return s1 + "&" + s2;
8757
});
88-
ids = ids.substring(0, ids.length() - 1);
89-
String response = HttpRequest.get(shared.server + "api/user/info/" + ids).body();
90-
JSONArray body = JSONObject.fromObject(response).getJSONArray("body");
91-
return IntStream.range(0, body.size())
92-
.mapToObj(index -> {
93-
JSONObject object = body.getJSONObject(index);
58+
System.out.println(shared.server + "v1/user/info/public?" + ids);
59+
String response = HttpRequest.get(shared.server + "v1/user/info/public?" + ids).body();
60+
JSONObject body = JSONObject.fromObject(response).getJSONObject("body");
61+
62+
return Arrays.stream(body.keySet().toArray())
63+
.map(key -> {
64+
JSONObject object = body.getJSONObject((String) key);
9465
PlutoUserInfo info = new PlutoUserInfo();
9566
info.setUserId(object.getLong("id"));
9667
if (object.containsKey("err_code") && object.getInt("err_code") == 403) {
Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,28 @@
11
package org.mushare.pluto;
22

3+
import com.auth0.jwt.interfaces.Claim;
34
import net.sf.json.JSONArray;
45
import net.sf.json.JSONObject;
56

67
import java.util.ArrayList;
78
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Set;
811

912
public class PlutoUser {
1013
private long userId;
11-
private String deviceId;
1214
private List<String> scopes;
13-
private LoginType loginType;
1415

1516
public long getUserId() {
1617
return userId;
1718
}
1819

19-
public String getDeviceId() {
20-
return deviceId;
21-
}
22-
2320
public List<String> getScopes() {
2421
return scopes;
2522
}
2623

27-
public LoginType getLoginType() {
28-
return loginType;
29-
}
30-
31-
public PlutoUser(JSONObject payload) {
32-
userId = payload.getLong("userId");
33-
deviceId = payload.getString("deviceId");
34-
scopes = new ArrayList<>();
35-
if (payload.containsKey("scopes") && !payload.get("scopes").equals("null")) {
36-
JSONArray scopeArray = payload.getJSONArray("scopes");
37-
for (int i = 0; i < scopeArray.size(); i++) {
38-
scopes.add(scopeArray.getString(i));
39-
}
40-
}
41-
loginType = LoginType.fromIdentifier(payload.getString("login_type"));
24+
public PlutoUser(Map<String, Claim> claims) {
25+
userId = claims.get("sub").asLong();
26+
scopes = claims.get("scopes").asList(String.class);
4227
}
4328
}

src/main/java/org/mushare/pluto/PublicKeyManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void setPublicKey(PublicKey publicKey) {
2626

2727
public PublicKey getPublicKey() {
2828
if (publicKey == null) {
29-
String response = HttpRequest.get(server + "api/auth/publickey").body();
29+
String response = HttpRequest.get(server + "v1/token/publickey").body();
3030
String text = JSONObject.fromObject(response).getJSONObject("body").getString("public_key");
3131
text = text.replaceAll("\r|\n", "")
3232
.replace("-----BEGIN PUBLIC KEY-----", "")

src/test/java/org/mushare/pluto/PlutoTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
public class PlutoTest {
1515

1616
private void setup() {
17-
Pluto.setup("https://staging.easyjapanese-api-gateway.mushare.cn/pluto/", "org.mushare.easyjapanese");
17+
Pluto.setup("https://beta-pluto.kaboocha.com/", "org.mushare.easyjapanese");
1818
}
1919

2020
@Test
2121
public void testAuth() throws PlutoException {
2222
setup();
23-
String scope = "easyjapanese.admin";
24-
String token = "eyJ0eXBlIjoiand0IiwiYWxnIjoicnNhIn0.eyJ0eXBlIjoiYWNjZXNzIiwiY3JlYXRlX3RpbWUiOjE1ODQ4NTQyNjEsImV4cGlyZV90aW1lIjoxNjE2MjE3NDYxLCJ1c2VySWQiOjMsImRldmljZUlkIjoiQ0Y0Mzc5MzMtQ0MyMS00QUFCLTgxNjEtMUU1MTVCNjQxQTU5IiwiYXBwSWQiOiJvcmcubXVzaGFyZS5lYXN5amFwYW5lc2UiLCJzY29wZXMiOlsiZWFzeWphcGFuZXNlLmFkbWluIl0sImxvZ2luX3R5cGUiOiJtYWlsIn0.RlrKt8fy+rXF9h9/H5w8es4Ni9IlCapDkuA8rxqX09Hv3VUFyY3tt+wdGTihNv9zZSm8Sf7m9ILkq0er826V9p1lUHk50pYlwigTvu4lG5g2uU/bAwt7EwPhxfco6XlA4Z0sQUqS49yM+dmVWnl66bhookkipcDZchpFzr3TOdbmPjTgJ8mMUZO46Kx1fSQhzOJdCAjdkMP44oeuTwcfn1NRZBXacF/HzVLpg+R8APv5oXAmvN4A8HSYoVMRaZn7Z1L6mspOyLfkOuNsOYT1iDM6jkAq0Lj/ST/n7fhNw+2+cadGe2aPBkeU8H7qzgQJEqE0iAlTtFQF6O1Qt0duxQ";
23+
String scope = "easyjapanese.user";
24+
String token = "eyJ0eXBlIjoiand0IiwiYWxnIjoicnNhIn0.eyJ0eXBlIjoiYWNjZXNzIiwiY3JlYXRlX3RpbWUiOjE1ODQ3MTA3NzksImV4cGlyZV90aW1lIjoxNTg0NzE0Mzc5LCJ1c2VySWQiOjMsImRldmljZUlkIjoiQ0Y0Mzc5MzMtQ0MyMS00QUFCLTgxNjEtMUU1MTVCNjQxQTU5IiwiYXBwSWQiOiJvcmcubXVzaGFyZS5lYXN5amFwYW5lc2UiLCJzY29wZXMiOlsiZWFzeWphcGFuZXNlLmFkbWluIl0sImxvZ2luX3R5cGUiOiJtYWlsIn0.TR0A/fen5f+kg4APscFQ7JZtp2zNw5KMUeKBzm/GJg4WZp90ihg/OPfU9fcaJhoVNHWqxQ/OIHfAcXaXV+l8EEsTbh/+/Qs0glujX09Vm5z23wITzC36X+a/9bJJj5J1kXDtyx/CVdMmf8vm81T8PJFJuJtnyzA3IMRUGK0KecJ5MQjaOvh7NxZRhJfsCNYQz4V5hxvtI8urs+gi3/QVN04UQ5i0BX+DDdQ1E4MX8+3v2zDPc3ipQ8r9nZl00wmPdcqW5zx9Xooha6X8eTujgQuLiSFDheZGRR6/N+ZYktt/PMI49KIXVseenTTTzpX1Vmg9PAabPLJotdjcRpKtiA";
2525
assertTrue("testAuth should return 'true'", Pluto.auth(token).getScopes().contains(scope));
2626
}
2727

@@ -40,14 +40,14 @@ public void testAuthExpired() throws PlutoException {
4040

4141
@Test
4242
public void testScopes() throws PlutoException {
43-
Pluto.setup("https://easyjapanese-api-gateway.mushare.cn/pluto/", "org.mushare.easyjapanese");
44-
String token = "eyJ0eXBlIjoiand0IiwiYWxnIjoicnNhIn0.eyJ0eXBlIjoiYWNjZXNzIiwiY3JlYXRlX3RpbWUiOjE1ODU0NDg1MTcsImV4cGlyZV90aW1lIjoxNTg1NDUyMTE3LCJ1c2VySWQiOjQ4LCJkZXZpY2VJZCI6IkM5QTExQTIzLTg1QTMtNEMwRC04RjQxLTE5QURBNEIwOTJFRSIsImFwcElkIjoib3JnLm11c2hhcmUuZWFzeWphcGFuZXNlIiwic2NvcGVzIjpudWxsLCJsb2dpbl90eXBlIjoiZ29vZ2xlIn0.LyYEgEIxc1ghirESmvvpDE/EEd9B0U10BRgbKt3YdfiCb6L3URizHIqJVoc6IhPZOOBNy2x2Dk/sCZ5JV1rtaZ8niMdAb7VL7PwObZiWuWT8TieuV+7DaPxcJrPdzgz46GOOX7o3+fWRr64ucOYZeiY/g0PBCL07dMPRHYyzHK7e8Wrb/59rUjO4GCad5vM5qny9jTXihB+IokBPOYDkhKCinOfMAqOOFOtVRVdxt/mCLhnjkxexwWM/a7vmJQ4qOZYHxAB9mbgDq+mbBfzWafFWhbADdZdSqyKnKUDguBE1fPRODLqTIqB0KL4cttgSHKivlwwiVaaxZUEkwGN4GQ";
43+
setup();
44+
String token = "eyJ0eXBlIjoiand0IiwiYWxnIjoiUlMyNTYifQ.eyJ0eXBlIjoiYWNjZXNzIiwiaWF0IjoxNjAxNTUxMjU1LCJleHAiOjE2MDE1NTQ4NTUsInN1YiI6NTIwMywiaXNzIjoib3JnLm11c2hhcmUuZWFzeWphcGFuZXNlIiwic2NvcGVzIjpbIiJdfQ.S3ZTAFFlwmV3DJCYQrN_N0GDbpAfIPZlwXd1uxlDdMxXw_vkkLDffYjJjEA-nQ2Q1wgImb3YqA-SI3mbv_giiqo83NgiCeLVYTRDqVxj-ZJYJQRllD0o09DeeI_v5_Mll2toWF3cQaF8yEkzPR-AmOph-0eOw7vC5VJoMYy1SiVIf04wOR7wByidhkwbkPnn7X3cjFytOLKA2ZE_ikMpZ02NfRllddxK9IBa-NYVd_1cIleU-a8WSz8xPKCDDrq5He0M2ZCN0DpXdi4p2FI4wCGRSi3YvcSHwszJWV8dQQcgNhVi9T_oPhWSSTG_dZjQB9TTLq4IkJj6XYyQv8kI6Q";
4545
Pluto.auth(token);
4646
}
4747

4848
@Test
4949
public void testFetchUserInfos() {
50-
Pluto.setup("https://staging.easyjapanese-api-gateway.mushare.cn/pluto/", "org.mushare.easyjapanese");
50+
setup();
5151
Pluto.fetUserInfos(Stream.of(1L, 2L, 3L).collect(Collectors.toList())).forEach(plutoUserInfo -> {
5252
System.out.println(plutoUserInfo);
5353
});

0 commit comments

Comments
 (0)