33
33
import java .security .NoSuchAlgorithmException ;
34
34
import java .security .spec .InvalidKeySpecException ;
35
35
import java .security .spec .X509EncodedKeySpec ;
36
+ import java .util .Objects ;
36
37
import java .util .Set ;
37
38
38
39
import io .jsonwebtoken .Claims ;
@@ -51,41 +52,8 @@ public static void authTokenByPublicKey(AclProperties aclProperties) {
51
52
throw new AclException ("group:" + aclProperties .getExtendedField ("group " ) + " has no auth to access the topic:"
52
53
+ aclProperties .getTopic ());
53
54
}
54
- String publicKeyUrl = null ;
55
- token = token .replace ("Bearer " , "" );
56
- for (String key : ConfigurationContextUtil .KEYS ) {
57
- CommonConfiguration commonConfiguration = ConfigurationContextUtil .get (key );
58
- if (commonConfiguration == null ) {
59
- continue ;
60
- }
61
- if (StringUtils .isBlank (commonConfiguration .getEventMeshSecurityPublickey ())) {
62
- throw new AclException ("publicKeyUrl cannot be null" );
63
- }
64
- publicKeyUrl = commonConfiguration .getEventMeshSecurityPublickey ();
65
- }
66
- byte [] validationKeyBytes = new byte [0 ];
67
- try {
68
- validationKeyBytes = Files .readAllBytes (Paths .get (publicKeyUrl ));
69
- X509EncodedKeySpec spec = new X509EncodedKeySpec (validationKeyBytes );
70
- KeyFactory kf = KeyFactory .getInstance ("RSA" );
71
- Key validationKey = kf .generatePublic (spec );
72
- JwtParser signedParser = Jwts .parserBuilder ().setSigningKey (validationKey ).build ();
73
- Jwt <?, Claims > signJwt = signedParser .parseClaimsJws (token );
74
- String sub = signJwt .getBody ().get ("sub" , String .class );
75
- if (!sub .contains (aclProperties .getExtendedField ("group" ).toString ()) && !sub .contains ("pulsar-admin" )) {
76
- throw new AclException ("group:" + aclProperties .getExtendedField ("group " ) + " has no auth to access eventMesh:"
77
- + aclProperties .getTopic ());
78
- }
79
- } catch (IOException e ) {
80
- throw new AclException ("public key read error!" , e );
81
- } catch (NoSuchAlgorithmException e ) {
82
- throw new AclException ("no such RSA algorithm!" , e );
83
- } catch (InvalidKeySpecException e ) {
84
- throw new AclException ("invalid public key spec!" , e );
85
- } catch (JwtException e ) {
86
- throw new AclException ("invalid token!" , e );
87
- }
88
-
55
+ String publicKeyUrl = getPublicKeyUrl ();
56
+ validateToken (token , publicKeyUrl , aclProperties );
89
57
} else {
90
58
throw new AclException ("invalid token!" );
91
59
}
@@ -94,40 +62,7 @@ public static void authTokenByPublicKey(AclProperties aclProperties) {
94
62
public static void helloTaskAuthTokenByPublicKey (AclProperties aclProperties ) {
95
63
String token = aclProperties .getToken ();
96
64
if (StringUtils .isNotBlank (token )) {
97
- String publicKeyUrl = null ;
98
- token = token .replace ("Bearer " , "" );
99
- for (String key : ConfigurationContextUtil .KEYS ) {
100
- CommonConfiguration commonConfiguration = ConfigurationContextUtil .get (key );
101
- if (commonConfiguration == null ) {
102
- continue ;
103
- }
104
- if (StringUtils .isBlank (commonConfiguration .getEventMeshSecurityPublickey ())) {
105
- throw new AclException ("publicKeyUrl cannot be null" );
106
- }
107
- publicKeyUrl = commonConfiguration .getEventMeshSecurityPublickey ();
108
- }
109
- byte [] validationKeyBytes = new byte [0 ];
110
- try {
111
- validationKeyBytes = Files .readAllBytes (Paths .get (publicKeyUrl ));
112
- X509EncodedKeySpec spec = new X509EncodedKeySpec (validationKeyBytes );
113
- KeyFactory kf = KeyFactory .getInstance ("RSA" );
114
- Key validationKey = kf .generatePublic (spec );
115
- JwtParser signedParser = Jwts .parserBuilder ().setSigningKey (validationKey ).build ();
116
- Jwt <?, Claims > signJwt = signedParser .parseClaimsJws (token );
117
- String sub = signJwt .getBody ().get ("sub" , String .class );
118
- if (!sub .contains (aclProperties .getExtendedField ("group" ).toString ()) && !sub .contains ("pulsar-admin" )) {
119
- throw new AclException ("group:" + aclProperties .getExtendedField ("group " ) + " has no auth to access eventMesh:"
120
- + aclProperties .getTopic ());
121
- }
122
- } catch (IOException e ) {
123
- throw new AclException ("public key read error!" , e );
124
- } catch (NoSuchAlgorithmException e ) {
125
- throw new AclException ("no such RSA algorithm!" , e );
126
- } catch (InvalidKeySpecException e ) {
127
- throw new AclException ("invalid public key spec!" , e );
128
- } catch (JwtException e ) {
129
- throw new AclException ("invalid token!" , e );
130
- }
65
+ validateToken (token , getPublicKeyUrl (), aclProperties );
131
66
} else {
132
67
throw new AclException ("invalid token!" );
133
68
}
@@ -148,4 +83,45 @@ public static boolean authAccess(AclProperties aclProperties) {
148
83
return groupTopics .contains (topic );
149
84
}
150
85
86
+ private static String getPublicKeyUrl () {
87
+ String publicKeyUrl = null ;
88
+ for (String key : ConfigurationContextUtil .KEYS ) {
89
+ CommonConfiguration commonConfiguration = ConfigurationContextUtil .get (key );
90
+ if (null == commonConfiguration ) {
91
+ continue ;
92
+ }
93
+ if (StringUtils .isBlank (commonConfiguration .getEventMeshSecurityPublickey ())) {
94
+ throw new AclException ("publicKeyUrl cannot be null" );
95
+ }
96
+ publicKeyUrl = commonConfiguration .getEventMeshSecurityPublickey ();
97
+ }
98
+ return publicKeyUrl ;
99
+ }
100
+
101
+ private static void validateToken (String token , String publicKeyUrl , AclProperties aclProperties ) {
102
+ String sub ;
103
+ token = token .replace ("Bearer " , "" );
104
+ byte [] validationKeyBytes ;
105
+ try {
106
+ validationKeyBytes = Files .readAllBytes (Paths .get (Objects .requireNonNull (publicKeyUrl )));
107
+ X509EncodedKeySpec spec = new X509EncodedKeySpec (validationKeyBytes );
108
+ KeyFactory kf = KeyFactory .getInstance ("RSA" );
109
+ Key validationKey = kf .generatePublic (spec );
110
+ JwtParser signedParser = Jwts .parserBuilder ().setSigningKey (validationKey ).build ();
111
+ Jwt <?, Claims > signJwt = signedParser .parseClaimsJws (token );
112
+ sub = signJwt .getBody ().get ("sub" , String .class );
113
+ if (!sub .contains (aclProperties .getExtendedField ("group" ).toString ()) && !sub .contains ("pulsar-admin" )) {
114
+ throw new AclException ("group:" + aclProperties .getExtendedField ("group " ) + " has no auth to access eventMesh:"
115
+ + aclProperties .getTopic ());
116
+ }
117
+ } catch (IOException e ) {
118
+ throw new AclException ("public key read error!" , e );
119
+ } catch (NoSuchAlgorithmException e ) {
120
+ throw new AclException ("no such RSA algorithm!" , e );
121
+ } catch (InvalidKeySpecException e ) {
122
+ throw new AclException ("invalid public key spec!" , e );
123
+ } catch (JwtException e ) {
124
+ throw new AclException ("invalid token!" , e );
125
+ }
126
+ }
151
127
}
0 commit comments