44import org .mushare .pluto .exception .PlutoErrorCode ;
55import org .mushare .pluto .exception .PlutoException ;
66
7+ import java .io .UnsupportedEncodingException ;
78import java .security .Signature ;
89import 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}
0 commit comments