All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.smartcosmos.extension.stormpath.converter.AccountToUserDetailsConverter Maven / Gradle / Ivy

The newest version!
package net.smartcosmos.extension.stormpath.converter;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

import com.stormpath.sdk.account.Account;
import com.stormpath.sdk.directory.CustomData;
import com.stormpath.sdk.directory.Directory;
import com.stormpath.sdk.group.Group;
import com.stormpath.sdk.group.GroupStatus;
import lombok.extern.slf4j.Slf4j;

import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import net.smartcosmos.userdetails.domain.UserDetails;

@Component
@Slf4j
public class AccountToUserDetailsConverter implements Converter {

    public static final String KEY_AUTHORITIES = "authorities";
    public static final String KEY_URN = "urn";

    @Override
    public UserDetails convert(Account account) {

        return UserDetails.builder()
            .username(account.getUsername())
            .userUrn(getUserUrn(account))
            .tenantUrn(getTenantUrn(account))
            .authorities(getAuthorities(account))
            .build();
    }

    protected Set getAuthorities(Account account) {

        if (account.getGroups() == null) {
            return Collections.emptySet();
        }

        Set authoritiesSet = new HashSet<>();
        try {
            for (Group group : account.getGroups()) {
                if (GroupStatus.ENABLED.equals(group.getStatus())) {
                    authoritiesSet.addAll(getAuthoritiesFromCustomData(group.getCustomData()));
                }
            }
        } catch (UnsupportedOperationException | ClassCastException | NullPointerException | IllegalArgumentException e) {
            String msg = String.format("Unexpected exception when reading authorities from groups for account %s: %s", account, e.getMessage());
            log.warn(msg);
            log.debug(msg, e);
        }
        return authoritiesSet;
    }

    protected String getUserUrn(Account account) {

        return getUrnFromCustomData(account.getCustomData());
    }

    protected String getTenantUrn(Account account) {

        Directory directory = account.getDirectory();
        if (directory == null) {
            return "";
        }

        return getUrnFromCustomData(directory.getCustomData());
    }

    protected String getUrnFromCustomData(CustomData customData) {

        try {
            if (customData != null && customData.containsKey(KEY_URN) && customData.get(KEY_URN) instanceof String) {
                return (String) customData.get(KEY_URN);
            }
        } catch (ClassCastException e) {
            String msg = String.format("Unexpected exception when reading URN from custom data %s: %s", customData, e.getMessage());
            log.warn(msg);
            log.debug(msg, e);
        }
        return "";
    }

    @SuppressWarnings("unchecked")
    protected Collection getAuthoritiesFromCustomData(CustomData customData) {

        try {
            if (customData != null && customData.containsKey(KEY_AUTHORITIES) && customData.get(KEY_AUTHORITIES) instanceof Collection) {
                return ((Collection) customData.get(KEY_AUTHORITIES)).stream()
                    .filter(element -> element != null)
                    .map(element -> (String) element)
                    .collect(Collectors.toSet());
            }
        } catch (ClassCastException e) {
            String msg = String.format("Unexpected exception when reading authorities from custom data %s: %s", customData, e.getMessage());
            log.warn(msg);
            log.debug(msg, e);
        }
        return Collections.emptySet();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy