15
15
*************************************************************************************/
16
16
package org .spin .service .grpc .authentication ;
17
17
18
- import java .security .Key ;
19
18
import java .sql .PreparedStatement ;
20
19
import java .sql .ResultSet ;
21
20
import java .sql .SQLException ;
22
21
import java .sql .Timestamp ;
22
+ import java .util .Base64 ;
23
23
import java .util .Collections ;
24
24
import java .util .Date ;
25
25
import java .util .Hashtable ;
28
28
import java .util .Properties ;
29
29
import java .util .logging .Level ;
30
30
31
+ import javax .crypto .SecretKey ;
32
+
31
33
import org .adempiere .core .domains .models .I_AD_Language ;
32
34
import org .adempiere .core .domains .models .I_AD_Session ;
33
35
import org .adempiere .core .domains .models .I_AD_User_Authentication ;
64
66
65
67
import io .jsonwebtoken .Claims ;
66
68
import io .jsonwebtoken .Jws ;
69
+ import io .jsonwebtoken .JwtBuilder ;
67
70
import io .jsonwebtoken .JwtParser ;
68
71
import io .jsonwebtoken .Jwts ;
69
- import io .jsonwebtoken .SignatureAlgorithm ;
70
- import io .jsonwebtoken .io .Decoders ;
71
72
import io .jsonwebtoken .security .Keys ;
72
73
73
74
/**
@@ -151,15 +152,15 @@ public static String getDefaultLanguage(String language) {
151
152
return defaultLanguage ;
152
153
}
153
154
154
- public static void loadValuesWithClaims (Claims claimsBody ) {
155
- if (claimsBody == null || claimsBody .isEmpty ()) {
155
+ public static void loadValuesWithClaims (Claims claimsPayload ) {
156
+ if (claimsPayload == null || claimsPayload .isEmpty ()) {
156
157
throw new AdempiereException ("Claims.Body @NotFound@" );
157
158
}
158
- SessionManager .userId = claimsBody .get ("AD_User_ID" , Integer .class );
159
- SessionManager .roleId = claimsBody .get ("AD_Role_ID" , Integer .class );
160
- SessionManager .organizationId = claimsBody .get ("AD_Org_ID" , Integer .class );
161
- SessionManager .warehouseId = claimsBody .get ("M_Warehouse_ID" , Integer .class );
162
- SessionManager .language = claimsBody .get ("AD_Language" , String .class );
159
+ SessionManager .userId = claimsPayload .get ("AD_User_ID" , Integer .class );
160
+ SessionManager .roleId = claimsPayload .get ("AD_Role_ID" , Integer .class );
161
+ SessionManager .organizationId = claimsPayload .get ("AD_Org_ID" , Integer .class );
162
+ SessionManager .warehouseId = claimsPayload .get ("M_Warehouse_ID" , Integer .class );
163
+ SessionManager .language = claimsPayload .get ("AD_Language" , String .class );
163
164
}
164
165
165
166
public static void loadValuesWithMADToken (MADToken token ) {
@@ -226,16 +227,20 @@ public static Properties getSessionFromToken(String tokenValue) {
226
227
boolean isNewSession = false ;
227
228
int sessionId = getSessionIdByOpenID (tokenValue );
228
229
if (sessionId <= 0 ) {
230
+ SecretKey secretKey = getJWT_SecretKey ();
229
231
// Validate if is token based
230
- JwtParser parser = Jwts .parserBuilder ().setSigningKey (
231
- getJWT_SecretKey ()
232
- ).build ();
233
- Jws <Claims > claims = parser .parseClaimsJws (tokenValue );
232
+ JwtParser parser = Jwts .parser ()
233
+ .verifyWith (secretKey )
234
+ .build ()
235
+ ;
236
+ Jws <Claims > claims = parser .parseSignedClaims (tokenValue );
234
237
sessionId = NumberManager .getIntFromString (
235
- claims .getBody ().getId ()
238
+ claims .getPayload ().getId ()
236
239
);
237
240
if (sessionId > 0 ) {
238
- loadValuesWithClaims (claims .getBody ());
241
+ loadValuesWithClaims (
242
+ claims .getPayload ()
243
+ );
239
244
} else {
240
245
MADToken token = createSessionFromToken (tokenValue );
241
246
if (Optional .ofNullable (token ).isPresent ()) {
@@ -389,7 +394,7 @@ public static String getOpenIDToken(MSession session) {
389
394
* Get JWT Secrect Key generates with HMAC-SHA algorithms
390
395
* @return
391
396
*/
392
- private static String getJWT_SecretKey () {
397
+ private static String getJWT_SecretKeyAsString () {
393
398
// get by SysConfig client
394
399
String secretKey = MSysConfig .getValue (
395
400
JWTUtil .ECA52_JWT_SECRET_KEY ,
@@ -410,6 +415,14 @@ private static String getJWT_SecretKey() {
410
415
}
411
416
return secretKey ;
412
417
}
418
+ private static SecretKey getJWT_SecretKey () {
419
+ byte [] keyBytes = Base64 .getDecoder ().decode (
420
+ getJWT_SecretKeyAsString ()
421
+ );
422
+ SecretKey secretKey = Keys .hmacShaKeyFor (keyBytes );
423
+ return secretKey ;
424
+ }
425
+
413
426
414
427
/**
415
428
* Create token as bearer
@@ -422,27 +435,28 @@ private static String createAndGetBearerToken(MSession session, int warehouseId,
422
435
MUser user = MUser .get (session .getCtx (), session .getCreatedBy ());
423
436
long sessionTimeout = getSessionTimeout (user );
424
437
425
- byte [] keyBytes = Decoders .BASE64 .decode (
426
- getJWT_SecretKey ()
427
- );
428
- Key key = Keys .hmacShaKeyFor (keyBytes );
429
- return Jwts .builder ()
430
- .setId (String .valueOf (session .getAD_Session_ID ()))
438
+ SecretKey secretKey = getJWT_SecretKey ();
439
+ JwtBuilder jwtBuilder = Jwts .builder ()
440
+ .id (String .valueOf (session .getAD_Session_ID ()))
441
+ // .claims()
431
442
.claim ("AD_Client_ID" , session .getAD_Client_ID ())
432
443
.claim ("AD_Org_ID" , session .getAD_Org_ID ())
433
444
.claim ("AD_Role_ID" , session .getAD_Role_ID ())
434
445
.claim ("AD_User_ID" , session .getCreatedBy ())
435
446
.claim ("M_Warehouse_ID" , warehouseId )
436
447
.claim ("AD_Language" , language )
437
- .setIssuedAt (
438
- new Date (System . currentTimeMillis () )
448
+ .issuedAt (
449
+ new Date ()
439
450
)
440
- .setExpiration (
441
- new Date (System .currentTimeMillis () + sessionTimeout )
451
+ .expiration (
452
+ new Date (
453
+ System .currentTimeMillis () + sessionTimeout
454
+ )
442
455
)
443
- .signWith (key , SignatureAlgorithm .HS256 )
444
- .compact ()
456
+ .signWith (secretKey , Jwts .SIG .HS256 )
445
457
;
458
+
459
+ return jwtBuilder .compact ();
446
460
}
447
461
448
462
/**
0 commit comments