View Javadoc
1   /*
2    * Cloud Foundry 2012.02.03 Beta
3    * Copyright (c) [2009-2012] VMware, Inc. All Rights Reserved.
4    *
5    * This product is licensed to you under the Apache License, Version 2.0 (the "License").
6    * You may not use this product except in compliance with the License.
7    *
8    * This product includes a number of subcomponents with
9    * separate copyright notices and license terms. Your use of these
10   * subcomponents is subject to the terms and conditions of the
11   * subcomponent's license, as noted in the LICENSE file.
12   */
13  
14  package org.springframework.security.oauth2.provider.token;
15  
16  import java.util.Collection;
17  import java.util.LinkedHashMap;
18  import java.util.Map;
19  
20  import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
21  import org.springframework.security.core.Authentication;
22  import org.springframework.security.core.GrantedAuthority;
23  import org.springframework.security.core.authority.AuthorityUtils;
24  import org.springframework.security.core.userdetails.UserDetails;
25  import org.springframework.security.core.userdetails.UserDetailsService;
26  import org.springframework.util.StringUtils;
27  
28  /**
29   * Default implementation of {@link UserAuthenticationConverter}. Converts to and from an Authentication using only its
30   * name and authorities.
31   * 
32   * @author Dave Syer
33   * 
34   */
35  public class DefaultUserAuthenticationConverter implements UserAuthenticationConverter {
36  
37  	private Collection<? extends GrantedAuthority> defaultAuthorities;
38  
39  	private UserDetailsService userDetailsService;
40  
41  	/**
42  	 * Optional {@link UserDetailsService} to use when extracting an {@link Authentication} from the incoming map.
43  	 * 
44  	 * @param userDetailsService the userDetailsService to set
45  	 */
46  	public void setUserDetailsService(UserDetailsService userDetailsService) {
47  		this.userDetailsService = userDetailsService;
48  	}
49  
50  	/**
51  	 * Default value for authorities if an Authentication is being created and the input has no data for authorities.
52  	 * Note that unless this property is set, the default Authentication created by {@link #extractAuthentication(Map)}
53  	 * will be unauthenticated.
54  	 * 
55  	 * @param defaultAuthorities the defaultAuthorities to set. Default null.
56  	 */
57  	public void setDefaultAuthorities(String[] defaultAuthorities) {
58  		this.defaultAuthorities = AuthorityUtils.commaSeparatedStringToAuthorityList(StringUtils
59  				.arrayToCommaDelimitedString(defaultAuthorities));
60  	}
61  
62  	public Map<String, ?> convertUserAuthentication(Authentication authentication) {
63  		Map<String, Object> response = new LinkedHashMap<String, Object>();
64  		response.put(USERNAME, authentication.getName());
65  		if (authentication.getAuthorities() != null && !authentication.getAuthorities().isEmpty()) {
66  			response.put(AUTHORITIES, AuthorityUtils.authorityListToSet(authentication.getAuthorities()));
67  		}
68  		return response;
69  	}
70  
71  	public Authentication extractAuthentication(Map<String, ?> map) {
72  		if (map.containsKey(USERNAME)) {
73  			Object principal = map.get(USERNAME);
74  			Collection<? extends GrantedAuthority> authorities = getAuthorities(map);
75  			if (userDetailsService != null) {
76  				UserDetails user = userDetailsService.loadUserByUsername((String) map.get(USERNAME));
77  				authorities = user.getAuthorities();
78  				principal = user;
79  			}
80  			return new UsernamePasswordAuthenticationToken(principal, "N/A", authorities);
81  		}
82  		return null;
83  	}
84  
85  	private Collection<? extends GrantedAuthority> getAuthorities(Map<String, ?> map) {
86  		if (!map.containsKey(AUTHORITIES)) {
87  			return defaultAuthorities;
88  		}
89  		Object authorities = map.get(AUTHORITIES);
90  		if (authorities instanceof String) {
91  			return AuthorityUtils.commaSeparatedStringToAuthorityList((String) authorities);
92  		}
93  		if (authorities instanceof Collection) {
94  			return AuthorityUtils.commaSeparatedStringToAuthorityList(StringUtils
95  					.collectionToCommaDelimitedString((Collection<?>) authorities));
96  		}
97  		throw new IllegalArgumentException("Authorities must be either a String or a Collection");
98  	}
99  }