Java Code Examples for org.springframework.security.saml.key.KeyManager

The following examples show how to use org.springframework.security.saml.key.KeyManager. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * Used to load .Jks file
 * 
 * @return
 */
@Bean
@Conditional(InsightsSAMLBeanInitializationCondition.class)
public KeyManager keyManager() {
	Resource storeFile = resourceLoaderService.getResource("file:" + singleSignOnConfig.getKeyStoreFilePath());
	Map<String, String> passwords = new HashMap<>();
	passwords.put(singleSignOnConfig.getKeyAlias(), singleSignOnConfig.getKeyPass());
	return new JKSKeyManager(storeFile, singleSignOnConfig.getKeyStorePass(), passwords,
			singleSignOnConfig.getKeyAlias());
}
 
Example 2
@Before
    public void setup() {
        SAMLSSOProperties properties = mock(SAMLSSOProperties.class);
        keyManagerProperties = mock(KeyManagerProperties.class);
        when(properties.getKeyManager()).thenReturn(keyManagerProperties);
//        when(keyManagerProperties.getDefaultKey()).thenReturn("default");
//        when(keyManagerProperties.getKeyPasswords()).thenReturn(Collections.singletonMap("default", "password"));
//        when(keyManagerProperties.getPrivateKeyDerLocation()).thenReturn("classpath:localhost:key.der");
//        when(keyManagerProperties.getPublicKeyPemLocation()).thenReturn("classpath:localhost.cert");
//        when(keyManagerProperties.getStoreLocation()).thenReturn("classpath:KeyStore.jks");
//        when(keyManagerProperties.getStorePass()).thenReturn("storePass");
        builder = mock(ServiceProviderBuilder.class);
        when(builder.getSharedObject(KeyManager.class)).thenReturn(null);
        when(builder.getSharedObject(SAMLSSOProperties.class)).thenReturn(properties);
        when(builder.getSharedObject(ResourceLoader.class)).thenReturn(new DefaultResourceLoader());
    }
 
Example 3
@Test
public void testArguments_keystore() throws Exception {
    KeyManagerConfigurer configurer = new KeyManagerConfigurer();
    configurer
            .keyStore(new KeystoreFactory(new DefaultResourceLoader()).createEmptyKeystore());
    configurer.init(builder);
    configurer.configure(builder);
    ArgumentCaptor<KeyManager> providerCaptor = ArgumentCaptor.forClass(KeyManager.class);
    verify(builder).setSharedObject(eq(KeyManager.class), providerCaptor.capture());
    verify(keyManagerProperties).getDefaultKey();
    verify(keyManagerProperties).getKeyPasswords();
    verify(keyManagerProperties).getPrivateKeyDerLocation();
    verify(keyManagerProperties).getPublicKeyPemLocation();
    verify(keyManagerProperties).getStoreLocation();
    verify(keyManagerProperties).getStorePass();
    assertThat(providerCaptor.getValue()).isNotNull();
    KeyManager keyManager = providerCaptor.getValue();
    assertThat(keyManager.getAvailableCredentials()).isEmpty();
}
 
Example 4
@Test
public void testArguments_keystore_location() throws Exception {
    KeyManagerConfigurer configurer = new KeyManagerConfigurer();
    configurer
            .storeLocation("classpath:KeyStore.jks")
            .storePass("password")
            .defaultKey("default")
            .keyPassword("default", "password");
    configurer.init(builder);
    configurer.configure(builder);
    ArgumentCaptor<KeyManager> providerCaptor = ArgumentCaptor.forClass(KeyManager.class);
    verify(builder).setSharedObject(eq(KeyManager.class), providerCaptor.capture());
    verify(keyManagerProperties, never()).getDefaultKey();
    verify(keyManagerProperties, never()).getKeyPasswords();
    verify(keyManagerProperties, never()).getStoreLocation();
    verify(keyManagerProperties, never()).getStorePass();
    verify(keyManagerProperties).getPrivateKeyDerLocation();
    verify(keyManagerProperties).getPublicKeyPemLocation();
    assertThat(providerCaptor.getValue()).isNotNull();
    KeyManager keyManager = providerCaptor.getValue();
    assertThat(keyManager.getAvailableCredentials()).containsExactly("default");
    assertThat(keyManager.getDefaultCredential().getEntityId()).isEqualTo("default");
    assertThat(keyManager.getDefaultCredentialName()).isEqualTo("default");
}
 
