-
Notifications
You must be signed in to change notification settings - Fork 824
/
SamlIdentityProviderDefinition.java
270 lines (229 loc) · 8.91 KB
/
SamlIdentityProviderDefinition.java
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*******************************************************************************
* Cloud Foundry
* Copyright (c) [2009-2015] Pivotal Software, Inc. All Rights Reserved.
*
* This product is licensed to you under the Apache License, Version 2.0 (the "License").
* You may not use this product except in compliance with the License.
*
* This product includes a number of subcomponents with
* separate copyright notices and license terms. Your use of these
* subcomponents is subject to the terms and conditions of the
* subcomponent's license, as noted in the LICENSE file.
*******************************************************************************/
package org.cloudfoundry.identity.uaa.provider;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.springframework.util.StringUtils;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@JsonIgnoreProperties(ignoreUnknown = true)
public class SamlIdentityProviderDefinition extends ExternalIdentityProviderDefinition {
public enum MetadataLocation {
URL,
DATA,
UNKNOWN
}
public enum ExternalGroupMappingMode {
EXPLICITLY_MAPPED,
AS_SCOPES
}
private String metaDataLocation;
private String idpEntityAlias;
private String zoneId;
private String nameID;
private int assertionConsumerIndex;
private boolean metadataTrustCheck;
private boolean showSamlLink;
private String linkText;
private String iconUrl;
private ExternalGroupMappingMode groupMappingMode = ExternalGroupMappingMode.EXPLICITLY_MAPPED;
private boolean skipSslValidation = false;
public SamlIdentityProviderDefinition() {}
public SamlIdentityProviderDefinition clone() {
List<String> emailDomain = getEmailDomain() != null ? new ArrayList<>(getEmailDomain()) : null;
List<String> externalGroupsWhitelist = getExternalGroupsWhitelist() != null ? new ArrayList<>(getExternalGroupsWhitelist()) : null;
Map<String, Object> attributeMappings = getAttributeMappings() != null ? new HashMap(getAttributeMappings()) : null;
SamlIdentityProviderDefinition def = new SamlIdentityProviderDefinition();
def.setMetaDataLocation(metaDataLocation);
def.setIdpEntityAlias(idpEntityAlias);
def.setZoneId(zoneId);
def.setNameID(nameID);
def.setAssertionConsumerIndex(assertionConsumerIndex);
def.setMetadataTrustCheck(metadataTrustCheck);
def.setShowSamlLink(showSamlLink);
def.setLinkText(linkText);
def.setIconUrl(iconUrl);
def.setAddShadowUserOnLogin(isAddShadowUserOnLogin());
def.setEmailDomain(emailDomain);
def.setExternalGroupsWhitelist(externalGroupsWhitelist);
def.setAttributeMappings(attributeMappings);
def.setAdditionalConfiguration(getAdditionalConfiguration());
def.setProviderDescription(getProviderDescription());
def.setGroupMappingMode(getGroupMappingMode());
def.setSocketFactoryClassName(getSocketFactoryClassName());
def.setSkipSslValidation(isSkipSslValidation());
def.setStoreCustomAttributes(isStoreCustomAttributes());
return def;
}
@JsonIgnore
public MetadataLocation getType() {
String trimmedLocation = metaDataLocation.trim();
if (trimmedLocation.startsWith("<?xml") ||
trimmedLocation.startsWith("<md:EntityDescriptor") ||
trimmedLocation.startsWith("<EntityDescriptor")) {
if(validateXml(trimmedLocation)) {
return MetadataLocation.DATA;
}
} else if (trimmedLocation.startsWith("http")) {
try {
URL uri = new URL(trimmedLocation);
return MetadataLocation.URL;
} catch (MalformedURLException e) {
//invalid URL
}
}
return MetadataLocation.UNKNOWN;
}
private boolean validateXml(String xml) {
if (xml==null || xml.toUpperCase().contains("<!DOCTYPE")) {
return false;
}
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setExpandEntityReferences(false);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.parse(new InputSource(new StringReader(xml)));
} catch (ParserConfigurationException | SAXException | IOException e) {
return false;
}
return true;
}
public String getMetaDataLocation() {
return metaDataLocation;
}
public SamlIdentityProviderDefinition setMetaDataLocation(String metaDataLocation) {
this.metaDataLocation = metaDataLocation;
return this;
}
public String getIdpEntityAlias() {
return idpEntityAlias;
}
public SamlIdentityProviderDefinition setIdpEntityAlias(String idpEntityAlias) {
this.idpEntityAlias = idpEntityAlias;
return this;
}
public String getNameID() {
return nameID;
}
public SamlIdentityProviderDefinition setNameID(String nameID) {
this.nameID = nameID;
return this;
}
public int getAssertionConsumerIndex() {
return assertionConsumerIndex;
}
public SamlIdentityProviderDefinition setAssertionConsumerIndex(int assertionConsumerIndex) {
this.assertionConsumerIndex = assertionConsumerIndex;
return this;
}
public boolean isMetadataTrustCheck() {
return metadataTrustCheck;
}
public SamlIdentityProviderDefinition setMetadataTrustCheck(boolean metadataTrustCheck) {
this.metadataTrustCheck = metadataTrustCheck;
return this;
}
public boolean isShowSamlLink() {
return showSamlLink;
}
public SamlIdentityProviderDefinition setShowSamlLink(boolean showSamlLink) {
this.showSamlLink = showSamlLink;
return this;
}
public ExternalGroupMappingMode getGroupMappingMode() {
return groupMappingMode;
}
public void setGroupMappingMode(ExternalGroupMappingMode asScopes) {
this.groupMappingMode = asScopes;
}
public String getSocketFactoryClassName() {
return null;
}
public SamlIdentityProviderDefinition setSocketFactoryClassName(String socketFactoryClassName) {
//no op
return this;
}
public String getLinkText() {
return StringUtils.hasText(linkText) ? linkText : idpEntityAlias;
}
public SamlIdentityProviderDefinition setLinkText(String linkText) {
this.linkText = linkText;
return this;
}
public String getIconUrl() {
return iconUrl;
}
public SamlIdentityProviderDefinition setIconUrl(String iconUrl) {
this.iconUrl = iconUrl;
return this;
}
public String getZoneId() {
return zoneId;
}
public SamlIdentityProviderDefinition setZoneId(String zoneId) {
this.zoneId = zoneId;
return this;
}
public boolean isSkipSslValidation() {
return skipSslValidation;
}
public void setSkipSslValidation(boolean skipSslValidation) {
this.skipSslValidation = skipSslValidation;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
SamlIdentityProviderDefinition that = (SamlIdentityProviderDefinition) o;
return Objects.equals(getUniqueAlias(), that.getUniqueAlias());
}
@Override
public int hashCode() {
String alias = getUniqueAlias();
return alias==null ? 0 : alias.hashCode();
}
@JsonIgnore
public String getUniqueAlias() {
return getIdpEntityAlias()+"###"+getZoneId();
}
@Override
public String toString() {
return "SamlIdentityProviderDefinition{" +
"idpEntityAlias='" + idpEntityAlias + '\'' +
", metaDataLocation='" + metaDataLocation + '\'' +
", nameID='" + nameID + '\'' +
", assertionConsumerIndex=" + assertionConsumerIndex +
", metadataTrustCheck=" + metadataTrustCheck +
", showSamlLink=" + showSamlLink +
", socketFactoryClassName='deprected-not used'" +
", skipSslValidation=" + skipSslValidation +
", linkText='" + linkText + '\'' +
", iconUrl='" + iconUrl + '\'' +
", zoneId='" + zoneId + '\'' +
", addShadowUserOnLogin='" + isAddShadowUserOnLogin() + '\'' +
'}';
}
}