All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.flyfish.oauth.utils.OAuth2Utils Maven / Gradle / Ivy

There is a newer version: 1.0.7
Show newest version
package com.flyfish.oauth.utils;

import java.util.*;

/**
 * @author Ryan Heaton
 * @author Dave Syer
 */
public abstract class OAuth2Utils {

	/**
	 * Constant to use while parsing and formatting parameter maps for OAuth2 requests
	 */
	public static final String CLIENT_ID = "client_id";

	/**
	 * Constant to use while parsing and formatting parameter maps for OAuth2 requests
	 */
	public static final String CLIENT_SECRET = "client_secret";

	/**
	 * Constant to use while parsing and formatting parameter maps for OAuth2 requests
	 */
	public static final String CODE = "code";

	/**
	 * Constant to use while parsing and formatting parameter maps for OAuth2 requests
	 */
	public static final String STATE = "state";

	/**
	 * Constant to use while parsing and formatting parameter maps for OAuth2 requests
	 */
	public static final String SCOPE = "scope";

	/**
	 * Constant to use while parsing and formatting parameter maps for OAuth2 requests
	 */
	public static final String REDIRECT_URI = "redirect_uri";

	/**
	 * Constant to use while parsing and formatting parameter maps for OAuth2 requests
	 */
	public static final String RESPONSE_TYPE = "response_type";

	/**
	 * Constant to use while parsing and formatting parameter maps for OAuth2 requests
	 */
	public static final String USER_OAUTH_APPROVAL = "user_oauth_approval";

	/**
	 * Constant to use as a prefix for scope approval
	 */
	public static final String SCOPE_PREFIX = "scope.";

	/**
	 * Constant to use while parsing and formatting parameter maps for OAuth2 requests
	 */
	public static final String GRANT_TYPE = "grant_type";

	/**
	 * Parses a string parameter value into a set of strings.
	 * 
	 * @param values The values of the set.
	 * @return The set.
	 */
	public static Set parseParameterList(String values) {
		Set result = new TreeSet();
		if (values != null && values.trim().length() > 0) {
			// the spec says the scope is separated by spaces
			String[] tokens = values.split("[\\s+]");
			result.addAll(Arrays.asList(tokens));
		}
		return result;
	}

	/**
	 * Extract a map from a query string.
	 * 
	 * @param query a query (or fragment) string from a URI
	 * @return a Map of the values in the query
	 */
	public static Map extractMap(String query) {
		Map map = new HashMap<>();
		Properties properties = SpringStringUtils.splitArrayElementsIntoProperties(
				SpringStringUtils.delimitedListToStringArray(query, "&"), "=");
		if (properties != null) {
			for (Object key : properties.keySet()) {
				map.put(key.toString(), properties.get(key).toString());
			}
		}
		return map;
	}

	/**
	 * map转换为query
	 * @param map map
	 * @return 结果字符串
	 */
	public static String mapToQuery(Map map) {
		StringBuilder sb = new StringBuilder("?");
		for (Map.Entry entry : map.entrySet()) {
			sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
		}
		if (sb.length() != 1) {
			sb.deleteCharAt(sb.length() -1);
			return sb.toString();
		}
		return "";
	}

	/**
	 * Compare 2 sets and check that one contains all members of the other.
	 * 
	 * @param target set of strings to check
	 * @param members the members to compare to
	 * @return true if all members are in the target
	 */
	public static boolean containsAll(Set target, Set members) {
		target = new HashSet(target);
		target.retainAll(members);
		return target.size() == members.size();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy