br.com.itw.qopsearch.api.security.ScaAuthenticationProvider.java Source code

Java tutorial

Introduction

Here is the source code for br.com.itw.qopsearch.api.security.ScaAuthenticationProvider.java

Source

/**
 *  Guick Generate class: https://github.com/wdavilaneto/guick
 *  Author: service-wdavilaneto@redhat.com
 *  This source is free under The Apache Software License, Version 2.0
 *  license url http://www.apache.org/licenses/LICENSE-2.0.txt
 */
package br.com.itw.qopsearch.api.security;

import org.apache.commons.validator.routines.EmailValidator;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by igor.custodio on 23/01/2015.
 */
@Service
public class ScaAuthenticationProvider implements AuthenticationProvider {

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        String login = authentication.getName().trim().toLowerCase();
        //        String password = authentication.getCredentials().toString();

        //        byte[] decodedBytes = Base64.decode(password.getBytes());
        //        String decodedPasswd = new String(decodedBytes, Charset.forName("UTF-8"));

        if (EmailValidator.getInstance().isValid(login)) {
            //        if (ValidarEmail.validate(login) && login.split("\\@")[0].equals(decodedPasswd)) {
            List<GrantedAuthority> grantedAuths = new ArrayList<>();
            grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
            Authentication auth = new UsernamePasswordAuthenticationToken(login, null, grantedAuths);
            return auth;
        } else {
            return null;
        }
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return authentication.equals(UsernamePasswordAuthenticationToken.class);
    }

}