-
Notifications
You must be signed in to change notification settings - Fork 2
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 #72 from InseeFr/feat/auth
Feat: add auth with oidc
- Loading branch information
Showing
35 changed files
with
846 additions
and
81 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
19 changes: 19 additions & 0 deletions
19
src/main/java/fr/insee/publicenemy/api/application/web/auth/AuthenticationHelper.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,19 @@ | ||
package fr.insee.publicenemy.api.application.web.auth; | ||
|
||
import org.springframework.security.core.Authentication; | ||
|
||
public interface AuthenticationHelper { | ||
/** | ||
* Retrieve the auth token of the current user | ||
* | ||
* @return auth token | ||
*/ | ||
String getUserToken(); | ||
|
||
/** | ||
* Retrieve the authentication principal for current user | ||
* | ||
* @return {@link Authentication} the authentication user object | ||
*/ | ||
Authentication getAuthenticationPrincipal(); | ||
} |
7 changes: 7 additions & 0 deletions
7
...main/java/fr/insee/publicenemy/api/application/web/auth/AuthenticationTokenException.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 fr.insee.publicenemy.api.application.web.auth; | ||
|
||
public class AuthenticationTokenException extends RuntimeException { | ||
public AuthenticationTokenException(String message) { | ||
super(message); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/fr/insee/publicenemy/api/application/web/auth/AuthenticationUserHelper.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,26 @@ | ||
package fr.insee.publicenemy.api.application.web.auth; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class AuthenticationUserHelper implements AuthenticationHelper { | ||
@Override | ||
public String getUserToken() { | ||
if(getAuthenticationPrincipal() instanceof JwtAuthenticationToken auth) { | ||
return auth.getToken().getTokenValue(); | ||
} | ||
throw new AuthenticationTokenException("Cannot retrieve token for the user."); | ||
} | ||
|
||
@Override | ||
public Authentication getAuthenticationPrincipal() { | ||
return SecurityContextHolder.getContext().getAuthentication(); | ||
} | ||
} |
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
22 changes: 0 additions & 22 deletions
22
src/main/java/fr/insee/publicenemy/api/configuration/SwaggerBeanConfig.java
This file was deleted.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
src/main/java/fr/insee/publicenemy/api/configuration/auth/AuthConstants.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 fr.insee.publicenemy.api.configuration.auth; | ||
|
||
public class AuthConstants { | ||
private AuthConstants() { | ||
throw new IllegalStateException("Constants class"); | ||
} | ||
|
||
public static final String ROLE_PREFIX = "ROLE_"; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/fr/insee/publicenemy/api/configuration/auth/AuthorityRole.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,11 @@ | ||
package fr.insee.publicenemy.api.configuration.auth; | ||
|
||
public class AuthorityRole { | ||
private AuthorityRole() { | ||
throw new IllegalArgumentException("Constant class"); | ||
} | ||
|
||
public static final String HAS_ROLE_DESIGNER = "hasRole('DESIGNER')"; | ||
public static final String HAS_ANY_ROLE = "hasAnyRole('DESIGNER', 'ADMIN')"; | ||
public static final String HAS_ADMIN_PRIVILEGES = "hasAnyRole('ADMIN')"; | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/fr/insee/publicenemy/api/configuration/auth/AuthorityRoleEnum.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,6 @@ | ||
package fr.insee.publicenemy.api.configuration.auth; | ||
|
||
public enum AuthorityRoleEnum { | ||
ADMIN, | ||
DESIGNER | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/fr/insee/publicenemy/api/configuration/auth/GrantedAuthorityConverter.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,55 @@ | ||
package fr.insee.publicenemy.api.configuration.auth; | ||
|
||
import fr.insee.publicenemy.api.configuration.properties.OidcProperties; | ||
import fr.insee.publicenemy.api.configuration.properties.RoleProperties; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.oauth2.jwt.Jwt; | ||
|
||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
@AllArgsConstructor | ||
public class GrantedAuthorityConverter implements Converter<Jwt, Collection<GrantedAuthority>> { | ||
private final OidcProperties oidcProperties; | ||
private final RoleProperties roleProperties; | ||
|
||
/** | ||
* | ||
* @param map: Map that represent JWT token | ||
* @param keyPath : jsonPath to wanted value, ex: realm_access.roles | ||
* @return the value of keyPath inside Map | ||
* @param <T> | ||
*/ | ||
public <T> T getDeepPropsOfMapForRoles(Map<String, Object> map, String keyPath){ | ||
Map subMap = (Map) map; | ||
String[] propertyPath = keyPath.toString().split("\\."); | ||
for (int i = 0; i < propertyPath.length -1; i++) { | ||
subMap = (Map) subMap.get(propertyPath[i]); | ||
} | ||
return (T) subMap.get(propertyPath[propertyPath.length -1]); | ||
|
||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public Collection<GrantedAuthority> convert(Jwt jwt) { | ||
Map<String, Object> claims = jwt.getClaims(); | ||
|
||
List<String> roles = getDeepPropsOfMapForRoles(claims, oidcProperties.roleClaim()); | ||
|
||
return roles.stream() | ||
.map(role -> { | ||
if (role.equals(roleProperties.designer())) { | ||
return new SimpleGrantedAuthority(AuthConstants.ROLE_PREFIX + AuthorityRoleEnum.DESIGNER); | ||
} | ||
if (role.equals(roleProperties.admin())) { | ||
return new SimpleGrantedAuthority(AuthConstants.ROLE_PREFIX + AuthorityRoleEnum.ADMIN); | ||
} | ||
return null; | ||
}) | ||
.filter(Objects::nonNull) | ||
.collect(Collectors.toCollection(ArrayList::new)); | ||
} | ||
} |
Oops, something went wrong.