Search in sources :

Example 6 with AccessToken

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);
}
Also used : AccessToken(org.apache.syncope.core.persistence.api.entity.AccessToken) Date(java.util.Date) Test(org.junit.jupiter.api.Test) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest)

Example 7 with 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);
}
Also used : AccessToken(org.apache.syncope.core.persistence.api.entity.AccessToken) Date(java.util.Date)

Example 8 with AccessToken

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;
}
Also used : SyncopeGrantedAuthority(org.apache.syncope.core.spring.security.SyncopeGrantedAuthority) AccessToken(org.apache.syncope.core.persistence.api.entity.AccessToken) TypeReference(com.fasterxml.jackson.core.type.TypeReference) AnyType(org.apache.syncope.core.persistence.api.entity.AnyType) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) HashSet(java.util.HashSet)

Example 9 with AccessToken

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;
}
Also used : SyncopeGrantedAuthority(org.apache.syncope.core.spring.security.SyncopeGrantedAuthority) AccessToken(org.apache.syncope.core.persistence.api.entity.AccessToken) AnyTypeTO(org.apache.syncope.common.lib.to.AnyTypeTO) TypeReference(com.fasterxml.jackson.core.type.TypeReference) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) HashSet(java.util.HashSet)

Example 10 with AccessToken

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;
}
Also used : AccessToken(org.apache.syncope.core.persistence.api.entity.AccessToken) JPAAccessToken(org.apache.syncope.core.persistence.jpa.entity.JPAAccessToken) NoResultException(javax.persistence.NoResultException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

AccessToken (org.apache.syncope.core.persistence.api.entity.AccessToken)11 HashSet (java.util.HashSet)4 Transactional (org.springframework.transaction.annotation.Transactional)4 TypeReference (com.fasterxml.jackson.core.type.TypeReference)3 Date (java.util.Date)3 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)3 User (org.apache.syncope.core.persistence.api.entity.user.User)3 Set (java.util.Set)2 JPAAccessToken (org.apache.syncope.core.persistence.jpa.entity.JPAAccessToken)2 SyncopeGrantedAuthority (org.apache.syncope.core.spring.security.SyncopeGrantedAuthority)2 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Resource (javax.annotation.Resource)1 NoResultException (javax.persistence.NoResultException)1 BooleanUtils (org.apache.commons.lang3.BooleanUtils)1