Search in sources :

Example 1 with RSAConsumerTokenManager

use of org.apache.servicecomb.authentication.consumer.RSAConsumerTokenManager in project incubator-servicecomb-java-chassis by apache.

the class TestRSAProviderTokenManager method testTokenFromVaidatePool.

@Test
public void testTokenFromVaidatePool() {
    RSAKeyPairEntry rsaKeyPairEntry = RSAUtils.generateRSAKeyPair();
    RSAKeypair4Auth.INSTANCE.setPrivateKey(rsaKeyPairEntry.getPrivateKey());
    RSAKeypair4Auth.INSTANCE.setPublicKey(rsaKeyPairEntry.getPublicKey());
    RSAKeypair4Auth.INSTANCE.setPublicKeyEncoded(rsaKeyPairEntry.getPublicKeyEncoded());
    String serviceId = "c8636e5acf1f11e7b701286ed488fc20";
    String instanceId = "e8a04b54cf2711e7b701286ed488fc20";
    RSAConsumerTokenManager rsaCoumserTokenManager = new RSAConsumerTokenManager();
    MicroserviceInstance microserviceInstance = new MicroserviceInstance();
    microserviceInstance.setInstanceId(instanceId);
    Map<String, String> properties = new HashMap<>();
    microserviceInstance.setProperties(properties);
    properties.put(Const.INSTANCE_PUBKEY_PRO, rsaKeyPairEntry.getPublicKeyEncoded());
    Microservice microservice = new Microservice();
    microservice.setServiceId(serviceId);
    new Expectations(RegistryUtils.class) {

        {
            RegistryUtils.getMicroservice();
            result = microservice;
            RegistryUtils.getMicroserviceInstance();
            result = microserviceInstance;
        }
    };
    // Test Consumer first create token
    String token = rsaCoumserTokenManager.getToken();
    Assert.assertNotNull(token);
    // use cache token
    Assert.assertEquals(token, rsaCoumserTokenManager.getToken());
    new Expectations(MicroserviceInstanceCache.class) {

        {
            MicroserviceInstanceCache.getOrCreate(serviceId, instanceId);
            result = microserviceInstance;
        }
    };
    RSAProviderTokenManager rsaProviderTokenManager = new RSAProviderTokenManager();
    // first validate need to verify use RSA
    Assert.assertTrue(rsaProviderTokenManager.valid(token));
    // second validate use validated pool
    Assert.assertTrue(rsaProviderTokenManager.valid(token));
}
Also used : Expectations(mockit.Expectations) Microservice(org.apache.servicecomb.serviceregistry.api.registry.Microservice) RSAKeyPairEntry(org.apache.servicecomb.foundation.common.utils.RSAKeyPairEntry) HashMap(java.util.HashMap) RSAProviderTokenManager(org.apache.servicecomb.authentication.provider.RSAProviderTokenManager) MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance) RSAConsumerTokenManager(org.apache.servicecomb.authentication.consumer.RSAConsumerTokenManager) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)1 Expectations (mockit.Expectations)1 RSAConsumerTokenManager (org.apache.servicecomb.authentication.consumer.RSAConsumerTokenManager)1 RSAProviderTokenManager (org.apache.servicecomb.authentication.provider.RSAProviderTokenManager)1 RSAKeyPairEntry (org.apache.servicecomb.foundation.common.utils.RSAKeyPairEntry)1 Microservice (org.apache.servicecomb.serviceregistry.api.registry.Microservice)1 MicroserviceInstance (org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance)1 Test (org.junit.Test)1