-
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.
Storing users in database + Cleanups
- Loading branch information
Showing
137 changed files
with
2,628 additions
and
328 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
src/main/java/pl/bartlomiejstepien/armaserverwebgui/application/auth/AuthService.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,35 @@ | ||
package pl.bartlomiejstepien.armaserverwebgui.application.auth; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.security.authentication.BadCredentialsException; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.stereotype.Service; | ||
import pl.bartlomiejstepien.armaserverwebgui.application.config.security.JwtService; | ||
import pl.bartlomiejstepien.armaserverwebgui.domain.user.UserService; | ||
import reactor.core.publisher.Mono; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class AuthService | ||
{ | ||
private final PasswordEncoder passwordEncoder; | ||
private final UserService userService; | ||
private final JwtService jwtService; | ||
|
||
public Mono<JwtToken> authenticate(String username, String password, String ipAddress) | ||
{ | ||
log.info("Login attempt for {} from {}", username, ipAddress); | ||
return userService.getUserWithPassword(username) | ||
.filter(user -> passwordEncoder.matches(password, user.getPassword())) | ||
.switchIfEmpty(Mono.error(new BadCredentialsException("Invalid username or password"))) | ||
.flatMap(user -> this.userService.getUser(username)) | ||
.map(user -> new JwtToken(this.jwtService.createJwt(user), user.getAuthorities())); | ||
} | ||
|
||
public Mono<Void> logout(String jwt) | ||
{ | ||
return this.jwtService.invalidate(jwt); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/pl/bartlomiejstepien/armaserverwebgui/application/auth/JwtToken.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,10 @@ | ||
package pl.bartlomiejstepien.armaserverwebgui.application.auth; | ||
|
||
import pl.bartlomiejstepien.armaserverwebgui.application.security.AswgAuthority; | ||
|
||
import java.util.Set; | ||
|
||
public record JwtToken(String jwt, Set<AswgAuthority> authorities) | ||
{ | ||
|
||
} |
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
47 changes: 47 additions & 0 deletions
47
...tepien/armaserverwebgui/application/config/security/JwtReactiveAuthenticationManager.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,47 @@ | ||
package pl.bartlomiejstepien.armaserverwebgui.application.config.security; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.authentication.BadCredentialsException; | ||
import org.springframework.security.authentication.ReactiveAuthenticationManager; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import pl.bartlomiejstepien.armaserverwebgui.domain.user.UserService; | ||
import pl.bartlomiejstepien.armaserverwebgui.domain.user.dto.AswgUser; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.util.List; | ||
|
||
@RequiredArgsConstructor | ||
public class JwtReactiveAuthenticationManager implements ReactiveAuthenticationManager | ||
{ | ||
private final UserService userService; | ||
private final JwtService jwtService; | ||
|
||
@Override | ||
public Mono<Authentication> authenticate(Authentication authentication) | ||
{ | ||
return Mono.just(authentication) | ||
.map(authentication1 -> jwtService.validateJwt(String.valueOf(authentication1.getCredentials()))) | ||
.onErrorResume(Exception.class, err -> Mono.error(new BadCredentialsException("Bad auth token!"))) | ||
.flatMap(jws -> userService.getUser(jws.getPayload().getSubject())) | ||
.mapNotNull(this::toAuthentication); | ||
} | ||
|
||
//TODO: Create custom Authentication implementation or store AswgUser in UsernamePasswordAuthenticationToken | ||
private Authentication toAuthentication(AswgUser aswgUser) | ||
{ | ||
return UsernamePasswordAuthenticationToken.authenticated( | ||
aswgUser, | ||
aswgUser.getUsername(), | ||
prepareAuthorities(aswgUser) | ||
); | ||
} | ||
|
||
private List<SimpleGrantedAuthority> prepareAuthorities(AswgUser aswgUser) | ||
{ | ||
return aswgUser.getAuthorities().stream() | ||
.map(authority -> new SimpleGrantedAuthority(authority.getCode())) | ||
.toList(); | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
...pl/bartlomiejstepien/armaserverwebgui/application/config/security/RateLimitWebFilter.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,20 @@ | ||
package pl.bartlomiejstepien.armaserverwebgui.application.config.security; | ||
|
||
import org.springframework.core.annotation.Order; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.server.ServerWebExchange; | ||
import org.springframework.web.server.WebFilter; | ||
import org.springframework.web.server.WebFilterChain; | ||
import reactor.core.publisher.Mono; | ||
|
||
//TODO: Implement it... | ||
@Order(-4) | ||
@Component | ||
public class RateLimitWebFilter implements WebFilter | ||
{ | ||
@Override | ||
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) | ||
{ | ||
return chain.filter(exchange); | ||
} | ||
} |
Oops, something went wrong.