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();
    }

}
@@ -151,105 +151,113 @@
 pal.
-SaasMgrPrincipal;%0Aimport org.springframework.security.authentication.AnonymousAuthenticationToken
+Feature;%0Aimport com.neoteric.starter.saasmgr.principal.SaasMgrPrincipal;%0Aimport lombok.extern.slf4j.Slf4j
 ;%0Aim
@@ -384,16 +384,53 @@
 older;%0A%0A
+import java.util.Collection;%0A%0A@Slf4j%0A
 public f
@@ -936,25 +936,120 @@
 olean is
-Anonymous
+NotAuthenticated() %7B%0A        return !isAuthenticated();%0A    %7D%0A%0A    public static boolean isAuthenticated
 () %7B%0A   
@@ -1149,23 +1149,22 @@
         
-return 
+if (!(
 authenti
@@ -1185,123 +1185,852 @@
 eof 
-AnonymousAuthenticationToken;%0A    %7D%0A%0A    public static boolean isAuthorized() %7B%0A        return !isAnonymous();%0A    %7D%0A
+SaasMgrAuthenticationToken)) %7B%0A            return false;%0A        %7D%0A        SaasMgrAuthenticationToken saasToken = (SaasMgrAuthenticationToken)authentication;%0A        return saasToken.isAuthenticated();%0A    %7D%0A%0A    @SuppressWarnings(%22unchecked%22)%0A    public static boolean isAuthorizedForFeature(String feature) %7B%0A        SaasMgrPrincipal principal = getPrincipal();%0A        Collection%3CFeature%3E authorities = (Collection%3CFeature%3E) principal.getAuthorities();%0A        boolean authorisedForFeature = authorities.stream().%0A                map(Feature::getAuthority)%0A                .filter(f -%3E f.equals(feature))%0A                .findAny().isPresent();%0A        LOG.debug(%22User %5BuserId:%7B%7D, customerId:%7B%7D%5D authorised for feature: %7B%7D%22,%0A                principal.getUserId(), principal.getCustomerId(), feature);%0A        return authorisedForFeature;%0A    %7D
 %0A%7D

Fix ClassCastException, fixes #66 (#68)