Skip to content

Commit f5a525e

Browse files
committed
Add Registration to Saml2Authentication
Closes gh-9487
1 parent 822e59a commit f5a525e

File tree

6 files changed

+49
-6
lines changed

6 files changed

+49
-6
lines changed

docs/manual/src/docs/asciidoc/_includes/servlet/saml2/saml2-login.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ where
107107
* `https://idp.example.com/issuer` is the value contained in the `Issuer` attribute of the SAML responses that the identity provider will issue
108108
* `classpath:idp.crt` is the location on the classpath for the identity provider's certificate for verifying SAML responses, and
109109
* `https://idp.example.com/issuer/sso` is the endpoint where the identity provider is expecting `AuthnRequest` s.
110+
* `adfs` is <<servlet-saml2login-relyingpartyregistrationid, an arbitrary identifier you choose>>
110111

111112
And that's it!
112113

@@ -196,6 +197,7 @@ image:{icondir}/number_10.png[] And finally, it takes the `NameID` from the firs
196197
Then, it places that principal and the authorities into a `Saml2Authentication`.
197198

198199
The resulting `Authentication#getPrincipal` is a Spring Security `Saml2AuthenticatedPrincipal` object, and `Authentication#getName` maps to the first assertion's `NameID` element.
200+
`Saml2AuthenticatedPrincipal#getRelyingPartyRegistrationId` holds the <<servlet-saml2login-relyingpartyregistrationid,identifier to the associated `RelyingPartyRegistration`>>.
199201

200202
[[servlet-saml2login-opensaml-customization]]
201203
==== Customizing OpenSAML Configuration
@@ -410,6 +412,10 @@ open fun relyingPartyRegistrations(): RelyingPartyRegistrationRepository? {
410412
----
411413
====
412414

415+
[[servlet-saml2login-relyingpartyregistrationid]]
416+
[NOTE]
417+
The `registrationId` is an arbitrary value that you choose for differentiating between registrations.
418+
413419
Or you can provide each detail manually, as you can see below:
414420

415421
.Relying Party Registration Repository Manual Configuration

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,14 @@ public class DefaultSaml2AuthenticatedPrincipal implements Saml2AuthenticatedPri
3434

3535
private final Map<String, List<Object>> attributes;
3636

37+
private String registrationId;
38+
3739
public DefaultSaml2AuthenticatedPrincipal(String name, Map<String, List<Object>> attributes) {
3840
Assert.notNull(name, "name cannot be null");
3941
Assert.notNull(attributes, "attributes cannot be null");
4042
this.name = name;
4143
this.attributes = attributes;
44+
this.registrationId = null;
4245
}
4346

4447
@Override
@@ -51,4 +54,14 @@ public Map<String, List<Object>> getAttributes() {
5154
return this.attributes;
5255
}
5356

57+
@Override
58+
public String getRelyingPartyRegistrationId() {
59+
return this.registrationId;
60+
}
61+
62+
public void setRelyingPartyRegistrationId(String registrationId) {
63+
Assert.notNull(registrationId, "relyingPartyRegistrationId cannot be null");
64+
this.registrationId = registrationId;
65+
}
66+
5467
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.springframework.lang.Nullable;
2424
import org.springframework.security.core.AuthenticatedPrincipal;
25+
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration;
2526
import org.springframework.util.CollectionUtils;
2627

2728
/**
@@ -66,4 +67,13 @@ default Map<String, List<Object>> getAttributes() {
6667
return Collections.emptyMap();
6768
}
6869

70+
/**
71+
* Get the {@link RelyingPartyRegistration} identifier
72+
* @return the {@link RelyingPartyRegistration} identifier
73+
* @since 5.6
74+
*/
75+
default String getRelyingPartyRegistrationId() {
76+
return null;
77+
}
78+
6979
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ public class Saml2Authentication extends AbstractAuthenticationToken {
4141

4242
private final String saml2Response;
4343

44+
/**
45+
* Construct a {@link Saml2Authentication} using the provided parameters
46+
* @param principal the logged in user
47+
* @param saml2Response the SAML 2.0 response used to authenticate the user
48+
* @param authorities the authorities for the logged in user
49+
*/
4450
public Saml2Authentication(AuthenticatedPrincipal principal, String saml2Response,
4551
Collection<? extends GrantedAuthority> authorities) {
4652
super(authorities);

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,11 @@ public static Converter<ResponseToken, Saml2Authentication> createDefaultRespons
424424
Assertion assertion = CollectionUtils.firstElement(response.getAssertions());
425425
String username = assertion.getSubject().getNameID().getValue();
426426
Map<String, List<Object>> attributes = getAssertionAttributes(assertion);
427-
return new Saml2Authentication(new DefaultSaml2AuthenticatedPrincipal(username, attributes),
428-
token.getSaml2Response(), Collections.singleton(new SimpleGrantedAuthority("ROLE_USER")));
427+
DefaultSaml2AuthenticatedPrincipal principal = new DefaultSaml2AuthenticatedPrincipal(username, attributes);
428+
String registrationId = responseToken.token.getRelyingPartyRegistration().getRegistrationId();
429+
principal.setRelyingPartyRegistrationId(registrationId);
430+
return new Saml2Authentication(principal, token.getSaml2Response(),
431+
Collections.singleton(new SimpleGrantedAuthority("ROLE_USER")));
429432
};
430433
}
431434

@@ -626,8 +629,10 @@ private Converter<ResponseToken, Saml2Authentication> createCompatibleResponseAu
626629
Assertion assertion = CollectionUtils.firstElement(response.getAssertions());
627630
String username = assertion.getSubject().getNameID().getValue();
628631
Map<String, List<Object>> attributes = getAssertionAttributes(assertion);
629-
return new Saml2Authentication(new DefaultSaml2AuthenticatedPrincipal(username, attributes),
630-
token.getSaml2Response(),
632+
DefaultSaml2AuthenticatedPrincipal principal = new DefaultSaml2AuthenticatedPrincipal(username, attributes);
633+
String registrationId = responseToken.token.getRelyingPartyRegistration().getRegistrationId();
634+
principal.setRelyingPartyRegistrationId(registrationId);
635+
return new Saml2Authentication(principal, token.getSaml2Response(),
631636
this.authoritiesMapper.mapAuthorities(getAssertionAuthorities(assertion)));
632637
};
633638
}

saml2/saml2-service-provider/src/opensaml4Main/java/org/springframework/security/saml2/provider/service/authentication/OpenSaml4AuthenticationProvider.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,11 @@ public static Converter<ResponseToken, Saml2Authentication> createDefaultRespons
425425
Assertion assertion = CollectionUtils.firstElement(response.getAssertions());
426426
String username = assertion.getSubject().getNameID().getValue();
427427
Map<String, List<Object>> attributes = getAssertionAttributes(assertion);
428-
return new Saml2Authentication(new DefaultSaml2AuthenticatedPrincipal(username, attributes),
429-
token.getSaml2Response(), AuthorityUtils.createAuthorityList("ROLE_USER"));
428+
DefaultSaml2AuthenticatedPrincipal principal = new DefaultSaml2AuthenticatedPrincipal(username, attributes);
429+
String registrationId = responseToken.token.getRelyingPartyRegistration().getRegistrationId();
430+
principal.setRelyingPartyRegistrationId(registrationId);
431+
return new Saml2Authentication(principal, token.getSaml2Response(),
432+
AuthorityUtils.createAuthorityList("ROLE_USER"));
430433
};
431434
}
432435

0 commit comments

Comments
 (0)