@PreAuthorize("isAuthenticated()") public Pair<String, Date> refresh() { AccessToken accessToken = accessTokenDAO.findByOwner(AuthContextUtils.getUsername()); if (accessToken == null) { throw new NotFoundException("AccessToken for " + AuthContextUtils.getUsername()); } return binder.update(accessToken, getAuthorities()); }
@PreAuthorize("isAuthenticated()") public void logout() { AccessToken accessToken = accessTokenDAO.findByOwner(AuthContextUtils.getUsername()); if (accessToken == null) { throw new NotFoundException("AccessToken for " + AuthContextUtils.getUsername()); } delete(accessToken.getKey()); }
@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; }
@Override public Pair<String, Date> create( final String subject, final Map<String, Object> claims, final byte[] authorities, final boolean replace) { AccessToken accessToken = accessTokenDAO.findByOwner(subject); if (accessToken == null) { // no AccessToken found: create new accessToken = entityFactory.newEntity(AccessToken.class); accessToken.setKey(SecureRandomUtils.generateRandomUUID().toString()); accessToken = replace(subject, claims, authorities, accessToken); } else if (replace || accessToken.getExpiryTime() == null || accessToken.getExpiryTime().before(new Date())) { // AccessToken found, but either replace was requested or it is expired: update existing accessToken = replace(subject, claims, authorities, accessToken); } return Pair.of(accessToken.getBody(), accessToken.getExpiryTime()); }
@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; }
@Override public void delete(final User user) { roleDAO.removeDynMemberships(user.getKey()); groupDAO.removeDynMemberships(user); dynRealmDAO.removeDynMemberships(user.getKey()); AccessToken accessToken = accessTokenDAO.findByOwner(user.getUsername()); if (accessToken != null) { accessTokenDAO.delete(accessToken); } entityManager().remove(user); publisher.publishEvent(new AnyDeletedEvent( this, AnyTypeKind.USER, user.getKey(), user.getUsername(), AuthContextUtils.getDomain())); }
@PreAuthorize("isAuthenticated() " + "and not(hasRole('" + StandardEntitlement.ANONYMOUS + "')) " + "and not(hasRole('" + StandardEntitlement.MUST_CHANGE_PASSWORD + "'))") public ProvisioningResult<UserTO> selfUpdate(final UserPatch userPatch, final boolean nullPriorityAsync) { UserTO userTO = binder.getAuthenticatedUserTO(); userPatch.setKey(userTO.getKey()); ProvisioningResult<UserTO> updated = doUpdate(userPatch, true, nullPriorityAsync); // Ensures that, if the self update above moves the user into a status from which no authentication // is possible, the existing Access Token is clean up to avoid issues with future authentications if (!confDAO.getValuesAsStrings("authentication.statuses").contains(updated.getEntity().getStatus())) { String accessToken = accessTokenDAO.findByOwner(updated.getEntity().getUsername()).getKey(); if (accessToken != null) { accessTokenDAO.delete(accessToken); } } return updated; }
AccessToken accessToken = accessTokenDAO.findByOwner(oldUsername); if (accessToken != null) { accessToken.setOwner(userPatch.getUsername().getValue());