<commit_before>package com.neoteric.starter.saasmgr;

import com.neoteric.starter.saasmgr.auth.SaasMgrAuthenticationToken;
import com.neoteric.starter.saasmgr.principal.SaasMgrPrincipal;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public final class SaasMgrAuthUtils {

    private SaasMgrAuthUtils() {
        throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
    }

    public static SaasMgrPrincipal getPrincipal() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        SaasMgrAuthenticationToken saasToken = (SaasMgrAuthenticationToken)authentication;
        return (SaasMgrPrincipal)saasToken.getPrincipal();
    }

    public static boolean isAnonymous() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return authentication instanceof AnonymousAuthenticationToken;
    }

    public static boolean isAuthorized() {
        return !isAnonymous();
    }

}<commit_msg>Fix ClassCastException, fixes #66 (#68)<commit_after>package com.neoteric.starter.saasmgr;

import com.neoteric.starter.saasmgr.auth.SaasMgrAuthenticationToken;
import com.neoteric.starter.saasmgr.principal.Feature;
import com.neoteric.starter.saasmgr.principal.SaasMgrPrincipal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Collection;

@Slf4j
public final class SaasMgrAuthUtils {

    private SaasMgrAuthUtils() {
        throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
    }

    public static SaasMgrPrincipal getPrincipal() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        SaasMgrAuthenticationToken saasToken = (SaasMgrAuthenticationToken)authentication;
        return (SaasMgrPrincipal)saasToken.getPrincipal();
    }

    public static boolean isNotAuthenticated() {
        return !isAuthenticated();
    }

    public static boolean isAuthenticated() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (!(authentication instanceof SaasMgrAuthenticationToken)) {
            return false;
        }
        SaasMgrAuthenticationToken saasToken = (SaasMgrAuthenticationToken)authentication;
        return saasToken.isAuthenticated();
    }

    @SuppressWarnings("unchecked")
    public static boolean isAuthorizedForFeature(String feature) {
        SaasMgrPrincipal principal = getPrincipal();
        Collection<Feature> authorities = (Collection<Feature>) principal.getAuthorities();
        boolean authorisedForFeature = authorities.stream().
                map(Feature::getAuthority)
                .filter(f -> f.equals(feature))
                .findAny().isPresent();
        LOG.debug("User [userId:{}, customerId:{}] authorised for feature: {}",
                principal.getUserId(), principal.getCustomerId(), feature);
        return authorisedForFeature;
    }
}
{'fraction_non_alphanumeric': 0.0861753958587089, 'fraction_numerical': 0.002131546894031669, 'mean_word_length': 4.2898550724637685, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2408793', 'n_tokens_mistral': 846, 'n_tokens_neox': 825, 'n_words': 165}