metadata.adoc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. [[servlet-saml2login-metadata]]
  2. = Saml 2.0 Metadata
  3. Spring Security can <<parsing-asserting-party-metadata,parse asserting party metadata>> to produce an `AssertingPartyMetadata` instance as well as <<publishing-relying-party-metadata,publish relying party metadata>> from a `RelyingPartyRegistration` instance.
  4. [[parsing-asserting-party-metadata]]
  5. == Parsing `<saml2:IDPSSODescriptor>` metadata
  6. You can parse an asserting party's metadata xref:servlet/saml2/login/overview.adoc#servlet-saml2login-relyingpartyregistrationrepository[using `RelyingPartyRegistrations`].
  7. When using the OpenSAML vendor support, the resulting `AssertingPartyMetadata` will be of type `OpenSamlAssertingPartyDetails`.
  8. This means you'll be able to do get the underlying OpenSAML XMLObject by doing the following:
  9. [tabs]
  10. ======
  11. Java::
  12. +
  13. [source,java,role="primary"]
  14. ----
  15. OpenSamlAssertingPartyDetails details = (OpenSamlAssertingPartyDetails)
  16. registration.getAssertingPartyMetadata();
  17. EntityDescriptor openSamlEntityDescriptor = details.getEntityDescriptor();
  18. ----
  19. Kotlin::
  20. +
  21. [source,kotlin,role="secondary"]
  22. ----
  23. val details: OpenSamlAssertingPartyDetails =
  24. registration.getAssertingPartyMetadata() as OpenSamlAssertingPartyDetails
  25. val openSamlEntityDescriptor: EntityDescriptor = details.getEntityDescriptor()
  26. ----
  27. ======
  28. === Using `AssertingPartyMetadataRepository`
  29. You can also be more targeted than `RelyingPartyRegistrations` by using `AssertingPartyMetadataRepository`, an interface that allows for only retrieving the asserting party metadata.
  30. This allows three valuable features:
  31. * Implementations can refresh asserting party metadata in an expiry-aware fashion
  32. * Implementations of `RelyingPartyRegistrationRepository` can more easily articulate a relationship between a relying party and its one or many corresponding asserting parties
  33. * Implementations can verify metadata signatures
  34. For example, `OpenSaml4AssertingPartyMetadataRepository` uses OpenSAML's `MetadataResolver`, and API whose implementations regularly refresh the underlying metadata in an expiry-aware fashion.
  35. This means that you can now create a refreshable `RelyingPartyRegistrationRepository` in just a few lines of code:
  36. [tabs]
  37. ======
  38. Java::
  39. +
  40. [source,java,role="primary"]
  41. ----
  42. @Component
  43. public class RefreshableRelyingPartyRegistrationRepository
  44. implements IterableRelyingPartyRegistrationRepository {
  45. private final AssertingPartyMetadataRepository metadata =
  46. OpenSamlAssertingPartyMetadataRepository
  47. .fromTrustedMetadataLocation("https://idp.example.org/metadata").build();
  48. @Override
  49. public RelyingPartyRegistration findByRegistrationId(String registrationId) {
  50. AssertingPartyMetadata metadata = this.metadata.findByEntityId(registrationId);
  51. if (metadata == null) {
  52. return null;
  53. }
  54. return applyRelyingParty(metadata);
  55. }
  56. @Override
  57. public Iterator<RelyingPartyRegistration> iterator() {
  58. return StreamSupport.stream(this.metadata.spliterator(), false)
  59. .map(this::applyRelyingParty).iterator();
  60. }
  61. private RelyingPartyRegistration applyRelyingParty(AssertingPartyMetadata metadata) {
  62. return RelyingPartyRegistration.withAssertingPartyMetadata(metadata)
  63. // apply any relying party configuration
  64. .build();
  65. }
  66. }
  67. ----
  68. Kotlin::
  69. +
  70. [source,kotlin,role="secondary"]
  71. ----
  72. @Component
  73. class RefreshableRelyingPartyRegistrationRepository : IterableRelyingPartyRegistrationRepository {
  74. private val metadata: AssertingPartyMetadataRepository =
  75. OpenSamlAssertingPartyMetadataRepository.fromTrustedMetadataLocation(
  76. "https://idp.example.org/metadata").build()
  77. fun findByRegistrationId(registrationId:String?): RelyingPartyRegistration {
  78. val metadata = this.metadata.findByEntityId(registrationId)
  79. if (metadata == null) {
  80. return null
  81. }
  82. return applyRelyingParty(metadata)
  83. }
  84. fun iterator(): Iterator<RelyingPartyRegistration> {
  85. return StreamSupport.stream(this.metadata.spliterator(), false)
  86. .map(this::applyRelyingParty).iterator()
  87. }
  88. private fun applyRelyingParty(metadata: AssertingPartyMetadata): RelyingPartyRegistration {
  89. val details: AssertingPartyMetadata = metadata as AssertingPartyMetadata
  90. return RelyingPartyRegistration.withAssertingPartyMetadata(details)
  91. // apply any relying party configuration
  92. .build()
  93. }
  94. }
  95. ----
  96. ======
  97. [TIP]
  98. `OpenSaml4AssertingPartyMetadataRepository` also ships with a constructor so you can provide a custom `MetadataResolver`. Since the underlying `MetadataResolver` is doing the expirying and refreshing, if you use the constructor directly, you will only get these features by providing an implementation that does so.
  99. === Verifying Metadata Signatures
  100. You can also verify metadata signatures using `OpenSaml4AssertingPartyMetadataRepository` by providing the appropriate set of ``Saml2X509Credential``s as follows:
  101. [tabs]
  102. ======
  103. Java::
  104. +
  105. [source,java,role="primary"]
  106. ----
  107. OpenSamlAssertingPartyMetadataRepository.withMetadataLocation("https://idp.example.org/metadata")
  108. .verificationCredentials((c) -> c.add(myVerificationCredential))
  109. .build();
  110. ----
  111. Kotlin::
  112. +
  113. [source,kotlin,role="secondary"]
  114. ----
  115. OpenSamlAssertingPartyMetadataRepository.withMetadataLocation("https://idp.example.org/metadata")
  116. .verificationCredentials({ c : Collection<Saml2X509Credential> ->
  117. c.add(myVerificationCredential) })
  118. .build()
  119. ----
  120. ======
  121. [NOTE]
  122. If no credentials are provided, the component will not perform signature validation.
  123. [[publishing-relying-party-metadata]]
  124. == Producing `<saml2:SPSSODescriptor>` Metadata
  125. You can publish a metadata endpoint using the `saml2Metadata` DSL method, as you'll see below:
  126. [tabs]
  127. ======
  128. Java::
  129. +
  130. [source,java,role="primary"]
  131. ----
  132. http
  133. // ...
  134. .saml2Login(withDefaults())
  135. .saml2Metadata(withDefaults());
  136. ----
  137. Kotlin::
  138. +
  139. [source,kotlin,role="secondary"]
  140. ----
  141. http {
  142. //...
  143. saml2Login { }
  144. saml2Metadata { }
  145. }
  146. ----
  147. ======
  148. You can use this metadata endpoint to register your relying party with your asserting party.
  149. This is often as simple as finding the correct form field to supply the metadata endpoint.
  150. By default, the metadata endpoint is `+/saml2/metadata+`, though it also responds to `+/saml2/metadata/{registrationId}+` and `+/saml2/service-provider-metadata/{registrationId}+`.
  151. You can change this by calling the `metadataUrl` method in the DSL:
  152. [tabs]
  153. ======
  154. Java::
  155. +
  156. [source,java,role="primary"]
  157. ----
  158. .saml2Metadata((saml2) -> saml2.metadataUrl("/saml/metadata"))
  159. ----
  160. Kotlin::
  161. +
  162. [source,kotlin,role="secondary"]
  163. ----
  164. saml2Metadata {
  165. metadataUrl = "/saml/metadata"
  166. }
  167. ----
  168. ======
  169. == Changing the Way a `RelyingPartyRegistration` Is Looked Up
  170. If you have a different strategy for identifying which `RelyingPartyRegistration` to use, you can configure your own `Saml2MetadataResponseResolver` like the one below:
  171. [tabs]
  172. ======
  173. Java::
  174. +
  175. [source,java,role="primary"]
  176. ----
  177. @Bean
  178. Saml2MetadataResponseResolver metadataResponseResolver(RelyingPartyRegistrationRepository registrations) {
  179. RequestMatcherMetadataResponseResolver metadata = new RequestMatcherMetadataResponseResolver(
  180. (id) -> registrations.findByRegistrationId("relying-party"));
  181. metadata.setMetadataFilename("metadata.xml");
  182. return metadata;
  183. }
  184. ----
  185. Kotlin::
  186. +
  187. [source,kotlin,role="secondary"]
  188. ----
  189. @Bean
  190. fun metadataResponseResolver(val registrations: RelyingPartyRegistrationRepository): Saml2MetadataResponseResolver {
  191. val metadata = new RequestMatcherMetadataResponseResolver(
  192. id: String -> registrations.findByRegistrationId("relying-party"))
  193. metadata.setMetadataFilename("metadata.xml")
  194. return metadata
  195. }
  196. ----
  197. ======