-
Notifications
You must be signed in to change notification settings - Fork 738
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
208 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 0 additions & 112 deletions
112
...eshable-metadata/src/main/java/example/RefreshableRelyingPartyRegistrationRepository.java
This file was deleted.
Oops, something went wrong.
126 changes: 126 additions & 0 deletions
126
...ring-boot/java/saml2/refreshable-metadata/src/main/java/example/RelyingPartyMetadata.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* Copyright 2002-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package example; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.security.cert.CertificateException; | ||
import java.security.cert.CertificateFactory; | ||
import java.security.cert.X509Certificate; | ||
import java.security.interfaces.RSAPrivateKey; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.boot.io.ApplicationResourceLoader; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.core.io.ResourceLoader; | ||
import org.springframework.security.saml2.core.Saml2X509Credential; | ||
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration; | ||
import org.springframework.security.saml2.provider.service.registration.Saml2MessageBinding; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@ConfigurationProperties("saml2") | ||
public class RelyingPartyMetadata { | ||
|
||
private final ResourceLoader resourceLoader = new ApplicationResourceLoader(); | ||
|
||
private String entityId = "{baseUrl}/saml2/metadata"; | ||
|
||
private String sso = "{baseUrl}/login/saml2/sso"; | ||
|
||
private SingleLogout slo = new SingleLogout(); | ||
|
||
private X509Certificate certificate; | ||
|
||
private RSAPrivateKey key; | ||
|
||
public RelyingPartyRegistration apply(RelyingPartyRegistration.Builder builder) { | ||
Saml2X509Credential signing = Saml2X509Credential.signing(this.key, this.certificate); | ||
return builder.entityId(this.entityId) | ||
.assertionConsumerServiceLocation(this.sso) | ||
.singleLogoutServiceBinding(this.slo.getBinding()) | ||
.singleLogoutServiceLocation(this.slo.getLocation()) | ||
.singleLogoutServiceResponseLocation(this.slo.getResponseLocation()) | ||
.signingX509Credentials((c) -> c.add(signing)) | ||
.build(); | ||
} | ||
|
||
public void setEntityId(String entityId) { | ||
this.entityId = entityId; | ||
} | ||
|
||
public void setSso(String sso) { | ||
this.sso = sso; | ||
} | ||
|
||
public void setSlo(SingleLogout slo) { | ||
this.slo = slo; | ||
} | ||
|
||
public void setCertificate(String certificate) { | ||
Resource source = this.resourceLoader.getResource(certificate); | ||
try (InputStream in = source.getInputStream()) { | ||
CertificateFactory certificates = CertificateFactory.getInstance("X.509"); | ||
this.certificate = (X509Certificate) certificates.generateCertificate(in); | ||
} | ||
catch (CertificateException | IOException ex) { | ||
throw new IllegalArgumentException(ex); | ||
} | ||
} | ||
|
||
public void setKey(RSAPrivateKey key) { | ||
this.key = key; | ||
} | ||
|
||
public static class SingleLogout { | ||
|
||
private Saml2MessageBinding binding = Saml2MessageBinding.REDIRECT; | ||
|
||
private String location = "{baseUrl}/logout/saml2/slo"; | ||
|
||
private String responseLocation = "{baseUrl}/logout/saml2/slo"; | ||
|
||
public Saml2MessageBinding getBinding() { | ||
return this.binding; | ||
} | ||
|
||
public void setBinding(Saml2MessageBinding binding) { | ||
this.binding = binding; | ||
} | ||
|
||
public String getLocation() { | ||
return this.location; | ||
} | ||
|
||
public void setLocation(String location) { | ||
this.location = location; | ||
} | ||
|
||
public String getResponseLocation() { | ||
if (this.responseLocation == null) { | ||
return this.location; | ||
} | ||
return this.responseLocation; | ||
} | ||
|
||
public void setResponseLocation(String responseLocation) { | ||
this.responseLocation = responseLocation; | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...refreshable-metadata/src/main/java/example/SourcedRelyingPartyRegistrationRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2021 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package example; | ||
|
||
import java.util.Iterator; | ||
|
||
import org.springframework.security.saml2.provider.service.registration.AssertingPartyMetadata; | ||
import org.springframework.security.saml2.provider.service.registration.AssertingPartyMetadataRepository; | ||
import org.springframework.security.saml2.provider.service.registration.IterableRelyingPartyRegistrationRepository; | ||
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class SourcedRelyingPartyRegistrationRepository implements IterableRelyingPartyRegistrationRepository { | ||
|
||
private final AssertingPartyMetadataRepository assertingParties; | ||
|
||
private final RelyingPartyMetadata metadata; | ||
|
||
public SourcedRelyingPartyRegistrationRepository(AssertingPartyMetadataRepository assertingParties, | ||
RelyingPartyMetadata metadata) { | ||
this.assertingParties = assertingParties; | ||
this.metadata = metadata; | ||
} | ||
|
||
@Override | ||
public RelyingPartyRegistration findByRegistrationId(String registrationId) { | ||
AssertingPartyMetadata metadata = this.assertingParties.findByEntityId(registrationId); | ||
return this.metadata.apply(RelyingPartyRegistration.withAssertingPartyMetadata(metadata)); | ||
} | ||
|
||
@Override | ||
public Iterator<RelyingPartyRegistration> iterator() { | ||
Iterator<AssertingPartyMetadata> assertingParties = this.assertingParties.iterator(); | ||
RelyingPartyMetadata metadata = this.metadata; | ||
return new Iterator<>() { | ||
@Override | ||
public boolean hasNext() { | ||
return assertingParties.hasNext(); | ||
} | ||
|
||
@Override | ||
public RelyingPartyRegistration next() { | ||
return metadata.apply(RelyingPartyRegistration.withAssertingPartyMetadata(assertingParties.next())); | ||
} | ||
}; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters