Skip to content

Commit

Permalink
AYS-165 | Code Refactor and Optimization (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
agitrubard authored Mar 9, 2024
1 parent 8a4dcc1 commit 08a3e46
Show file tree
Hide file tree
Showing 21 changed files with 78 additions and 341 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.validation.Valid;
import jakarta.validation.constraints.AssertTrue;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections4.CollectionUtils;
Expand All @@ -21,6 +22,7 @@
*/
@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
public class AdminUserRegisterApplicationListRequest extends AysPagingRequest implements AysFilteringRequest {

@Valid
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class AysAdminUserRegisterApplicationSummaryNotExistByIdException extends
* Unique identifier for serialization.
*/
@Serial
private static final long serialVersionUID = 8416712253227498925L;
private static final long serialVersionUID = 1262328240991745084L;

/**
* Constructs a new AysAdminUserRegisterApplicationSummaryNotExistByIdException with the specified ID.
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/org/ays/auth/config/SecurityConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class SecurityConfiguration {
* @return the new instance of {@link SessionAuthenticationStrategy}
*/
@Bean
protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
SessionAuthenticationStrategy sessionAuthenticationStrategy() {
return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
}

Expand All @@ -64,9 +64,9 @@ protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
* @throws Exception if there is an error setting up the filter chain
*/
@Bean
public SecurityFilterChain filterChain(HttpSecurity httpSecurity,
AysBearerTokenAuthenticationFilter bearerTokenAuthenticationFilter,
CustomAuthenticationEntryPoint customAuthenticationEntryPoint)
SecurityFilterChain filterChain(HttpSecurity httpSecurity,
AysBearerTokenAuthenticationFilter bearerTokenAuthenticationFilter,
CustomAuthenticationEntryPoint customAuthenticationEntryPoint)
throws Exception {

httpSecurity
Expand Down Expand Up @@ -109,7 +109,8 @@ private CorsConfigurationSource corsConfigurationSource() {
* @return the new instance of {@link PasswordEncoder}
*/
@Bean
public PasswordEncoder passwordEncoder() {
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

}
65 changes: 38 additions & 27 deletions src/main/java/org/ays/auth/service/impl/AysTokenServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,34 +48,26 @@ class AysTokenServiceImpl implements AysTokenService {
*/
@Override
public AysToken generate(final Map<String, Object> claims) {

final long currentTimeMillis = System.currentTimeMillis();

final Date tokenIssuedAt = new Date(currentTimeMillis);
final JwtBuilder tokenBuilder = this.initializeTokenBuilder(currentTimeMillis);

final Date accessTokenExpiresAt = DateUtils.addMinutes(new Date(currentTimeMillis), tokenConfiguration.getAccessTokenExpireMinute());
final String accessToken = Jwts.builder()
.header()
.add(AysTokenClaims.TYPE.getValue(), OAuth2AccessToken.TokenType.BEARER.getValue())
.and()
final Date accessTokenExpiresAt = DateUtils.addMinutes(
new Date(currentTimeMillis), tokenConfiguration.getAccessTokenExpireMinute()
);
final String accessToken = tokenBuilder
.id(AysRandomUtil.generateUUID())
.issuer(tokenConfiguration.getIssuer())
.issuedAt(tokenIssuedAt)
.expiration(accessTokenExpiresAt)
.signWith(tokenConfiguration.getPrivateKey())
.claims(claims)
.compact();

final Date refreshTokenExpiresAt = DateUtils.addDays(new Date(currentTimeMillis), tokenConfiguration.getRefreshTokenExpireDay());
final JwtBuilder refreshTokenBuilder = Jwts.builder();
final String refreshToken = refreshTokenBuilder
.header()
.add(AysTokenClaims.TYPE.getValue(), OAuth2AccessToken.TokenType.BEARER.getValue())
.and()
final Date refreshTokenExpiresAt = DateUtils.addDays(
new Date(currentTimeMillis), tokenConfiguration.getRefreshTokenExpireDay()
);
final String refreshToken = tokenBuilder
.id(AysRandomUtil.generateUUID())
.issuer(tokenConfiguration.getIssuer())
.issuedAt(tokenIssuedAt)
.expiration(refreshTokenExpiresAt)
.signWith(tokenConfiguration.getPrivateKey())
.claim(AysTokenClaims.USER_ID.getValue(), claims.get(AysTokenClaims.USER_ID.getValue()))
.compact();

Expand All @@ -86,6 +78,7 @@ public AysToken generate(final Map<String, Object> claims) {
.build();
}


/**
* Generates an access token based on the provided claims and refresh token.
*
Expand All @@ -97,18 +90,13 @@ public AysToken generate(final Map<String, Object> claims) {
public AysToken generate(final Map<String, Object> claims, final String refreshToken) {

final long currentTimeMillis = System.currentTimeMillis();
final Date accessTokenIssuedAt = new Date(currentTimeMillis);
final Date accessTokenExpiresAt = DateUtils.addMinutes(new Date(currentTimeMillis), tokenConfiguration.getAccessTokenExpireMinute());

final String accessToken = Jwts.builder()
.header()
.add(AysTokenClaims.TYPE.getValue(), OAuth2AccessToken.TokenType.BEARER.getValue())
.and()
final Date accessTokenExpiresAt = DateUtils.addMinutes(
new Date(currentTimeMillis), tokenConfiguration.getAccessTokenExpireMinute()
);
final String accessToken = this.initializeTokenBuilder(currentTimeMillis)
.id(AysRandomUtil.generateUUID())
.issuer(tokenConfiguration.getIssuer())
.issuedAt(accessTokenIssuedAt)
.expiration(accessTokenExpiresAt)
.signWith(tokenConfiguration.getPrivateKey())
.claims(claims)
.compact();

Expand All @@ -119,6 +107,29 @@ public AysToken generate(final Map<String, Object> claims, final String refreshT
.build();
}

/**
* Initializes a JwtBuilder for creating a JSON Web Token (JWT) with the specified current time.
*
* @param currentTimeMillis The current time in milliseconds to be used as the "issued at" claim.
* @return JwtBuilder instance configured with default and provided settings.
* <p>
* The JWT will have the following claims set:
* - Header with the token type set to Bearer.
* - Issuer claim set to the configured issuer from the token configuration.
* - Issued At (iat) claim set to the specified current time.
* - Signature configured with the private key from the token configuration.
*/
private JwtBuilder initializeTokenBuilder(long currentTimeMillis) {
return Jwts.builder()
.header()
.add(AysTokenClaims.TYPE.getValue(), OAuth2AccessToken.TokenType.BEARER.getValue())
.and()
.issuer(tokenConfiguration.getIssuer())
.issuedAt(new Date(currentTimeMillis))
.signWith(tokenConfiguration.getPrivateKey());
}


/**
* Verifies and validates the given JWT (JSON Web Token).
* This method parses the token using the public key from the {@link AysTokenConfigurationParameter},
Expand Down
Loading

0 comments on commit 08a3e46

Please sign in to comment.