Example 5
@Test
public void testArguments_der_and_pem() throws Exception {
    KeyManagerConfigurer configurer = new KeyManagerConfigurer();
    configurer
            .publicKeyPEMLocation("classpath:localhost.cert")
            .privateKeyDERLocation("classpath:localhost.key.der")
            .defaultKey("localhost")
            .keyPassword("localhost", "");
    configurer.init(builder);
    configurer.configure(builder);
    ArgumentCaptor<KeyManager> providerCaptor = ArgumentCaptor.forClass(KeyManager.class);
    verify(builder).setSharedObject(eq(KeyManager.class), providerCaptor.capture());
    verify(keyManagerProperties, never()).getDefaultKey();
    verify(keyManagerProperties, never()).getKeyPasswords();
    verify(keyManagerProperties, never()).getPrivateKeyDerLocation();
    verify(keyManagerProperties, never()).getPublicKeyPemLocation();
    verify(keyManagerProperties).getStoreLocation();
    verify(keyManagerProperties).getStorePass();
    assertThat(providerCaptor.getValue()).isNotNull();
    KeyManager keyManager = providerCaptor.getValue();
    assertThat(keyManager.getAvailableCredentials()).containsExactly("localhost");
    assertThat(keyManager.getDefaultCredential().getEntityId()).isEqualTo("localhost");
    assertThat(keyManager.getDefaultCredentialName()).isEqualTo("localhost");
}
 
Example 6
@Test(expected = IllegalArgumentException.class)
public void testArguments_der_and_pem_error() throws Exception {
    KeyManagerConfigurer configurer = new KeyManagerConfigurer();
    configurer
            .publicKeyPEMLocation("classpath:localhost.cert")
            .privateKeyDERLocation("classpath:localhost.key.der");
    configurer.init(builder);
    configurer.configure(builder);
    ArgumentCaptor<KeyManager> providerCaptor = ArgumentCaptor.forClass(KeyManager.class);
    verify(builder).setSharedObject(eq(KeyManager.class), providerCaptor.capture());
    verify(keyManagerProperties, never()).getDefaultKey();
    verify(keyManagerProperties, never()).getKeyPasswords();
    verify(keyManagerProperties, never()).getPrivateKeyDerLocation();
    verify(keyManagerProperties, never()).getPublicKeyPemLocation();
    verify(keyManagerProperties).getStoreLocation();
    verify(keyManagerProperties).getStorePass();
    assertThat(providerCaptor.getValue()).isNotNull();
    KeyManager keyManager = providerCaptor.getValue();
    assertThat(keyManager.getAvailableCredentials()).containsExactly("localhost");
    assertThat(keyManager.getDefaultCredential().getEntityId()).isEqualTo("localhost");
    assertThat(keyManager.getDefaultCredentialName()).isEqualTo("localhost");
}
 
Example 7
Source Project: spring-boot-security-saml   Source File: TLSConfigurer.java    License: MIT License 5 votes vote down vote up
@Override
public void configure(ServiceProviderBuilder builder) throws Exception {
    KeyManager keyManager = builder.getSharedObject(KeyManager.class);
    TLSProtocolConfigurer configurer = createDefaultTlsProtocolConfigurer();
    configurer.setProtocolName(Optional.ofNullable(protocolName).orElseGet(config::getProtocolName));
    configurer.setProtocolPort(Optional.ofNullable(protocolPort).orElseGet(config::getProtocolPort));
    configurer.setSslHostnameVerification(Optional.ofNullable(sslHostnameVerification).orElseGet(config::getSslHostnameVerification));
    configurer.setTrustedKeys(Optional.ofNullable(trustedKeys).orElseGet(config::getTrustedKeys));
    configurer.setKeyManager(keyManager);
    configurer.afterPropertiesSet();
    builder.setSharedObject(TLSProtocolConfigurer.class, configurer);
}
 
Example 8
Source Project: spring-boot-security-saml   Source File: KeyManagerConfigurer.java    License: MIT License 5 votes vote down vote up
@Override
public void init(ServiceProviderBuilder builder) throws Exception {
    keyManagerBean = builder.getSharedObject(KeyManager.class);
    config = builder.getSharedObject(SAMLSSOProperties.class).getKeyManager();
    resourceLoader = builder.getSharedObject(ResourceLoader.class);
    keystoreFactory = new KeystoreFactory(resourceLoader);
}
 
