package org.springframework.security.oauth.examples.sparklr.mvc;

import java.security.Principal;
import java.util.LinkedHashMap;
import java.util.Map;

import org.springframework.security.oauth2.common.util.OAuth2Utils;
import org.springframework.security.oauth2.provider.AuthorizationRequest;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.approval.Approval;
import org.springframework.security.oauth2.provider.approval.Approval.ApprovalStatus;
import org.springframework.security.oauth2.provider.approval.ApprovalStore;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

/**
 * Controller for retrieving the model for and displaying the confirmation page for access to a protected resource.
 * 
 * @author Ryan Heaton
 */
@Controller
@SessionAttributes("authorizationRequest")
public class AccessConfirmationController {

	private ClientDetailsService clientDetailsService;

	private ApprovalStore approvalStore;

	@RequestMapping("/oauth/confirm_access")
	public ModelAndView getAccessConfirmation(Map<String, Object> model, Principal principal) throws Exception {
		AuthorizationRequest clientAuth = (AuthorizationRequest) model.remove("authorizationRequest");
		ClientDetails client = clientDetailsService.loadClientByClientId(clientAuth.getClientId());
		model.put("auth_request", clientAuth);
		model.put("client", client);
		Map<String,String> scopes = new LinkedHashMap<String, String>();
		for (Approval approval : approvalStore.getApprovals(principal.getName(), client.getClientId())) {
			if (clientAuth.getScope().contains(approval.getScope())) {				
				scopes.put(OAuth2Utils.SCOPE_PREFIX + approval.getScope(), approval.getStatus()==ApprovalStatus.APPROVED ? "true" : "false");
			}
		}
		for (String scope : clientAuth.getScope()) {
			if (!scopes.containsKey(OAuth2Utils.SCOPE_PREFIX + scope)) {
				scopes.put(OAuth2Utils.SCOPE_PREFIX + scope, "false");
			}
		}
		model.put("scopes", scopes);
		return new ModelAndView("access_confirmation", model);
	}

	@RequestMapping("/oauth/error")
	public String handleError(Map<String,Object> model) throws Exception {
		// We can add more stuff to the model here for JSP rendering.  If the client was a machine then
		// the JSON will already have been rendered.
		model.put("message", "There was a problem with the OAuth2 protocol");
		return "oauth_error";
	}

	public void setClientDetailsService(ClientDetailsService clientDetailsService) {
		this.clientDetailsService = clientDetailsService;
	}

	public void setApprovalStore(ApprovalStore approvalStore) {
		this.approvalStore = approvalStore;
	}

}

@@ -1666,16 +1666,17 @@
 %3CString,
+ 
 String%3E 
@@ -1721,16 +1721,125 @@
 ing%3E();%0A
+%09%09for (String scope : clientAuth.getScope()) %7B%0A%09%09%09scopes.put(OAuth2Utils.SCOPE_PREFIX + scope, %22false%22);%0A%09%09%7D%0A
 %09%09for (A
@@ -1991,20 +1991,16 @@
 pe())) %7B
-%09%09%09%09
 %0A%09%09%09%09sco
@@ -2054,17 +2054,23 @@
 Scope(),
- 
+%0A%09%09%09%09%09%09
 approval
@@ -2085,10 +2085,12 @@
 us()
+ 
 ==
+ 
 Appr
@@ -2143,187 +2143,8 @@
 %09%09%7D%0A
-%09%09for (String scope : clientAuth.getScope()) %7B%0A%09%09%09if (!scopes.containsKey(OAuth2Utils.SCOPE_PREFIX + scope)) %7B%0A%09%09%09%09scopes.put(OAuth2Utils.SCOPE_PREFIX + scope, %22false%22);%0A%09%09%09%7D%0A%09%09%7D%0A
 %09%09mo
@@ -2302,16 +2302,17 @@
 %3CString,
+ 
 Object%3E 
@@ -2400,17 +2400,16 @@
 ndering.
- 
  If the 

Update sparklr approvals UI