Java Code Examples for org.wso2.carbon.apimgt.impl.utils.APIUtil#generateHeader()

The following examples show how to use org.wso2.carbon.apimgt.impl.utils.APIUtil#generateHeader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractJWTGenerator.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * Helper method to add public certificate to JWT_HEADER to signature verification.
 *
 * @param endUserName - The end user name
 * @throws APIManagementException
 */
protected String addCertToHeader(String endUserName) throws APIManagementException {

    try {
        //get tenant domain
        String tenantDomain = MultitenantUtils.getTenantDomain(endUserName);
        Certificate publicCert = CertificateMgtUtils.getInstance().getPublicCertificate(tenantDomain);

        //TODO: maintain a hashmap with tenants' pubkey thumbprints after first initialization
        if (publicCert == null) {
            throw new APIManagementException("Error in obtaining keystore for tenantDomain = " + tenantDomain);
        } else {
            return APIUtil.generateHeader(publicCert, signatureAlgorithm);
        }
    } catch (APIManagementException e) {
        String error = "Error in obtaining tenant's keystore";
        throw new APIManagementException(error, e);
    }
}
 
Example 2
Source File: TokenGenTest.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
@Test
public void testJWTx5tEncoding() throws Exception {
    //Read public certificat
    InputStream inputStream = new FileInputStream("src/test/resources/wso2carbon.jks");
    KeyStore keystore = KeyStore.getInstance("JKS");
    char[] pwd = "wso2carbon".toCharArray();
    keystore.load(inputStream, pwd);
    Certificate cert = keystore.getCertificate("wso2carbon");

    //Generate JWT header using the above certificate
    String header = APIUtil.generateHeader(cert, "SHA256withRSA");

    //Get the public certificate's thumbprint and base64url encode it
    byte[] der = cert.getEncoded();
    MessageDigest digestValue = MessageDigest.getInstance("SHA-1");
    digestValue.update(der);
    byte[] digestInBytes = digestValue.digest();
    String publicCertThumbprint = hexify(digestInBytes);
    String encodedThumbprint = java.util.Base64.getUrlEncoder()
            .encodeToString(publicCertThumbprint.getBytes("UTF-8"));
    //Check if the encoded thumbprint get matched with JWT header's x5t
    Assert.assertTrue(header.contains(encodedThumbprint));
}
 
Example 3
Source File: AbstractAPIMgtGatewayJWTGenerator.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to add public certificate to JWT_HEADER to signature verification.
 *
 * @throws APIManagementException
 */
protected String addCertToHeader() throws APIManagementException {

    try {
        KeyStoreManager keyStoreManager = KeyStoreManager.getInstance(MultitenantConstants.SUPER_TENANT_ID);
        Certificate publicCert = keyStoreManager.getDefaultPrimaryCertificate();
        return APIUtil.generateHeader(publicCert, signatureAlgorithm);
    } catch (Exception e) {
        String error = "Error in obtaining keystore";
        throw new APIManagementException(error, e);
    }
}