1
1
package org .mushare .pluto ;
2
2
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 ;
3
8
import com .github .kevinsawicki .http .HttpRequest ;
4
9
import net .sf .json .JSONArray ;
5
10
import net .sf .json .JSONObject ;
6
11
import org .mushare .pluto .exception .PlutoErrorCode ;
7
12
import org .mushare .pluto .exception .PlutoException ;
8
13
9
14
import java .io .UnsupportedEncodingException ;
15
+ import java .nio .charset .StandardCharsets ;
10
16
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 ;
14
20
import java .util .stream .Collectors ;
15
21
import java .util .stream .IntStream ;
16
22
@@ -33,64 +39,29 @@ public static void setup(String server, String appId) {
33
39
}
34
40
35
41
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 ());
77
47
}
78
48
79
49
public static List <PlutoUserInfo > fetUserInfos (List <Long > userIds ) {
80
50
if (userIds == null || userIds .size () == 0 ) {
81
51
return new ArrayList <>();
82
52
}
83
53
String ids = userIds .stream ()
84
- .map (userId -> userId + "-" )
54
+ .map (userId -> "ids=" + userId )
85
55
.reduce ("" , (s1 , s2 ) -> {
86
- return s1 + s2 ;
56
+ return s1 + "&" + s2 ;
87
57
});
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 );
94
65
PlutoUserInfo info = new PlutoUserInfo ();
95
66
info .setUserId (object .getLong ("id" ));
96
67
if (object .containsKey ("err_code" ) && object .getInt ("err_code" ) == 403 ) {
0 commit comments