Skip to content

Commit fb93269

Browse files
committed
Add AttributeConsumingService support
1 parent ac8ae33 commit fb93269

4 files changed

Lines changed: 351 additions & 1 deletion

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.onelogin.saml2.model;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import com.onelogin.saml2.model.RequestedAttribute;
6+
7+
/**
8+
* AttributeConsumingService class of OneLogin's Java Toolkit.
9+
*
10+
* A class that stores AttributeConsumingService
11+
*/
12+
public class AttributeConsumingService {
13+
/**
14+
* Service Name
15+
*/
16+
private final String serviceName;
17+
18+
/**
19+
* Service Description
20+
*/
21+
private final String serviceDescription;
22+
23+
/**
24+
* Requested Attributes
25+
*/
26+
private final List<RequestedAttribute> requestedAttributes;
27+
28+
/**
29+
* Constructor
30+
*
31+
* @param serviceName
32+
* String. Service Name
33+
* @param serviceDescription
34+
* String. Service Description
35+
*/
36+
public AttributeConsumingService(String serviceName, String serviceDescription) {
37+
this.serviceName = serviceName != null? serviceName : "";
38+
this.serviceDescription = serviceDescription != null? serviceDescription : "";
39+
this.requestedAttributes = new ArrayList<RequestedAttribute>();
40+
}
41+
42+
/**
43+
* @param attr
44+
* RequestedAttribute. The requested attribute to be included
45+
*/
46+
public final void addRequestedAttribute(RequestedAttribute attr) {
47+
this.requestedAttributes.add(attr);
48+
}
49+
50+
/**
51+
* @return string the service name
52+
*/
53+
public final String getServiceName() {
54+
return serviceName;
55+
}
56+
57+
/**
58+
* @return string the service description
59+
*/
60+
public final String getServiceDescription() {
61+
return serviceDescription;
62+
}
63+
64+
/**
65+
* @return List<RequestedAttribute> the requested attributes
66+
*/
67+
public final List<RequestedAttribute> getRequestedAttributes() {
68+
return requestedAttributes;
69+
}
70+
71+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.onelogin.saml2.model;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
7+
/**
8+
* RequestedAttribute class of OneLogin's Java Toolkit.
9+
*
10+
* A class that stores RequestedAttribute of the AttributeConsumingService
11+
*/
12+
public class RequestedAttribute {
13+
/**
14+
* Name of the attribute
15+
*/
16+
private final String name;
17+
18+
/**
19+
* FriendlyName of the attribute
20+
*/
21+
private final String friendlyName;
22+
23+
/**
24+
* If the attribute is or not required
25+
*/
26+
private final Boolean isRequired;
27+
28+
/**
29+
* NameFormat of the attribute
30+
*/
31+
private final String nameFormat;
32+
33+
/**
34+
* Values of the attribute
35+
*/
36+
private final List<String> attributeValues;
37+
38+
/**
39+
* Constructor
40+
*
41+
* @param name
42+
* String. RequestedAttribute Name
43+
* @param friendlyName
44+
* String. RequestedAttribute FriendlyName
45+
* @param isRequired
46+
* Boolean. RequestedAttribute isRequired value
47+
* @param nameFormat
48+
* Boolean. RequestedAttribute NameFormat
49+
* @param attributeValues
50+
* List<String>. RequestedAttribute values
51+
*/
52+
public RequestedAttribute(String name, String friendlyName, Boolean isRequired, String nameFormat, List<String> attributeValues) {
53+
this.name = name;
54+
this.friendlyName = friendlyName;
55+
this.isRequired = isRequired;
56+
this.nameFormat = nameFormat;
57+
this.attributeValues = attributeValues;
58+
}
59+
60+
/**
61+
* @return string the RequestedAttribute name
62+
*/
63+
public final String getName() {
64+
return name;
65+
}
66+
67+
/**
68+
* @return string the RequestedAttribute fiendlyname
69+
*/
70+
public final String getFriendlyName() {
71+
return friendlyName;
72+
}
73+
74+
/**
75+
* @return boolean the RequestedAttribute isRequired value
76+
*/
77+
public final Boolean isRequired() {
78+
return isRequired;
79+
}
80+
81+
/**
82+
* @return string the RequestedAttribute nameformat
83+
*/
84+
public final String getNameFormat() {
85+
return nameFormat;
86+
}
87+
88+
/**
89+
* @return string the RequestedAttribute nameformat
90+
*/
91+
public final List<String> getAttributeValues() {
92+
return attributeValues;
93+
}
94+
}

core/src/main/java/com/onelogin/saml2/settings/Metadata.java

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.onelogin.saml2.settings;
22

33
import java.net.URL;
4+
import java.util.ArrayList;
45
import java.util.Calendar;
56
import java.util.HashMap;
67
import java.util.List;
@@ -21,6 +22,8 @@
2122

2223
import com.onelogin.saml2.model.Contact;
2324
import com.onelogin.saml2.model.Organization;
25+
import com.onelogin.saml2.model.AttributeConsumingService;
26+
import com.onelogin.saml2.model.RequestedAttribute;
2427
import com.onelogin.saml2.util.Util;
2528

2629
/**
@@ -38,6 +41,11 @@ public class Metadata {
3841
private static final int N_DAYS_VALID_UNTIL = 2;
3942
private static final int SECONDS_CACHED = 604800; // 1 week
4043

44+
/**
45+
* AttributeConsumingService
46+
*/
47+
private AttributeConsumingService attributeConsumingService = null;
48+
4149
/**
4250
* Generated metadata in string format
4351
*/
@@ -62,16 +70,20 @@ public class Metadata {
6270
* Metadata's valid time
6371
* @param cacheDuration
6472
* Duration of the cache in seconds
73+
* @param attributeConsumingService
74+
* AttributeConsumingService of service provider
6575
*
6676
* @throws CertificateEncodingException
6777
*/
68-
public Metadata(Saml2Settings settings, Calendar validUntilTime, Integer cacheDuration) throws CertificateEncodingException {
78+
public Metadata(Saml2Settings settings, Calendar validUntilTime, Integer cacheDuration, AttributeConsumingService attributeConsumingService) throws CertificateEncodingException {
6979
if (validUntilTime == null) {
7080
this.validUntilTime = Calendar.getInstance();
7181
this.validUntilTime.add(Calendar.DAY_OF_YEAR, N_DAYS_VALID_UNTIL);
7282
} else {
7383
this.validUntilTime = validUntilTime;
7484
}
85+
86+
this.attributeConsumingService = attributeConsumingService;
7587

7688
if (cacheDuration == null) {
7789
this.cacheDuration = SECONDS_CACHED;
@@ -86,6 +98,22 @@ public Metadata(Saml2Settings settings, Calendar validUntilTime, Integer cacheDu
8698
metadataString = unsignedMetadataString;
8799
}
88100

101+
/**
102+
* Constructs the Metadata object.
103+
*
104+
* @param settings
105+
* Saml2Settings object. Setting data
106+
* @param validUntilTime
107+
* Metadata's valid time
108+
* @param cacheDuration
109+
* Duration of the cache in seconds
110+
*
111+
* @throws CertificateEncodingException
112+
*/
113+
public Metadata(Saml2Settings settings, Calendar validUntilTime, Integer cacheDuration) throws CertificateEncodingException {
114+
this(settings, validUntilTime, cacheDuration, null);
115+
}
116+
89117
/**
90118
* Constructs the Metadata object.
91119
*
@@ -121,6 +149,8 @@ private StrSubstitutor generateSubstitutor(Saml2Settings settings) throws Certif
121149
valueMap.put("spAssertionConsumerServiceUrl", settings.getSpAssertionConsumerServiceUrl().toString());
122150
valueMap.put("sls", toSLSXml(settings.getSpSingleLogoutServiceUrl(), settings.getSpSingleLogoutServiceBinding()).toString());
123151

152+
valueMap.put("strAttributeConsumingService", getAttributeConsumingServiceXml());
153+
124154
valueMap.put("strKeyDescriptor", toX509KeyDescriptorsXML(settings.getSPcert()).toString());
125155
valueMap.put("strContacts", toContactsXml(settings.getContacts()));
126156
valueMap.put("strOrganization", toOrganizationXml(settings.getOrganization(), "en"));
@@ -146,12 +176,76 @@ private static StringBuilder getMetadataTemplate() {
146176
template.append("<md:AssertionConsumerService Binding=\"${spAssertionConsumerServiceBinding}\"");
147177
template.append(" Location=\"${spAssertionConsumerServiceUrl}\"");
148178
template.append(" index=\"1\"/>");
179+
template.append("${strAttributeConsumingService}");
149180
template.append("</md:SPSSODescriptor>${strOrganization}${strContacts}");
150181
template.append("</md:EntityDescriptor>");
151182

152183
return template;
153184
}
154185

186+
/**
187+
* Generates the AttributeConsumingService section of the metadata's template
188+
*
189+
*
190+
* @return the AttributeConsumingService section of the metadata's template
191+
*/
192+
private String getAttributeConsumingServiceXml() {
193+
StringBuilder attributeConsumingServiceXML = new StringBuilder();
194+
if (attributeConsumingService != null) {
195+
String serviceName = attributeConsumingService.getServiceName();
196+
String serviceDescription = attributeConsumingService.getServiceDescription();
197+
List<RequestedAttribute> requestedAttributes = attributeConsumingService.getRequestedAttributes();
198+
199+
attributeConsumingServiceXML.append("<md:AttributeConsumingService index=\"1\">");
200+
if (serviceName != null && !serviceName.isEmpty()) {
201+
attributeConsumingServiceXML.append("<md:ServiceName xml:lang=\"en\">" + serviceName + "</md:ServiceName>");
202+
}
203+
if (serviceDescription != null && !serviceDescription.isEmpty()) {
204+
attributeConsumingServiceXML.append("<md:ServiceDescription xml:lang=\"en\">" + serviceDescription + "</md:ServiceDescription>");
205+
}
206+
if (requestedAttributes != null && !requestedAttributes.isEmpty()) {
207+
for (RequestedAttribute requestedAttribute : requestedAttributes) {
208+
String name = requestedAttribute.getName();
209+
String friendlyName = requestedAttribute.getFriendlyName();
210+
String nameFormat = requestedAttribute.getNameFormat();
211+
Boolean isRequired = requestedAttribute.isRequired();
212+
List<String> attrValues = requestedAttribute.getAttributeValues() ;
213+
214+
String contentStr = "<md:RequestedAttribute";
215+
216+
if (name != null && !name.isEmpty()) {
217+
contentStr += " Name=\"" + name + "\"";
218+
}
219+
220+
if (nameFormat != null && !nameFormat.isEmpty()) {
221+
contentStr += " NameFormat=\"" + nameFormat + "\"";
222+
}
223+
224+
if (friendlyName != null && !friendlyName.isEmpty()) {
225+
contentStr += " FriendlyName=\"" + friendlyName + "\"";
226+
}
227+
228+
if (isRequired != null) {
229+
contentStr += " isRequired=\"" + isRequired.toString() + "\"";
230+
}
231+
232+
if (attrValues != null && !attrValues.isEmpty()) {
233+
contentStr += ">";
234+
for (String attrValue : attrValues) {
235+
contentStr += "<saml:AttributeValue xmlns:saml=\"urn:oasis:names:tc:SAML:2.0:assertion\">" + attrValue + "</saml:AttributeValue>";
236+
}
237+
attributeConsumingServiceXML.append(contentStr + "</md:RequestedAttribute>");
238+
} else {
239+
attributeConsumingServiceXML.append(contentStr + " />");
240+
}
241+
}
242+
}
243+
attributeConsumingServiceXML.append("</md:AttributeConsumingService>");
244+
}
245+
246+
return attributeConsumingServiceXML.toString();
247+
}
248+
155249
/**
156250
* Generates the contact section of the metadata's template
157251
*

0 commit comments

Comments
 (0)