Example 9
@Override
public void afterPropertiesSet() {
    //All existing beans are thrown as shared objects to the ServiceProviderSecurityBuilder, which will wire all
    //beans/objects related to spring security SAML.
    serviceProviderBuilder.setSharedObject(ParserPool.class, ParserPoolHolder.getPool());
    serviceProviderBuilder.setSharedObject(WebSSOProfileConsumerImpl.class, (WebSSOProfileConsumerImpl) webSSOProfileConsumer);
    serviceProviderBuilder.setSharedObject(WebSSOProfileConsumerHoKImpl.class, hokWebSSOProfileConsumer);
    serviceProviderBuilder.setSharedObject(ServiceProviderEndpoints.class, new ServiceProviderEndpoints());
    serviceProviderBuilder.setSharedObject(ResourceLoader.class, resourceLoader);
    serviceProviderBuilder.setSharedObject(SAMLSSOProperties.class, sAMLSsoProperties);
    serviceProviderBuilder.setSharedObject(ExtendedMetadata.class, extendedMetadata);
    serviceProviderBuilder.setSharedObject(LocalExtendedMetadata.class, localExtendedMetadata);
    serviceProviderBuilder.setSharedObject(SAMLAuthenticationProvider.class, samlAuthenticationProvider);
    serviceProviderBuilder.setSharedObject(SAMLContextProvider.class, samlContextProvider);
    serviceProviderBuilder.setSharedObject(KeyManager.class, keyManager);
    serviceProviderBuilder.setSharedObject(MetadataManager.class, metadataManager);
    serviceProviderBuilder.setSharedObject(MetadataGenerator.class, metadataGenerator);
    serviceProviderBuilder.setSharedObject(SAMLProcessor.class, samlProcessor);
    serviceProviderBuilder.setSharedObject(WebSSOProfile.class, webSSOProfile);
    serviceProviderBuilder.setSharedObject(WebSSOProfileECPImpl.class, ecpProfile);
    serviceProviderBuilder.setSharedObject(WebSSOProfileHoKImpl.class, hokWebSSOProfile);
    serviceProviderBuilder.setSharedObject(SingleLogoutProfile.class, sloProfile);
    serviceProviderBuilder.setSharedObject(WebSSOProfileConsumer.class, webSSOProfileConsumer);
    serviceProviderBuilder.setSharedObject(WebSSOProfileConsumerHoKImpl.class, hokWebSSOProfileConsumer);
    serviceProviderBuilder.setSharedObject(SAMLLogger.class, samlLogger);
    serviceProviderBuilder.setSharedObject(ApplicationEventPublisher.class, eventPublisher);
}
 
Example 10
@Test
public void init() throws Exception {
    KeyManagerConfigurer configurer = new KeyManagerConfigurer();
    configurer.init(builder);
    verify(builder).getSharedObject(eq(KeyManager.class));
    verify(builder).getSharedObject(eq(SAMLSSOProperties.class));
}
 
Example 11
@Test
public void configure() throws Exception {
    KeyManagerConfigurer configurer = new KeyManagerConfigurer();
    configurer.init(builder);
    configurer.configure(builder);
    verify(builder).setSharedObject(eq(KeyManager.class), any(KeyManager.class));
}
 
Example 12
@Test
public void configure_forBean() throws Exception {
    KeyManager keyManager = mock(KeyManager.class);
    when(builder.getSharedObject(KeyManager.class)).thenReturn(keyManager);
    KeyManagerConfigurer configurer = new KeyManagerConfigurer();
    configurer.init(builder);
    configurer.configure(builder);
    verify(builder, never()).setSharedObject(any(), any());
    verifyZeroInteractions(keyManager, keyManagerProperties);
}
 
Example 13
@Test
public void configure_forConstructor() throws Exception {
    KeyManager keyManager = mock(KeyManager.class);
    KeyManagerConfigurer configurer = new KeyManagerConfigurer(keyManager);
    configurer.init(builder);
    configurer.configure(builder);
    verify(builder).setSharedObject(eq(KeyManager.class), eq(keyManager));
    verifyZeroInteractions(keyManager, keyManagerProperties);
}
 
Example 14
@Test
public void testProperties() throws Exception {
    KeyManagerConfigurer configurer = new KeyManagerConfigurer();
    configurer.init(builder);
    configurer.configure(builder);
    ArgumentCaptor<KeyManager> providerCaptor = ArgumentCaptor.forClass(KeyManager.class);
    verify(builder).setSharedObject(eq(KeyManager.class), providerCaptor.capture());
    assertThat(providerCaptor.getValue()).isNotNull();
    KeyManager keyManager = providerCaptor.getValue();
    assertThat(keyManager).isExactlyInstanceOf(EmptyKeyManager.class);
}
 
