[58] | 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.openid |
---|
| 16 | |
---|
| 17 | import org.springframework.security.Authentication |
---|
| 18 | import org.springframework.security.AuthenticationException |
---|
| 19 | import org.springframework.security.AuthenticationServiceException |
---|
| 20 | import org.springframework.security.BadCredentialsException |
---|
| 21 | import org.springframework.security.providers.openid.AuthenticationCancelledException |
---|
| 22 | import org.springframework.security.providers.openid.OpenIDAuthenticationStatus |
---|
| 23 | import org.springframework.security.providers.openid.OpenIDAuthenticationProvider |
---|
| 24 | import org.springframework.security.providers.openid.OpenIDAuthenticationToken |
---|
| 25 | import org.springframework.security.userdetails.UserDetails |
---|
| 26 | import 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 | */ |
---|
| 33 | class 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 | } |
---|