-
-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
vitalii
committed
Jul 29, 2020
1 parent
49c8a71
commit b5e1cd0
Showing
5 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
...thentication/server-java/src/main/java/com/sysgears/authentication/config/AuthConfig.java
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.sysgears.authentication.config; | ||
|
||
import io.jsonwebtoken.security.Keys; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.security.Key; | ||
|
||
@Configuration | ||
public class AuthConfig { | ||
@Bean | ||
public Key jwtSecretKey(JwtConfig config) { | ||
return Keys.hmacShaKeyFor(config.getSecret().getBytes()); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...uthentication/server-java/src/main/java/com/sysgears/authentication/config/JwtConfig.java
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.sysgears.authentication.config; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Getter | ||
@Setter | ||
@Configuration | ||
@ConfigurationProperties("jwt") | ||
public class JwtConfig { | ||
private String secret; | ||
private long accessTokenExpirationInSec; | ||
private long refreshTokenExpirationInSec; | ||
} |
15 changes: 15 additions & 0 deletions
15
...tion/server-java/src/main/java/com/sysgears/authentication/model/jwt/JwtUserIdentity.java
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.sysgears.authentication.model.jwt; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class JwtUserIdentity { | ||
private final int id; | ||
private final String username; | ||
private final String passwordHash; | ||
private final String role; | ||
private final Boolean isActive; | ||
private final String email; | ||
private final String firstName; | ||
private final String lastName; | ||
} |
60 changes: 60 additions & 0 deletions
60
...ation/server-java/src/main/java/com/sysgears/authentication/service/jwt/JWTGenerator.java
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.sysgears.authentication.service.jwt; | ||
|
||
import com.sysgears.authentication.config.JwtConfig; | ||
import com.sysgears.authentication.model.jwt.JwtUserIdentity; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.security.Key; | ||
import java.time.Instant; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.Date; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class JWTGenerator { | ||
private final Key secretKey; | ||
private final JwtConfig jwtConfig; | ||
|
||
//todo: need to separate generation and parsing JWT. | ||
public String generateToken(JwtUserIdentity identity) { | ||
Map<String, Object> claims = new HashMap<>(); | ||
claims.put("identity", identity); | ||
log.debug("Generating new access JWT for user {}", identity.getId()); | ||
return Jwts.builder() | ||
.setClaims(claims) | ||
.setIssuedAt(Date.from(Instant.now())) | ||
.setExpiration(Date.from(Instant.now().plus(jwtConfig.getAccessTokenExpirationInSec(), ChronoUnit.SECONDS))) | ||
.setHeaderParam("typ", "JWT") | ||
.signWith(secretKey, SignatureAlgorithm.HS256) | ||
.compact(); | ||
} | ||
|
||
public String generateRefreshToken(JwtUserIdentity identity) { | ||
Map<String, Object> claims = new HashMap<>(); | ||
claims.put("id", identity.getId()); | ||
log.debug("Generating new refresh JWT for user {}", identity.getId()); | ||
return Jwts.builder() | ||
.setClaims(claims) | ||
.setIssuedAt(Date.from(Instant.now())) | ||
.setExpiration(Date.from(Instant.now().plus(jwtConfig.getRefreshTokenExpirationInSec(), ChronoUnit.SECONDS))) | ||
.setHeaderParam("typ", "JWT") | ||
.signWith(secretKey, SignatureAlgorithm.HS256) | ||
.compact(); | ||
} | ||
|
||
|
||
public String getAllClaimsFromToken(String token) { | ||
return Jwts.parserBuilder() | ||
.setSigningKey(secretKey) | ||
.build() | ||
.parseClaimsJws(token) | ||
.toString(); | ||
} | ||
} |
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