All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.identityx.clientSDK.credentialsProviders.PropFileCredentialsProvider Maven / Gradle / Ivy

There is a newer version: 5.6.0.2
Show newest version
/*
* Copyright Daon.
*
* 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
*
*     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 com.identityx.clientSDK.credentialsProviders;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.identityx.auth.def.IApiKey;
import com.identityx.auth.def.ITokenKey;
import com.identityx.auth.impl.keys.SharedSecretApiKey;
import com.identityx.clientSDK.def.ICredentialsProvider;
import com.identityx.clientSDK.exceptions.ClientInitializationException;

/**
 * Provides credentials from a properties file containing an un-encrypted shared secret.
 * The credentials file is loaded from the class path.
 * The file has to be a ".properties" file and contain the fields
 * sharedKeyId, sharedKey, serviceUrl  
 * 
 */
public class PropFileCredentialsProvider implements ICredentialsProvider {

	protected static String defaultCredentialsFileName = "credentials.properties"; 
	
	private String baseUrl;
	private ITokenKey apiKey;
	private IApiKey responseApiKey = null;
	

	/**
	 * Initialises credentials based on the input .properties file. 
	 * @param credentialsFileName if null, "credentials.properties" will be used. File will be opened from the class path.
	 * @throws ClientInitializationException
	 */
	public PropFileCredentialsProvider(String credentialsFileName) throws ClientInitializationException {
		init(credentialsFileName);
	}

	/**
	 * Initialises credentials based on the input stream of a .properties file. 
	 * @param credentialsFile Input stream associated with the .properties file
	 * @throws ClientInitializationException
	 */
	public PropFileCredentialsProvider(InputStream credentialsFile) throws ClientInitializationException {
		init(credentialsFile);
	}
	
	protected void init(InputStream credentialsFile) throws ClientInitializationException {
		
		Properties properties = new Properties();		
		try {	 
			properties.load(credentialsFile);
			apiKey = new SharedSecretApiKey(properties.getProperty("sharedKeyId"), properties.getProperty("sharedKey"));
			baseUrl = properties.getProperty("serviceUrl");
	 
		} catch (Exception ex) {
			throw new ClientInitializationException("Failed to initialize the Credential Provider", ex);
		}
	}	
	
	protected void init(String credentialsFileName) throws ClientInitializationException {
		
		Properties properties = new Properties();
		InputStream input = null;
		
		if (credentialsFileName == null) credentialsFileName = defaultCredentialsFileName; 
		
		try {
	 
			ClassLoader loader = Thread.currentThread().getContextClassLoader();
			input = loader.getResourceAsStream(credentialsFileName);
			properties.load(input);
			apiKey = new SharedSecretApiKey(properties.getProperty("sharedKeyId"), properties.getProperty("sharedKey"));
			baseUrl = properties.getProperty("serviceUrl");
	 
		} catch (Exception ex) {
			throw new ClientInitializationException("Failed to initialize the Credential Provider", ex);
		} finally {
			if (input != null) {
				try {
					input.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	@Override
	public ITokenKey getApiKey() {
		return apiKey;
	}

	public void setApiKey(ITokenKey apiKey) {
		this.apiKey = apiKey;
	}

	@Override
	public String getBaseUrl() {
		return baseUrl;
	}

	public void setBaseUrl(String baseUrl) {
		this.baseUrl = baseUrl;
	}
	
	@Override
	public IApiKey getResponseApiKey() {
		if (responseApiKey == null) return apiKey;
		return responseApiKey;
	}

	public void setResponseApiKey(IApiKey responseApiKey) {
		this.responseApiKey = responseApiKey;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy