Skip to content

Commit efe42b9

Browse files
committed
Add Registration to Saml2Authentication
Closes gh-9487
1 parent 88c1475 commit efe42b9

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

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

Lines changed: 7 additions & 1 deletion
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

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

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

194196
[[servlet-saml2login-opensaml-customization]]
195197
==== Customizing OpenSAML Configuration
@@ -230,7 +232,7 @@ static {
230232
authnRequest.setForceAuthN(true);
231233
}
232234
}
233-
235+
234236
factory.getMarshallerFactory().registerMarshaller(AuthnRequest.DEFAULT_ELEMENT_NAME, marshaller);
235237
});
236238
}
@@ -342,6 +344,10 @@ public RelyingPartyRegistrationRepository relyingPartyRegistrations() {
342344
----
343345
====
344346

347+
[[servlet-saml2login-relyingpartyregistrationid]]
348+
[NOTE]
349+
The `registrationId` is an arbitrary value that you choose for differentiating between registrations.
350+
345351
Or you can provide each detail manually, as you can see below:
346352

347353
.Relying Party Registration Repository Manual Configuration

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.security.core.AuthenticatedPrincipal;
2323
import org.springframework.security.core.Authentication;
2424
import org.springframework.security.core.GrantedAuthority;
25+
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration;
2526
import org.springframework.util.Assert;
2627

2728
/**
@@ -41,14 +42,40 @@ public class Saml2Authentication extends AbstractAuthenticationToken {
4142

4243
private final String saml2Response;
4344

45+
private final String relyingPartyRegistrationId;
46+
47+
/**
48+
* Construct a {@link Saml2Authentication} using the provided parameters
49+
* @param principal the logged in user
50+
* @param saml2Response the SAML 2.0 response used to authenticate the user
51+
* @param authorities the authorities for the logged in user
52+
* @deprecated Use
53+
* {@link #Saml2Authentication(AuthenticatedPrincipal, String, Collection, String)}
54+
*/
55+
@Deprecated
4456
public Saml2Authentication(AuthenticatedPrincipal principal, String saml2Response,
4557
Collection<? extends GrantedAuthority> authorities) {
58+
this(principal, saml2Response, authorities, null);
59+
}
60+
61+
/**
62+
* Construct a {@link Saml2Authentication} using the provided parameters
63+
* @param principal the logged in user
64+
* @param saml2Response the SAML 2.0 response used to authenticate the user
65+
* @param authorities the authorities for the logged in user
66+
* @param relyingPartyRegistrationId the
67+
* {@link RelyingPartyRegistration#getRegistrationId} associated with this user
68+
* @since 5.5
69+
*/
70+
public Saml2Authentication(AuthenticatedPrincipal principal, String saml2Response,
71+
Collection<? extends GrantedAuthority> authorities, String relyingPartyRegistrationId) {
4672
super(authorities);
4773
Assert.notNull(principal, "principal cannot be null");
4874
Assert.hasText(saml2Response, "saml2Response cannot be null");
4975
this.principal = principal;
5076
this.saml2Response = saml2Response;
5177
setAuthenticated(true);
78+
this.relyingPartyRegistrationId = relyingPartyRegistrationId;
5279
}
5380

5481
@Override
@@ -69,4 +96,14 @@ public Object getCredentials() {
6996
return getSaml2Response();
7097
}
7198

99+
/**
100+
* Get the registration id associated with the {@link RelyingPartyRegistration} that
101+
* this user belongs to
102+
* @return the relying party registration id
103+
* @since 5.5
104+
*/
105+
public String getRelyingPartyRegistrationId() {
106+
return this.relyingPartyRegistrationId;
107+
}
108+
72109
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,8 @@ public static Converter<ResponseToken, Saml2Authentication> createDefaultRespons
425425
String username = assertion.getSubject().getNameID().getValue();
426426
Map<String, List<Object>> attributes = getAssertionAttributes(assertion);
427427
return new Saml2Authentication(new DefaultSaml2AuthenticatedPrincipal(username, attributes),
428-
token.getSaml2Response(), Collections.singleton(new SimpleGrantedAuthority("ROLE_USER")));
428+
token.getSaml2Response(), Collections.singleton(new SimpleGrantedAuthority("ROLE_USER")),
429+
responseToken.token.getRelyingPartyRegistration().getRegistrationId());
429430
};
430431
}
431432

@@ -627,8 +628,8 @@ private Converter<ResponseToken, Saml2Authentication> createCompatibleResponseAu
627628
String username = assertion.getSubject().getNameID().getValue();
628629
Map<String, List<Object>> attributes = getAssertionAttributes(assertion);
629630
return new Saml2Authentication(new DefaultSaml2AuthenticatedPrincipal(username, attributes),
630-
token.getSaml2Response(),
631-
this.authoritiesMapper.mapAuthorities(getAssertionAuthorities(assertion)));
631+
token.getSaml2Response(), this.authoritiesMapper.mapAuthorities(getAssertionAuthorities(assertion)),
632+
responseToken.token.getRelyingPartyRegistration().getRegistrationId());
632633
};
633634
}
634635

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,8 @@ public static Converter<ResponseToken, Saml2Authentication> createDefaultRespons
365365
String username = assertion.getSubject().getNameID().getValue();
366366
Map<String, List<Object>> attributes = getAssertionAttributes(assertion);
367367
return new Saml2Authentication(new DefaultSaml2AuthenticatedPrincipal(username, attributes),
368-
token.getSaml2Response(), AuthorityUtils.createAuthorityList("ROLE_USER"));
368+
token.getSaml2Response(), AuthorityUtils.createAuthorityList("ROLE_USER"),
369+
responseToken.token.getRelyingPartyRegistration().getRegistrationId());
369370
};
370371
}
371372

0 commit comments

Comments
 (0)