Search in sources :

Example 1 with ServerConfigurationService

use of org.wso2.carbon.base.api.ServerConfigurationService in project carbon-apimgt by wso2.

the class DefaultApiKeyGenerator method getApiKeySignKeyStore.

private KeyStore getApiKeySignKeyStore(KeyStoreManager keyStoreManager) throws Exception {
    KeyStore apiKeySignKeyStore;
    ServerConfigurationService config = keyStoreManager.getServerConfigService();
    String apiKeySignKeyStoreName = APIUtil.getApiKeySignKeyStoreName();
    if (config.getFirstProperty(APIConstants.KeyStoreManagement.SERVER_APIKEYSIGN_KEYSTORE_FILE.replaceFirst(APIConstants.KeyStoreManagement.KeyStoreName, apiKeySignKeyStoreName)) == null) {
        return null;
    }
    String file = new File(config.getFirstProperty(APIConstants.KeyStoreManagement.SERVER_APIKEYSIGN_KEYSTORE_FILE.replaceFirst(APIConstants.KeyStoreManagement.KeyStoreName, apiKeySignKeyStoreName))).getAbsolutePath();
    KeyStore store = KeyStore.getInstance(config.getFirstProperty(APIConstants.KeyStoreManagement.SERVER_APIKEYSIGN_KEYSTORE_TYPE.replaceFirst(APIConstants.KeyStoreManagement.KeyStoreName, apiKeySignKeyStoreName)));
    String password = config.getFirstProperty(APIConstants.KeyStoreManagement.SERVER_APIKEYSIGN_KEYSTORE_PASSWORD.replaceFirst(APIConstants.KeyStoreManagement.KeyStoreName, apiKeySignKeyStoreName));
    try (FileInputStream in = new FileInputStream(file)) {
        store.load(in, password.toCharArray());
        apiKeySignKeyStore = store;
    }
    return apiKeySignKeyStore;
}
Also used : ServerConfigurationService(org.wso2.carbon.base.api.ServerConfigurationService) KeyStore(java.security.KeyStore) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 2 with ServerConfigurationService

use of org.wso2.carbon.base.api.ServerConfigurationService in project carbon-apimgt by wso2.

the class DefaultApiKeyGenerator method buildSignature.

protected byte[] buildSignature(String assertion) throws APIManagementException {
    PrivateKey privateKey = null;
    // get super tenant's key store manager
    KeyStoreManager tenantKSM = KeyStoreManager.getInstance(MultitenantConstants.SUPER_TENANT_ID);
    try {
        ServerConfigurationService config = tenantKSM.getServerConfigService();
        String apiKeySignKeyStoreName = APIUtil.getApiKeySignKeyStoreName();
        String keyStorePassword = config.getFirstProperty(APIConstants.KeyStoreManagement.SERVER_APIKEYSIGN_PRIVATE_KEY_PASSWORD.replaceFirst(APIConstants.KeyStoreManagement.KeyStoreName, apiKeySignKeyStoreName));
        String apiKeySignAlias = config.getFirstProperty(APIConstants.KeyStoreManagement.SERVER_APIKEYSIGN_KEYSTORE_KEY_ALIAS.replaceFirst(APIConstants.KeyStoreManagement.KeyStoreName, apiKeySignKeyStoreName));
        KeyStore apiKeySignKeyStore = getApiKeySignKeyStore(tenantKSM);
        if (apiKeySignKeyStore != null) {
            privateKey = (PrivateKey) apiKeySignKeyStore.getKey(apiKeySignAlias, keyStorePassword.toCharArray());
        }
    } catch (Exception e) {
        throw new APIManagementException("Error while signing Api Key", e);
    }
    return APIUtil.signJwt(assertion, privateKey, "SHA256withRSA");
}
Also used : KeyStoreManager(org.wso2.carbon.core.util.KeyStoreManager) PrivateKey(java.security.PrivateKey) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ServerConfigurationService(org.wso2.carbon.base.api.ServerConfigurationService) KeyStore(java.security.KeyStore) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException)

Aggregations

KeyStore (java.security.KeyStore)2 ServerConfigurationService (org.wso2.carbon.base.api.ServerConfigurationService)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 PrivateKey (java.security.PrivateKey)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 KeyStoreManager (org.wso2.carbon.core.util.KeyStoreManager)1