-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #504 from COS301-SE-2023/frontend/files2
Frontend/files2
- Loading branch information
Showing
35 changed files
with
690 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 13 additions & 41 deletions
54
...fosafe_backend/src/main/java/com/fragile/infosafe/primary/auth/AuthenticationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,101 +1,73 @@ | ||
package com.fragile.infosafe.primary.auth; | ||
|
||
import com.fragile.infosafe.primary.model.Role; | ||
import com.fragile.infosafe.primary.model.User; | ||
import com.fragile.infosafe.primary.repository.RoleRepository; | ||
import com.fragile.infosafe.primary.repository.UserRepository; | ||
import com.fragile.infosafe.primary.requests.RegisterRequest; | ||
import com.fragile.infosafe.primary.config.JwtService; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fragile.infosafe.primary.service.EncryptionService; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.security.core.AuthenticationException; | ||
import javax.crypto.Cipher; | ||
import org.springframework.security.crypto.codec.Hex; | ||
|
||
import java.io.IOException; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class AuthenticationService { | ||
private final UserRepository repository; | ||
private final PasswordEncoder passwordEncoder; | ||
private final JwtService jwtService; | ||
private final AuthenticationManager authenticationManager; | ||
private final RoleRepository roleRepository; | ||
private final EncryptionService encryptionService; | ||
|
||
public AuthenticationResponse register(RegisterRequest request) { | ||
var user = User.builder() | ||
.first_name(request.getFirst_name()) | ||
.last_name(request.getLast_name()) | ||
.email(request.getEmail()) | ||
.first_name(encryptionService.encryptString(request.getFirst_name())) | ||
.last_name(encryptionService.encryptString(request.getLast_name())) | ||
.email(encryptionService.encryptString(request.getEmail())) | ||
.password(passwordEncoder.encode(request.getPassword())) | ||
.role(roleRepository.findByRole_name(request.getRole().getRole_name())) | ||
.build(); | ||
|
||
repository.save(user); | ||
var jwtToken = jwtService.generateToken(user); | ||
var refreshToken = jwtService.generateRefreshToken(user); | ||
return AuthenticationResponse.builder() | ||
.accessToken(jwtToken) | ||
.refreshToken(refreshToken) | ||
.build(); | ||
} | ||
|
||
|
||
|
||
|
||
public AuthenticationResponse authenticate(AuthenticationRequest request) { | ||
try { | ||
authenticationManager.authenticate( | ||
new UsernamePasswordAuthenticationToken( | ||
request.getEmail(), | ||
encryptionService.encryptString(request.getEmail()), | ||
request.getPassword() | ||
) | ||
); | ||
var user = repository.findByEmail(request.getEmail()) | ||
var user = repository.findByEmail(encryptionService.encryptString(request.getEmail())) | ||
.orElseThrow(); | ||
log.info(String.valueOf(user)); | ||
var jwtToken = jwtService.generateToken(user); | ||
var refreshToken = jwtService.generateRefreshToken(user); | ||
|
||
return AuthenticationResponse.builder() | ||
.accessToken(jwtToken) | ||
.refreshToken(refreshToken) | ||
.build(); | ||
} catch (AuthenticationException e) { | ||
AuthenticationResponse auth = new AuthenticationResponse(); | ||
auth.setError(); | ||
return auth; | ||
} | ||
} | ||
|
||
public void refreshToken( | ||
HttpServletRequest request, | ||
HttpServletResponse response | ||
) throws IOException { | ||
final String authHeader = request.getHeader(HttpHeaders.AUTHORIZATION); | ||
final String refreshToken; | ||
final String userEmail; | ||
if (authHeader == null ||!authHeader.startsWith("Bearer ")) { | ||
return; | ||
} | ||
refreshToken = authHeader.substring(7); | ||
userEmail = jwtService.extractUsername(refreshToken); | ||
if (userEmail != null) { | ||
var user = this.repository.findByEmail(userEmail) | ||
.orElseThrow(); | ||
if (jwtService.isTokenValid(refreshToken, user)) { | ||
var accessToken = jwtService.generateToken(user); | ||
var authResponse = AuthenticationResponse.builder() | ||
.accessToken(accessToken) | ||
.refreshToken(refreshToken) | ||
.build(); | ||
new ObjectMapper().writeValue(response.getOutputStream(), authResponse); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.