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  
17  package org.springframework.security.oauth2.provider.client;
18  
19  import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
20  import org.springframework.security.oauth2.common.OAuth2AccessToken;
21  import org.springframework.security.oauth2.provider.ClientDetailsService;
22  import org.springframework.security.oauth2.provider.OAuth2RequestFactory;
23  import org.springframework.security.oauth2.provider.TokenRequest;
24  import org.springframework.security.oauth2.provider.token.AbstractTokenGranter;
25  import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
26  
27  /**
28   * @author Dave Syer
29   * 
30   */
31  public class ClientCredentialsTokenGranter extends AbstractTokenGranter {
32  
33  	private static final String GRANT_TYPE = "client_credentials";
34  	private boolean allowRefresh = false;
35  
36  	public ClientCredentialsTokenGranter(AuthorizationServerTokenServices tokenServices,
37  			ClientDetailsService clientDetailsService, OAuth2RequestFactory requestFactory) {
38  		this(tokenServices, clientDetailsService, requestFactory, GRANT_TYPE);
39  	}
40  
41  	protected ClientCredentialsTokenGranter(AuthorizationServerTokenServices tokenServices,
42  			ClientDetailsService clientDetailsService, OAuth2RequestFactory requestFactory, String grantType) {
43  		super(tokenServices, clientDetailsService, requestFactory, grantType);
44  	}
45  	
46  	public void setAllowRefresh(boolean allowRefresh) {
47  		this.allowRefresh = allowRefresh;
48  	}
49  
50  	@Override
51  	public OAuth2AccessToken grant(String grantType, TokenRequest tokenRequest) {
52  		OAuth2AccessToken token = super.grant(grantType, tokenRequest);
53  		if (token != null) {
54  			DefaultOAuth2AccessToken norefresh = new DefaultOAuth2AccessToken(token);
55  			// The spec says that client credentials should not be allowed to get a refresh token
56  			if (!allowRefresh) {
57  				norefresh.setRefreshToken(null);
58  			}
59  			token = norefresh;
60  		}
61  		return token;
62  	}
63  
64  }