Skip to content

Commit 2950e5d

Browse files
committed
Removed deprecated functions
1 parent cabc10f commit 2950e5d

File tree

6 files changed

+118
-130
lines changed

6 files changed

+118
-130
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/checkstyle/checkstyle.xml

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
44
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
55

6+
67
<module name="Checker">
78
<property name="charset" value="UTF-8"/>
89
<property name="localeLanguage" value=""/>
@@ -11,7 +12,8 @@
1112
<module name="SuppressionFilter">
1213
<property name="file" value="config/checkstyle/suppressions.xml"/>
1314
</module>
14-
15+
<!--Add more checks from google style guide:-->
16+
<!--https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml-->
1517
<module name="TreeWalker">
1618
<property name="tabWidth" value="2"/>
1719
<module name="PackageName">
@@ -22,7 +24,7 @@
2224
<property name="illegalClasses" value="java.util.logging.Logger"/>
2325
</module>
2426
<module name="RedundantImport"/>
25-
<module name="AvoidStarImport"/> <!-- already blocks star imports -->
27+
<module name="AvoidStarImport"/>
2628
<module name="UnusedImports">
2729
<property name="processJavadoc" value="true"/>
2830
</module>
@@ -31,8 +33,10 @@
3133
<module name="MethodParamPad"/>
3234
<module name="NoWhitespaceAfter"/>
3335
<module name="NoWhitespaceBefore"/>
34-
<module name="WhitespaceAfter"/>
35-
<module name="WhitespaceAround"/>
36+
<module name="WhitespaceAfter">
37+
</module>
38+
<module name="WhitespaceAround">
39+
</module>
3640

3741
<module name="ModifierOrder"/>
3842
<module name="RedundantModifier"/>
@@ -51,30 +55,37 @@
5155
</module>
5256
<module name="NPathComplexity"/>
5357

58+
5459
<module name="ArrayTypeStyle"/>
5560
<module name="Indentation">
56-
<property name="basicOffset" value="4"/>
57-
<property name="caseIndent" value="4"/>
58-
<property name="throwsIndent" value="4"/>
59-
<property name="arrayInitIndent" value="4"/>
60-
<property name="lineWrappingIndentation" value="4"/>
61+
<property name="basicOffset" value="2"/>
62+
<property name="caseIndent" value="2"/>
63+
<property name="throwsIndent" value="2"/>
64+
<property name="arrayInitIndent" value="2"/>
65+
<property name="lineWrappingIndentation" value="2"/>
6166
</module>
6267

6368
<module name="JavadocMethod">
6469
<property name="severity" value="ignore"/>
6570
</module>
71+
6672
<module name="JavadocType">
6773
<property name="severity" value="ignore"/>
6874
</module>
75+
6976
<module name="JavadocVariable">
7077
<property name="severity" value="ignore"/>
7178
</module>
79+
7280
</module>
7381

7482
<module name="LineLength">
7583
<property name="ignorePattern" value="^ *\* *@see.+$"/>
7684
<property name="max" value="136"/>
7785
</module>
7886

87+
<module name="NewlineAtEndOfFile">
88+
<property name="lineSeparator" value="lf"/>
89+
</module>
7990
<module name="FileTabCharacter"/>
80-
</module>
91+
</module>

src/main/java/org/spacehub/entities/RegistrationRequest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
@AllArgsConstructor
99
@NoArgsConstructor
1010
public class RegistrationRequest {
11-
private String firstName;
12-
private String lastName;
13-
private String email;
14-
private String password;
11+
private String firstName;
12+
private String lastName;
13+
private String email;
14+
private String password;
1515
}
Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package org.spacehub.service;
22

