AuthorizeBaseEndpoint.java 4.0 KB
Newer Older
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright [2020] [MaxKey of copyright http://www.maxkey.top]
 * 
 * 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.
 */
 

MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
18 19 20 21 22
/**
 * 
 */
package org.maxkey.authz.endpoint;

M
MaxKey 已提交
23
import org.apache.commons.lang3.StringUtils;
M
MaxKey 已提交
24
import org.maxkey.authz.endpoint.adapter.AbstractAuthorizeAdapter;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
25
import org.maxkey.configuration.ApplicationConfig;
M
MaxKey 已提交
26
import org.maxkey.crypto.password.PasswordReciprocal;
M
MaxKey 已提交
27 28 29
import org.maxkey.entity.Accounts;
import org.maxkey.entity.UserInfo;
import org.maxkey.entity.apps.Apps;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
30 31
import org.maxkey.persistence.service.AccountsService;
import org.maxkey.persistence.service.AppsService;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
32
import org.maxkey.web.WebConstants;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45
import org.maxkey.web.WebContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Crystal.Sea
 *
 */
public class AuthorizeBaseEndpoint {
	final static Logger _logger = LoggerFactory.getLogger(AuthorizeBaseEndpoint.class);
	
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
46 47 48
	@Autowired 
    protected ApplicationConfig applicationConfig;
	
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
49
	@Autowired
MaxKey单点登录官方's avatar
m-11/6  
MaxKey单点登录官方 已提交
50
	protected AppsService appsService;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
51
		
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
52
	@Autowired
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
53 54
	protected AccountsService accountsService;
		
MaxKey单点登录官方's avatar
m-11/6  
MaxKey单点登录官方 已提交
55
	protected Apps getApp(String id){
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
56
		Apps  app=(Apps)WebContext.getAttribute(WebConstants.AUTHORIZE_SIGN_ON_APP);
M
MaxKey 已提交
57 58 59 60 61
		if(StringUtils.isBlank(id)) {
			_logger.error("parameter for app id " + id + "  is null.");
		}else {
			//session中为空或者id不一致重新加载
			if(app == null || !app.getId().equalsIgnoreCase(id)) {
M
MaxKey 已提交
62
				app = appsService.get(id,true);
M
MaxKey 已提交
63
			}
M
jwt  
MaxKey 已提交
64
			WebContext.setAttribute(WebConstants.AUTHORIZE_SIGN_ON_APP, app);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
65
		}
MaxKey单点登录官方's avatar
m-11/6  
MaxKey单点登录官方 已提交
66
		if(app	==	null){
M
MaxKey 已提交
67
			_logger.error("Applications id " + id + "  is not exist.");
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
68
		}
MaxKey单点登录官方's avatar
m-11/6  
MaxKey单点登录官方 已提交
69
		return app;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
70 71
	}
	
M
MaxKey 已提交
72
	protected Accounts getAccounts(Apps app,UserInfo userInfo){
M
MaxKey 已提交
73
		Apps  loadApp = getApp(app.getId());
M
MaxKey 已提交
74 75 76
		
		Accounts account = new Accounts(userInfo.getId(),loadApp.getId());
		account.setUsername(userInfo.getUsername());
M
MaxKey 已提交
77
		account.setAppName(app.getAppName());
M
MaxKey 已提交
78
		
M
MaxKey 已提交
79
		if(loadApp.getCredential().equalsIgnoreCase(Apps.CREDENTIALS.USER_DEFINED)){
M
MaxKey 已提交
80 81
			account = accountsService.load(new Accounts(userInfo.getId(),loadApp.getId()));
			if(account != null){
M
MaxKey 已提交
82 83
				account.setRelatedPassword(
						PasswordReciprocal.getInstance().decoder(account.getRelatedPassword()));
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
84 85
			}
			
M
MaxKey 已提交
86
		}else if(loadApp.getCredential().equalsIgnoreCase(Apps.CREDENTIALS.SHARED)){
M
MaxKey 已提交
87
			account.setRelatedUsername(loadApp.getSharedUsername());
M
MaxKey 已提交
88
			account.setRelatedPassword(PasswordReciprocal.getInstance().decoder(loadApp.getSharedPassword()));	
M
MaxKey 已提交
89
		}else if(loadApp.getCredential().equalsIgnoreCase( Apps.CREDENTIALS.SYSTEM)){
M
MaxKey 已提交
90 91 92
			account.setUsername(
					AbstractAuthorizeAdapter.getValueByUserAttr(userInfo, loadApp.getSystemUserAttr())
			);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
93
			//decoder database stored encode password
M
MaxKey 已提交
94
			account.setRelatedPassword(
M
MaxKey 已提交
95
					PasswordReciprocal.getInstance().decoder(userInfo.getDecipherable()));
M
MaxKey 已提交
96
		}else if(loadApp.getCredential().equalsIgnoreCase(Apps.CREDENTIALS.NONE)){
MaxKey单点登录官方's avatar
m-11/6  
MaxKey单点登录官方 已提交
97 98
			account.setUsername(userInfo.getUsername());
			account.setRelatedPassword(userInfo.getUsername());
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
99 100
			
		}
MaxKey单点登录官方's avatar
m-11/6  
MaxKey单点登录官方 已提交
101
		return account;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
102 103
	}
	
M
MaxKey 已提交
104 105
	public ModelAndView initCredentialView(String appId,String redirect_uri){
		String initCredentialURL = 
M
MaxKey 已提交
106
				"" + 
M
MaxKey 已提交
107 108 109 110 111
				applicationConfig.getFrontendUri() + 
				"/#/authz/credential?appId=%s&redirect_uri=%s";
		
		initCredentialURL = String.format(initCredentialURL,appId, redirect_uri);
		_logger.debug("redirect to {}.",initCredentialURL);
M
MaxKey 已提交
112 113 114
		ModelAndView  modelAndView =new ModelAndView("redirect");
		modelAndView.addObject("redirect_uri", initCredentialURL);
		return modelAndView;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
115 116 117
	}
	
}