Example 15
Source Project: spring-tsers-auth   Source File: WebSecurityConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public KeyManager keyManager() {
    DefaultResourceLoader loader = new DefaultResourceLoader();
    Resource storeFile = loader
            .getResource("classpath:/saml/samlKeystore.jks");
    String storePass = "nalle123";
    Map<String, String> passwords = new HashMap<String, String>();
    passwords.put("apollo", "nalle123");
    String defaultKey = "apollo";
    return new JKSKeyManager(storeFile, storePass, passwords, defaultKey);
}
 
Example 16
Source Project: spring-boot-security-saml-samples   Source File: SAMLConfig.java    License: MIT License 5 votes vote down vote up
@Bean
public MetadataGenerator metadataGenerator(KeyManager keyManager) {
    MetadataGenerator generator = new MetadataGenerator();
    generator.setEntityId("localhost-demo");
    generator.setExtendedMetadata(extendedMetadata());
    generator.setIncludeDiscoveryExtension(false);
    generator.setKeyManager(keyManager);
    return generator;
}
 
Example 17
@Bean
public KeyManager keyManager() {
    DefaultResourceLoader loader = new DefaultResourceLoader();
    Resource storeFile = loader
            .getResource("classpath:/saml/samlKeystore.jks");
    String storePass = "nalle123";
    Map<String, String> passwords = new HashMap<String, String>();
    passwords.put("apollo", "nalle123");
    String defaultKey = "apollo";
    return new JKSKeyManager(storeFile, storePass, passwords, defaultKey);
}
 
Example 18
Source Project: blackduck-alert   Source File: AuthenticationHandler.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
public KeyManager keyManager() {
    return new EmptyKeyManager();
}
 
Example 19
Source Project: spring-boot-security-saml   Source File: KeyManagerConfigurer.java    License: MIT License 4 votes vote down vote up
public KeyManagerConfigurer(KeyManager keyManager) {
    this.keyManager = keyManager;
}
 
Example 20
/**
 * {@inheritDoc}
 */
@Override
@Autowired(required = false)
public void setKeyManager(KeyManager keyManager) {
    super.setKeyManager(keyManager);
}
 
Example 21
Source Project: spring-boot-security-saml-samples   Source File: SAMLConfig.java    License: MIT License 4 votes vote down vote up
@Bean
public KeyManager keyManager(KeystoreFactory keystoreFactory) {
    KeyStore keystore = keystoreFactory.loadKeystore("classpath:/localhost.cert", "classpath:/localhost.key.der", "localhost", "");
    return new JKSKeyManager(keystore, ImmutableMap.of("localhost", ""), "localhost");
}
 
Example 22
Source Project: spring-boot-security-saml-samples   Source File: SAMLConfig.java    License: MIT License 4 votes vote down vote up
@Bean
public TLSProtocolConfigurer tlsProtocolConfigurer(KeyManager keyManager) {
    TLSProtocolConfigurer configurer = new TLSProtocolConfigurer();
    configurer.setKeyManager(keyManager);
    return configurer;
}
 
Example 23
/**
 * Key manager provides information about private certificate and trusted keys provide in addition to
 * cryptographic material present in entity metadata documents.
 *
 * @param keyManager key manager
 */
@Override
@Autowired(required = false)
public void setKeyManager(KeyManager keyManager) {
    super.setKeyManager(keyManager);
}
 
Example 24
Source Project: spring-boot-security-saml   Source File: DSLMetadataManager.java    License: MIT License 2 votes vote down vote up
/**
 * Key manager provides information about private certificate and trusted keys provide in addition to
 * cryptographic material present in entity metadata documents.
 *
 * @param keyManager key manager
 */
@Override
@Autowired(required = false)
public void setKeyManager(KeyManager keyManager) {
    super.setKeyManager(keyManager);
}
 
Example 25
Source Project: spring-boot-security-saml   Source File: DSLMetadataGenerator.java    License: MIT License 2 votes vote down vote up
/**
 * Key manager provides information about private certificate and trusted keys provide in addition to
 * cryptographic material present in entity metadata documents.
 *
 * @param keyManager key manager
 */
@Override
@Autowired(required = false)
public void setKeyManager(KeyManager keyManager) {
    super.setKeyManager(keyManager);
}