SubjectGenerator.java 5.9 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.authz.saml20.provider.xml;


MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
22
import org.apache.commons.lang3.StringUtils;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
23
import org.maxkey.authz.saml.service.TimeService;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
24 25
import org.maxkey.domain.UserInfo;
import org.maxkey.domain.apps.AppsSAML20Details;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
26
import org.maxkey.web.WebContext;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
27 28 29 30 31 32 33 34 35 36 37 38
import org.opensaml.saml2.core.NameID;
import org.opensaml.saml2.core.NameIDType;
import org.opensaml.saml2.core.Subject;
import org.opensaml.saml2.core.SubjectConfirmation;
import org.opensaml.saml2.core.SubjectConfirmationData;
import org.opensaml.saml2.core.impl.NameIDBuilder;
import org.opensaml.saml2.core.impl.SubjectBuilder;
import org.opensaml.saml2.core.impl.SubjectConfirmationBuilder;
import org.opensaml.saml2.core.impl.SubjectConfirmationDataBuilder;

public class SubjectGenerator {

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
39
	//private final XMLObjectBuilderFactory builderFactory = Configuration.getBuilderFactory();
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
40 41 42 43 44 45 46
	private final TimeService timeService;
		
	public SubjectGenerator(TimeService timeService) {
		super();
		this.timeService = timeService;
	}

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
47
	public Subject generateSubject( AppsSAML20Details saml20Details,
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
48 49
							String assertionConsumerURL, 
							String inResponseTo, 
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
50
							int validInSeconds) {
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
		UserInfo userInfo = WebContext.getUserInfo();
		String nameIdValue = userInfo.getUsername();
		if(saml20Details.getNameidFormat().equalsIgnoreCase("persistent")) {
		    
		}else if(saml20Details.getNameidFormat().equalsIgnoreCase("transient")) {
            
        }else if(saml20Details.getNameidFormat().equalsIgnoreCase("unspecified")) {
            
        }else if(saml20Details.getNameidFormat().equalsIgnoreCase("emailAddress")) {
            if(userInfo.getEmail()!=null && !userInfo.getEmail().equals("")) {
                nameIdValue = userInfo.getEmail();
            }
        }else if(saml20Details.getNameidFormat().equalsIgnoreCase("X509SubjectName")) {
            
        }else if(saml20Details.getNameidFormat().equalsIgnoreCase("WindowsDomainQualifiedName")) {
            if(userInfo.getWindowsAccount()!=null && !userInfo.getWindowsAccount().equals("")) {
                nameIdValue = userInfo.getWindowsAccount();
            }
        }else if(saml20Details.getNameidFormat().equalsIgnoreCase("entity")) {
            
        }else if(saml20Details.getNameidFormat().equalsIgnoreCase("custom")) {
            
        }else if(saml20Details.getNameidFormat().equalsIgnoreCase("Mobile")) {
            if(userInfo.getMobile()!=null && !userInfo.getMobile().equals("")) {
                nameIdValue = userInfo.getMobile();
            }
        }else if(saml20Details.getNameidFormat().equalsIgnoreCase("EmployeeNumber")) {
            if(userInfo.getEmployeeNumber()!=null && !userInfo.getEmployeeNumber().equals("")) {
                nameIdValue = userInfo.getEmployeeNumber();
            }
        }
		
		if(!StringUtils.isEmpty(saml20Details.getNameIdSuffix())) {
		    nameIdValue = nameIdValue + saml20Details.getNameIdSuffix();
		}
		
		if(saml20Details.getNameIdConvert()==0) {
		    
		}else if(saml20Details.getNameIdConvert()==1) {
		    nameIdValue = nameIdValue.toUpperCase();
        }else if(saml20Details.getNameIdConvert()==1) {
            nameIdValue = nameIdValue.toLowerCase();
        }
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
94 95 96
		
		NameID nameID =builderNameID(nameIdValue,assertionConsumerURL);
		Subject subject =builderSubject(nameID);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
97 98
		
		String clientAddress=WebContext.getRequestIpAddress(WebContext.getRequest());
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
99 100 101 102 103 104 105 106 107 108 109 110
		SubjectConfirmation subjectConfirmation =builderSubjectConfirmation(
								assertionConsumerURL,
								inResponseTo,
								validInSeconds,
								clientAddress);

		subject.getSubjectConfirmations().add(subjectConfirmation);
		
		return subject;
	}
	
	public NameID builderNameID(String value,String strSPNameQualifier){
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
111 112
		//Response/Assertion/Subject/NameID	
		NameID nameID = new NameIDBuilder().buildObject();
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
113
		nameID.setValue(value);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
114 115 116
		//nameID.setFormat(NameIDType.PERSISTENT);
		nameID.setFormat(NameIDType.UNSPECIFIED);
		//nameID.setSPNameQualifier(strSPNameQualifier);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
117 118 119 120 121 122
		
		return nameID;
	}
	
	public Subject builderSubject (NameID nameID){
		//Response/Assertion/Subject
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
123
		Subject subject = new SubjectBuilder().buildObject();
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
124 125 126 127 128
		subject.setNameID(nameID);
		return subject;
	}
	
	public SubjectConfirmation builderSubjectConfirmation(String recipient,String inResponseTo,int validInSeconds,String clientAddress){
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
129 130
		//SubjectConfirmationBuilder subjectConfirmationBuilder = (SubjectConfirmationBuilder)builderFactory.getBuilder(SubjectConfirmation.DEFAULT_ELEMENT_NAME);
		SubjectConfirmation subjectConfirmation = new SubjectConfirmationBuilder().buildObject();
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
131 132
		subjectConfirmation.setMethod(SubjectConfirmation.METHOD_BEARER);
		
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
133 134
		//SubjectConfirmationDataBuilder subjectConfirmationDataBuilder = (SubjectConfirmationDataBuilder)builderFactory.getBuilder(SubjectConfirmationData.DEFAULT_ELEMENT_NAME);
		SubjectConfirmationData subjectConfirmationData = new SubjectConfirmationDataBuilder().buildObject();
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
		
		subjectConfirmationData.setRecipient(recipient);
		//if idp-init not need inResponseTo
		if(null!=inResponseTo){
			subjectConfirmationData.setInResponseTo(inResponseTo);
		}
		subjectConfirmationData.setNotOnOrAfter(timeService.getCurrentDateTime().plusSeconds(validInSeconds));
		subjectConfirmationData.setAddress(clientAddress);
		
		subjectConfirmation.setSubjectConfirmationData(subjectConfirmationData);
		
		return subjectConfirmation;
	}
	
}