-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SMART Authentication ready for testing on connectathon
- Loading branch information
Showing
5 changed files
with
192 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,68 @@ | ||
package org.immregistries.iis.kernal; | ||
|
||
import io.jsonwebtoken.*; | ||
import io.jsonwebtoken.security.MacAlgorithm; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.crypto.SecretKey; | ||
import javax.crypto.spec.SecretKeySpec; | ||
import javax.xml.bind.DatatypeConverter; | ||
import java.util.Date; | ||
|
||
@Component | ||
public class JwtUtils { | ||
private static final Logger logger = LoggerFactory.getLogger(JwtUtils.class); | ||
@Value("${iis.api.app.jwtSecret}") | ||
private String jwtSecret; | ||
@Value("${iis.api.app.jwtExpirationMs}") | ||
private int jwtExpirationMs; | ||
public String generateJwtToken(Authentication authentication) { | ||
UserDetails userPrincipal = (UserDetails) authentication.getPrincipal(); | ||
return Jwts.builder() | ||
.setSubject((userPrincipal.getUsername())) | ||
.setIssuedAt(new Date()) | ||
.setExpiration(new Date((new Date()).getTime() + jwtExpirationMs)) | ||
.signWith(SignatureAlgorithm.HS512, jwtSecret) | ||
.compact(); | ||
} | ||
public String getUserNameFromJwtToken(String token) { | ||
return Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(token).getBody().getSubject(); | ||
} | ||
public boolean validateJwtToken(String authToken) { | ||
try { | ||
Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(authToken); | ||
return true; | ||
} catch (SignatureException e) { | ||
logger.error("Invalid JWT signature: {}", e.getMessage()); | ||
} catch (MalformedJwtException e) { | ||
logger.error("Invalid JWT token: {}", e.getMessage()); | ||
} catch (ExpiredJwtException e) { | ||
logger.error("JWT token is expired: {}", e.getMessage()); | ||
} catch (UnsupportedJwtException e) { | ||
logger.error("JWT token is unsupported: {}", e.getMessage()); | ||
} catch (IllegalArgumentException e) { | ||
logger.error("JWT claims string is empty: {}", e.getMessage()); | ||
} | ||
return false; | ||
} | ||
private static final Logger logger = LoggerFactory.getLogger(JwtUtils.class); | ||
@Value("${iis.api.app.jwtSecret}") | ||
private String jwtSecret; | ||
@Value("${iis.api.app.jwtExpirationMs}") | ||
private int jwtExpirationMs; | ||
|
||
private final static MacAlgorithm SIGNATURE_ALGORITHM = Jwts.SIG.HS512; | ||
private final static String SIGNATURE_ALGORITHM_NAME = "HmacSha512"; | ||
|
||
private SecretKey key; | ||
|
||
public String generateJwtToken(Authentication authentication) { | ||
init(); | ||
return Jwts.builder().id(authentication.getName()) | ||
.subject(authentication.getName()) | ||
.issuedAt(new Date()) | ||
.expiration(new Date(System.currentTimeMillis() + jwtExpirationMs)) | ||
.signWith(key, SIGNATURE_ALGORITHM) | ||
.compact(); | ||
} | ||
|
||
public String getUserNameFromJwtToken(String token) { | ||
init(); | ||
return Jwts.parser().verifyWith(key).build().parseSignedClaims(token).getPayload().getSubject(); | ||
} | ||
|
||
public boolean validateJwtToken(String authToken) { | ||
init(); | ||
try { | ||
Jwts.parser().verifyWith(key).build().parseSignedClaims(authToken); | ||
return true; | ||
} catch (MalformedJwtException e) { | ||
logger.error("Invalid JWT token: {}", e.getMessage()); | ||
} catch (ExpiredJwtException e) { | ||
logger.error("JWT token is expired: {}", e.getMessage()); | ||
} catch (UnsupportedJwtException e) { | ||
logger.error("JWT token is unsupported: {}", e.getMessage()); | ||
} catch (IllegalArgumentException e) { | ||
logger.error("JWT claims string is empty: {}", e.getMessage()); | ||
} catch (JwtException e) { | ||
logger.error("Invalid JWT signature: {}", e.getMessage()); | ||
} | ||
return false; | ||
} | ||
|
||
private void init() { | ||
if (key == null) { | ||
this.key = new SecretKeySpec(DatatypeConverter.parseBase64Binary(jwtSecret), SIGNATURE_ALGORITHM_NAME); | ||
} | ||
} | ||
} |
Oops, something went wrong.