use of org.apache.syncope.core.spring.security.SyncopeGrantedAuthority in project syncope by apache.
the class AnyTypeDataBinderImpl method create.
@Override
public AnyType create(final AnyTypeTO anyTypeTO) {
AnyType anyType = entityFactory.newEntity(AnyType.class);
update(anyType, anyTypeTO);
Set<String> added = EntitlementsHolder.getInstance().addFor(anyType.getKey());
if (!adminUser.equals(AuthContextUtils.getUsername())) {
AccessToken accessToken = accessTokenDAO.findByOwner(AuthContextUtils.getUsername());
try {
Set<SyncopeGrantedAuthority> authorities = new HashSet<>(POJOHelper.deserialize(ENCRYPTOR.decode(new String(accessToken.getAuthorities()), CipherAlgorithm.AES), new TypeReference<Set<SyncopeGrantedAuthority>>() {
}));
added.forEach(entitlement -> {
authorities.add(new SyncopeGrantedAuthority(entitlement, SyncopeConstants.ROOT_REALM));
});
accessToken.setAuthorities(ENCRYPTOR.encode(POJOHelper.serialize(authorities), CipherAlgorithm.AES).getBytes());
accessTokenDAO.save(accessToken);
} catch (Exception e) {
LOG.error("Could not fetch or store authorities", e);
}
}
return anyType;
}
use of org.apache.syncope.core.spring.security.SyncopeGrantedAuthority in project syncope by apache.
the class AnyTypeDataBinderImpl method delete.
@Override
public AnyTypeTO delete(final AnyType anyType) {
AnyTypeTO deleted = getAnyTypeTO(anyType);
anyTypeDAO.delete(anyType.getKey());
final Set<String> removed = EntitlementsHolder.getInstance().removeFor(deleted.getKey());
if (!adminUser.equals(AuthContextUtils.getUsername())) {
AccessToken accessToken = accessTokenDAO.findByOwner(AuthContextUtils.getUsername());
try {
Set<SyncopeGrantedAuthority> authorities = new HashSet<>(POJOHelper.deserialize(ENCRYPTOR.decode(new String(accessToken.getAuthorities()), CipherAlgorithm.AES), new TypeReference<Set<SyncopeGrantedAuthority>>() {
}));
authorities.removeAll(authorities.stream().filter(authority -> removed.contains(authority.getAuthority())).collect(Collectors.toList()));
accessToken.setAuthorities(ENCRYPTOR.encode(POJOHelper.serialize(authorities), CipherAlgorithm.AES).getBytes());
accessTokenDAO.save(accessToken);
} catch (Exception e) {
LOG.error("Could not fetch or store authorities", e);
}
}
return deleted;
}
use of org.apache.syncope.core.spring.security.SyncopeGrantedAuthority in project syncope by apache.
the class ResourceDataBinderTest method setAuthContext.
@BeforeAll
public static void setAuthContext() {
List<GrantedAuthority> authorities = StandardEntitlement.values().stream().map(entitlement -> new SyncopeGrantedAuthority(entitlement, SyncopeConstants.ROOT_REALM)).collect(Collectors.toList());
UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(new org.springframework.security.core.userdetails.User("admin", "FAKE_PASSWORD", authorities), "FAKE_PASSWORD", authorities);
auth.setDetails(new SyncopeAuthenticationDetails("Master"));
SecurityContextHolder.getContext().setAuthentication(auth);
}
Aggregations