Skip to content

Commit

Permalink
New generic keystore file loader.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla committed Feb 2, 2024
1 parent 18049d7 commit b4c5cdc
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
20 changes: 20 additions & 0 deletions sso-samples/oidc-sso-sample/oidc-keystore-loader/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.wso2.samples.is</groupId>
<artifactId>oidc-sso-sample</artifactId>
<version>4.6.0-SNAPSHOT</version>
</parent>

<artifactId>oidc-keystore-loader</artifactId>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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
*
* http://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 org.wso2.sample.identity;

import java.net.URISyntaxException;
import java.nio.file.Paths;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* A listener to get invoked at application deployment.
* This will allow us to set the carbon keystore for HTTPS communication.
*/
public class KeystoreLoader implements ServletContextListener {

private static final Logger LOGGER = Logger.getLogger(KeystoreLoader.class.getName());

@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
// First find keystore properties
final InputStream keystoreInputStream = this.getClass().getClassLoader()
.getResourceAsStream("keystore.properties");

if (keystoreInputStream == null) {
LOGGER.log(Level.SEVERE, "keystore.properties not found. Trust store properties will not be set.");
return;
}

// Load properties
final Properties keystoreProperties = new Properties();

try {
keystoreProperties.load(keystoreInputStream);
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Error while loading properties.", e);
return;
}

// Find and set keystore required for IS server communication
final URL resource = this.getClass().getClassLoader()
.getResource(keystoreProperties.getProperty("keystorename"));

if (resource != null) {
try {
String trustStorePath = Paths.get(resource.toURI()).toFile().getAbsolutePath();
LOGGER.log(Level.INFO, "Setting trust store path to : " + trustStorePath);
System.setProperty("javax.net.ssl.trustStore", trustStorePath);
} catch (URISyntaxException e) {
LOGGER.log(Level.SEVERE, "Unable to find keystore defined by properties. " +
"Trust store properties will not be set.", e);
}
System.setProperty("javax.net.ssl.trustStorePassword", keystoreProperties.getProperty("keystorepassword"));
} else {
LOGGER.log(Level.INFO, "Unable to find keystore defined by properties. " +
"Trust store properties will not be set.");
}
}

@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
// Ignored
}
}

0 comments on commit b4c5cdc

Please sign in to comment.