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

com.payneteasy.superfly.security.SuperflyUsernamePasswordAuthenticationProvider Maven / Gradle / Ivy

Go to download

Module for Spring Security which enables application to use Superfly authentication/authorization declaratively through Spring Security

There is a newer version: 1.7-32
Show newest version
package com.payneteasy.superfly.security;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;

import com.payneteasy.superfly.api.SSOService;
import com.payneteasy.superfly.api.SSOUser;
import com.payneteasy.superfly.security.authentication.UsernamePasswordAuthRequestInfoAuthenticationToken;
import com.payneteasy.superfly.security.authentication.UsernamePasswordCheckedToken;

/**
 * {@link AuthenticationProvider} which uses Superfly password authentication
 * to authenticate user.
 * 
 * @author Roman Puchkovskiy
 */
public class SuperflyUsernamePasswordAuthenticationProvider implements AuthenticationProvider {
	
	private SSOService ssoService;

	@Required
	public void setSsoService(SSOService ssoService) {
		this.ssoService = ssoService;
	}

	public Authentication authenticate(Authentication authentication)
			throws AuthenticationException {
		if (authentication instanceof UsernamePasswordAuthRequestInfoAuthenticationToken) {
			UsernamePasswordAuthRequestInfoAuthenticationToken authRequest = (UsernamePasswordAuthRequestInfoAuthenticationToken) authentication;
			if (authRequest.getCredentials() == null) {
				throw new BadCredentialsException("Null credentials");
			}
			SSOUser ssoUser = ssoService.authenticate(authRequest.getName(),
					authRequest.getCredentials().toString(),
					authRequest.getAuthRequestInfo());
			if (ssoUser == null) {
				throw new BadCredentialsException("Bad credentials");
			}
			if (ssoUser.getActionsMap().isEmpty()) {
				throw new BadCredentialsException("No roles");
			}
			return createAuthentication(ssoUser);
		}
		return null;
	}

	public boolean supports(Class authentication) {
		return UsernamePasswordAuthRequestInfoAuthenticationToken.class.isAssignableFrom(authentication);
	}

	protected Authentication createAuthentication(SSOUser ssoUser) {
		return new UsernamePasswordCheckedToken(ssoUser);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy