Skip to content

Commit e88b270

Browse files
committed
Implement auth method
1 parent e81358f commit e88b270

5 files changed

Lines changed: 107 additions & 34 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.mushare.pluto;
2+
3+
public enum LoginType {
4+
mail,
5+
google,
6+
apple;
7+
8+
public static LoginType fromIdentifier(String identifier) {
9+
switch (identifier) {
10+
case "mail":
11+
return mail;
12+
case "google":
13+
return google;
14+
case "apple":
15+
return apple;
16+
default:
17+
return null;
18+
}
19+
}
20+
}
Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package org.mushare.pluto;
22

3+
import net.sf.json.JSONObject;
4+
import org.mushare.pluto.exception.PlutoErrorCode;
5+
import org.mushare.pluto.exception.PlutoException;
6+
7+
import java.security.Signature;
8+
import java.util.Base64;
9+
310
public class Pluto {
411

512
private PublicKeyManager keyManager;
@@ -9,35 +16,44 @@ private Pluto() {
916
super();
1017
}
1118

12-
public PublicKeyManager getKeyManager() {
13-
return keyManager;
14-
}
15-
16-
public void setKeyManager(PublicKeyManager keyManager) {
17-
this.keyManager = keyManager;
18-
}
19-
20-
public String getAppId() {
21-
return appId;
22-
}
23-
24-
public void setAppId(String appId) {
25-
this.appId = appId;
26-
}
27-
2819
private static Pluto shared = new Pluto();
2920

3021
public static void setup(String server, String appId) {
31-
shared.setKeyManager(new PublicKeyManager(server));
32-
shared.setAppId(appId);
33-
}
34-
35-
public static void auth(String token) {
36-
37-
}
38-
39-
public static void auth(String token, String scope) {
40-
22+
shared.keyManager = new PublicKeyManager(server);
23+
shared.appId = appId;
24+
}
25+
26+
public static PlutoUser auth(String token) throws PlutoException {
27+
if (token == null) {
28+
throw new PlutoException(PlutoErrorCode.jwtFormatError);
29+
}
30+
String[] parts = token.split("\\.");
31+
if (parts.length != 3) {
32+
throw new PlutoException(PlutoErrorCode.jwtFormatError);
33+
}
34+
35+
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+
;
41+
}
42+
Long expire = payload.getLong("expire_time") * 1000;
43+
if (expire < System.currentTimeMillis()) {
44+
throw new PlutoException(PlutoErrorCode.expired);
45+
}
46+
Signature signature = Signature.getInstance("SHA256withRSA");
47+
signature.initVerify(shared.keyManager.getPublicKey());
48+
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);
53+
} catch (Exception e) {
54+
e.printStackTrace();
55+
throw new PlutoException(PlutoErrorCode.other);
56+
}
4157
}
4258

4359
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.mushare.pluto;
2+
3+
import net.sf.json.JSONArray;
4+
import net.sf.json.JSONObject;
5+
6+
public class PlutoUser {
7+
private long userId;
8+
private String deviceId;
9+
private String [] scopes;
10+
private LoginType loginType;
11+
12+
public long getUserId() {
13+
return userId;
14+
}
15+
16+
public String getDeviceId() {
17+
return deviceId;
18+
}
19+
20+
public LoginType getLoginType() {
21+
return loginType;
22+
}
23+
24+
public PlutoUser(JSONObject payload) {
25+
userId = payload.getLong("userId");
26+
deviceId = payload.getString("deviceId");
27+
JSONArray scopeArray = payload.getJSONArray("scopes");
28+
scopes = new String[scopeArray.size()];
29+
for (int i = 0; i < scopeArray.size(); i++) {
30+
scopes[i] = scopeArray.getString(i);
31+
}
32+
loginType = LoginType.fromIdentifier(payload.getString("login_type"));
33+
}
34+
}

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

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

33
public enum PlutoErrorCode {
4+
jwtFormatError,
45
expired,
56
appIdError,
6-
unauthorized,
7-
signatureError;
7+
notVerified,
8+
other;
89

910
@Override
1011
public String toString() {
1112
switch (this) {
13+
case jwtFormatError:
14+
return "JWT must be like `header.payload.sign`";
1215
case expired:
1316
return "JWT token is expired.";
1417
case appIdError:
1518
return "App id is not compatiable.";
16-
case unauthorized:
17-
return "Unauthorized user, make sure the user contains the scopes.";
18-
case signatureError:
19-
return "Cannot verify signature";
19+
case notVerified:
20+
return "Verify failed, cannot verify signature";
21+
case other:
22+
return "Unhandled exception: InvalidKeyException, IOException, NoSuchAlgorithmException, SignatureException";
2023
default:
2124
return "Unknown pluto error";
2225
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ public class PlutoException extends RuntimeException {
44

55
private final PlutoError error;
66

7-
public PlutoException(PlutoError error) {
7+
public PlutoException(PlutoErrorCode code) {
88
super();
9-
this.error = error;
9+
this.error = new PlutoError(code);
1010
}
1111

1212
}

0 commit comments

Comments
 (0)