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

fi.evolver.ai.vaadin.util.AuthUtils Maven / Gradle / Ivy

There is a newer version: 1.5.5
Show newest version
package fi.evolver.ai.vaadin.util;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal;

public class AuthUtils {
	private static Set adminUsers = new HashSet<>();
	private static Set domains = new HashSet<>();

	private AuthUtils() { /* Utility class */ }

	public static String getUsername() {
		return getAttribute("given_name");
	}

	public static String getEmail() {
		return getAttribute("email");
	}

	private static String getAttribute(String attribute) {
		OAuth2AuthenticatedPrincipal principal = getPrincipal();
		return principal != null ? principal.getAttribute(attribute) : null;
	}

	public static boolean isAdminUser() {
		String email = getEmail();
		return email != null && ((domains.contains(email.substring(email.indexOf('@') + 1))) || adminUsers.contains(email));
	}

	public static void setAdminUsers(List adminUserList) {
		if (adminUserList != null && !adminUserList.isEmpty() && adminUsers.isEmpty())
			adminUsers = new HashSet<>(adminUserList);
	}

	public static void setAdminDomains(List domainsList) {
		if (domainsList != null && !domainsList.isEmpty() && domains.isEmpty())
			domains = new HashSet<>(domainsList);
	}

	private static OAuth2AuthenticatedPrincipal getPrincipal() {
		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if (!(principal instanceof OAuth2AuthenticatedPrincipal))
			return null;
		return (OAuth2AuthenticatedPrincipal) principal;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy