forked from Shanexpert/Jenkins-saml-SP
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathMoIDPMetadata.java
More file actions
178 lines (152 loc) · 7.69 KB
/
MoIDPMetadata.java
File metadata and controls
178 lines (152 loc) · 7.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package org.miniorange.saml;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xerces.parsers.DOMParser;
import org.opensaml.Configuration;
import org.opensaml.common.xml.SAMLConstants;
import org.opensaml.saml2.metadata.*;
import org.opensaml.xml.XMLObject;
import org.opensaml.xml.io.Unmarshaller;
import org.opensaml.xml.io.UnmarshallerFactory;
import org.opensaml.xml.security.credential.UsageType;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MoIDPMetadata {
private static Log LOGGER = LogFactory.getLog(MoIDPMetadata.class);
private String metadata;
private EntityDescriptor entityDescriptor;
public String entityId;
private Map<String, String> singleSignOnServices;
private Map<String, String> singleLogoutServices;
public List<String> signingCertificates;
public static String certificate;
public static String nameIdFormat;
public MoIDPMetadata(String metadata) {
try {
if (StringUtils.isNotBlank(metadata) && metadata.trim().startsWith("<") && metadata.trim().endsWith(">")) {
this.metadata = StringUtils.trimToEmpty(metadata);
MoSAMLUtils.doBootstrap();
DOMParser parser = new DOMParser();
parser.parse(new InputSource(new StringReader(this.metadata)));
Document document = parser.getDocument();
Element element = document.getDocumentElement();
UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory();
Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(element);
XMLObject xmlObj = unmarshaller.unmarshall(element);
entityDescriptor = (EntityDescriptor) xmlObj;
IDPSSODescriptor idpssoDescriptor = entityDescriptor.getIDPSSODescriptor(SAMLConstants.SAML20P_NS);
if (idpssoDescriptor != null) {
entityId = entityDescriptor.getEntityID();
LOGGER.debug("EntityID: " + entityId);
singleSignOnServices = new HashMap<>();
for (SingleSignOnService ssoService : idpssoDescriptor.getSingleSignOnServices()) {
String binding = ssoService.getBinding();
String location = ssoService.getLocation();
if (StringUtils.equals(binding, SAMLConstants.SAML2_REDIRECT_BINDING_URI) || StringUtils
.equals(binding, SAMLConstants.SAML2_POST_BINDING_URI)) {
LOGGER.debug("SingleSignOnService Binding: " + binding + ", Location: " + location);
singleSignOnServices.put(binding, location);
}
}
singleLogoutServices = new HashMap<>();
for (SingleLogoutService sloService : idpssoDescriptor.getSingleLogoutServices()) {
String binding = sloService.getBinding();
String location = sloService.getLocation();
if (StringUtils.equals(binding, SAMLConstants.SAML2_REDIRECT_BINDING_URI) || StringUtils
.equals(binding, SAMLConstants.SAML2_POST_BINDING_URI)) {
LOGGER.debug("SingleLogoutService Binding: " + binding + ", Location: " + location);
singleLogoutServices.put(binding, location);
}
}
if(!idpssoDescriptor.getNameIDFormats().isEmpty()) {
nameIdFormat = StringUtils.defaultIfBlank(idpssoDescriptor.getNameIDFormats().get(0).getFormat(),
"urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified");
} else {
nameIdFormat = "urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified";
}
signingCertificates = new ArrayList<>();
for (KeyDescriptor key : idpssoDescriptor.getKeyDescriptors()) {
certificate = null;
if (key.getKeyInfo().getX509Datas().size() > 0
&& key.getKeyInfo().getX509Datas().get(0).getX509Certificates().size() > 0) {
certificate = key.getKeyInfo().getX509Datas().get(0).getX509Certificates().get(0)
.getValue();
}
if (StringUtils.isBlank(certificate)) {
break;
}
if (UsageType.UNSPECIFIED.equals(key.getUse())) {
// LOGGER.debug("Certificate UsageType: " + key.getUse() + ", Certificate: " + certificate);
if(MoSAMLUtils.isValidPublicCertificate(certificate))
signingCertificates.add(certificate);
}
if (UsageType.SIGNING.equals(key.getUse())) {
// LOGGER.debug("Certificate UsageType: " + key.getUse() + ", Location: " + certificate);
if(MoSAMLUtils.isValidPublicCertificate(certificate))
signingCertificates.add(certificate);
}
}
if (StringUtils.isNotBlank(entityId) && singleSignOnServices.size() > 0 && !signingCertificates.isEmpty()) {
return;
}
}
}
} catch (Throwable t) {
return ;
// LOGGER.error(MoPluginException.PluginErrorCode.METADATA_PARSE.getMessage(), t);
// throw new MoPluginException(MoPluginException.PluginErrorCode.METADATA_PARSE, t.getMessage(), t);
}
// Throw exception. Not a valid metadata.
// throw new MoPluginException(MoPluginException.PluginErrorCode.METADATA_PARSE, MoPluginException
// .PluginErrorCode.METADATA_PARSE.getMessage());
}
public EntityDescriptor getEntityDescriptor() {
return entityDescriptor;
}
public void setEntityDescriptor(EntityDescriptor entityDescriptor) {
this.entityDescriptor = entityDescriptor;
}
public String getEntityId() {
return entityId;
}
public void setEntityId(String entityId) {
this.entityId = entityId;
}
public Map<String, String> getSingleSignOnServices() {
return singleSignOnServices;
}
public void setSingleSignOnServices(Map<String, String> singleSignOnServices) {
this.singleSignOnServices = singleSignOnServices;
}
public Map<String, String> getSingleLogoutServices() {
return singleLogoutServices;
}
public void setSingleLogoutServices(Map<String, String> singleLogoutServices) {
this.singleLogoutServices = singleLogoutServices;
}
public List<String> getSigningCertificates() {
return signingCertificates;
}
public void setSigningCertificates(List<String> signingCertificates) {
this.signingCertificates = signingCertificates;
}
public static String getCertificate() {
return certificate;
}
public static void setCertificate(String certificate) {
MoIDPMetadata.certificate = certificate;
}
public static String getNameIdFormat() {
return nameIdFormat;
}
public static void setNameIdFormat(String nameIdFormat) {
MoIDPMetadata.nameIdFormat = nameIdFormat;
}
}