All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.security.oauth2.provider.endpoint.WhitelabelApprovalEndpoint Maven / Gradle / Ivy

There is a newer version: 2.5.2.RELEASE
Show newest version
package org.springframework.security.oauth2.provider.endpoint;

import org.springframework.security.oauth2.provider.AuthorizationRequest;
import org.springframework.security.web.csrf.CsrfToken;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.web.util.HtmlUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Controller for displaying the approval page for the authorization server.
 * 
 * @author Dave Syer
 */
@FrameworkEndpoint
@SessionAttributes("authorizationRequest")
public class WhitelabelApprovalEndpoint {

	@RequestMapping("/oauth/confirm_access")
	public ModelAndView getAccessConfirmation(Map model, HttpServletRequest request) throws Exception {
		final String approvalContent = createTemplate(model, request);
		if (request.getAttribute("_csrf") != null) {
			model.put("_csrf", request.getAttribute("_csrf"));
		}
		View approvalView = new View() {
			@Override
			public String getContentType() {
				return "text/html";
			}

			@Override
			public void render(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception {
				response.setContentType(getContentType());
				response.getWriter().append(approvalContent);
			}
		};
		return new ModelAndView(approvalView, model);
	}

	protected String createTemplate(Map model, HttpServletRequest request) {
		AuthorizationRequest authorizationRequest = (AuthorizationRequest) model.get("authorizationRequest");
		String clientId = authorizationRequest.getClientId();

		StringBuilder builder = new StringBuilder();
		builder.append("

OAuth Approval

"); builder.append("

Do you authorize \"").append(HtmlUtils.htmlEscape(clientId)); builder.append("\" to access your protected resources?

"); builder.append("
"); builder.append(""); String csrfTemplate = null; CsrfToken csrfToken = (CsrfToken) (model.containsKey("_csrf") ? model.get("_csrf") : request.getAttribute("_csrf")); if (csrfToken != null) { csrfTemplate = ""; } if (csrfTemplate != null) { builder.append(csrfTemplate); } String authorizeInputTemplate = "
"; if (model.containsKey("scopes") || request.getAttribute("scopes") != null) { builder.append(createScopes(model, request)); builder.append(authorizeInputTemplate); } else { builder.append(authorizeInputTemplate); builder.append("
"); builder.append(""); if (csrfTemplate != null) { builder.append(csrfTemplate); } builder.append("
"); } builder.append(""); return builder.toString(); } private CharSequence createScopes(Map model, HttpServletRequest request) { StringBuilder builder = new StringBuilder("
    "); @SuppressWarnings("unchecked") Map scopes = (Map) (model.containsKey("scopes") ? model.get("scopes") : request.getAttribute("scopes")); for (String scope : scopes.keySet()) { String approved = "true".equals(scopes.get(scope)) ? " checked" : ""; String denied = !"true".equals(scopes.get(scope)) ? " checked" : ""; scope = HtmlUtils.htmlEscape(scope); builder.append("
  • "); builder.append(scope).append(": Approve "); builder.append("Deny
  • "); } builder.append("
"); return builder.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy