View Javadoc

1   package org.springframework.security.oauth.examples.sparklr.mvc;
2   
3   import java.security.Principal;
4   import java.util.LinkedHashMap;
5   import java.util.Map;
6   
7   import org.springframework.security.oauth2.common.util.OAuth2Utils;
8   import org.springframework.security.oauth2.provider.AuthorizationRequest;
9   import org.springframework.security.oauth2.provider.ClientDetails;
10  import org.springframework.security.oauth2.provider.ClientDetailsService;
11  import org.springframework.security.oauth2.provider.approval.Approval;
12  import org.springframework.security.oauth2.provider.approval.Approval.ApprovalStatus;
13  import org.springframework.security.oauth2.provider.approval.ApprovalStore;
14  import org.springframework.stereotype.Controller;
15  import org.springframework.web.bind.annotation.RequestMapping;
16  import org.springframework.web.bind.annotation.SessionAttributes;
17  import org.springframework.web.servlet.ModelAndView;
18  
19  /**
20   * Controller for retrieving the model for and displaying the confirmation page for access to a protected resource.
21   * 
22   * @author Ryan Heaton
23   */
24  @Controller
25  @SessionAttributes("authorizationRequest")
26  public class AccessConfirmationController {
27  
28  	private ClientDetailsService clientDetailsService;
29  
30  	private ApprovalStore approvalStore;
31  
32  	@RequestMapping("/oauth/confirm_access")
33  	public ModelAndView getAccessConfirmation(Map<String, Object> model, Principal principal) throws Exception {
34  		AuthorizationRequest clientAuth = (AuthorizationRequest) model.remove("authorizationRequest");
35  		ClientDetails client = clientDetailsService.loadClientByClientId(clientAuth.getClientId());
36  		model.put("auth_request", clientAuth);
37  		model.put("client", client);
38  		Map<String, String> scopes = new LinkedHashMap<String, String>();
39  		for (String scope : clientAuth.getScope()) {
40  			scopes.put(OAuth2Utils.SCOPE_PREFIX + scope, "false");
41  		}
42  		for (Approval approval : approvalStore.getApprovals(principal.getName(), client.getClientId())) {
43  			if (clientAuth.getScope().contains(approval.getScope())) {
44  				scopes.put(OAuth2Utils.SCOPE_PREFIX + approval.getScope(),
45  						approval.getStatus() == ApprovalStatus.APPROVED ? "true" : "false");
46  			}
47  		}
48  		model.put("scopes", scopes);
49  		return new ModelAndView("access_confirmation", model);
50  	}
51  
52  	@RequestMapping("/oauth/error")
53  	public String handleError(Map<String, Object> model) throws Exception {
54  		// We can add more stuff to the model here for JSP rendering. If the client was a machine then
55  		// the JSON will already have been rendered.
56  		model.put("message", "There was a problem with the OAuth2 protocol");
57  		return "oauth_error";
58  	}
59  
60  	public void setClientDetailsService(ClientDetailsService clientDetailsService) {
61  		this.clientDetailsService = clientDetailsService;
62  	}
63  
64  	public void setApprovalStore(ApprovalStore approvalStore) {
65  		this.approvalStore = approvalStore;
66  	}
67  
68  }