AbstractSocialSignOnEndpoint.java 4.4 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.authn.support.socialsignon;

23
import org.maxkey.authn.AbstractAuthenticationProvider;
M
MaxKey 已提交
24
import org.maxkey.authn.jwt.AuthTokenService;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
25
import org.maxkey.authn.support.socialsignon.service.SocialSignOnProviderService;
MaxKey单点登录官方's avatar
fix  
MaxKey单点登录官方 已提交
26
import org.maxkey.authn.support.socialsignon.service.SocialsAssociateService;
M
MaxKey 已提交
27
import org.maxkey.configuration.ApplicationConfig;
M
sso  
MaxKey 已提交
28
import org.maxkey.entity.SocialsAssociate;
29
import org.maxkey.entity.SocialsProvider;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
30 31 32 33
import org.maxkey.web.WebContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
34
import org.springframework.beans.factory.annotation.Qualifier;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
35

36 37 38 39
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.request.AuthRequest;

MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
40 41 42 43 44 45 46
/**
 * @author Crystal.Sea
 *
 */
public class AbstractSocialSignOnEndpoint {
	final static Logger _logger = LoggerFactory.getLogger(AbstractSocialSignOnEndpoint.class);
	
47
	protected AuthRequest authRequest;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
48 49 50 51 52 53 54
	
	protected String accountJsonString;
	
	@Autowired
	protected SocialSignOnProviderService socialSignOnProviderService;
	
	@Autowired
MaxKey单点登录官方's avatar
fix  
MaxKey单点登录官方 已提交
55
	protected SocialsAssociateService socialsAssociateService;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
56
	
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
57 58
	@Autowired
    @Qualifier("authenticationProvider")
59
	AbstractAuthenticationProvider authenticationProvider ;
M
MaxKey 已提交
60
	
M
MaxKey 已提交
61
	@Autowired
M
MaxKey 已提交
62
	AuthTokenService authTokenService;
M
MaxKey 已提交
63
	
M
MaxKey 已提交
64 65
	@Autowired
	ApplicationConfig applicationConfig;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
66
 	
M
sso  
MaxKey 已提交
67
  	protected AuthRequest buildAuthRequest(String instId,String provider){
M
MaxKey 已提交
68
  		try {
M
sso  
MaxKey 已提交
69
			SocialsProvider socialSignOnProvider = socialSignOnProviderService.get(instId,provider);
M
MaxKey 已提交
70 71
			_logger.debug("socialSignOn Provider : "+socialSignOnProvider);
			
M
sso  
MaxKey 已提交
72 73
			if(socialSignOnProvider != null){
				authRequest = socialSignOnProviderService.getAuthRequest(instId,provider,WebContext.getBaseUri());
M
MaxKey 已提交
74 75 76 77 78
				return authRequest;
			}
  		}catch(Exception e) {
  			_logger.debug("buildAuthRequest Exception ",e);
  		}
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
79 80
		return null;
	}
81
    	
M
sso  
MaxKey 已提交
82 83
	protected SocialsAssociate  authCallback(String instId,String provider)  throws Exception {
		SocialsAssociate socialsAssociate = null;
84 85 86 87 88 89 90
	    AuthCallback authCallback=new AuthCallback();
        authCallback.setCode(WebContext.getRequest().getParameter("code"));
        authCallback.setAuth_code(WebContext.getRequest().getParameter("auth_code"));
        authCallback.setOauth_token(WebContext.getRequest().getParameter("oauthToken"));
        authCallback.setAuthorization_code(WebContext.getRequest().getParameter("authorization_code"));
        authCallback.setOauth_verifier(WebContext.getRequest().getParameter("oauthVerifier"));
        authCallback.setState(WebContext.getRequest().getParameter("state"));
M
sso  
MaxKey 已提交
91
        _logger.debug("Callback OAuth code {}, auth_code {}, oauthToken {}, authorization_code {}, oauthVerifier {} , state {}", 
92 93 94 95
                authCallback.getCode(),
                authCallback.getAuth_code(),
                authCallback.getOauth_token(),
                authCallback.getAuthorization_code(),
M
sso  
MaxKey 已提交
96 97
                authCallback.getOauth_verifier(),
                authCallback.getState());
98 99
        
  		if(authRequest == null) {//if authRequest is null renew one
M
sso  
MaxKey 已提交
100
  		    authRequest=socialSignOnProviderService.getAuthRequest(instId,provider,WebContext.getBaseUri());  		    
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
101
  		    _logger.debug("session authRequest is null , renew one");
102
  		}
103
  		
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
104 105 106 107 108
  		//State time out, re set
  		if(authCallback.getState() != null) {
            authRequest.authorize(WebContext.getRequest().getSession().getId());
        }
  		
109
  		AuthResponse<?> authResponse=authRequest.login(authCallback);
110
  		_logger.debug("Response  : " + authResponse.getData());
M
sso  
MaxKey 已提交
111 112 113 114 115 116 117
  		socialsAssociate =new SocialsAssociate();
		socialsAssociate.setProvider(provider);
		socialsAssociate.setSocialUserId(
				socialSignOnProviderService.getAccountId(provider, authResponse));
		socialsAssociate.setInstId(instId);
		
 		return socialsAssociate;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
118 119 120
 	}
  	
}