[EDIT] MEJORA DE TOKEN

parent 5c563726
package web.multitask.trismegistoservices.api;
import org.json.JSONObject;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.userdetails.UserDetails;
......@@ -9,15 +8,13 @@ import web.multitask.trismegistoservices.model.User;
import web.multitask.trismegistoservices.repository.UserRepository;
import web.multitask.trismegistoservices.singleton.TokenSingleton;
import web.multitask.trismegistoservices.utils.JWTokenUtil;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@RestController
@RequestMapping("/token")
@CrossOrigin
......@@ -124,8 +121,14 @@ class JWTokenApi {
public ResponseEntity<?> remainingTime(@RequestBody String token) {
JSONObject json = new JSONObject(token);
try {
int remaining = jwtTokenUtil.getExperyTime(json.getString("token"));
return ResponseEntity.ok(new JSONObject().put("remaining", remaining).put("message", "OK").put("status", true).toMap());
Long remaining = jwtTokenUtil.getExperyTime(json.getString("token"));
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) {
return ResponseEntity.status(401).body(new JSONObject().put("remaining", 0).put("message", "Invalid Token").put("status", false).toMap());
}
......@@ -136,8 +139,8 @@ class JWTokenApi {
JSONObject json = new JSONObject(data);
try {
boolean onelife = json.optBoolean("onelife", false);
int ms = json.optInt("ms", 3600000);
String tokenized = jwtTokenUtil.tokenizeData(data, BigInteger.valueOf(ms), onelife);
BigInteger ms = json.optBigInteger("ms", BigInteger.valueOf(3600000));
String tokenized = jwtTokenUtil.tokenizeData(data, ms, onelife);
if(onelife){
tokenSingleton.addToken(tokenized);
}
......
......@@ -25,26 +25,28 @@ public class JWTokenUtil implements Serializable{
TokenSingleton tokenSingleton = new TokenSingleton();
public String generateToken(User user, BigInteger ms,boolean onelife) {
JSONObject json = new JSONObject();
json.put("username", user.getUsername());
if(ms == null){
ms = BigInteger.valueOf(3600000);
}
Date now = new Date();
Date expiryDate = new Date(now.getTime() + ms.longValue());
JSONObject json = new JSONObject();
json.put("username", user.getUsername());
Date expirationDate = new Date(System.currentTimeMillis() + ms.longValue());
return Jwts.builder()
.setSubject(json.toString())
.setIssuedAt(new Date())
.setExpiration(expiryDate)
.setExpiration(expirationDate)
.signWith(Keys.hmacShaKeyFor(onelife ? jwtSecret2.getBytes() : jwtSecret.getBytes()))
.compact();
}
public String tokenizeData(String data, BigInteger ms,boolean onelife){
Date expirationDate = new Date(System.currentTimeMillis() + ms.longValue());
return Jwts.builder()
.setSubject(data)
.setIssuedAt(new Date())
.setExpiration(new Date(new Date().getTime() + ms.longValue()))
.setExpiration(expirationDate)
.signWith(Keys.hmacShaKeyFor(onelife ? jwtSecret2.getBytes() : jwtSecret.getBytes()))
.compact();
}
......@@ -133,8 +135,7 @@ public class JWTokenUtil implements Serializable{
.build()
.parseClaimsJws(token)
.getBody()
.getExpiration()
.before(new Date());
.getExpiration();
return false;
} catch (Exception e) {
try {
......@@ -143,8 +144,7 @@ public class JWTokenUtil implements Serializable{
.build()
.parseClaimsJws(token)
.getBody()
.getExpiration()
.before(new Date());
.getExpiration();
return false;
} catch (Exception e2) {
System.out.println(e2.getMessage());
......@@ -153,9 +153,9 @@ public class JWTokenUtil implements Serializable{
}
}
public int getExperyTime(String token){
public Long getExperyTime(String token){
try{
return (int) ((Jwts.parserBuilder()
return ((Jwts.parserBuilder()
.setSigningKey(Keys.hmacShaKeyFor(jwtSecret.getBytes()))
.build()
.parseClaimsJws(token)
......@@ -163,18 +163,13 @@ public class JWTokenUtil implements Serializable{
.getExpiration()
.getTime() - new Date().getTime()));
}catch (Exception e){
try{
return (int) ((Jwts.parserBuilder()
.setSigningKey(Keys.hmacShaKeyFor(jwtSecret2.getBytes()))
.build()
.parseClaimsJws(token)
.getBody()
.getExpiration()
.getTime() - new Date().getTime()));
}catch (Exception e2){
System.out.println(e2.getMessage());
return 0;
}
return ((Jwts.parserBuilder()
.setSigningKey(Keys.hmacShaKeyFor(jwtSecret2.getBytes()))
.build()
.parseClaimsJws(token)
.getBody()
.getExpiration()
.getTime() - new Date().getTime()));
}
}
}
\ 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