Search in sources :

Example 6 with KeyStoreManager

use of org.wso2.carbon.core.util.KeyStoreManager in project carbon-apimgt by wso2.

the class UtilTest method testShouldThrowEceptionWhenRetrievingCertificateFails.

@Test(expected = Exception.class)
public void testShouldThrowEceptionWhenRetrievingCertificateFails() throws Exception {
    PowerMockito.mockStatic(KeyStoreManager.class);
    KeyStoreManager keyStoreManager = Mockito.mock(KeyStoreManager.class);
    PowerMockito.when(KeyStoreManager.getInstance(Mockito.anyInt())).thenReturn(keyStoreManager);
    Mockito.when(keyStoreManager.getDefaultPrimaryCertificate()).thenThrow(Exception.class);
    Mockito.when(keyStoreManager.getDefaultPrivateKey()).thenReturn(privateKey);
    Util.getAuthHeader("admin");
}
Also used : KeyStoreManager(org.wso2.carbon.core.util.KeyStoreManager) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with KeyStoreManager

use of org.wso2.carbon.core.util.KeyStoreManager in project carbon-apimgt by wso2.

the class UtilTest method testShouldSetAuthHeaders.

@Test
public void testShouldSetAuthHeaders() throws Exception {
    Options options = new Options();
    PowerMockito.mockStatic(KeyStoreManager.class);
    KeyStoreManager keyStoreManager = Mockito.mock(KeyStoreManager.class);
    ServiceClient serviceClient = Mockito.mock(ServiceClient.class);
    PowerMockito.when(KeyStoreManager.getInstance(Mockito.anyInt())).thenReturn(keyStoreManager);
    Mockito.when(serviceClient.getOptions()).thenReturn(options);
    Mockito.when(keyStoreManager.getDefaultPrimaryCertificate()).thenReturn(null);
    Mockito.when(keyStoreManager.getDefaultPrivateKey()).thenReturn(privateKey);
    Util.setAuthHeaders(serviceClient, "admin");
}
Also used : Options(org.apache.axis2.client.Options) KeyStoreManager(org.wso2.carbon.core.util.KeyStoreManager) ServiceClient(org.apache.axis2.client.ServiceClient) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with KeyStoreManager

use of org.wso2.carbon.core.util.KeyStoreManager in project carbon-apimgt by wso2.

the class ServiceReferenceHolder method setPrivateKey.

public void setPrivateKey() {
    try {
        KeyStoreManager keyStoreManager = KeyStoreManager.getInstance(MultitenantConstants.SUPER_TENANT_ID);
        this.privateKey = keyStoreManager.getDefaultPrivateKey();
    } catch (Exception e) {
        String error = "Error in obtaining keystore";
        log.debug(error, e);
    }
}
Also used : KeyStoreManager(org.wso2.carbon.core.util.KeyStoreManager)

Example 9 with KeyStoreManager

use of org.wso2.carbon.core.util.KeyStoreManager in project carbon-apimgt by wso2.

the class InternalAPIKeyGenerator method buildSignature.

protected void buildSignature(SignedJWT assertion) throws APIManagementException {
    // get super tenant's key store manager
    KeyStoreManager tenantKSM = KeyStoreManager.getInstance(MultitenantConstants.SUPER_TENANT_ID);
    try {
        PrivateKey privateKey = tenantKSM.getDefaultPrivateKey();
        JWSSigner jwsSigner = new RSASSASigner(privateKey);
        assertion.sign(jwsSigner);
    } catch (Exception e) {
        throw new APIManagementException("Error while signing Api Key", e);
    }
}
Also used : KeyStoreManager(org.wso2.carbon.core.util.KeyStoreManager) PrivateKey(java.security.PrivateKey) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) RSASSASigner(com.nimbusds.jose.crypto.RSASSASigner) JWSSigner(com.nimbusds.jose.JWSSigner) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException)

Example 10 with KeyStoreManager

use of org.wso2.carbon.core.util.KeyStoreManager in project carbon-apimgt by wso2.

the class DefaultApiKeyGenerator method buildHeader.

protected String buildHeader() throws APIManagementException {
    Certificate publicCert;
    JSONObject headerWithKid;
    try {
        KeyStoreManager tenantKSM = KeyStoreManager.getInstance(MultitenantConstants.SUPER_TENANT_ID);
        publicCert = tenantKSM.getDefaultPrimaryCertificate();
        String headerWithoutKid = APIUtil.generateHeader(publicCert, APIConstants.SIGNATURE_ALGORITHM_SHA256_WITH_RSA);
        headerWithKid = new JSONObject(headerWithoutKid);
        headerWithKid.put("kid", APIUtil.getApiKeyAlias());
    } catch (Exception e) {
        throw new APIManagementException("Error while building Api key header", e);
    }
    return headerWithKid.toString();
}
Also used : KeyStoreManager(org.wso2.carbon.core.util.KeyStoreManager) JSONObject(org.json.JSONObject) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Certificate(java.security.cert.Certificate)

Aggregations

KeyStoreManager (org.wso2.carbon.core.util.KeyStoreManager)13 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)7 PrivateKey (java.security.PrivateKey)5 KeyStore (java.security.KeyStore)3 Certificate (java.security.cert.Certificate)3 Test (org.junit.Test)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 JWSSigner (com.nimbusds.jose.JWSSigner)2 RSASSASigner (com.nimbusds.jose.crypto.RSASSASigner)2 IOException (java.io.IOException)2 ServerConfigurationService (org.wso2.carbon.base.api.ServerConfigurationService)2 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)2 UserStoreException (org.wso2.carbon.user.api.UserStoreException)2 JWSHeader (com.nimbusds.jose.JWSHeader)1 JWTClaimsSet (com.nimbusds.jwt.JWTClaimsSet)1 SignedJWT (com.nimbusds.jwt.SignedJWT)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 Key (java.security.Key)1 KeyPairGenerator (java.security.KeyPairGenerator)1