-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https://jira.fingo.info/browse/IJ-317 Prepare backend for no-auth (#199)
Co-authored-by: Dawid Dudek <dawid.dudek@fingo.pl>
- Loading branch information
Showing
18 changed files
with
234 additions
and
139 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
49 changes: 49 additions & 0 deletions
49
src/main/java/info/fingo/urlopia/api/v2/authentication/noauth/NoAuthSessionController.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,49 @@ | ||
package info.fingo.urlopia.api.v2.authentication.noauth; | ||
|
||
import info.fingo.urlopia.api.v2.authentication.oauth.OAuthRedirectService; | ||
import info.fingo.urlopia.api.v2.user.UserOutput; | ||
import info.fingo.urlopia.config.authentication.UserData; | ||
import info.fingo.urlopia.config.persistance.filter.Filter; | ||
import info.fingo.urlopia.history.HistoryLogService; | ||
import info.fingo.urlopia.user.UserExcerptProjection; | ||
import info.fingo.urlopia.user.UserService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
|
||
import javax.annotation.security.RolesAllowed; | ||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping(value = "/api/v2/noauth/session") | ||
@RequiredArgsConstructor | ||
@ConditionalOnProperty(name = "ad.configuration.enabled", havingValue = "false") | ||
public class NoAuthSessionController { | ||
|
||
private final OAuthRedirectService oAuthRedirectService; | ||
private final HistoryLogService historyLogService; | ||
private final UserService userService; | ||
|
||
@RolesAllowed({"ROLES_ADMIN", "ROLES_LEADER", "ROLES_WORKER"}) | ||
@GetMapping() | ||
public UserData getUserData(Long userId) { | ||
var userData = oAuthRedirectService.getUserData(userId); | ||
var userEmploymentYear = historyLogService.getEmploymentYear(userData.getUserId()); | ||
userData.setEmploymentYear(userEmploymentYear); | ||
return userData; | ||
} | ||
|
||
public List<UserOutput> getAll(Sort sort) { | ||
var users = userService.get(Filter.empty(), sort); | ||
return mapUserProjectionListToUserOutputList(users); | ||
} | ||
|
||
private List<UserOutput> mapUserProjectionListToUserOutputList(List<UserExcerptProjection> users){ | ||
return users.stream() | ||
.map(UserOutput::fromUserExcerptProjection) | ||
.toList(); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...api/v2/oauth/OAuthRedirectController.java → ...cation/oauth/OAuthRedirectController.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
2 changes: 1 addition & 1 deletion
2
...ia/api/v2/oauth/OAuthRedirectService.java → ...ntication/oauth/OAuthRedirectService.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
9 changes: 9 additions & 0 deletions
9
src/main/java/info/fingo/urlopia/config/authentication/AuthorizationFilterProvider.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 info.fingo.urlopia.config.authentication; | ||
|
||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
public interface AuthorizationFilterProvider { | ||
|
||
OncePerRequestFilter getAuthorizationFilter(); | ||
|
||
} |
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
34 changes: 34 additions & 0 deletions
34
src/main/java/info/fingo/urlopia/config/authentication/UserAuthoritiesProvider.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,34 @@ | ||
package info.fingo.urlopia.config.authentication; | ||
|
||
import info.fingo.urlopia.api.v2.user.UserRolesProvider; | ||
import info.fingo.urlopia.user.User; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class UserAuthoritiesProvider { | ||
|
||
public static final String ROLE_PREFIX = "ROLE_"; | ||
private final UserRolesProvider userRolesProvider; | ||
|
||
public Set<SimpleGrantedAuthority> getAuthoritiesFromUser(User user) { | ||
var authorities = new HashSet<SimpleGrantedAuthority>(); | ||
var roles = userRolesProvider.getRolesFromUser(user); | ||
roles.forEach(role -> addAuthorityFromRole(role, authorities)); | ||
return authorities; | ||
} | ||
|
||
private void addAuthorityFromRole(String role, | ||
Set<SimpleGrantedAuthority> authorities){ | ||
var authority = role; | ||
if (!role.startsWith(ROLE_PREFIX)){ | ||
authority = ROLE_PREFIX + role; | ||
} | ||
authorities.add(new SimpleGrantedAuthority(authority)); | ||
} | ||
} |
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
57 changes: 57 additions & 0 deletions
57
src/main/java/info/fingo/urlopia/config/authentication/noauth/NoAuthFilter.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,57 @@ | ||
package info.fingo.urlopia.config.authentication.noauth; | ||
|
||
import info.fingo.urlopia.config.authentication.UserAuthoritiesProvider; | ||
import info.fingo.urlopia.config.authentication.UserIdInterceptor; | ||
import info.fingo.urlopia.config.authentication.oauth.InvalidTokenException; | ||
import info.fingo.urlopia.user.NoSuchUserException; | ||
import info.fingo.urlopia.user.User; | ||
import info.fingo.urlopia.user.UserService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class NoAuthFilter extends OncePerRequestFilter { | ||
|
||
private final UserAuthoritiesProvider userAuthoritiesProvider; | ||
private final UserService userService; | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, | ||
HttpServletResponse response, | ||
FilterChain filterChain) throws ServletException, IOException { | ||
String header = request.getHeader(UserIdInterceptor.USER_ID_ATTRIBUTE); | ||
if (header == null) { | ||
filterChain.doFilter(request, response); | ||
return; | ||
} | ||
var user = userService.get(Long.valueOf(header)); | ||
var authResult = getAuthenticationForUser(user, response); | ||
if (authResult != null){ | ||
SecurityContextHolder.getContext().setAuthentication(authResult); | ||
filterChain.doFilter(request, response); | ||
} | ||
} | ||
|
||
private Authentication getAuthenticationForUser(User user, | ||
HttpServletResponse response) { | ||
try{ | ||
var principal = user.getPrincipalName(); | ||
var authorities = userAuthoritiesProvider.getAuthoritiesFromUser(user); | ||
return new UsernamePasswordAuthenticationToken(principal, null, authorities); | ||
}catch (InvalidTokenException | NoSuchUserException exception){ | ||
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); | ||
return null; | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/info/fingo/urlopia/config/authentication/noauth/NoAuthFilterProvider.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 info.fingo.urlopia.config.authentication.noauth; | ||
|
||
import info.fingo.urlopia.config.authentication.AuthorizationFilterProvider; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@ConditionalOnProperty(name = "ad.configuration.enabled", havingValue = "false") | ||
public class NoAuthFilterProvider implements AuthorizationFilterProvider { | ||
|
||
private final NoAuthFilter noAuthFilter; | ||
|
||
@Override | ||
public OncePerRequestFilter getAuthorizationFilter() { | ||
return noAuthFilter; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/info/fingo/urlopia/config/authentication/noauth/NoAuthWebConfig.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 info.fingo.urlopia.config.authentication.noauth; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.config.annotation.CorsRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
@ConditionalOnProperty(name = "ad.configuration.enabled", havingValue = "false") | ||
public class NoAuthWebConfig implements WebMvcConfigurer { | ||
|
||
@Override | ||
public void addCorsMappings(CorsRegistry registry) { | ||
registry.addMapping("/**") | ||
.allowedMethods("*"); | ||
} | ||
} | ||
|
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
27 changes: 3 additions & 24 deletions
27
...main/java/info/fingo/urlopia/config/authentication/oauth/JwtTokenAuthoritiesProvider.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,44 +1,23 @@ | ||
package info.fingo.urlopia.config.authentication.oauth; | ||
|
||
import com.auth0.jwt.interfaces.DecodedJWT; | ||
import info.fingo.urlopia.api.v2.user.UserRolesProvider; | ||
import info.fingo.urlopia.user.User; | ||
import info.fingo.urlopia.config.authentication.UserAuthoritiesProvider; | ||
import info.fingo.urlopia.user.UserService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class JwtTokenAuthoritiesProvider { | ||
|
||
public static final String ROLE_PREFIX = "ROLE_"; | ||
|
||
private final UserService userService; | ||
private final UserRolesProvider userRolesProvider; | ||
private final UserAuthoritiesProvider userAuthoritiesProvider; | ||
|
||
public Set<SimpleGrantedAuthority> getAuthoritiesFromJWT(DecodedJWT decodedToken){ | ||
var principal = JwtUtils.getPrincipalNameFromDecodedToken(decodedToken); | ||
var user = userService.getByPrincipal(principal); | ||
return getAuthoritiesFromUser(user); | ||
} | ||
|
||
private Set<SimpleGrantedAuthority> getAuthoritiesFromUser(User user) { | ||
var authorities = new HashSet<SimpleGrantedAuthority>(); | ||
var roles = userRolesProvider.getRolesFromUser(user); | ||
roles.forEach(role -> addAuthorityFromRole(role, authorities)); | ||
return authorities; | ||
} | ||
|
||
private void addAuthorityFromRole(String role, | ||
Set<SimpleGrantedAuthority> authorities){ | ||
var authority = role; | ||
if (!role.startsWith(ROLE_PREFIX)){ | ||
authority = ROLE_PREFIX + role; | ||
} | ||
authorities.add(new SimpleGrantedAuthority(authority)); | ||
return userAuthoritiesProvider.getAuthoritiesFromUser(user); | ||
} | ||
} |
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
src/main/java/info/fingo/urlopia/config/authentication/oauth/OAuthFilterProvider.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 info.fingo.urlopia.config.authentication.oauth; | ||
|
||
import info.fingo.urlopia.config.authentication.AuthorizationFilterProvider; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@ConditionalOnProperty(name = "ad.configuration.enabled", havingValue = "true", matchIfMissing = true) | ||
public class OAuthFilterProvider implements AuthorizationFilterProvider { | ||
|
||
private final JwtFilter jwtFilter; | ||
|
||
@Override | ||
public OncePerRequestFilter getAuthorizationFilter() { | ||
return jwtFilter; | ||
} | ||
} |
7 changes: 5 additions & 2 deletions
7
...opia/config/authentication/WebConfig.java → .../authentication/oauth/OAuthWebConfig.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
Oops, something went wrong.