@@ -48,34 +48,26 @@ class AysTokenServiceImpl implements AysTokenService {
4848 */
4949 @ Override
5050 public AysToken generate (final Map <String , Object > claims ) {
51+
5152 final long currentTimeMillis = System .currentTimeMillis ();
5253
53- final Date tokenIssuedAt = new Date (currentTimeMillis );
54+ final JwtBuilder tokenBuilder = this . initializeTokenBuilder (currentTimeMillis );
5455
55- final Date accessTokenExpiresAt = DateUtils .addMinutes (new Date (currentTimeMillis ), tokenConfiguration .getAccessTokenExpireMinute ());
56- final String accessToken = Jwts .builder ()
57- .header ()
58- .add (AysTokenClaims .TYPE .getValue (), OAuth2AccessToken .TokenType .BEARER .getValue ())
59- .and ()
56+ final Date accessTokenExpiresAt = DateUtils .addMinutes (
57+ new Date (currentTimeMillis ), tokenConfiguration .getAccessTokenExpireMinute ()
58+ );
59+ final String accessToken = tokenBuilder
6060 .id (AysRandomUtil .generateUUID ())
61- .issuer (tokenConfiguration .getIssuer ())
62- .issuedAt (tokenIssuedAt )
6361 .expiration (accessTokenExpiresAt )
64- .signWith (tokenConfiguration .getPrivateKey ())
6562 .claims (claims )
6663 .compact ();
6764
68- final Date refreshTokenExpiresAt = DateUtils .addDays (new Date (currentTimeMillis ), tokenConfiguration .getRefreshTokenExpireDay ());
69- final JwtBuilder refreshTokenBuilder = Jwts .builder ();
70- final String refreshToken = refreshTokenBuilder
71- .header ()
72- .add (AysTokenClaims .TYPE .getValue (), OAuth2AccessToken .TokenType .BEARER .getValue ())
73- .and ()
65+ final Date refreshTokenExpiresAt = DateUtils .addDays (
66+ new Date (currentTimeMillis ), tokenConfiguration .getRefreshTokenExpireDay ()
67+ );
68+ final String refreshToken = tokenBuilder
7469 .id (AysRandomUtil .generateUUID ())
75- .issuer (tokenConfiguration .getIssuer ())
76- .issuedAt (tokenIssuedAt )
7770 .expiration (refreshTokenExpiresAt )
78- .signWith (tokenConfiguration .getPrivateKey ())
7971 .claim (AysTokenClaims .USER_ID .getValue (), claims .get (AysTokenClaims .USER_ID .getValue ()))
8072 .compact ();
8173
@@ -86,6 +78,7 @@ public AysToken generate(final Map<String, Object> claims) {
8678 .build ();
8779 }
8880
81+
8982 /**
9083 * Generates an access token based on the provided claims and refresh token.
9184 *
@@ -97,18 +90,13 @@ public AysToken generate(final Map<String, Object> claims) {
9790 public AysToken generate (final Map <String , Object > claims , final String refreshToken ) {
9891
9992 final long currentTimeMillis = System .currentTimeMillis ();
100- final Date accessTokenIssuedAt = new Date (currentTimeMillis );
101- final Date accessTokenExpiresAt = DateUtils .addMinutes (new Date (currentTimeMillis ), tokenConfiguration .getAccessTokenExpireMinute ());
10293
103- final String accessToken = Jwts . builder ()
104- . header ()
105- . add ( AysTokenClaims . TYPE . getValue (), OAuth2AccessToken . TokenType . BEARER . getValue ())
106- . and ( )
94+ final Date accessTokenExpiresAt = DateUtils . addMinutes (
95+ new Date ( currentTimeMillis ), tokenConfiguration . getAccessTokenExpireMinute ()
96+ );
97+ final String accessToken = this . initializeTokenBuilder ( currentTimeMillis )
10798 .id (AysRandomUtil .generateUUID ())
108- .issuer (tokenConfiguration .getIssuer ())
109- .issuedAt (accessTokenIssuedAt )
11099 .expiration (accessTokenExpiresAt )
111- .signWith (tokenConfiguration .getPrivateKey ())
112100 .claims (claims )
113101 .compact ();
114102
@@ -119,6 +107,29 @@ public AysToken generate(final Map<String, Object> claims, final String refreshT
119107 .build ();
120108 }
121109
110+ /**
111+ * Initializes a JwtBuilder for creating a JSON Web Token (JWT) with the specified current time.
112+ *
113+ * @param currentTimeMillis The current time in milliseconds to be used as the "issued at" claim.
114+ * @return JwtBuilder instance configured with default and provided settings.
115+ * <p>
116+ * The JWT will have the following claims set:
117+ * - Header with the token type set to Bearer.
118+ * - Issuer claim set to the configured issuer from the token configuration.
119+ * - Issued At (iat) claim set to the specified current time.
120+ * - Signature configured with the private key from the token configuration.
121+ */
122+ private JwtBuilder initializeTokenBuilder (long currentTimeMillis ) {
123+ return Jwts .builder ()
124+ .header ()
125+ .add (AysTokenClaims .TYPE .getValue (), OAuth2AccessToken .TokenType .BEARER .getValue ())
126+ .and ()
127+ .issuer (tokenConfiguration .getIssuer ())
128+ .issuedAt (new Date (currentTimeMillis ))
129+ .signWith (tokenConfiguration .getPrivateKey ());
130+ }
131+
132+
122133 /**
123134 * Verifies and validates the given JWT (JSON Web Token).
124135 * This method parses the token using the public key from the {@link AysTokenConfigurationParameter},
0 commit comments