11package com .onelogin .saml2 .settings ;
22
33import java .net .URL ;
4+ import java .util .ArrayList ;
45import java .util .Calendar ;
56import java .util .HashMap ;
67import java .util .List ;
2122
2223import com .onelogin .saml2 .model .Contact ;
2324import com .onelogin .saml2 .model .Organization ;
25+ import com .onelogin .saml2 .model .AttributeConsumingService ;
26+ import com .onelogin .saml2 .model .RequestedAttribute ;
2427import 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