source: branches/TaskRewrite/src/plugins/acegi-0.5.1/src/groovy/org/codehaus/groovy/grails/plugins/springsecurity/openid/GrailsOpenIdAuthenticationProvider.groovy @ 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.6 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.openid
16
17import org.springframework.security.Authentication
18import org.springframework.security.AuthenticationException
19import org.springframework.security.AuthenticationServiceException
20import org.springframework.security.BadCredentialsException
21import org.springframework.security.providers.openid.AuthenticationCancelledException
22import org.springframework.security.providers.openid.OpenIDAuthenticationStatus
23import org.springframework.security.providers.openid.OpenIDAuthenticationProvider
24import org.springframework.security.providers.openid.OpenIDAuthenticationToken
25import org.springframework.security.userdetails.UserDetails
26import org.springframework.security.userdetails.UserDetailsService
27
28/**
29 * Subclass that returns a {@link GrailsOpenIdAuthenticationToken}.
30 *
31 * @author <a href='mailto:beckwithb@studentsonly.com'>Burt Beckwith</a>
32 */
33class GrailsOpenIdAuthenticationProvider extends OpenIDAuthenticationProvider {
34
35        private _userDetailsService
36
37        /**
38         * {@inheritDoc}
39         * @see org.springframework.security.providers.openid.OpenIDAuthenticationProvider#authenticate(
40         *      org.springframework.security.Authentication)
41         */
42        @Override
43        Authentication authenticate(Authentication authentication) throws AuthenticationException {
44
45                if (!supports(authentication.getClass())) {
46                        return null
47                }
48
49                if (authentication instanceof OpenIDAuthenticationToken) {
50                        OpenIDAuthenticationToken response = (OpenIDAuthenticationToken) authentication
51                        OpenIDAuthenticationStatus status = response.status
52
53                        // handle the various possibilites
54                        if (status == OpenIDAuthenticationStatus.SUCCESS) {
55                                // Lookup user details
56                                UserDetails userDetails = _userDetailsService.loadUserByUsername(response.identityUrl)
57                                return new GrailsOpenIdAuthenticationToken(userDetails, response.status, response.identityUrl)
58                        }
59
60                        if (status == OpenIDAuthenticationStatus.CANCELLED) {
61                                throw new AuthenticationCancelledException("Log in cancelled")
62                        }
63
64                        if (status == OpenIDAuthenticationStatus.ERROR) {
65                                throw new AuthenticationServiceException("Error message from server: $response.message")
66                        }
67
68                        if (status == OpenIDAuthenticationStatus.FAILURE) {
69                                throw new BadCredentialsException("Log in failed - identity could not be verified")
70                        }
71
72                        if (status == OpenIDAuthenticationStatus.SETUP_NEEDED) {
73                                throw new AuthenticationServiceException(
74                                                "The server responded setup was needed, which shouldn't happen")
75                        }
76
77                        throw new AuthenticationServiceException("Unrecognized return value $status")
78                }
79
80                return null
81        }
82
83        /**
84         * {@inheritDoc}
85         * @see org.springframework.security.providers.openid.OpenIDAuthenticationProvider#setUserDetailsService(
86         *      org.springframework.security.userdetails.UserDetailsService)
87         */
88        @Override
89        void setUserDetailsService(UserDetailsService userDetailsService) {
90                _userDetailsService = userDetailsService
91                super.setUserDetailsService(userDetailsService)
92        }
93}
Note: See TracBrowser for help on using the repository browser.