001/**
002 * Copyright 2014 Tampere University of Technology, Pori Department
003 * 
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 *   http://www.apache.org/licenses/LICENSE-2.0
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package service.tut.pori.users.ip;
017
018import org.apache.log4j.Logger;
019import org.springframework.security.authentication.AuthenticationProvider;
020import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
021import org.springframework.security.core.Authentication;
022import org.springframework.security.core.AuthenticationException;
023import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;
024
025import core.tut.pori.users.UserIdentity;
026
027/**
028 * A simple authentication provider which converts the given authentication to UsernamePasswordAuthenticationToken, setting UserIdentity, credentials and authorities
029 *
030 */
031public class PreAuthenticationProvider implements AuthenticationProvider {
032  private static final Logger LOGGER = Logger.getLogger(PreAuthenticationProvider.class);
033  
034  @Override
035  public Authentication authenticate(Authentication authentication) throws AuthenticationException {
036    UserIdentity userIdentity = (UserIdentity) authentication.getPrincipal();
037    LOGGER.debug("Granting access for user, id: "+userIdentity.getUserId());
038    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(userIdentity, userIdentity.getPassword(), userIdentity.getAuthorities());
039    return token;
040  }
041
042  @Override
043  public boolean supports(Class<?> authentication) {
044    return authentication.isAssignableFrom(PreAuthenticatedAuthenticationToken.class);
045  }
046
047}