use of org.wso2.carbon.core.util.KeyStoreManager in project carbon-apimgt by wso2.
the class Util method getAuthHeader.
public static String getAuthHeader(String username) throws Exception {
// Get the filesystem key store default primary certificate
KeyStoreManager keyStoreManager;
keyStoreManager = KeyStoreManager.getInstance(MultitenantConstants.SUPER_TENANT_ID);
try {
keyStoreManager.getDefaultPrimaryCertificate();
JWSSigner signer = new RSASSASigner((RSAPrivateKey) keyStoreManager.getDefaultPrivateKey());
JWTClaimsSet.Builder jwtClaimsSetBuilder = new JWTClaimsSet.Builder();
jwtClaimsSetBuilder.claim("Username", username);
SignedJWT signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.RS512), jwtClaimsSetBuilder.build());
signedJWT.sign(signer);
// generate authorization header value
return "Bearer " + Base64Utils.encode(signedJWT.serialize().getBytes(Charset.defaultCharset()));
} catch (SignatureException e) {
String msg = "Failed to sign with signature instance";
log.error(msg, e);
throw new Exception(msg, e);
} catch (Exception e) {
String msg = "Failed to get primary default certificate";
log.error(msg, e);
throw new Exception(msg, e);
}
}
use of org.wso2.carbon.core.util.KeyStoreManager in project carbon-apimgt by wso2.
the class AbstractJWTGenerator method addCertToHeader.
/**
* Helper method to add public certificate to JWT_HEADER to signature verification.
*
* @throws APIManagementException
* @param tenantDomain
*/
protected String addCertToHeader(String tenantDomain) throws APIManagementException {
try {
Certificate publicCert;
if (tenantBasedSigningEnabled) {
publicCert = SigningUtil.getPublicCertificate(APIUtil.getTenantIdFromTenantDomain(tenantDomain));
} else {
KeyStoreManager keyStoreManager = KeyStoreManager.getInstance(MultitenantConstants.SUPER_TENANT_ID);
publicCert = keyStoreManager.getDefaultPrimaryCertificate();
}
return APIUtil.generateHeader(publicCert, signatureAlgorithm);
} catch (Exception e) {
String error = "Error in obtaining keystore";
throw new APIManagementException(error, e);
}
}
use of org.wso2.carbon.core.util.KeyStoreManager in project carbon-apimgt by wso2.
the class AbstractJWTGenerator method signJWT.
public byte[] signJWT(String assertion, String tenantDomain) throws APIManagementException {
try {
PrivateKey privateKey;
if (tenantBasedSigningEnabled) {
privateKey = SigningUtil.getSigningKey(APIUtil.getTenantIdFromTenantDomain(tenantDomain));
} else {
KeyStoreManager keyStoreManager = KeyStoreManager.getInstance(MultitenantConstants.SUPER_TENANT_ID);
privateKey = keyStoreManager.getDefaultPrivateKey();
}
return APIUtil.signJwt(assertion, privateKey, signatureAlgorithm);
} catch (Exception e) {
throw new APIManagementException(e);
}
}
use of org.wso2.carbon.core.util.KeyStoreManager 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;
}
use of org.wso2.carbon.core.util.KeyStoreManager in project carbon-apimgt by wso2.
the class UtilTest method testShouldThrowEceptionWhenSigningFails.
@Test(expected = Exception.class)
public void testShouldThrowEceptionWhenSigningFails() throws Exception {
KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA");
keygen.initialize(512);
PrivateKey pvtKey = keygen.generateKeyPair().getPrivate();
PowerMockito.mockStatic(KeyStoreManager.class);
KeyStoreManager keyStoreManager = Mockito.mock(KeyStoreManager.class);
PowerMockito.when(KeyStoreManager.getInstance(Mockito.anyInt())).thenReturn(keyStoreManager);
Mockito.when(keyStoreManager.getDefaultPrimaryCertificate()).thenReturn(null);
Mockito.when(keyStoreManager.getDefaultPrivateKey()).thenReturn(pvtKey);
Util.getAuthHeader("admin");
}
Aggregations