3-
import org.springframework.beans.factory.annotation.Autowired;
43
import org.springframework.mail.SimpleMailMessage;
54
import org.springframework.mail.javamail.JavaMailSender;
65
import org.springframework.stereotype.Service;
7-
86
import java.time.Instant;
97
import java.util.HashMap;
108
import java.util.Map;
@@ -13,51 +11,57 @@
1311
@Service
1412
public class OTPService {
1513

16-
@Autowired
17-
private JavaMailSender mailSender;
14+
private final JavaMailSender mailSender;
1815

19-
private final String DEFAULT_EMAIL = "[email protected]";
20-
private final Map<String, String> otpMap = new HashMap<>();
21-
private final Map<String, Instant> sendTimeMap = new HashMap<>();
16+
private final Map<String, String> otpMap = new HashMap<>();
17+
private final Map<String, Instant> sendTimeMap = new HashMap<>();
2218

23-
private static final int expirySeconds = 300;
24-
private static final int cooldownSeconds = 60;
19+
private static final int expirySeconds = 300;
20+
private static final int cooldownSeconds = 60;
2521

26-
public void sendOTP(String email) {
27-
if (email == null || email.isEmpty()) {
28-
email = DEFAULT_EMAIL;
29-
}
22+
public OTPService(JavaMailSender mailSender) {
23+
this.mailSender = mailSender;
24+
}
3025

31-
int num = new Random().nextInt(1000000);
32-
String otp = String.format("%06d", num);
26+
public void sendOTP(String email) {
27+
String DEFAULT_EMAIL = "[email protected]";
28+
if (email == null || email.isEmpty()) {
29+
email = DEFAULT_EMAIL;
30+
}
3331

34-
otpMap.put(email, otp);
35-
sendTimeMap.put(email, Instant.now());
32+
int num = new Random().nextInt(1000000);
33+
String otp = String.format("%06d", num);
3634

37-
SimpleMailMessage message = new SimpleMailMessage();
38-
message.setFrom(DEFAULT_EMAIL);
39-
message.setTo(email);
40-
message.setSubject("OTP Verification");
41-
message.setText("Your OTP is: " + otp);
35+
otpMap.put(email, otp);
36+
sendTimeMap.put(email, Instant.now());
4237

43-
mailSender.send(message);
44-
}
38+
SimpleMailMessage message = new SimpleMailMessage();
39+
message.setFrom(DEFAULT_EMAIL);
40+
message.setTo(email);
41+
message.setSubject("OTP Verification");
42+
message.setText("Your OTP is: " + otp);
4543

46-
public boolean validateOTP(String otp) {
47-
return otpMap.containsValue(otp);
48-
}
44+
mailSender.send(message);
45+
}
4946

50-
public boolean canSendOTP(String email) {
51-
Instant lastSent = sendTimeMap.get(email);
52-
if (lastSent == null) return true;
53-
return Instant.now().isAfter(lastSent.plusSeconds(cooldownSeconds));
54-
}
47+
public boolean validateOTP(String otp) {
48+
return otpMap.containsValue(otp);
49+
}
5550

51+
public boolean canSendOTP(String email) {
52+
Instant lastSent = sendTimeMap.get(email);
53+
if (lastSent == null) {
54+
return true;
55+
}
56+
return Instant.now().isAfter(lastSent.plusSeconds(cooldownSeconds));
57+
}
5658

57-
public long cooldownTime(String email) {
58-
Instant lastSent = sendTimeMap.get(email);
59-
if (lastSent == null) return 0;
60-
long diff = Instant.now().getEpochSecond() - lastSent.getEpochSecond();
61-
return Math.max(0, cooldownSeconds - diff);
59+
public long cooldownTime(String email) {
60+
Instant lastSent = sendTimeMap.get(email);
61+
if (lastSent == null) {
62+
return 0;
6263
}
64+
long diff = Instant.now().getEpochSecond() - lastSent.getEpochSecond();
65+
return Math.max(0, cooldownSeconds - diff);
66+
}
6367
}

src/main/java/org/spacehub/service/UserNameService.java

Lines changed: 44 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3,83 +3,57 @@
33
import io.jsonwebtoken.Claims;
44
import io.jsonwebtoken.Jwts;
55
import io.jsonwebtoken.security.Keys;
6-
import org.slf4j.Logger;
7-
import org.slf4j.LoggerFactory;
86
import org.springframework.security.core.userdetails.UserDetails;
97
import org.springframework.stereotype.Service;
10-
118
import javax.crypto.SecretKey;
12-
import java.util.Base64;
9+
import java.nio.charset.StandardCharsets;
1310
import java.util.Date;
14-
import java.util.Map;
1511
import java.util.function.Function;
1612

1713
@Service
1814
public class UserNameService {
1915

20-
private static final Logger logger = LoggerFactory.getLogger(UserNameService.class);
21-
22-
private static final String SECRET_KEY = "7de8e1761eeac40efc9314980ebd00fbd55978f497b50ffee42902bba14d0596";
23-
24-
private SecretKey getSigningKey() {
25-
byte[] keyBytes = Base64.getDecoder().decode(SECRET_KEY);
26-
return Keys.hmacShaKeyFor(keyBytes);
27-
}
28-
29-
public String generateToken(UserDetails userDetails) {
30-
return generateToken(Map.of(), userDetails);
31-
}
32-
33-
public String generateToken(Map<String, Object> extraClaims, UserDetails userDetails) {
34-
SecretKey key = getSigningKey();
35-
long nowMillis = System.currentTimeMillis();
36-
long expMillis = nowMillis + 1000L * 60 * 60 * 24;
37-
Map<String, Object> payload = Map.of(
38-
"username", userDetails.getUsername(),
39-
"iat", nowMillis / 1000,
40-
"exp", expMillis / 1000
41-
);
42-
43-
if (extraClaims != null && !extraClaims.isEmpty()) {
44-
payload = new java.util.HashMap<>(payload);
45-
payload.putAll(extraClaims);
46-
}
47-
48-
return Jwts.builder()
49-
.setHeaderParam("typ", "JWT")
50-
.setClaims(payload)
51-
.signWith(key)
52-
.compact();
53-
}
54-
55-
public String extractUsername(String token) {
56-
return extractClaim(token, Claims::getSubject);
57-
}
58-
59-
public <T> T extractClaim(String token, Function<Claims, T> claimsResolver) {
60-
try {
61-
Claims claims = (Claims) Jwts.parser()
62-
.setSigningKey(getSigningKey());
63-
return claimsResolver.apply(claims);
64-
}
65-
catch (Exception e) {
66-
logger.warn("Failed to extract claim from token: {}", e.getMessage());
67-
return null;
68-
}
69-
}
70-
71-
public boolean isTokenValid(String token, UserDetails userDetails) {
72-
final String username = extractUsername(token);
73-
return username != null && username.equals(userDetails.getUsername()) && !isTokenExpired(token);
74-
}
75-
76-
private boolean isTokenExpired(String token) {
77-
Date expiration = extractClaim(token, Claims::getExpiration);
78-
return expiration == null || expiration.before(new Date());
79-
}
80-
81-
public boolean validToken(String token, UserDetails userDetails) {
82-
final String username = extractUsername(token);
83-
return username != null && username.equals(userDetails.getUsername());
84-
}
16+
private static final String SECRET_KEY = "7de8e1761eeac40efc9314980ebd00fbd55978f497b50ffee42902bba14d0596";
17+
18+
private SecretKey getSigningKey() {
19+
byte[] keyBytes = SECRET_KEY.getBytes(StandardCharsets.UTF_8);
20+
return Keys.hmacShaKeyFor(keyBytes);
21+
}
22+
23+
public String generateToken(UserDetails userDetails) {
24+
25+
long nowMillis = System.currentTimeMillis();
26+
long expMillis = nowMillis + 1000L * 60 * 60 * 24; // 24h
27+
28+
return Jwts.builder()
29+
.claim("sub", userDetails.getUsername())
30+
.claim("iat", nowMillis / 1000L)
31+
.claim("exp", expMillis / 1000L)
32+
.signWith(getSigningKey())
33+
.compact();
34+
35+
}
36+
37+
public <T> T extractClaim(String token, Function<Claims, T> claimsResolver) {
38+
Claims claims = Jwts.parser()
39+
.verifyWith(getSigningKey())
40+
.build()
41+
.parseSignedClaims(token)
42+
.getPayload();
43+
return claimsResolver.apply(claims);
44+
}
45+
46+
public String extractUsername(String token) {
47+
return extractClaim(token, Claims::getSubject);
48+
}
49+
50+
public boolean validToken(String token, UserDetails userDetails) {
51+
final String username = extractUsername(token);
52+
return username != null && username.equals(userDetails.getUsername()) && !isTokenExpired(token);
53+
}
54+
55+
private boolean isTokenExpired(String token) {
56+
Date expiration = extractClaim(token, Claims::getExpiration);
57+
return expiration.before(new Date());
58+
}
8559
}

src/main/resources/application.properties

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
spring.datasource.url=${SPRING_DATASOURCE_URL}
2-
spring.datasource.username=${SPRING_DATASOURCE_USERNAME}
3-
spring.datasource.password=${SPRING_DATASOURCE_PASSWORD}
1+
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
2+
spring.datasource.username=somiljain
3+
spring.datasource.password=password
44
spring.datasource.driver-class-name=org.postgresql.Driver
55
spring.jpa.hibernate.ddl-auto=update
66
spring.jpa.show-sql=true
77
spring.jpa.properties.hibernate.format_sql=true
8-
spring.jpa.database=postgresql
98
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
10-
server.address=0.0.0.0
11-
server.port=8080
9+
spring.jpa.database=postgresql
10+
server.port=3000
1211

1312
spring.mail.host=smtp.gmail.com
1413
spring.mail.port=587
15-
spring.mail.username=${SPRING_EMAIL}
16-
spring.mail.password=${SPRING_EMAIL_PASSWORD}
14+
spring.mail.username=[email protected]
15+
spring.mail.password=ltnmoqbpmkmdhhtd
1716
spring.mail.properties.mail.smtp.auth=true
1817
spring.mail.properties.mail.smtp.starttls.enable=true
1918
spring.mail.properties.mail.smtp.starttls.required=true

0 commit comments

Comments
 (0)