View Javadoc
1   package org.springframework.security.oauth2.provider.code;
2   
3   import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
4   import org.springframework.security.oauth2.common.util.RandomValueStringGenerator;
5   import org.springframework.security.oauth2.provider.OAuth2Authentication;
6   
7   /**
8    * Base implementation for authorization code services that generates a random-value authorization code.
9    * 
10   * @author Ryan Heaton
11   * @author Dave Syer
12   */
13  public abstract class RandomValueAuthorizationCodeServices implements AuthorizationCodeServices {
14  
15  	private RandomValueStringGeneratoril/RandomValueStringGenerator.html#RandomValueStringGenerator">RandomValueStringGenerator generator = new RandomValueStringGenerator();
16  
17  	protected abstract void store(String code, OAuth2Authentication authentication);
18  
19  	protected abstract OAuth2Authentication remove(String code);
20  
21  	public String createAuthorizationCode(OAuth2Authentication authentication) {
22  		String code = generator.generate();
23  		store(code, authentication);
24  		return code;
25  	}
26  
27  	public OAuth2Authentication consumeAuthorizationCode(String code)
28  			throws InvalidGrantException {
29  		OAuth2Authentication auth = this.remove(code);
30  		if (auth == null) {
31  			throw new InvalidGrantException("Invalid authorization code: " + code);
32  		}
33  		return auth;
34  	}
35  
36  }