source: branches/TaskRewrite/src/plugins/acegi-0.5.1/src/java/org/codehaus/groovy/grails/plugins/springsecurity/facebook/FacebookAuthenticationProvider.java @ 58

Last change on this file since 58 was 58, checked in by gav, 15 years ago

Configure BootStrap? with latest concepts.
Install and setup Acegi plugin with custom views.
Test Fixture plugin in a test app but couldn't get it to work with Acegi encodePassword() so gave up.

File size: 3.3 KB
Line 
1/* Copyright 2006-2009 the original author or authors.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *      http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15package org.codehaus.groovy.grails.plugins.springsecurity.facebook;
16
17import org.springframework.beans.factory.InitializingBean;
18import org.springframework.security.Authentication;
19import org.springframework.security.AuthenticationException;
20import org.springframework.security.AuthenticationServiceException;
21import org.springframework.security.BadCredentialsException;
22import org.springframework.security.providers.AuthenticationProvider;
23import org.springframework.security.userdetails.UserDetails;
24import org.springframework.security.userdetails.UserDetailsService;
25import org.springframework.util.Assert;
26
27/**
28 * Finalizes the authentication process by populating the local authorities for the authenticated user.
29 *
30 * @author <a href='mailto:beckwithb@studentsonly.com'>Burt Beckwith</a>
31 */
32public class FacebookAuthenticationProvider implements AuthenticationProvider, InitializingBean {
33
34        private UserDetailsService _userDetailsService;
35
36        /**
37         * {@inheritDoc}
38         * @see org.springframework.security.providers.AuthenticationProvider#authenticate(
39         *      org.springframework.security.Authentication)
40         */
41        public Authentication authenticate(final Authentication authentication) throws AuthenticationException {
42
43                if (!supports(authentication.getClass()) ||
44                                !(authentication instanceof FacebookAuthenticationToken)) {
45                        return null;
46                }
47
48                FacebookAuthenticationToken token = (FacebookAuthenticationToken)authentication;
49                FacebookAuthenticationToken.Status status = token.getStatus();
50
51                switch (status) {
52                        case success:
53                                UserDetails userDetails = _userDetailsService.loadUserByUsername(String.valueOf(token.getUserId()));
54                                return new FacebookAuthenticationToken(userDetails.getAuthorities(),
55                                                token.getUserId(), token.getSessionKey());
56                        case failure:
57                                throw new BadCredentialsException("Log in failed - identity could not be verified");
58                        case error:
59                                throw new AuthenticationServiceException("Error message from server: " + token.getErrorMessage());
60                }
61
62                // unreachable
63                return null;
64        }
65
66        /**
67         * Dependency injection for the user detail service.
68         * @param userDetailsService  the service
69         */
70        public void setUserDetailsService(final UserDetailsService userDetailsService) {
71                _userDetailsService = userDetailsService;
72        }
73
74   /**
75    * {@inheritDoc}
76    * @see org.springframework.security.providers.AuthenticationProvider#supports(java.lang.Class)
77    */
78   @SuppressWarnings("unchecked")
79        public boolean supports(final Class authentication) {
80       return FacebookAuthenticationToken.class.isAssignableFrom(authentication);
81   }
82
83        /**
84         * {@inheritDoc}
85         * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
86         */
87        public void afterPropertiesSet() {
88                Assert.notNull(_userDetailsService, "The userDetailsService must be set");
89        }
90}
Note: See TracBrowser for help on using the repository browser.