Skip to content

Commit d152c80

Browse files
committed
fix: make Saml2Authentication serializable
1 parent b3d177f commit d152c80

File tree

5 files changed

+124
-1
lines changed

5 files changed

+124
-1
lines changed

saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/authentication/OpenSamlAuthenticationProvider.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public Authentication authenticate(Authentication authentication) throws Authent
178178
Assertion assertion = validateSaml2Response(token, token.getRecipientUri(), samlResponse);
179179
String username = getUsername(token, assertion);
180180
return new Saml2Authentication(
181-
() -> username, token.getSaml2Response(),
181+
new SimpleSaml2AuthenticatedPrincipal(username), token.getSaml2Response(),
182182
this.authoritiesMapper.mapAuthorities(getAssertionAuthorities(assertion))
183183
);
184184
} catch (Saml2AuthenticationException e) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2002-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.saml2.provider.service.authentication;
18+
19+
import org.springframework.security.core.AuthenticatedPrincipal;
20+
21+
/**
22+
* Saml2 representation of an {@link AuthenticatedPrincipal}.
23+
*
24+
* @author Clement Stoquart
25+
* @since 5.3
26+
*/
27+
public interface Saml2AuthenticatedPrincipal extends AuthenticatedPrincipal {
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2002-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.saml2.provider.service.authentication;
18+
19+
import java.io.Serializable;
20+
21+
/**
22+
* Default implementation of a {@link Saml2AuthenticatedPrincipal}.
23+
*
24+
* @author Clement Stoquart
25+
* @since 5.3
26+
*/
27+
class SimpleSaml2AuthenticatedPrincipal implements Saml2AuthenticatedPrincipal, Serializable {
28+
29+
private final String name;
30+
31+
SimpleSaml2AuthenticatedPrincipal(String name) {
32+
this.name = name;
33+
}
34+
35+
@Override
36+
public String getName() {
37+
return this.name;
38+
}
39+
}

saml2/saml2-service-provider/src/test/java/org/springframework/security/saml2/provider/service/authentication/OpenSamlAuthenticationProviderTests.java

+26
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
package org.springframework.security.saml2.provider.service.authentication;
1818

19+
import java.io.ByteArrayOutputStream;
20+
import java.io.IOException;
21+
import java.io.ObjectOutputStream;
22+
1923
import org.springframework.security.core.Authentication;
2024

2125
import org.hamcrest.BaseMatcher;
@@ -346,6 +350,28 @@ public void authenticateWhenDecryptionKeysAreWrongThenThrowAuthenticationExcepti
346350
provider.authenticate(token);
347351
}
348352

353+
@Test
354+
public void writeObjectWhenTypeIsSaml2AuthenticationThenNoException() throws IOException {
355+
Response response = response(recipientUri, idpEntityId);
356+
Assertion assertion = defaultAssertion();
357+
signXmlObject(
358+
assertion,
359+
assertingPartyCredentials(),
360+
recipientEntityId
361+
);
362+
EncryptedAssertion encryptedAssertion = encryptAssertion(assertion, assertingPartyCredentials());
363+
response.getEncryptedAssertions().add(encryptedAssertion);
364+
token = responseXml(response, idpEntityId);
365+
366+
Saml2Authentication authentication = (Saml2Authentication) provider.authenticate(token);
367+
368+
// the following code will throw an exception if authentication isn't serializable
369+
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(1024);
370+
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteStream);
371+
objectOutputStream.writeObject(authentication);
372+
objectOutputStream.flush();
373+
}
374+
349375
private Assertion defaultAssertion() {
350376
return assertion(
351377
username,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2002-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.saml2.provider.service.authentication;
18+
19+
import org.junit.Assert;
20+
import org.junit.Test;
21+
22+
public class SimpleSaml2AuthenticatedPrincipalTests {
23+
24+
@Test
25+
public void createSimpleSaml2AuthenticatedPrincipal() {
26+
SimpleSaml2AuthenticatedPrincipal principal = new SimpleSaml2AuthenticatedPrincipal("user");
27+
28+
Assert.assertEquals("user", principal.getName());
29+
}
30+
}

0 commit comments

Comments
 (0)