[EDIT] MEJORA DE TOKEN

parent 5c563726
package web.multitask.trismegistoservices.api; package web.multitask.trismegistoservices.api;
import org.json.JSONObject; import org.json.JSONObject;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
...@@ -9,15 +8,13 @@ import web.multitask.trismegistoservices.model.User; ...@@ -9,15 +8,13 @@ import web.multitask.trismegistoservices.model.User;
import web.multitask.trismegistoservices.repository.UserRepository; import web.multitask.trismegistoservices.repository.UserRepository;
import web.multitask.trismegistoservices.singleton.TokenSingleton; import web.multitask.trismegistoservices.singleton.TokenSingleton;
import web.multitask.trismegistoservices.utils.JWTokenUtil; import web.multitask.trismegistoservices.utils.JWTokenUtil;
import java.math.BigInteger; import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
@RestController @RestController
@RequestMapping("/token") @RequestMapping("/token")
@CrossOrigin @CrossOrigin
...@@ -124,8 +121,14 @@ class JWTokenApi { ...@@ -124,8 +121,14 @@ class JWTokenApi {
public ResponseEntity<?> remainingTime(@RequestBody String token) { public ResponseEntity<?> remainingTime(@RequestBody String token) {
JSONObject json = new JSONObject(token); JSONObject json = new JSONObject(token);
try { try {
int remaining = jwtTokenUtil.getExperyTime(json.getString("token")); Long remaining = jwtTokenUtil.getExperyTime(json.getString("token"));
return ResponseEntity.ok(new JSONObject().put("remaining", remaining).put("message", "OK").put("status", true).toMap()); Date expirationDate = new Date(System.currentTimeMillis() + remaining);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return ResponseEntity.ok(new JSONObject()
.put("remaining", remaining)
.put("message", "OK")
.put("expiration", dateFormat.format(expirationDate))
.put("status", true).toMap());
} catch (Exception e) { } catch (Exception e) {
return ResponseEntity.status(401).body(new JSONObject().put("remaining", 0).put("message", "Invalid Token").put("status", false).toMap()); return ResponseEntity.status(401).body(new JSONObject().put("remaining", 0).put("message", "Invalid Token").put("status", false).toMap());
} }
...@@ -136,8 +139,8 @@ class JWTokenApi { ...@@ -136,8 +139,8 @@ class JWTokenApi {
JSONObject json = new JSONObject(data); JSONObject json = new JSONObject(data);
try { try {
boolean onelife = json.optBoolean("onelife", false); boolean onelife = json.optBoolean("onelife", false);
int ms = json.optInt("ms", 3600000); BigInteger ms = json.optBigInteger("ms", BigInteger.valueOf(3600000));
String tokenized = jwtTokenUtil.tokenizeData(data, BigInteger.valueOf(ms), onelife); String tokenized = jwtTokenUtil.tokenizeData(data, ms, onelife);
if(onelife){ if(onelife){
tokenSingleton.addToken(tokenized); tokenSingleton.addToken(tokenized);
} }
......
...@@ -25,26 +25,28 @@ public class JWTokenUtil implements Serializable{ ...@@ -25,26 +25,28 @@ public class JWTokenUtil implements Serializable{
TokenSingleton tokenSingleton = new TokenSingleton(); TokenSingleton tokenSingleton = new TokenSingleton();
public String generateToken(User user, BigInteger ms,boolean onelife) { public String generateToken(User user, BigInteger ms,boolean onelife) {
JSONObject json = new JSONObject();
json.put("username", user.getUsername());
if(ms == null){ if(ms == null){
ms = BigInteger.valueOf(3600000); ms = BigInteger.valueOf(3600000);
} }
Date now = new Date(); Date expirationDate = new Date(System.currentTimeMillis() + ms.longValue());
Date expiryDate = new Date(now.getTime() + ms.longValue());
JSONObject json = new JSONObject();
json.put("username", user.getUsername());
return Jwts.builder() return Jwts.builder()
.setSubject(json.toString()) .setSubject(json.toString())
.setIssuedAt(new Date()) .setIssuedAt(new Date())
.setExpiration(expiryDate) .setExpiration(expirationDate)
.signWith(Keys.hmacShaKeyFor(onelife ? jwtSecret2.getBytes() : jwtSecret.getBytes())) .signWith(Keys.hmacShaKeyFor(onelife ? jwtSecret2.getBytes() : jwtSecret.getBytes()))
.compact(); .compact();
} }
public String tokenizeData(String data, BigInteger ms,boolean onelife){ public String tokenizeData(String data, BigInteger ms,boolean onelife){
Date expirationDate = new Date(System.currentTimeMillis() + ms.longValue());
return Jwts.builder() return Jwts.builder()
.setSubject(data) .setSubject(data)
.setIssuedAt(new Date()) .setIssuedAt(new Date())
.setExpiration(new Date(new Date().getTime() + ms.longValue())) .setExpiration(expirationDate)
.signWith(Keys.hmacShaKeyFor(onelife ? jwtSecret2.getBytes() : jwtSecret.getBytes())) .signWith(Keys.hmacShaKeyFor(onelife ? jwtSecret2.getBytes() : jwtSecret.getBytes()))
.compact(); .compact();
} }
...@@ -133,8 +135,7 @@ public class JWTokenUtil implements Serializable{ ...@@ -133,8 +135,7 @@ public class JWTokenUtil implements Serializable{
.build() .build()
.parseClaimsJws(token) .parseClaimsJws(token)
.getBody() .getBody()
.getExpiration() .getExpiration();
.before(new Date());
return false; return false;
} catch (Exception e) { } catch (Exception e) {
try { try {
...@@ -143,8 +144,7 @@ public class JWTokenUtil implements Serializable{ ...@@ -143,8 +144,7 @@ public class JWTokenUtil implements Serializable{
.build() .build()
.parseClaimsJws(token) .parseClaimsJws(token)
.getBody() .getBody()
.getExpiration() .getExpiration();
.before(new Date());
return false; return false;
} catch (Exception e2) { } catch (Exception e2) {
System.out.println(e2.getMessage()); System.out.println(e2.getMessage());
...@@ -153,9 +153,9 @@ public class JWTokenUtil implements Serializable{ ...@@ -153,9 +153,9 @@ public class JWTokenUtil implements Serializable{
} }
} }
public int getExperyTime(String token){ public Long getExperyTime(String token){
try{ try{
return (int) ((Jwts.parserBuilder() return ((Jwts.parserBuilder()
.setSigningKey(Keys.hmacShaKeyFor(jwtSecret.getBytes())) .setSigningKey(Keys.hmacShaKeyFor(jwtSecret.getBytes()))
.build() .build()
.parseClaimsJws(token) .parseClaimsJws(token)
...@@ -163,18 +163,13 @@ public class JWTokenUtil implements Serializable{ ...@@ -163,18 +163,13 @@ public class JWTokenUtil implements Serializable{
.getExpiration() .getExpiration()
.getTime() - new Date().getTime())); .getTime() - new Date().getTime()));
}catch (Exception e){ }catch (Exception e){
try{ return ((Jwts.parserBuilder()
return (int) ((Jwts.parserBuilder() .setSigningKey(Keys.hmacShaKeyFor(jwtSecret2.getBytes()))
.setSigningKey(Keys.hmacShaKeyFor(jwtSecret2.getBytes())) .build()
.build() .parseClaimsJws(token)
.parseClaimsJws(token) .getBody()
.getBody() .getExpiration()
.getExpiration() .getTime() - new Date().getTime()));
.getTime() - new Date().getTime()));
}catch (Exception e2){
System.out.println(e2.getMessage());
return 0;
}
} }
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment