WsFederationServiceImpl.java 3.7 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
package org.maxkey.authn.support.wsfederation;

import javax.servlet.http.HttpServletRequest;

22
import org.maxkey.authn.AbstractAuthenticationProvider;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
23
import org.maxkey.constants.ConstantsLoginType;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
24 25 26 27
import org.maxkey.util.StringUtils;
import org.opensaml.saml1.core.impl.AssertionImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
28 29
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
30 31 32 33 34 35 36


public class WsFederationServiceImpl implements   WsFederationService{
	final static Logger _logger = LoggerFactory.getLogger(WsFederationServiceImpl.class);
	
	private WsFederationConfiguration wsFederationConfiguration;
	
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
37 38
	@Autowired
    @Qualifier("authenticationProvider")
39
	AbstractAuthenticationProvider authenticationProvider ;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
40
	
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
	public boolean login(String wsFederationWA,String wsFederationWResult,HttpServletRequest request){
		// it's an authentication
        if (StringUtils.isNotEmpty(wsFederationWA) && wsFederationWA.equalsIgnoreCase(WsFederationConstants.WSIGNIN)) {
            _logger.debug("wresult : {}"+wsFederationWResult);

            final String wctx = request.getParameter(WsFederationConstants.WCTX);
            _logger.debug("wctx : {}"+ wctx);

            // create credentials
            final AssertionImpl assertion = WsFederationUtils.parseTokenFromString(wsFederationWResult);
            //Validate the signature
            if (assertion != null && WsFederationUtils.validateSignature(assertion, wsFederationConfiguration.getSigningCertificates())) {
                final WsFederationCredential wsFederationCredential = WsFederationUtils.createCredentialFromToken(assertion);

                if (wsFederationCredential != null && wsFederationCredential.isValid(wsFederationConfiguration.getRelyingParty(),
                		wsFederationConfiguration.getIdentifier(),
                		wsFederationConfiguration.getTolerance())) {

                    //Give the library user a chance to change the attributes as necessary
                    if (wsFederationConfiguration.getAttributeMutator() != null) {
                    	wsFederationConfiguration.getAttributeMutator().modifyAttributes(
                    			wsFederationCredential.getAttributes(),
                    			wsFederationConfiguration.getUpnSuffix());
                    }

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
66
                    authenticationProvider.trustAuthentication(
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
67
                    		wsFederationCredential.getAttributes().get("").toString(),
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
68
                    		ConstantsLoginType.WSFEDERATION,
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
69
                    		"","","success");
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
70
                    return true;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
                } else {
                    _logger.warn("SAML assertions are blank or no longer valid.");
                    return false;
                }
            } else {
                _logger.error("WS Requested Security Token is blank or the signature is not valid.");
                return false;
            }
        }
		return false;
	}

	public void setWsFederationConfiguration(
			WsFederationConfiguration wsFederationConfiguration) {
		this.wsFederationConfiguration = wsFederationConfiguration;
	}
	
}