Search in sources :

Example 6 with BasicCredentials

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

the class CustomSessionCredentialsProviderTest method testGetNormalWithoutTokenCredentials.

@Test
public void testGetNormalWithoutTokenCredentials() {
    try {
        CustomSessionCredentialsFetcher credentialsFetcher = new CustomSessionCredentialsFetcherMock(TestConfig.OSS_AUTH_SERVER_HOST).withResponseCategory(ResponseCategory.NormalWithoutToken);
        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)

Example 7 with BasicCredentials

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

the class BasicCredentialsTest method testBasicTest.

@Test
public void testBasicTest() {
    try {
        BasicCredentials credentials = new BasicCredentials(ACCESS_KEY_ID, ACCESS_KEY_SECRET, SECURITY_TOKEN).withExpiredFactor(0.8).withExpiredDuration(900);
        Assert.assertEquals(credentials.getAccessKeyId(), ACCESS_KEY_ID);
        Assert.assertEquals(credentials.getSecretAccessKey(), ACCESS_KEY_SECRET);
        Assert.assertEquals(credentials.getSecurityToken(), SECURITY_TOKEN);
        Assert.assertTrue(credentials.useSecurityToken());
        credentials = new BasicCredentials(ACCESS_KEY_ID, ACCESS_KEY_SECRET, null).withExpiredFactor(0.8).withExpiredDuration(900);
        Assert.assertEquals(credentials.getAccessKeyId(), ACCESS_KEY_ID);
        Assert.assertEquals(credentials.getSecretAccessKey(), ACCESS_KEY_SECRET);
        Assert.assertNull(credentials.getSecurityToken());
        Assert.assertFalse(credentials.useSecurityToken());
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : BasicCredentials(com.aliyun.oss.common.auth.BasicCredentials) Test(org.junit.Test)

Example 8 with BasicCredentials

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

the class BasicCredentialsTest method testWillSoonExpire.

@Test
public void testWillSoonExpire() {
    try {
        BasicCredentials credentials = new BasicCredentials(ACCESS_KEY_ID, ACCESS_KEY_SECRET, SECURITY_TOKEN).withExpiredFactor(1.0).withExpiredDuration(1);
        Thread.sleep(2000);
        Assert.assertTrue(credentials.willSoonExpire());
        credentials = new BasicCredentials(ACCESS_KEY_ID, ACCESS_KEY_SECRET, SECURITY_TOKEN).withExpiredFactor(1.0).withExpiredDuration(100);
        Thread.sleep(2000);
        Assert.assertFalse(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 9 with BasicCredentials

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

the class CustomSessionCredentialsProviderTest method testGetNormalCredentials.

@Test
public void testGetNormalCredentials() {
    try {
        CustomSessionCredentialsFetcher credentialsFetcher = new CustomSessionCredentialsFetcherMock(TestConfig.OSS_AUTH_SERVER_HOST).withResponseCategory(ResponseCategory.Normal);
        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.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)

Example 10 with BasicCredentials

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

the class CustomSessionCredentialsProviderTest method testRefreshCredentials.

@Test
public void testRefreshCredentials() {
    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.assertTrue(credentials.willSoonExpire());
        credentialsFetcher = new CustomSessionCredentialsFetcherMock(TestConfig.OSS_AUTH_SERVER_HOST).withResponseCategory(ResponseCategory.Normal);
        credentialsProvider = new CustomSessionCredentialsProvider(TestConfig.ECS_ROLE_NAME).withCredentialsFetcher(credentialsFetcher);
        credentials = (BasicCredentials) credentialsProvider.getCredentials();
        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