forked from Onlineberatung/onlineberatung-liveService
-
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.
Merge pull request #32 from Onlineberatung/develop
merge os
- Loading branch information
Showing
15 changed files
with
311 additions
and
62 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
FROM adoptopenjdk/openjdk11 | ||
FROM openjdk:17-oracle | ||
VOLUME ["/tmp","/log"] | ||
EXPOSE 8080 | ||
ARG JAR_FILE | ||
COPY ./LiveService.jar app.jar | ||
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"] | ||
ENTRYPOINT ["java","-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"] |
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
48 changes: 48 additions & 0 deletions
48
src/main/java/de/caritas/cob/liveservice/api/auth/AuthorisationService.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,48 @@ | ||
package de.caritas.cob.liveservice.api.auth; | ||
|
||
import com.google.common.collect.Lists; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.oauth2.jwt.Jwt; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class AuthorisationService { | ||
|
||
private final RoleAuthorizationAuthorityMapper roleAuthorizationAuthorityMapper = | ||
new RoleAuthorizationAuthorityMapper(); | ||
|
||
public Object getUsername() { | ||
return getPrincipal().getClaims().get("username"); | ||
} | ||
|
||
private Authentication getAuthentication() { | ||
return SecurityContextHolder.getContext().getAuthentication(); | ||
} | ||
|
||
private Jwt getPrincipal() { | ||
return (Jwt) getAuthentication().getPrincipal(); | ||
} | ||
|
||
public Collection<GrantedAuthority> extractRealmAuthorities(Jwt jwt) { | ||
var roles = extractRealmRoles(jwt); | ||
return roleAuthorizationAuthorityMapper.mapAuthorities( | ||
roles.stream().collect(Collectors.toSet())); | ||
} | ||
|
||
public Collection<String> extractRealmRoles(Jwt jwt) { | ||
Map<String, Object> realmAccess = (Map<String, Object>) jwt.getClaims().get("realm_access"); | ||
if (realmAccess != null) { | ||
var roles = (List<String>) realmAccess.get("roles"); | ||
if (roles != null) { | ||
return roles; | ||
} | ||
} | ||
return Lists.newArrayList(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/de/caritas/cob/liveservice/api/auth/Authority.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,46 @@ | ||
package de.caritas.cob.liveservice.api.auth; | ||
|
||
import com.google.common.collect.Lists; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
import lombok.Getter; | ||
|
||
/** Definition of all authorities and of the role-authority-mapping. */ | ||
@Getter | ||
public enum Authority { | ||
CONSULTANT(UserRole.CONSULTANT, "AUTHORIZATION_CONSULTANT_DEFAULT"), | ||
USER(UserRole.USER, "AUTHORIZATION_USER_DEFAULT"), | ||
|
||
JITSI_TECHNICAL(UserRole.JITSI_TECHNICAL, "AUTHORIZATION_JITSI_TECHNICAL_DEFAULT"); | ||
|
||
private final UserRole role; | ||
private final List<String> authorities; | ||
|
||
Authority(final UserRole role, final String authorityName) { | ||
this.role = role; | ||
this.authorities = Lists.newArrayList(authorityName); | ||
} | ||
|
||
/** | ||
* Finds a {@link Authority} instance by given roleName. | ||
* | ||
* @param roleName the role name to search for | ||
* @return the {@link Authority} instance | ||
*/ | ||
public static Authority fromRoleName(String roleName) { | ||
return Stream.of(values()) | ||
.filter(authority -> authority.role.getValue().equals(roleName)) | ||
.findFirst() | ||
.orElse(null); | ||
} | ||
|
||
public static class AuthorityValue { | ||
|
||
private AuthorityValue() {} | ||
|
||
public static final String PREFIX = "AUTHORIZATION_"; | ||
public static final String CONSULTANT = PREFIX + "CONSULTANT_DEFAULT"; | ||
public static final String USER = PREFIX + "USER_DEFAULT"; | ||
public static final String JITSI_TECHNICAL = PREFIX + "JITSI_TECHNICAL_DEFAULT"; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/de/caritas/cob/liveservice/api/auth/JwtAuthConverter.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,61 @@ | ||
package de.caritas.cob.liveservice.api.auth; | ||
|
||
import de.caritas.cob.liveservice.config.security.JwtAuthConverterProperties; | ||
import java.util.Collection; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.security.authentication.AbstractAuthenticationToken; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.oauth2.jwt.Jwt; | ||
import org.springframework.security.oauth2.jwt.JwtClaimNames; | ||
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken; | ||
import org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class JwtAuthConverter implements Converter<Jwt, AbstractAuthenticationToken> { | ||
|
||
private final @NonNull AuthorisationService authorisationService; | ||
|
||
private final JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = | ||
new JwtGrantedAuthoritiesConverter(); | ||
|
||
private final JwtAuthConverterProperties properties; | ||
|
||
public JwtAuthConverter( | ||
JwtAuthConverterProperties properties, AuthorisationService authorisationService) { | ||
this.properties = properties; | ||
this.authorisationService = authorisationService; | ||
} | ||
|
||
@Override | ||
public AbstractAuthenticationToken convert(Jwt jwt) { | ||
var authorities = getGrantedAuthorities(jwt); | ||
return new JwtAuthenticationToken(jwt, authorities, getPrincipalClaimName(jwt)); | ||
} | ||
|
||
private Collection<GrantedAuthority> getGrantedAuthorities(Jwt jwt) { | ||
Collection<GrantedAuthority> convertedGrantedAuthorities = | ||
jwtGrantedAuthoritiesConverter.convert(jwt); | ||
if (convertedGrantedAuthorities != null) { | ||
return Stream.concat( | ||
convertedGrantedAuthorities.stream(), | ||
authorisationService.extractRealmAuthorities(jwt).stream()) | ||
.collect(Collectors.toSet()); | ||
} else { | ||
return authorisationService.extractRealmAuthorities(jwt); | ||
} | ||
} | ||
|
||
private String getPrincipalClaimName(Jwt jwt) { | ||
String claimName = JwtClaimNames.SUB; | ||
if (properties.getPrincipalAttribute() != null) { | ||
claimName = properties.getPrincipalAttribute(); | ||
} | ||
return jwt.getClaim(claimName); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/de/caritas/cob/liveservice/api/auth/RoleAuthorizationAuthorityMapper.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,37 @@ | ||
package de.caritas.cob.liveservice.api.auth; | ||
|
||
import java.util.Collection; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** Own implementation of the Spring GrantedAuthoritiesMapper. */ | ||
@Component | ||
public class RoleAuthorizationAuthorityMapper implements GrantedAuthoritiesMapper { | ||
|
||
@Override | ||
public Collection<? extends GrantedAuthority> mapAuthorities( | ||
Collection<? extends GrantedAuthority> authorities) { | ||
Set<String> roleNames = | ||
authorities.stream() | ||
.map(GrantedAuthority::getAuthority) | ||
.map(String::toLowerCase) | ||
.collect(Collectors.toSet()); | ||
|
||
return mapAuthorities(roleNames); | ||
} | ||
|
||
public Set<GrantedAuthority> mapAuthorities(Set<String> roleNames) { | ||
return roleNames.stream() | ||
.map(Authority::fromRoleName) | ||
.filter(Objects::nonNull) | ||
.map(Authority::getAuthorities) | ||
.flatMap(Collection::parallelStream) | ||
.map(SimpleGrantedAuthority::new) | ||
.collect(Collectors.toSet()); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/de/caritas/cob/liveservice/api/auth/UserRole.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,14 @@ | ||
package de.caritas.cob.liveservice.api.auth; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@AllArgsConstructor | ||
@Getter | ||
public enum UserRole { | ||
USER("user"), | ||
CONSULTANT("consultant"), | ||
JITSI_TECHNICAL("jitsi-technical"); | ||
|
||
private final String value; | ||
} |
20 changes: 0 additions & 20 deletions
20
...in/java/de/caritas/cob/liveservice/api/controller/CustomSwaggerApiResourceController.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.