View Javadoc
1   package org.springframework.security.oauth2.provider;
2   
3   import java.util.Collection;
4   import java.util.HashMap;
5   import java.util.Map;
6   
7   import org.springframework.security.oauth2.common.util.OAuth2Utils;
8   import org.springframework.security.oauth2.provider.endpoint.AuthorizationEndpoint;
9   import org.springframework.security.oauth2.provider.endpoint.TokenEndpoint;
10  
11  /**
12   * Represents an OAuth2 token request, made at the {@link TokenEndpoint}. The requestParameters map should contain the
13   * original, unmodified parameters from the original OAuth2 request.
14   * 
15   * In the implicit flow, a token is requested through the {@link AuthorizationEndpoint} directly, and in that case the
16   * {@link AuthorizationRequest} is converted into a {@link TokenRequest} for processing through the token granting
17   * chain.
18   * 
19   * @author Amanda Anganes
20   * @author Dave Syer
21   * 
22   */
23  @SuppressWarnings("serial")
24  public class TokenRequest extends BaseRequest {
25  
26  	private String grantType;
27  
28  	/**
29  	 * Default constructor
30  	 */
31  	protected TokenRequest() {
32  	}
33  
34  	/**
35  	 * Full constructor. Sets this TokenRequest's requestParameters map to an unmodifiable version of the one provided.
36  	 * 
37  	 * @param requestParameters
38  	 * @param clientId
39  	 * @param scope
40  	 * @param grantType
41  	 */
42  	public TokenRequest(Map<String, String> requestParameters, String clientId, Collection<String> scope,
43  			String grantType) {
44  		setClientId(clientId);
45  		setRequestParameters(requestParameters);
46  		setScope(scope);
47  		this.grantType = grantType;
48  	}
49  
50  	public String getGrantType() {
51  		return grantType;
52  	}
53  
54  	public void setGrantType(String grantType) {
55  		this.grantType = grantType;
56  	}
57  
58  	public void setClientId(String clientId) {
59  		super.setClientId(clientId);
60  	}
61  
62  	/**
63  	 * Set the scope value. If the collection contains only a single scope value, this method will parse that value into
64  	 * a collection using {@link OAuth2Utils#parseParameterList}.
65  	 * 
66  	 * @see AuthorizationRequest#setScope
67  	 * 
68  	 * @param scope
69  	 */
70  	public void setScope(Collection<String> scope) {
71  		super.setScope(scope);
72  	}
73  
74  	/**
75  	 * Set the Request Parameters on this authorization request, which represent the original request parameters and
76  	 * should never be changed during processing. The map passed in is wrapped in an unmodifiable map instance.
77  	 * 
78  	 * @see AuthorizationRequest#setRequestParameters
79  	 * 
80  	 * @param requestParameters
81  	 */
82  	public void setRequestParameters(Map<String, String> requestParameters) {
83  		super.setRequestParameters(requestParameters);
84  	}
85  
86  	public OAuth2Request createOAuth2Request(ClientDetails client) {
87  		Map<String, String> requestParameters = getRequestParameters();
88  		HashMap<String, String> modifiable = new HashMap<String, String>(requestParameters);
89  		// Remove password if present to prevent leaks
90  		modifiable.remove("password");
91  		modifiable.remove("client_secret");
92  		// Add grant type so it can be retrieved from OAuth2Request
93  		modifiable.put("grant_type", grantType);
94  		return new OAuth2Request(modifiable, client.getClientId(), client.getAuthorities(), true, this.getScope(),
95  				client.getResourceIds(), null, null, null);
96  	}
97  
98  }