-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added security, CORS, Auth login, exception Handling and Suth tests
- Loading branch information
1 parent
8aadad8
commit 07a8ca7
Showing
40 changed files
with
998 additions
and
170 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
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
28 changes: 28 additions & 0 deletions
28
backend/src/main/java/com/shreyas/auth/AuthenticationController.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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.shreyas.auth; | ||
|
||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("api/v1/auth") | ||
public class AuthenticationController { | ||
private final AuthenticationService authenticationService; | ||
|
||
public AuthenticationController(AuthenticationService authenticationService) { | ||
this.authenticationService = authenticationService; | ||
} | ||
|
||
// With Http post we should not return any payload | ||
@PostMapping("/login") | ||
public ResponseEntity<?> login(@RequestBody AuthenticationRequest request) { | ||
AuthenticationResponse authenticationResponse = authenticationService.login(request); | ||
|
||
return ResponseEntity.ok() | ||
.header(HttpHeaders.AUTHORIZATION, authenticationResponse.token()) | ||
.body(authenticationResponse); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
backend/src/main/java/com/shreyas/auth/AuthenticationRequest.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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.shreyas.auth; | ||
|
||
public record AuthenticationRequest( | ||
String username, | ||
String password | ||
) { | ||
} |
9 changes: 9 additions & 0 deletions
9
backend/src/main/java/com/shreyas/auth/AuthenticationResponse.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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.shreyas.auth; | ||
|
||
import com.shreyas.customer.CustomerDTO; | ||
|
||
public record AuthenticationResponse( | ||
String token, | ||
CustomerDTO customerDTO | ||
) { | ||
} |
39 changes: 39 additions & 0 deletions
39
backend/src/main/java/com/shreyas/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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.shreyas.auth; | ||
|
||
import com.shreyas.customer.Customer; | ||
import com.shreyas.customer.CustomerDTO; | ||
import com.shreyas.customer.CustomerDTOMapper; | ||
import com.shreyas.jwt.JWTUtil; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class AuthenticationService { | ||
private final AuthenticationManager authenticationManager; | ||
private final CustomerDTOMapper customerDTOMapper; | ||
private final JWTUtil jwtUtil; | ||
|
||
public AuthenticationService(AuthenticationManager authenticationManager, CustomerDTOMapper customerDTOMapper, JWTUtil jwtUtil) { | ||
this.authenticationManager = authenticationManager; | ||
this.customerDTOMapper = customerDTOMapper; | ||
this.jwtUtil = jwtUtil; | ||
} | ||
|
||
//Method which allow customers to login | ||
public AuthenticationResponse login(AuthenticationRequest request) { | ||
Authentication authenticate = authenticationManager.authenticate( | ||
new UsernamePasswordAuthenticationToken( | ||
request.username(), | ||
request.password() | ||
) | ||
); | ||
Customer principal = (Customer) authenticate.getPrincipal(); | ||
CustomerDTO customerDTO = customerDTOMapper.apply(principal); | ||
String token = jwtUtil.issueToken(customerDTO.username(), customerDTO.roles()); | ||
|
||
|
||
return new AuthenticationResponse(token, customerDTO); | ||
} | ||
} |
27 changes: 0 additions & 27 deletions
27
backend/src/main/java/com/shreyas/config/WebMvcConfig.java
This file was deleted.
Oops, something went wrong.
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.