Skip to content

Commit

Permalink
implemented generating jwt
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalii committed Jul 29, 2020
1 parent 49c8a71 commit b5e1cd0
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 1 deletion.
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());
}
}
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;
}
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;
}
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();
}
}
7 changes: 6 additions & 1 deletion packages/server-java/app/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ graphiql:
enabled: true
endpoint:
subscriptions: /graphql
mapping: /graphiql
mapping: /graphiql

jwt:
secret: "676918AB4D29BFE59CCB943F3C09F5CC8FB3A8511E23E502B67DE95AB9A9D00C"
access-token-expiration-in-sec: 3600
refresh-token-expiration-in-sec: 604800

0 comments on commit b5e1cd0

Please sign in to comment.