Search in sources :

Example 1 with BasicCredentials

use of com.aliyun.oss.common.auth.BasicCredentials in project aliyun-oss-java-sdk by aliyun.

the class STSKeyPairSessionCredentialsProviderTest method testStsKeyPairCredentialsProviderExpire.

@Test
public void testStsKeyPairCredentialsProviderExpire() {
    try {
        PublicKey publicKey = AuthUtils.uploadPublicKey(TestConfig.RAM_REGION_ID, TestConfig.ROOT_ACCESS_KEY_ID, TestConfig.ROOT_ACCESS_KEY_SECRET, AuthUtils.loadPublicKeyFromFile(TestConfig.PUBLIC_KEY_PATH));
        CredentialsProvider credentialsProvider = CredentialsProviderFactory.newSTSKeyPairSessionCredentialsProvider(TestConfig.RAM_REGION_ID, publicKey.getPublicKeyId(), AuthUtils.loadPrivateKeyFromFile(TestConfig.PRIVATE_KEY_PATH)).withExpiredFactor(0.001).withExpiredDuration(2000);
        BasicCredentials credentials = (BasicCredentials) credentialsProvider.getCredentials();
        Assert.assertFalse(credentials.willSoonExpire());
        Thread.sleep(3000);
        Assert.assertTrue(credentials.willSoonExpire());
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : PublicKey(com.aliyun.oss.common.auth.PublicKey) CredentialsProvider(com.aliyun.oss.common.auth.CredentialsProvider) BasicCredentials(com.aliyun.oss.common.auth.BasicCredentials) IOException(java.io.IOException) ClientException(com.aliyuncs.exceptions.ClientException) Test(org.junit.Test)

Example 2 with BasicCredentials

use of com.aliyun.oss.common.auth.BasicCredentials in project aliyun-oss-java-sdk by aliyun.

the class STSAssumeRoleSessionCredentialsProviderTest method testStsAssumeRoleCredentialsProviderExpire.

@Test
public void testStsAssumeRoleCredentialsProviderExpire() {
    try {
        CredentialsProvider credentialsProvider = CredentialsProviderFactory.newSTSAssumeRoleSessionCredentialsProvider(TestConfig.RAM_REGION_ID, TestConfig.USER_ACCESS_KEY_ID, TestConfig.USER_ACCESS_KEY_SECRET, TestConfig.RAM_ROLE_ARN).withExpiredFactor(0.001).withExpiredDuration(2000);
        BasicCredentials credentials = (BasicCredentials) credentialsProvider.getCredentials();
        Assert.assertFalse(credentials.willSoonExpire());
        Thread.sleep(3000);
        Assert.assertTrue(credentials.willSoonExpire());
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : CredentialsProvider(com.aliyun.oss.common.auth.CredentialsProvider) BasicCredentials(com.aliyun.oss.common.auth.BasicCredentials) Test(org.junit.Test)

Example 3 with BasicCredentials

use of com.aliyun.oss.common.auth.BasicCredentials in project aliyun-oss-java-sdk by aliyun.

the class BasicCredentialsTest method testExpiredFactor.

@Test
public void testExpiredFactor() {
    try {
        BasicCredentials credentials = new BasicCredentials(ACCESS_KEY_ID, ACCESS_KEY_SECRET, SECURITY_TOKEN).withExpiredFactor(1.0).withExpiredDuration(3);
        Thread.sleep(2000);
        Assert.assertFalse(credentials.willSoonExpire());
        credentials = new BasicCredentials(ACCESS_KEY_ID, ACCESS_KEY_SECRET, SECURITY_TOKEN).withExpiredFactor(0.1).withExpiredDuration(3);
        Thread.sleep(1000);
        Assert.assertTrue(credentials.willSoonExpire());
        credentials = new BasicCredentials(ACCESS_KEY_ID, ACCESS_KEY_SECRET, SECURITY_TOKEN).withExpiredFactor(1.0).withExpiredDuration(1);
        Thread.sleep(1500);
        Assert.assertTrue(credentials.willSoonExpire());
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : BasicCredentials(com.aliyun.oss.common.auth.BasicCredentials) Test(org.junit.Test)

Example 4 with BasicCredentials

use of com.aliyun.oss.common.auth.BasicCredentials in project aliyun-oss-java-sdk by aliyun.

the class CustomSessionCredentialsProviderTest method testGetExpiredCredentials.

@Test
public void testGetExpiredCredentials() {
    try {
        CustomSessionCredentialsFetcher credentialsFetcher = new CustomSessionCredentialsFetcherMock(TestConfig.OSS_AUTH_SERVER_HOST).withResponseCategory(ResponseCategory.Expired);
        CustomSessionCredentialsProvider credentialsProvider = new CustomSessionCredentialsProvider(TestConfig.OSS_AUTH_SERVER_HOST).withCredentialsFetcher(credentialsFetcher);
        BasicCredentials credentials = (BasicCredentials) credentialsProvider.getCredentials();
        Assert.assertEquals(credentials.getAccessKeyId().length(), 29);
        Assert.assertEquals(credentials.getSecretAccessKey().length(), 44);
        Assert.assertEquals(credentials.getSecurityToken().length(), 536);
        Assert.assertTrue(credentials.useSecurityToken());
        Assert.assertTrue(credentials.willSoonExpire());
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : CustomSessionCredentialsFetcherMock(com.aliyun.oss.common.provider.mock.CustomSessionCredentialsFetcherMock) CustomSessionCredentialsProvider(com.aliyun.oss.common.auth.CustomSessionCredentialsProvider) CustomSessionCredentialsFetcher(com.aliyun.oss.common.auth.CustomSessionCredentialsFetcher) BasicCredentials(com.aliyun.oss.common.auth.BasicCredentials) Test(org.junit.Test)

Example 5 with BasicCredentials

use of com.aliyun.oss.common.auth.BasicCredentials in project aliyun-oss-java-sdk by aliyun.

the class CustomSessionCredentialsProviderTest method testGetNormalWithoutExpirationCredentials.

@Test
public void testGetNormalWithoutExpirationCredentials() {
    try {
        CustomSessionCredentialsFetcher credentialsFetcher = new CustomSessionCredentialsFetcherMock(TestConfig.OSS_AUTH_SERVER_HOST).withResponseCategory(ResponseCategory.NormalWithoutExpiration);
        CustomSessionCredentialsProvider credentialsProvider = new CustomSessionCredentialsProvider(TestConfig.OSS_AUTH_SERVER_HOST).withCredentialsFetcher(credentialsFetcher);
        BasicCredentials credentials = (BasicCredentials) credentialsProvider.getCredentials();
        Assert.assertEquals(credentials.getAccessKeyId().length(), 29);
        Assert.assertEquals(credentials.getSecretAccessKey().length(), 44);
        Assert.assertFalse(credentials.useSecurityToken());
        Assert.assertFalse(credentials.willSoonExpire());
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : CustomSessionCredentialsFetcherMock(com.aliyun.oss.common.provider.mock.CustomSessionCredentialsFetcherMock) CustomSessionCredentialsProvider(com.aliyun.oss.common.auth.CustomSessionCredentialsProvider) CustomSessionCredentialsFetcher(com.aliyun.oss.common.auth.CustomSessionCredentialsFetcher) BasicCredentials(com.aliyun.oss.common.auth.BasicCredentials) Test(org.junit.Test)

Aggregations

BasicCredentials (com.aliyun.oss.common.auth.BasicCredentials)13 Test (org.junit.Test)13 CredentialsProvider (com.aliyun.oss.common.auth.CredentialsProvider)5 CustomSessionCredentialsFetcher (com.aliyun.oss.common.auth.CustomSessionCredentialsFetcher)5 CustomSessionCredentialsProvider (com.aliyun.oss.common.auth.CustomSessionCredentialsProvider)5 CustomSessionCredentialsFetcherMock (com.aliyun.oss.common.provider.mock.CustomSessionCredentialsFetcherMock)5 PublicKey (com.aliyun.oss.common.auth.PublicKey)2 ClientException (com.aliyuncs.exceptions.ClientException)2 IOException (java.io.IOException)2