View Javadoc
1   /*
2    * Copyright 2002-2011 the original author or authors.
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    *      https://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  package org.springframework.security.oauth2.common.util;
17  
18  import java.util.Arrays;
19  import java.util.Collection;
20  import java.util.HashMap;
21  import java.util.HashSet;
22  import java.util.Map;
23  import java.util.Properties;
24  import java.util.Set;
25  import java.util.TreeSet;
26  
27  import org.springframework.util.StringUtils;
28  
29  /**
30   * @author Ryan Heaton
31   * @author Dave Syer
32   */
33  public abstract class OAuth2Utils {
34  
35  	/**
36  	 * Constant to use while parsing and formatting parameter maps for OAuth2 requests
37  	 */
38  	public static final String CLIENT_ID = "client_id";
39  
40  	/**
41  	 * Constant to use while parsing and formatting parameter maps for OAuth2 requests
42  	 */
43  	public static final String STATE = "state";
44  
45  	/**
46  	 * Constant to use while parsing and formatting parameter maps for OAuth2 requests
47  	 */
48  	public static final String SCOPE = "scope";
49  
50  	/**
51  	 * Constant to use while parsing and formatting parameter maps for OAuth2 requests
52  	 */
53  	public static final String REDIRECT_URI = "redirect_uri";
54  
55  	/**
56  	 * Constant to use while parsing and formatting parameter maps for OAuth2 requests
57  	 */
58  	public static final String RESPONSE_TYPE = "response_type";
59  
60  	/**
61  	 * Constant to use while parsing and formatting parameter maps for OAuth2 requests
62  	 */
63  	public static final String USER_OAUTH_APPROVAL = "user_oauth_approval";
64  
65  	/**
66  	 * Constant to use as a prefix for scope approval
67  	 */
68  	public static final String SCOPE_PREFIX = "scope.";
69  
70  	/**
71  	 * Constant to use while parsing and formatting parameter maps for OAuth2 requests
72  	 */
73  	public static final String GRANT_TYPE = "grant_type";
74  
75  	/**
76  	 * Parses a string parameter value into a set of strings.
77  	 * 
78  	 * @param values The values of the set.
79  	 * @return The set.
80  	 */
81  	public static Set<String> parseParameterList(String values) {
82  		Set<String> result = new TreeSet<String>();
83  		if (values != null && values.trim().length() > 0) {
84  			// the spec says the scope is separated by spaces
85  			String[] tokens = values.split("[\\s+]");
86  			result.addAll(Arrays.asList(tokens));
87  		}
88  		return result;
89  	}
90  
91  	/**
92  	 * Formats a set of string values into a format appropriate for sending as a single-valued form value.
93  	 * 
94  	 * @param value The value of the parameter.
95  	 * @return The value formatted for form submission etc, or null if the input is empty
96  	 */
97  	public static String formatParameterList(Collection<String> value) {
98  		return value == null ? null : StringUtils.collectionToDelimitedString(value, " ");
99  	}
100 
101 	/**
102 	 * Extract a map from a query string.
103 	 * 
104 	 * @param query a query (or fragment) string from a URI
105 	 * @return a Map of the values in the query
106 	 */
107 	public static Map<String, String> extractMap(String query) {
108 		Map<String, String> map = new HashMap<String, String>();
109 		Properties properties = StringUtils.splitArrayElementsIntoProperties(
110 				StringUtils.delimitedListToStringArray(query, "&"), "=");
111 		if (properties != null) {
112 			for (Object key : properties.keySet()) {
113 				map.put(key.toString(), properties.get(key).toString());
114 			}
115 		}
116 		return map;
117 	}
118 
119 	/**
120 	 * Compare 2 sets and check that one contains all members of the other.
121 	 * 
122 	 * @param target set of strings to check
123 	 * @param members the members to compare to
124 	 * @return true if all members are in the target
125 	 */
126 	public static boolean containsAll(Set<String> target, Set<String> members) {
127 		target = new HashSet<String>(target);
128 		target.retainAll(members);
129 		return target.size() == members.size();
130 	}
131 }