Skip to content
Snippets Groups Projects
Commit f9d035e1 authored by Evgeny Bardin's avatar Evgeny Bardin
Browse files

OZG-7092 fix attribute filter

parent 6c64ce4f
No related branches found
No related tags found
1 merge request!1OZG-7092 Anpassung TokenChecker
......@@ -26,7 +26,6 @@ package de.ozgcloud.token.saml;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
......@@ -63,8 +62,6 @@ import net.shibboleth.utilities.java.support.resolver.CriteriaSet;
@Builder
public class SamlAttributeService {
private static final String ID_AS_POSTFACH_ID_KEY = "OZG_CLOUD_POSTFACH_ID";
private final SignatureTrustEngine signatureTrustEngine;
private final Decrypter decrypter;
private final SAMLSignatureProfileValidator profileValidator;
......@@ -152,26 +149,29 @@ public class SamlAttributeService {
}
TokenAttributes buildTokenAttributes(Map<String, String> tokenAttributes, Response token) {
var result = TokenAttributes.builder().postfachId(getPostfachId(tokenAttributes, token)).trustLevel(getTrustLevel(tokenAttributes));
tokenAttributes.entrySet().stream().filter(this::isNotMappedField).map(this::buildTokenAttribute).forEach(result::otherAttribute);
return result.build();
var tokenAttributesBuilder = TokenAttributes.builder().postfachId(getPostfachId(tokenAttributes, token))
.trustLevel(getTrustLevel(tokenAttributes));
tokenAttributes.entrySet().stream().filter(this::isNotMappedField).map(this::buildTokenAttribute)
.forEach(tokenAttributesBuilder::otherAttribute);
return tokenAttributesBuilder.build();
}
String getPostfachId(Map<String, String> tokenAttributes, Response token) {
return tokenValidationProperty.isUseIdAsPostfachId() ? token.getID() : getMappedValue(tokenAttributes, TokenAttributes.POSTFACH_ID_KEY);
return tokenValidationProperty.isUseIdAsPostfachId() ? token.getID() : getValue(tokenAttributes, TokenAttributes.POSTFACH_ID_KEY);
}
String getTrustLevel(Map<String, String> tokenAttributes) {
return getMappedValue(tokenAttributes, TokenAttributes.TRUST_LEVEL_KEY);
return getValue(tokenAttributes, TokenAttributes.TRUST_LEVEL_KEY);
}
String getMappedValue(Map<String, String> tokenAttributes, String key) {
String getValue(Map<String, String> tokenAttributes, String key) {
var mappedKey = tokenValidationProperty.getMappings().getOrDefault(key, key);
return tokenAttributes.get(mappedKey);
}
boolean isNotMappedField(Map.Entry<String, String> attributeEntry) {
return !tokenValidationProperty.getMappings().containsValue(attributeEntry.getKey());
var mappedKey = tokenValidationProperty.getMappings().get(attributeEntry.getKey());
return !StringUtils.equalsAny(mappedKey, TokenAttributes.POSTFACH_ID_KEY, TokenAttributes.TRUST_LEVEL_KEY);
}
TokenAttribute buildTokenAttribute(Map.Entry<String, String> attribute) {
......
......@@ -747,17 +747,19 @@ class SamlAttributeServiceTest {
assertThat(result).isTrue();
}
@Test
void shouldReturnFalseWhenMapped() {
when(tokenValidationProperty.getMappings()).thenReturn(Map.of(KEY, TokenAttributeTestFactory.NAME));
@DisplayName("should return false when")
@ParameterizedTest(name = "key is {0}")
@ValueSource(strings = { TokenAttributes.POSTFACH_ID_KEY, TokenAttributes.TRUST_LEVEL_KEY })
void shouldReturnFalseWhenMapped(String mappedKey) {
when(tokenValidationProperty.getMappings()).thenReturn(Map.of(KEY, mappedKey));
var result = service.isNotMappedField(Map.entry(TokenAttributeTestFactory.NAME, TokenAttributeTestFactory.VALUE));
var result = isNotMappedField();
assertThat(result).isFalse();
}
private boolean isNotMappedField() {
return service.isNotMappedField(Map.entry(TokenAttributeTestFactory.NAME, TokenAttributeTestFactory.VALUE));
return service.isNotMappedField(Map.entry(KEY, TokenAttributeTestFactory.VALUE));
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment