-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJwtService.java
More file actions
221 lines (196 loc) · 8.79 KB
/
JwtService.java
File metadata and controls
221 lines (196 loc) · 8.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package smartpot.com.api.Security.Service;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;
import jakarta.validation.ValidationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import smartpot.com.api.Exception.EncryptionException;
import smartpot.com.api.Exception.InvalidTokenException;
import smartpot.com.api.Mail.Model.DTO.EmailDTO;
import smartpot.com.api.Mail.Service.EmailService;
import smartpot.com.api.Mail.Validator.EmailValidatorI;
import smartpot.com.api.Security.Model.DTO.ResetTokenDTO;
import smartpot.com.api.Users.Model.DTO.UserDTO;
import smartpot.com.api.Users.Service.SUserI;
import javax.crypto.SecretKey;
import java.util.*;
@Service
public class JwtService implements JwtServiceI {
private final SUserI serviceUser;
private final EmailService emailService;
private final EmailValidatorI emailValidator;
private final EncryptionServiceI encryptionService;
@Value("${application.security.jwt.secret-key}")
private String secretKey;
@Value("${application.security.jwt.expiration}")
private long expiration;
/**
* Constructor que inyecta las dependencias del servicio.
*
* @param serviceUser servicio que maneja las operaciones de base de datos.
*/
@Autowired
public JwtService(SUserI serviceUser, EmailService emailService, EmailValidatorI emailValidator, EncryptionServiceI encryptionService) {
this.serviceUser = serviceUser;
this.emailService = emailService;
this.emailValidator = emailValidator;
this.encryptionService = encryptionService;
}
@Override
public String Login(UserDTO reqUser) throws Exception {
return Optional.of(serviceUser.getUserByEmail(reqUser.getEmail()))
.filter(userDTO -> new BCryptPasswordEncoder().matches(reqUser.getPassword(), userDTO.getPassword()))
.map(validUser -> {
try {
return generateToken(validUser.getId(), validUser.getEmail());
} catch (Exception e) {
throw new ValidationException(e);
}
})
.orElseThrow(() -> new Exception("Credenciales Invalidas"));
}
/**
* @param reqUser
* @return
* @throws Exception
*/
@Override
public String Register(UserDTO reqUser) throws Exception {
return "";
}
private String generateToken(String id, String email) throws Exception {
// TODO: Refine token (email != subject)
Map<String, Object> claims = new HashMap<>();
claims.put("id", id);
claims.put("email", email);
// claims.put("roles", user.getAuthorities());
return createToken(claims, email);
// createToken (CustomClaims, subjectClaim)
}
@Override
public UserDTO validateAuthHeader(String authHeader) throws Exception {
if (authHeader == null || !authHeader.startsWith("SmartPot-OAuth ")) {
throw new Exception("El encabezado de autorización es inválido. Se esperaba 'SmartPot-OAuth <token>'.");
}
String token = authHeader.split(" ")[1];
token = encryptionService.decrypt(token);
String email = extractEmail(token);
UserDetails user = serviceUser.loadUserByUsername(email);
if (email == null) {
throw new InvalidTokenException("Correo no encontrado.");
}
if (!validateToken(token, user)) {
throw new InvalidTokenException("Se ha expirado.");
}
UserDTO finalUser = serviceUser.getUserByEmail(email);
finalUser.setPassword("");
return finalUser;
}
@Override
public String resetPassword(UserDTO user, String email, String resetToken) throws Exception {
return Optional.of(serviceUser.getUserByEmail(email))
.map(validUser -> {
try {
String decrypted = encryptionService.decrypt(resetToken);
ResetTokenDTO resetTokenDTO = ResetTokenDTO.convertToDTO(decrypted);
if (!validateResetToken(resetTokenDTO)) {
throw new ValidationException("Provided reset token is not valid");
}
return serviceUser.UpdateUserPassword(validUser, user.getPassword());
} catch (Exception e) {
throw new ValidationException(e);
}
})
.map(validUser -> {
try {
return generateToken(validUser.getId(), validUser.getEmail());
} catch (Exception e) {
throw new ValidationException(e);
}
})
.orElseThrow(() -> new Exception("Credenciales Invalidas"));
}
@Override
public Boolean forgotPassword(String email) throws Exception {
return Optional.of(serviceUser.getUserByEmail(email))
.map(validUser -> {
try {
return generateToken(validUser.getId(), validUser.getEmail());
} catch (Exception e) {
throw new ValidationException(e);
}
})
.map(token -> new ResetTokenDTO(token, "reset", new Date(System.currentTimeMillis() + expiration) ))
.map(token -> {
try {
return encryptionService.encrypt(ResetTokenDTO.convertToJson(token));
} catch (Exception e) {
throw new EncryptionException("Conversion to json or encryption failed: " + e);
}
})
.map(token -> new EmailDTO(null, email, "Token para recuperar contraseña: " + token, "Recuperar contraseña", "", null, "true"))
.map(emailService::sendSimpleMail)
.map(ValidDTO -> {
emailValidator.validateId(ValidDTO.getId());
emailValidator.validateMsgBody(ValidDTO.getMsgBody());
emailValidator.validateAttachment(ValidDTO.getAttachment());
emailValidator.validateRecipient(ValidDTO.getRecipient());
emailValidator.validateSubject(ValidDTO.getSubject());
boolean isValid = emailValidator.isValid();
emailValidator.reset();
return isValid;
})
.orElseThrow(() -> new IllegalStateException("Esta cuenta de correo no esta asociada a un usuario"));
}
private Boolean validateToken(String token, UserDetails userDetails) {
Date expirationDate = extractExpiration(token);
if (expirationDate.before(new Date())) {
return false;
}
String username = extractUsername(token);
return userDetails.getUsername().equals(username) && !expirationDate.before(new Date());
}
private Boolean validateResetToken(ResetTokenDTO resetTokenDTO) {
String token = encryptionService.decrypt(resetTokenDTO.getToken());
if (!validateToken(token, serviceUser.loadUserByUsername(extractEmail(token)))) {
return false;
}
return !resetTokenDTO.getExpiration().before(new Date());
}
private String createToken(Map<String, Object> claims, String username) throws Exception {
String token = Jwts.builder()
.claims(claims)
.subject(username)
.issuedAt(new Date(System.currentTimeMillis()))
.expiration(new Date(System.currentTimeMillis() + expiration))
.signWith(getSignKey())
//.signWith(getSignKey(), SignatureAlgorithm.HS256)
.compact();
return encryptionService.encrypt(token);
}
private SecretKey getSignKey() {
byte[] keyBytes = Decoders.BASE64.decode(secretKey);
return Keys.hmacShaKeyFor(keyBytes);
}
private Claims extractAllClaims(String token) {
return Jwts.parser()
.verifyWith(getSignKey())
.build()
.parseSignedClaims(token)
.getPayload();
}
private Date extractExpiration(String token) {
return extractAllClaims(token).getExpiration();
}
private String extractUsername(String token) {
return extractAllClaims(token).getSubject();
}
private String extractEmail(String token) {
return extractAllClaims(token).get("email", String.class);
}
}