Skip to content

Commit fe8d9e7

Browse files
committed
Test auth expired
1 parent 4169d33 commit fe8d9e7

3 files changed

Lines changed: 51 additions & 17 deletions

File tree

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

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.mushare.pluto.exception.PlutoErrorCode;
55
import org.mushare.pluto.exception.PlutoException;
66

7+
import java.io.UnsupportedEncodingException;
78
import java.security.Signature;
89
import java.util.Base64;
910

@@ -23,7 +24,7 @@ public static void setup(String server, String appId) {
2324
shared.appId = appId;
2425
}
2526

26-
public static PlutoUser auth(String token) throws PlutoException {
27+
public static PlutoUser auth(String token) {
2728
if (token == null) {
2829
throw new PlutoException(PlutoErrorCode.jwtFormatError);
2930
}
@@ -32,28 +33,39 @@ public static PlutoUser auth(String token) throws PlutoException {
3233
throw new PlutoException(PlutoErrorCode.jwtFormatError);
3334
}
3435

36+
String header = null;
37+
JSONObject payload = null;
38+
try {
39+
header = new String(Base64.getDecoder().decode(parts[0]), "utf-8");
40+
payload = JSONObject.fromObject(new String(Base64.getDecoder().decode(parts[1]), "utf-8"));
41+
} catch (UnsupportedEncodingException e) {
42+
e.printStackTrace();
43+
throw new PlutoException(PlutoErrorCode.other);
44+
}
45+
// Verify the appId of the jwt token.
46+
if (!payload.getString("appId").equals(shared.appId)) {
47+
throw new PlutoException(PlutoErrorCode.appIdError);
48+
}
49+
Long expire = payload.getLong("expire_time") * 1000;
50+
if (expire < System.currentTimeMillis()) {
51+
throw new PlutoException(PlutoErrorCode.expired);
52+
}
53+
54+
boolean verified = false;
3555
try {
36-
String header = new String(Base64.getDecoder().decode(parts[0]), "utf-8");
37-
JSONObject payload = JSONObject.fromObject(new String(Base64.getDecoder().decode(parts[1]), "utf-8"));
38-
// Verify the appId of the jwt token.
39-
if (!payload.getString("appId").equals(shared.appId)) {
40-
throw new PlutoException(PlutoErrorCode.appIdError);
41-
}
42-
Long expire = payload.getLong("expire_time") * 1000;
43-
if (expire < System.currentTimeMillis()) {
44-
throw new PlutoException(PlutoErrorCode.expired);
45-
}
4656
Signature signature = Signature.getInstance("SHA256withRSA");
4757
signature.initVerify(shared.keyManager.getPublicKey());
4858
signature.update((header + payload).getBytes());
49-
if (!signature.verify(Base64.getDecoder().decode(parts[2]))) {
50-
throw new PlutoException(PlutoErrorCode.notVerified);
51-
}
52-
return new PlutoUser(payload);
59+
verified = signature.verify(Base64.getDecoder().decode(parts[2]));
5360
} catch (Exception e) {
5461
e.printStackTrace();
5562
throw new PlutoException(PlutoErrorCode.other);
5663
}
64+
65+
if (!verified) {
66+
throw new PlutoException(PlutoErrorCode.notVerified);
67+
}
68+
return new PlutoUser(payload);
5769
}
5870

5971
}

src/main/java/org/mushare/pluto/exception/PlutoException.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ public PlutoException(PlutoErrorCode code) {
99
this.error = new PlutoError(code);
1010
}
1111

12+
@Override
13+
public String getMessage() {
14+
return error.getMessage();
15+
}
1216
}

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package org.mushare.pluto;
22

3+
import org.junit.Rule;
34
import org.junit.Test;
5+
import org.junit.rules.ExpectedException;
6+
import org.mushare.pluto.exception.PlutoErrorCode;
7+
import org.mushare.pluto.exception.PlutoException;
8+
9+
import java.util.Base64;
410

511
import static org.junit.Assert.assertTrue;
612

