use of org.apache.syncope.core.persistence.api.entity.AccessToken in project syncope by apache.
the class AccessTokenTest method crud.
@Test
public void crud() {
AccessToken accessToken = entityFactory.newEntity(AccessToken.class);
accessToken.setKey(UUID.randomUUID().toString());
accessToken.setBody("pointless body");
accessToken.setExpiryTime(new Date());
accessToken.setOwner("bellini");
accessToken = accessTokenDAO.save(accessToken);
assertNotNull(accessToken);
accessTokenDAO.flush();
accessToken = accessTokenDAO.findByOwner("bellini");
assertNotNull(accessToken);
assertEquals("bellini", accessToken.getOwner());
accessTokenDAO.deleteExpired();
accessTokenDAO.flush();
accessToken = accessTokenDAO.findByOwner("bellini");
assertNull(accessToken);
}
use of org.apache.syncope.core.persistence.api.entity.AccessToken in project syncope by apache.
the class AccessTokenDataBinderImpl method create.
@Override
public Pair<String, Date> create(final String subject, final Map<String, Object> claims, final byte[] authorities, final boolean replaceExisting) {
String body = null;
Date expiryTime = null;
AccessToken existing = accessTokenDAO.findByOwner(subject);
if (existing != null) {
body = existing.getBody();
expiryTime = existing.getExpiryTime();
}
if (replaceExisting || body == null) {
Triple<String, String, Date> created = generateJWT(subject, confDAO.find("jwt.lifetime.minutes", 120L), claims);
body = created.getMiddle();
expiryTime = created.getRight();
AccessToken accessToken = entityFactory.newEntity(AccessToken.class);
accessToken.setKey(created.getLeft());
accessToken.setBody(body);
accessToken.setExpiryTime(expiryTime);
accessToken.setOwner(subject);
if (!adminUser.equals(accessToken.getOwner())) {
accessToken.setAuthorities(authorities);
}
accessTokenDAO.save(accessToken);
}
if (replaceExisting && existing != null) {
accessTokenDAO.delete(existing);
}
return Pair.of(body, expiryTime);
}
use of org.apache.syncope.core.persistence.api.entity.AccessToken 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.persistence.api.entity.AccessToken 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.persistence.api.entity.AccessToken in project syncope by apache.
the class JPAAccessTokenDAO method findByOwner.
@Transactional(readOnly = true)
@Override
public AccessToken findByOwner(final String username) {
TypedQuery<AccessToken> query = entityManager().createQuery("SELECT e FROM " + JPAAccessToken.class.getSimpleName() + " e " + "WHERE e.owner=:username", AccessToken.class);
query.setParameter("username", username);
AccessToken result = null;
try {
result = query.getSingleResult();
} catch (NoResultException e) {
LOG.debug("No token for user {} could be found", username, e);
}
return result;
}
Aggregations