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 | */ |
---|
15 | package org.codehaus.groovy.grails.plugins.springsecurity.facebook; |
---|
16 | |
---|
17 | import org.springframework.beans.factory.InitializingBean; |
---|
18 | import org.springframework.security.Authentication; |
---|
19 | import org.springframework.security.AuthenticationException; |
---|
20 | import org.springframework.security.AuthenticationServiceException; |
---|
21 | import org.springframework.security.BadCredentialsException; |
---|
22 | import org.springframework.security.providers.AuthenticationProvider; |
---|
23 | import org.springframework.security.userdetails.UserDetails; |
---|
24 | import org.springframework.security.userdetails.UserDetailsService; |
---|
25 | import 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 | */ |
---|
32 | public 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 | } |
---|