|
|
@ -33,6 +33,7 @@ import org.apache.http.conn.ssl.SSLContextBuilder;
|
|
|
|
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
|
|
|
|
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
|
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
|
|
|
|
|
import org.wso2.carbon.base.MultitenantConstants;
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
import org.wso2.carbon.core.util.KeyStoreManager;
|
|
|
|
import org.wso2.carbon.core.util.KeyStoreManager;
|
|
|
|
import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService;
|
|
|
|
import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService;
|
|
|
@ -193,7 +194,8 @@ public class JWTClientUtil {
|
|
|
|
tenantRegistryLoader.loadTenantRegistry(tenantId);
|
|
|
|
tenantRegistryLoader.loadTenantRegistry(tenantId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static String generateSignedJWTAssertion(String username, JWTConfig jwtConfig) throws JWTClientException {
|
|
|
|
public static String generateSignedJWTAssertion(String username, JWTConfig jwtConfig, boolean isDefaultJWTClient)
|
|
|
|
|
|
|
|
throws JWTClientException {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
String subject = username;
|
|
|
|
String subject = username;
|
|
|
|
long currentTimeMillis = System.currentTimeMillis();
|
|
|
|
long currentTimeMillis = System.currentTimeMillis();
|
|
|
@ -227,15 +229,27 @@ public class JWTClientUtil {
|
|
|
|
String privateKeyAlias = jwtConfig.getPrivateKeyAlias();
|
|
|
|
String privateKeyAlias = jwtConfig.getPrivateKeyAlias();
|
|
|
|
String privateKeyPassword = jwtConfig.getPrivateKeyPassword();
|
|
|
|
String privateKeyPassword = jwtConfig.getPrivateKeyPassword();
|
|
|
|
KeyStore keyStore;
|
|
|
|
KeyStore keyStore;
|
|
|
|
RSAPrivateKey rsaPrivateKey;
|
|
|
|
RSAPrivateKey rsaPrivateKey = null;
|
|
|
|
if (keyStorePath != null && !keyStorePath.isEmpty()) {
|
|
|
|
if (keyStorePath != null && !keyStorePath.isEmpty()) {
|
|
|
|
String keyStorePassword = jwtConfig.getKeyStorePassword();
|
|
|
|
String keyStorePassword = jwtConfig.getKeyStorePassword();
|
|
|
|
keyStore = loadKeyStore(new File(keyStorePath), keyStorePassword, "JKS");
|
|
|
|
keyStore = loadKeyStore(new File(keyStorePath), keyStorePassword, "JKS");
|
|
|
|
rsaPrivateKey = (RSAPrivateKey) keyStore.getKey(privateKeyAlias, privateKeyPassword.toCharArray());
|
|
|
|
rsaPrivateKey = (RSAPrivateKey) keyStore.getKey(privateKeyAlias, privateKeyPassword.toCharArray());
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
|
|
|
|
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
|
|
|
|
KeyStoreManager tenantKeyStoreManager = KeyStoreManager.getInstance(tenantId);
|
|
|
|
JWTClientUtil.loadTenantRegistry(tenantId);
|
|
|
|
rsaPrivateKey = (RSAPrivateKey) tenantKeyStoreManager.getDefaultPrivateKey();
|
|
|
|
if (!(MultitenantConstants.SUPER_TENANT_ID == tenantId) && !isDefaultJWTClient) {
|
|
|
|
|
|
|
|
KeyStoreManager tenantKeyStoreManager = KeyStoreManager.getInstance(tenantId);
|
|
|
|
|
|
|
|
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true);
|
|
|
|
|
|
|
|
String ksName = tenantDomain.trim().replace('.', '-');
|
|
|
|
|
|
|
|
String jksName = ksName + ".jks";
|
|
|
|
|
|
|
|
rsaPrivateKey = (RSAPrivateKey) tenantKeyStoreManager.getPrivateKey(jksName, tenantDomain);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
PrivilegedCarbonContext.startTenantFlow();
|
|
|
|
|
|
|
|
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID);
|
|
|
|
|
|
|
|
KeyStoreManager tenantKeyStoreManager = KeyStoreManager.getInstance(MultitenantConstants.SUPER_TENANT_ID);
|
|
|
|
|
|
|
|
rsaPrivateKey = (RSAPrivateKey) tenantKeyStoreManager.getDefaultPrivateKey();
|
|
|
|
|
|
|
|
PrivilegedCarbonContext.endTenantFlow();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
JWSSigner signer = new RSASSASigner(rsaPrivateKey);
|
|
|
|
JWSSigner signer = new RSASSASigner(rsaPrivateKey);
|
|
|
|
SignedJWT signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.RS256), claimsSet);
|
|
|
|
SignedJWT signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.RS256), claimsSet);
|
|
|
|