15
15
*/
16
16
package org .springframework .security .saml2 .provider .service .authentication ;
17
17
18
- import java .io .ByteArrayInputStream ;
19
- import java .nio .charset .StandardCharsets ;
20
- import java .util .HashMap ;
21
- import java .util .List ;
22
- import java .util .Map ;
23
- import javax .xml .XMLConstants ;
24
- import javax .xml .namespace .QName ;
25
-
26
18
import org .springframework .security .saml2 .Saml2Exception ;
27
19
import org .springframework .security .saml2 .credentials .Saml2X509Credential ;
28
20
59
51
import org .w3c .dom .Document ;
60
52
import org .w3c .dom .Element ;
61
53
54
+ import java .io .ByteArrayInputStream ;
55
+ import java .nio .charset .StandardCharsets ;
56
+ import java .util .HashMap ;
57
+ import java .util .List ;
58
+ import java .util .Map ;
59
+ import javax .xml .XMLConstants ;
60
+ import javax .xml .namespace .QName ;
61
+
62
62
import static java .lang .Boolean .FALSE ;
63
63
import static java .lang .Boolean .TRUE ;
64
64
import static java .util .Arrays .asList ;
@@ -165,10 +165,7 @@ <T> T buildSAMLObject(final Class<T> clazz) {
165
165
QName defaultElementName = (QName ) clazz .getDeclaredField ("DEFAULT_ELEMENT_NAME" ).get (null );
166
166
return (T ) getBuilderFactory ().getBuilder (defaultElementName ).buildObject (defaultElementName );
167
167
}
168
- catch (IllegalAccessException e ) {
169
- throw new Saml2Exception ("Could not create SAML object" , e );
170
- }
171
- catch (NoSuchFieldException e ) {
168
+ catch (NoSuchFieldException | IllegalAccessException e ) {
172
169
throw new Saml2Exception ("Could not create SAML object" , e );
173
170
}
174
171
}
@@ -177,6 +174,28 @@ XMLObject resolve(String xml) {
177
174
return resolve (xml .getBytes (StandardCharsets .UTF_8 ));
178
175
}
179
176
177
+ String toXml (XMLObject object , List <Saml2X509Credential > signingCredentials , String localSpEntityId ) {
178
+ if (object instanceof SignableSAMLObject && null != hasSigningCredential (signingCredentials )) {
179
+ signXmlObject (
180
+ (SignableSAMLObject ) object ,
181
+ signingCredentials ,
182
+ localSpEntityId
183
+ );
184
+ }
185
+ final MarshallerFactory marshallerFactory = XMLObjectProviderRegistrySupport .getMarshallerFactory ();
186
+ try {
187
+ Element element = marshallerFactory .getMarshaller (object ).marshall (object );
188
+ return SerializeSupport .nodeToString (element );
189
+ } catch (MarshallingException e ) {
190
+ throw new Saml2Exception (e );
191
+ }
192
+ }
193
+
194
+ /*
195
+ * ==============================================================
196
+ * PRIVATE METHODS
197
+ * ==============================================================
198
+ */
180
199
private XMLObject resolve (byte [] xml ) {
181
200
XMLObject parsed = parse (xml );
182
201
if (parsed != null ) {
@@ -200,18 +219,6 @@ private UnmarshallerFactory getUnmarshallerFactory() {
200
219
return XMLObjectProviderRegistrySupport .getUnmarshallerFactory ();
201
220
}
202
221
203
- String toXml (XMLObject object , List <Saml2X509Credential > signingCredentials , String localSpEntityId )
204
- throws MarshallingException , SignatureException , SecurityException {
205
- if (object instanceof SignableSAMLObject && null != hasSigningCredential (signingCredentials )) {
206
- signXmlObject (
207
- (SignableSAMLObject ) object ,
208
- getSigningCredential (signingCredentials , localSpEntityId )
209
- );
210
- }
211
- final MarshallerFactory marshallerFactory = XMLObjectProviderRegistrySupport .getMarshallerFactory ();
212
- Element element = marshallerFactory .getMarshaller (object ).marshall (object );
213
- return SerializeSupport .nodeToString (element );
214
- }
215
222
216
223
private Saml2X509Credential hasSigningCredential (List <Saml2X509Credential > credentials ) {
217
224
for (Saml2X509Credential c : credentials ) {
@@ -222,29 +229,34 @@ private Saml2X509Credential hasSigningCredential(List<Saml2X509Credential> crede
222
229
return null ;
223
230
}
224
231
225
- private void signXmlObject (SignableSAMLObject object , Credential credential )
226
- throws MarshallingException , SecurityException , SignatureException {
227
- SignatureSigningParameters parameters = new SignatureSigningParameters ();
228
- parameters .setSigningCredential (credential );
229
- parameters .setSignatureAlgorithm (SignatureConstants .ALGO_ID_SIGNATURE_RSA_SHA256 );
230
- parameters .setSignatureReferenceDigestMethod (SignatureConstants .ALGO_ID_DIGEST_SHA256 );
231
- parameters .setSignatureCanonicalizationAlgorithm (SignatureConstants .ALGO_ID_C14N_EXCL_OMIT_COMMENTS );
232
- SignatureSupport .signObject (object , parameters );
233
- }
234
-
235
232
private Credential getSigningCredential (List <Saml2X509Credential > signingCredential ,
236
233
String localSpEntityId
237
234
) {
238
235
Saml2X509Credential credential = hasSigningCredential (signingCredential );
239
236
if (credential == null ) {
240
- throw new IllegalArgumentException ("no signing credential configured" );
237
+ throw new Saml2Exception ("no signing credential configured" );
241
238
}
242
239
BasicCredential cred = getBasicCredential (credential );
243
240
cred .setEntityId (localSpEntityId );
244
241
cred .setUsageType (UsageType .SIGNING );
245
242
return cred ;
246
243
}
247
244
245
+ private void signXmlObject (SignableSAMLObject object , List <Saml2X509Credential > signingCredentials , String entityId ) {
246
+ SignatureSigningParameters parameters = new SignatureSigningParameters ();
247
+ Credential credential = getSigningCredential (signingCredentials , entityId );
248
+ parameters .setSigningCredential (credential );
249
+ parameters .setSignatureAlgorithm (SignatureConstants .ALGO_ID_SIGNATURE_RSA_SHA256 );
250
+ parameters .setSignatureReferenceDigestMethod (SignatureConstants .ALGO_ID_DIGEST_SHA256 );
251
+ parameters .setSignatureCanonicalizationAlgorithm (SignatureConstants .ALGO_ID_C14N_EXCL_OMIT_COMMENTS );
252
+ try {
253
+ SignatureSupport .signObject (object , parameters );
254
+ } catch (MarshallingException | SignatureException | SecurityException e ) {
255
+ throw new Saml2Exception (e );
256
+ }
257
+
258
+ }
259
+
248
260
private BasicX509Credential getBasicCredential (Saml2X509Credential credential ) {
249
261
return CredentialSupport .getSimpleCredential (
250
262
credential .getCertificate (),
0 commit comments