View Javadoc

1   /*
2    * Copyright 2010 James Pether Sörling
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   *	$Id$
17   *  $HeadURL$
18  */
19  package com.hack23.cia.web.impl.ui.common;
20  
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.springframework.security.core.GrantedAuthority;
26  import org.springframework.security.core.authority.AuthorityUtils;
27  import org.springframework.security.core.userdetails.AuthenticationUserDetailsService;
28  import org.springframework.security.core.userdetails.UserDetails;
29  import org.springframework.security.core.userdetails.UserDetailsService;
30  import org.springframework.security.core.userdetails.UsernameNotFoundException;
31  import org.springframework.security.openid.OpenIDAttribute;
32  import org.springframework.security.openid.OpenIDAuthenticationToken;
33  
34  /**
35   * The Class CustomUserDetailsService.
36   */
37  public final class CustomUserDetailsService implements UserDetailsService, AuthenticationUserDetailsService<OpenIDAuthenticationToken> {
38  
39      /** The registered users. */
40      private final Map<String, CustomUserDetails> registeredUsers = new HashMap<String, CustomUserDetails>();
41  
42      /** The Constant DEFAULT_AUTHORITIES. */
43      private static final List<GrantedAuthority> DEFAULT_AUTHORITIES = AuthorityUtils.createAuthorityList("ROLE_VISITOR");
44  
45      /** The Constant DEFAULT_ADMIN_AUTHORITIES. */
46      private static final List<GrantedAuthority> DEFAULT_ADMIN_AUTHORITIES = AuthorityUtils.createAuthorityList("ROLE_VISITOR","ROLE_ADMIN","ROLE_VIP");
47  
48      /**
49  	 * Instantiates a new custom user details service.
50  	 *
51  	 * @param defaultAdminOpenId
52  	 *            the default admin open id
53  	 * @param email
54  	 *            the email
55  	 * @param fullName
56  	 *            the full name
57  	 */
58      public CustomUserDetailsService(final String defaultAdminOpenId,final String email,final String fullName) {
59  		super();
60          final CustomUserDetails user = new CustomUserDetails(defaultAdminOpenId, DEFAULT_ADMIN_AUTHORITIES);
61          user.setEmail(email);
62          user.setName(fullName);
63          registeredUsers.put(defaultAdminOpenId, user);
64  	}
65  
66  	/* (non-Javadoc)
67  	 * @see org.springframework.security.core.userdetails.UserDetailsService#loadUserByUsername(java.lang.String)
68  	 */
69      @Override
70  	public UserDetails loadUserByUsername(final String id) {
71          final UserDetails user = registeredUsers.get(id);
72  
73          if (user == null) {
74              throw new UsernameNotFoundException(id);
75          }
76  
77          return user;
78      }
79  
80      /* (non-Javadoc)
81       * @see org.springframework.security.core.userdetails.AuthenticationUserDetailsService#loadUserDetails(org.springframework.security.core.Authentication)
82       */
83      @Override
84  	public UserDetails loadUserDetails(final OpenIDAuthenticationToken token) {
85          final String id = token.getIdentityUrl();
86  
87          CustomUserDetails user = registeredUsers.get(id);
88  
89          if (user != null) {
90              return user;
91          }
92  
93          String email = null;
94          String firstName = null;
95          String lastName = null;
96          String fullName = null;
97  
98          final List<OpenIDAttribute> attributes = token.getAttributes();
99  
100         for (final OpenIDAttribute attribute : attributes) {
101             if (attribute.getName().equals("email")) {
102                 email = attribute.getValues().get(0);
103             }
104 
105             if (attribute.getName().equals("firstname")) {
106                 firstName = attribute.getValues().get(0);
107             }
108 
109             if (attribute.getName().equals("lastname")) {
110                 lastName = attribute.getValues().get(0);
111             }
112 
113             if (attribute.getName().equals("fullname")) {
114                 fullName = attribute.getValues().get(0);
115             }
116         }
117 
118         if (fullName == null) {
119             final StringBuilder fullNameBldr = new StringBuilder();
120 
121             if (firstName != null) {
122                 fullNameBldr.append(firstName);
123             }
124 
125             if (lastName != null) {
126                 fullNameBldr.append(" ").append(lastName);
127             }
128             fullName = fullNameBldr.toString();
129         }
130 
131         user = new CustomUserDetails(id, DEFAULT_AUTHORITIES);
132         user.setEmail(email);
133         user.setName(fullName);
134 
135         registeredUsers.put(id, user);
136 
137         user = new CustomUserDetails(id, DEFAULT_AUTHORITIES);
138         user.setEmail(email);
139         user.setName(fullName);
140         user.setNewUser(true);
141 
142         return user;
143     }
144 
145 
146 }