@@ -11,8 +17,20 @@ public void testAuth() {
1117
Pluto.setup("https://staging.easyjapanese-api-gateway.mushare.cn/pluto/", "org.mushare.easyjapanese");
1218
String scope = "easyjapanese.admin";
1319
String token = "eyJ0eXBlIjoiand0IiwiYWxnIjoicnNhIn0.eyJ0eXBlIjoiYWNjZXNzIiwiY3JlYXRlX3RpbWUiOjE1ODQ3MjIyMjQsImV4cGlyZV90aW1lIjoxNTg0NzI1ODI0LCJ1c2VySWQiOjMsImRldmljZUlkIjoiQ0Y0Mzc5MzMtQ0MyMS00QUFCLTgxNjEtMUU1MTVCNjQxQTU5IiwiYXBwSWQiOiJvcmcubXVzaGFyZS5lYXN5amFwYW5lc2UiLCJzY29wZXMiOlsiZWFzeWphcGFuZXNlLmFkbWluIl0sImxvZ2luX3R5cGUiOiJtYWlsIn0.V6HVNGsRvzLkgCzPxV3JH9H1GqF/ycNrKQDp/mzq/OdCOy1v8cu/O9HCjtW2J5NZuYgdtletEuoEdIQBUqtRHAseRPcyUmddWf3NxCHcqpAQZW2hJzp8WRgfP2Gug+O++IBoysntYz6FWSLUqw0HzGlU46J4jQnmzWvW/MR/mvM/7IWS5mtPM8DftlI3hBeeXH6FRKMtRbzr4PD0sTb/rSpJPtcs2YXmlj8C5DOhZd2XQjqnA7HhA3MZ2PAy6hkeZ99qyDUZav5SoOQQ8qCNbGDg/nQbeIaUHcg4Fx0F5lIlFAyHHaEkyhwSN4dLI+4ZD1rIxIXLH+uzcWOCYTIxag";
14-
PlutoUser user = Pluto.auth(token);
15-
assertTrue("testAuth should return 'true'", user.getScopes().contains(scope));
20+
assertTrue("testAuth should return 'true'", Pluto.auth(token).getScopes().contains(scope));
21+
}
22+
23+
@Rule
24+
public ExpectedException exceptionRule = ExpectedException.none();
25+
26+
@Test
27+
public void testAuthExpired() {
28+
exceptionRule.expect(PlutoException.class);
29+
exceptionRule.expectMessage(PlutoErrorCode.expired.toString());
30+
31+
Pluto.setup("https://staging.easyjapanese-api-gateway.mushare.cn/pluto/", "org.mushare.easyjapanese");
32+
String token = "eyJ0eXBlIjoiand0IiwiYWxnIjoicnNhIn0.eyJ0eXBlIjoiYWNjZXNzIiwiY3JlYXRlX3RpbWUiOjE1ODQ3MTA3NzksImV4cGlyZV90aW1lIjoxNTg0NzE0Mzc5LCJ1c2VySWQiOjMsImRldmljZUlkIjoiQ0Y0Mzc5MzMtQ0MyMS00QUFCLTgxNjEtMUU1MTVCNjQxQTU5IiwiYXBwSWQiOiJvcmcubXVzaGFyZS5lYXN5amFwYW5lc2UiLCJzY29wZXMiOlsiZWFzeWphcGFuZXNlLmFkbWluIl0sImxvZ2luX3R5cGUiOiJtYWlsIn0.TR0A/fen5f+kg4APscFQ7JZtp2zNw5KMUeKBzm/GJg4WZp90ihg/OPfU9fcaJhoVNHWqxQ/OIHfAcXaXV+l8EEsTbh/+/Qs0glujX09Vm5z23wITzC36X+a/9bJJj5J1kXDtyx/CVdMmf8vm81T8PJFJuJtnyzA3IMRUGK0KecJ5MQjaOvh7NxZRhJfsCNYQz4V5hxvtI8urs+gi3/QVN04UQ5i0BX+DDdQ1E4MX8+3v2zDPc3ipQ8r9nZl00wmPdcqW5zx9Xooha6X8eTujgQuLiSFDheZGRR6/N+ZYktt/PMI49KIXVseenTTTzpX1Vmg9PAabPLJotdjcRpKtiA";
33+
Pluto.auth(token);
1634
}
1735

1836
}

0 commit comments

Comments
 (0)