Search in sources :

Example 1 with SwitchUserGrantedAuthority

use of org.springframework.security.web.authentication.switchuser.SwitchUserGrantedAuthority in project ORCID-Source by ORCID.

the class SourceManagerImpl method isDelegatedByAnAdmin.

@Override
public boolean isDelegatedByAnAdmin() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null) {
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        if (authorities != null) {
            for (GrantedAuthority authority : authorities) {
                if (authority instanceof SwitchUserGrantedAuthority) {
                    SwitchUserGrantedAuthority suga = (SwitchUserGrantedAuthority) authority;
                    Authentication sourceAuthentication = suga.getSource();
                    if (sourceAuthentication instanceof UsernamePasswordAuthenticationToken && sourceAuthentication.getPrincipal() instanceof OrcidProfileUserDetails) {
                        org.orcid.jaxb.model.message.OrcidType legacyOrcidType = ((OrcidProfileUserDetails) sourceAuthentication.getPrincipal()).getOrcidType();
                        OrcidType sourceUserType = legacyOrcidType == null ? null : OrcidType.fromValue(legacyOrcidType.value());
                        return OrcidType.ADMIN.equals(sourceUserType);
                    }
                }
            }
        }
    }
    return false;
}
Also used : OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) OrcidType(org.orcid.jaxb.model.common_v2.OrcidType) GrantedAuthority(org.springframework.security.core.GrantedAuthority) SwitchUserGrantedAuthority(org.springframework.security.web.authentication.switchuser.SwitchUserGrantedAuthority) OrcidProfileUserDetails(org.orcid.core.oauth.OrcidProfileUserDetails) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) SwitchUserGrantedAuthority(org.springframework.security.web.authentication.switchuser.SwitchUserGrantedAuthority)

Example 2 with SwitchUserGrantedAuthority

use of org.springframework.security.web.authentication.switchuser.SwitchUserGrantedAuthority in project ORCID-Source by ORCID.

the class SourceManagerImpl method getRealUserIfInDelegationMode.

private String getRealUserIfInDelegationMode(Authentication authentication) {
    if (authentication != null) {
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        if (authorities != null) {
            for (GrantedAuthority authority : authorities) {
                if (authority instanceof SwitchUserGrantedAuthority) {
                    SwitchUserGrantedAuthority suga = (SwitchUserGrantedAuthority) authority;
                    Authentication sourceAuthentication = suga.getSource();
                    if ((sourceAuthentication instanceof UsernamePasswordAuthenticationToken || sourceAuthentication instanceof PreAuthenticatedAuthenticationToken) && sourceAuthentication.getPrincipal() instanceof OrcidProfileUserDetails) {
                        return ((OrcidProfileUserDetails) sourceAuthentication.getPrincipal()).getOrcid();
                    }
                }
            }
        }
    }
    return null;
}
Also used : OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) GrantedAuthority(org.springframework.security.core.GrantedAuthority) SwitchUserGrantedAuthority(org.springframework.security.web.authentication.switchuser.SwitchUserGrantedAuthority) OrcidProfileUserDetails(org.orcid.core.oauth.OrcidProfileUserDetails) PreAuthenticatedAuthenticationToken(org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) SwitchUserGrantedAuthority(org.springframework.security.web.authentication.switchuser.SwitchUserGrantedAuthority)

Aggregations

OrcidProfileUserDetails (org.orcid.core.oauth.OrcidProfileUserDetails)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 Authentication (org.springframework.security.core.Authentication)2 GrantedAuthority (org.springframework.security.core.GrantedAuthority)2 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)2 SwitchUserGrantedAuthority (org.springframework.security.web.authentication.switchuser.SwitchUserGrantedAuthority)2 OrcidType (org.orcid.jaxb.model.common_v2.OrcidType)1 PreAuthenticatedAuthenticationToken (org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken)1