source: branches/TaskRewrite/src/plugins/acegi-0.5.1/src/java/org/codehaus/groovy/grails/plugins/springsecurity/facebook/FacebookAuthenticationProcessingFilter.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: 5.7 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 javax.servlet.http.HttpServletRequest;
18import javax.servlet.http.HttpServletResponse;
19import javax.servlet.http.HttpSession;
20
21import org.codehaus.groovy.grails.plugins.springsecurity.SecurityRequestHolder;
22import org.springframework.security.Authentication;
23import org.springframework.security.AuthenticationException;
24import org.springframework.security.ui.AbstractProcessingFilter;
25import org.springframework.security.ui.FilterChainOrder;
26import org.springframework.security.ui.webapp.AuthenticationProcessingFilter;
27import org.springframework.util.Assert;
28import org.springframework.util.StringUtils;
29import org.w3c.dom.Document;
30
31import com.google.code.facebookapi.FacebookWebappHelper;
32
33/**
34 * Intercepts j_spring_facebook_security_check to trigger Facebook login.
35 *
36 * @author <a href='mailto:beckwithb@studentsonly.com'>Burt Beckwith</a>
37 */
38public class FacebookAuthenticationProcessingFilter extends AbstractProcessingFilter {
39
40        private String _apiKey;
41        private String _secretKey;
42        private String _authenticationUrlRoot;
43
44        /**
45         * {@inheritDoc}
46         * @see org.springframework.security.ui.AbstractProcessingFilter#attemptAuthentication(
47         *      javax.servlet.http.HttpServletRequest)
48         */
49        @Override
50        public Authentication attemptAuthentication(final HttpServletRequest request) throws AuthenticationException {
51
52                String authToken = request.getParameter("auth_token");
53                if (!StringUtils.hasText(authToken)) {
54                        // trigger a redirect to the Facebook login
55                        throw new FacebookAuthenticationRequiredException();
56                }
57
58                FacebookAuthenticationToken token = createToken(
59                                authToken, request, SecurityRequestHolder.getResponse(),
60                                _apiKey, _secretKey);
61
62                token.setDetails(authenticationDetailsSource.buildDetails(request));
63
64                Authentication authentication = getAuthenticationManager().authenticate(token);
65                if (authentication.isAuthenticated()) {
66                        setLastUsername(token.getUserId(), request);
67                }
68
69                return authentication;
70        }
71
72        private void setLastUsername(final long userId, final HttpServletRequest request) {
73                HttpSession session = request.getSession(false);
74                if (session != null || getAllowSessionCreation()) {
75                        request.getSession().setAttribute(
76                                        AuthenticationProcessingFilter.SPRING_SECURITY_LAST_USERNAME_KEY,
77                                        String.valueOf(userId));
78                }
79        }
80
81        /**
82         * Build an authentication from a login <code>auth_token</code>.
83         * @param authToken  the <code>auth_token</code>
84         * @param request  the http request
85         * @param response  the http response
86         * @param apiKey  the API key
87         * @param secretKey  the secret key
88         * @return  the auth token
89         */
90        protected FacebookAuthenticationToken createToken(
91                        final String authToken, final HttpServletRequest request, final HttpServletResponse response,
92                        final String apiKey, final String secretKey) {
93
94                try {
95                        FacebookWebappHelper<Document> helper = FacebookWebappHelper.newInstanceXml(
96                                        request, response, apiKey, secretKey);
97
98                        if (helper.isLogin()) {
99                                String sessionKey = helper.doGetSession(authToken);
100                                return new FacebookAuthenticationToken(helper.getUser().longValue(), sessionKey);
101                        }
102
103                        return new FacebookAuthenticationToken(FacebookAuthenticationToken.Status.failure, null);
104                }
105                catch (RuntimeException e) {
106                        return new FacebookAuthenticationToken(FacebookAuthenticationToken.Status.error, e.getMessage());
107                }
108        }
109
110        /**
111         * {@inheritDoc}
112         * @see org.springframework.security.ui.AbstractProcessingFilter#determineFailureUrl(
113         *      javax.servlet.http.HttpServletRequest, org.springframework.security.AuthenticationException)
114         */
115        @Override
116        protected String determineFailureUrl(final HttpServletRequest request, final AuthenticationException failed) {
117                if (failed instanceof FacebookAuthenticationRequiredException) {
118                        return _authenticationUrlRoot + _apiKey;
119                }
120
121                return super.determineFailureUrl(request, failed);
122        }
123
124        /**
125         * {@inheritDoc}
126         * @see org.springframework.security.ui.AbstractProcessingFilter#getDefaultFilterProcessesUrl()
127         */
128        @Override
129        public String getDefaultFilterProcessesUrl() {
130                return "/j_spring_facebook_security_check";
131        }
132
133        /**
134         * {@inheritDoc}
135         * @see org.springframework.security.ui.SpringSecurityFilter#getOrder()
136         */
137        public int getOrder() {
138                return FilterChainOrder.OPENID_PROCESSING_FILTER + 1;
139        }
140
141        /**
142         * Dependency injection for the API key.
143         * @param key  the key
144         */
145        public void setApiKey(final String key) {
146                _apiKey = key;
147        }
148
149        /**
150         * Dependency injection for the secret key.
151         * @param key  the key
152         */
153        public void setSecretKey(final String key) {
154                _secretKey = key;
155        }
156
157        /**
158         * Dependency injection for the Facebook auth url root.
159         * @param authenticationUrlRoot  the url root
160         */
161        public void setAuthenticationUrlRoot(String authenticationUrlRoot) {
162                _authenticationUrlRoot = authenticationUrlRoot;
163        }
164
165        /**
166         * {@inheritDoc}
167         * @see org.springframework.security.ui.AbstractProcessingFilter#afterPropertiesSet()
168         */
169        @Override
170        public void afterPropertiesSet() throws Exception {
171                super.afterPropertiesSet();
172      Assert.notNull(_apiKey, "API key must be specified");
173      Assert.notNull(_secretKey, "Secret key must be specified");
174        }
175}
Note: See TracBrowser for help on using the repository browser.