OAuth20AccessConfirmationEndpoint.java 5.4 KB
Newer Older
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
1
/*
M
220413  
MaxKey 已提交
2
 * Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 

M
MaxKey 已提交
18
package org.maxkey.authz.oauth2.provider.approval.endpoint;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
19 20 21

import java.util.LinkedHashMap;
import java.util.Map;
M
MaxKey 已提交
22
import org.maxkey.authn.web.AuthorizationUtils;
M
MaxKey 已提交
23
import org.maxkey.authz.oauth2.common.OAuth2Constants;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
24 25 26 27
import org.maxkey.authz.oauth2.provider.AuthorizationRequest;
import org.maxkey.authz.oauth2.provider.ClientDetailsService;
import org.maxkey.authz.oauth2.provider.approval.Approval;
import org.maxkey.authz.oauth2.provider.approval.Approval.ApprovalStatus;
M
MaxKey 已提交
28 29
import org.maxkey.entity.apps.Apps;
import org.maxkey.entity.apps.oauth2.provider.ClientDetails;
30
import org.maxkey.authz.oauth2.provider.approval.ApprovalStore;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
31
import org.maxkey.persistence.service.AppsService;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
32
import org.maxkey.web.WebConstants;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
33
import org.maxkey.web.WebContext;
M
MaxKey 已提交
34 35
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
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")
M
MaxKey 已提交
52 53 54
public class OAuth20AccessConfirmationEndpoint {
	static final Logger _logger = LoggerFactory.getLogger(OAuth20AccessConfirmationEndpoint.class);
	 
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    @Autowired
    @Qualifier("appsService")
    protected AppsService appsService;
    
    @Autowired
    @Qualifier("oauth20JdbcClientDetailsService")
    private ClientDetailsService clientDetailsService;

    @Autowired
    @Qualifier("oauth20ApprovalStore")
    private ApprovalStore approvalStore;

    @Autowired
    @Qualifier("oauth20UserApprovalHandler")
    OAuth20UserApprovalHandler oauth20UserApprovalHandler;

    /**
     * getAccessConfirmation.
     * @param model  Map
     * @return
     * throws Exception  
     */
M
MaxKey 已提交
77
    @RequestMapping(OAuth2Constants.ENDPOINT.ENDPOINT_APPROVAL_CONFIRM)
78
    public ModelAndView getAccessConfirmation(
M
MaxKey 已提交
79 80 81 82 83 84 85
            @RequestParam Map<String, Object> model) {
    	try {
	        model.remove("authorizationRequest");
	        
	        // Map<String, Object> model
	        AuthorizationRequest clientAuth = 
	                (AuthorizationRequest) WebContext.getAttribute("authorizationRequest");
M
MaxKey 已提交
86
	        ClientDetails client = clientDetailsService.loadClientByClientId(clientAuth.getClientId(),true);
M
MaxKey 已提交
87
	        Apps  app = (Apps)WebContext.getAttribute(WebConstants.AUTHORIZE_SIGN_ON_APP);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
88
	        WebContext.setAttribute(app.getId(), app.getIcon());
M
MaxKey 已提交
89 90 91 92 93 94 95
	       
	        model.put("auth_request", clientAuth);
	        model.put("client", client);
	        model.put("app", app);
	        model.put("oauth_version", "oauth 2.0");
	        Map<String, String> scopes = new LinkedHashMap<String, String>();
	        for (String scope : clientAuth.getScope()) {
96
	            scopes.put(OAuth2Constants.PARAMETER.SCOPE_PREFIX + scope, "false");
M
MaxKey 已提交
97
	        }
M
MaxKey 已提交
98
	        String principal = AuthorizationUtils.getPrincipal().getUsername();
M
MaxKey 已提交
99 100
	        for (Approval approval : approvalStore.getApprovals(principal, client.getClientId())) {
	            if (clientAuth.getScope().contains(approval.getScope())) {
101
	                scopes.put(OAuth2Constants.PARAMETER.SCOPE_PREFIX + approval.getScope(),
M
MaxKey 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114
	                        approval.getStatus() == ApprovalStatus.APPROVED ? "true" : "false");
	            }
	        }
	        
	        model.put("scopes", scopes);
	
	        if(!model.containsKey(OAuth2Constants.PARAMETER.APPROVAL_PROMPT)) {
	        	model.put(OAuth2Constants.PARAMETER.APPROVAL_PROMPT, client.getApprovalPrompt());
	        }
    	}catch(Exception e) {
    		 _logger.debug("OAuth Access Confirmation process error." ,e);
    	}
	        
115
        ModelAndView modelAndView = new ModelAndView("authorize/oauth_access_confirmation");
M
MaxKey 已提交
116
        _logger.trace("Confirmation details ");
M
MaxKey 已提交
117
        for (Object key : model.keySet()) {
M
MaxKey 已提交
118
            _logger.trace("key " + key +"=" + model.get(key));
M
MaxKey 已提交
119
        }
120 121 122
        modelAndView.addObject("model", model);
        return modelAndView;
    }
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
123

124 125 126 127 128 129
    /**
     * handleError.
     * @param model Map
     * @return
     * throws Exception
     */
M
MaxKey 已提交
130
    @RequestMapping(OAuth2Constants.ENDPOINT.ENDPOINT_ERROR)
131 132 133 134 135 136 137
    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";
    }
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
138
}