Search in sources :

Example 1 with Credentials

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

the class EnvironmentVariableCredentialsProviderTest method testGetEnvironmentVariableCredentials.

@Test
public void testGetEnvironmentVariableCredentials() {
    try {
        // unset evn
        List<String> envSet = new ArrayList<String>();
        envSet.add(AuthUtils.ACCESS_KEY_ENV_VAR);
        envSet.add(AuthUtils.SECRET_KEY_ENV_VAR);
        envSet.add(AuthUtils.SESSION_TOKEN_ENV_VAR);
        unsetEnv(envSet);
        // set env
        Map<String, String> envMap = new HashMap<String, String>(System.getenv());
        envMap.put(AuthUtils.ACCESS_KEY_ENV_VAR, TestConfig.ROOT_ACCESS_KEY_ID);
        envMap.put(AuthUtils.SECRET_KEY_ENV_VAR, TestConfig.ROOT_ACCESS_KEY_SECRET);
        setEnv(envMap);
        // env provider
        EnvironmentVariableCredentialsProvider credentialsProvider = new EnvironmentVariableCredentialsProvider();
        Credentials credentials = credentialsProvider.getCredentials();
        Assert.assertEquals(credentials.getAccessKeyId(), TestConfig.ROOT_ACCESS_KEY_ID);
        Assert.assertEquals(credentials.getSecretAccessKey(), TestConfig.ROOT_ACCESS_KEY_SECRET);
        Assert.assertFalse(credentials.useSecurityToken());
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : EnvironmentVariableCredentialsProvider(com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Credentials(com.aliyun.oss.common.auth.Credentials) Test(org.junit.Test)

Example 2 with Credentials

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

the class SystemPropertiesCredentialsProviderTest method testSystemPropertiesCredentialsProvider.

@Test
public void testSystemPropertiesCredentialsProvider() {
    try {
        System.setProperty(AuthUtils.ACCESS_KEY_SYSTEM_PROPERTY, TestConfig.ROOT_ACCESS_KEY_ID);
        System.setProperty(AuthUtils.SECRET_KEY_SYSTEM_PROPERTY, TestConfig.ROOT_ACCESS_KEY_SECRET);
        SystemPropertiesCredentialsProvider credentialsProvider = new SystemPropertiesCredentialsProvider();
        Credentials credentials = credentialsProvider.getCredentials();
        Assert.assertEquals(credentials.getAccessKeyId(), TestConfig.ROOT_ACCESS_KEY_ID);
        Assert.assertEquals(credentials.getSecretAccessKey(), TestConfig.ROOT_ACCESS_KEY_SECRET);
        Assert.assertFalse(credentials.useSecurityToken());
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : SystemPropertiesCredentialsProvider(com.aliyun.oss.common.auth.SystemPropertiesCredentialsProvider) Credentials(com.aliyun.oss.common.auth.Credentials) Test(org.junit.Test)

Example 3 with Credentials

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

the class CustomSessionCredentialsProviderTest method testGetCredentialsNegative.

@Test
public void testGetCredentialsNegative() {
    try {
        CustomSessionCredentialsProvider credentialsProvider = new CustomSessionCredentialsProvider(TestConfig.OSS_AUTH_SERVER_HOST + "/noteixst");
        Credentials credentials = credentialsProvider.getCredentials();
        Assert.assertNull(credentials);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : CustomSessionCredentialsProvider(com.aliyun.oss.common.auth.CustomSessionCredentialsProvider) BasicCredentials(com.aliyun.oss.common.auth.BasicCredentials) Credentials(com.aliyun.oss.common.auth.Credentials) Test(org.junit.Test)

Example 4 with Credentials

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

the class CustomSessionCredentialsProviderTest method testGetCredentialsServerHalt.

@Test
public void testGetCredentialsServerHalt() {
    try {
        CustomSessionCredentialsFetcher credentialsFetcher = new CustomSessionCredentialsFetcherMock(TestConfig.OSS_AUTH_SERVER_HOST).withResponseCategory(ResponseCategory.ServerHalt);
        CustomSessionCredentialsProvider credentialsProvider = new CustomSessionCredentialsProvider(TestConfig.OSS_AUTH_SERVER_HOST).withCredentialsFetcher(credentialsFetcher);
        Credentials credentials = credentialsProvider.getCredentials();
        Assert.assertNull(credentials);
    } 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) Credentials(com.aliyun.oss.common.auth.Credentials) Test(org.junit.Test)

Example 5 with Credentials

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

the class LiveChannelOperation method generateRtmpUri.

public String generateRtmpUri(GenerateRtmpUriRequest request) throws OSSException, ClientException {
    assertParameterNotNull(request, "request");
    String bucketName = request.getBucketName();
    String liveChannelName = request.getLiveChannelName();
    String playlistName = request.getPlaylistName();
    Long expires = request.getExpires();
    assertParameterNotNull(bucketName, "bucketName");
    ensureBucketNameValid(bucketName);
    assertParameterNotNull(liveChannelName, "liveChannelName");
    ensureLiveChannelNameValid(liveChannelName);
    assertParameterNotNull(playlistName, "playlistName");
    assertParameterNotNull(expires, "expires");
    Credentials currentCreds = this.credsProvider.getCredentials();
    String accessId = currentCreds.getAccessKeyId();
    String accessKey = currentCreds.getSecretAccessKey();
    boolean useSecurityToken = currentCreds.useSecurityToken();
    // Endpoint
    RequestMessage requestMessage = new RequestMessage(bucketName, liveChannelName);
    ClientConfiguration config = this.client.getClientConfiguration();
    requestMessage.setEndpoint(OSSUtils.determineFinalEndpoint(this.endpoint, bucketName, config));
    // Headers
    requestMessage.addHeader(HttpHeaders.DATE, expires.toString());
    // Parameters
    requestMessage.addParameter(RequestParameters.PLAYLIST_NAME, playlistName);
    if (useSecurityToken) {
        requestMessage.addParameter(SECURITY_TOKEN, currentCreds.getSecurityToken());
    }
    // Signature
    String canonicalResource = "/" + bucketName + "/" + liveChannelName;
    String canonicalString = SignUtils.buildRtmpCanonicalString(canonicalResource, requestMessage, expires.toString());
    String signature = ServiceSignature.create().computeSignature(accessKey, canonicalString);
    // Build query string
    Map<String, String> params = new LinkedHashMap<String, String>();
    params.put(HttpHeaders.EXPIRES, expires.toString());
    params.put(OSS_ACCESS_KEY_ID, accessId);
    params.put(SIGNATURE, signature);
    params.putAll(requestMessage.getParameters());
    String queryString = HttpUtil.paramToQueryString(params, DEFAULT_CHARSET_NAME);
    // Compose rtmp request uri
    String uri = requestMessage.getEndpoint().toString();
    String livChan = RequestParameters.SUBRESOURCE_LIVE + "/" + liveChannelName;
    if (uri.startsWith(OSSConstants.PROTOCOL_HTTP)) {
        uri = uri.replaceFirst(OSSConstants.PROTOCOL_HTTP, OSSConstants.PROTOCOL_RTMP);
    } else if (uri.startsWith(OSSConstants.PROTOCOL_HTTPS)) {
        uri = uri.replaceFirst(OSSConstants.PROTOCOL_HTTPS, OSSConstants.PROTOCOL_RTMP);
    } else if (!uri.startsWith(OSSConstants.PROTOCOL_RTMP)) {
        uri = OSSConstants.PROTOCOL_RTMP + uri;
    }
    if (!uri.endsWith("/")) {
        uri += "/";
    }
    uri += livChan + "?" + queryString;
    return uri;
}
Also used : RequestMessage(com.aliyun.oss.common.comm.RequestMessage) Credentials(com.aliyun.oss.common.auth.Credentials) ClientConfiguration(com.aliyun.oss.ClientConfiguration) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

Credentials (com.aliyun.oss.common.auth.Credentials)25 Test (org.junit.Test)18 CredentialsProvider (com.aliyun.oss.common.auth.CredentialsProvider)8 DefaultCredentials (com.aliyun.oss.common.auth.DefaultCredentials)7 HashMap (java.util.HashMap)7 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)5 OSS (com.aliyun.oss.OSS)4 OSSException (com.aliyun.oss.OSSException)4 BasicCredentials (com.aliyun.oss.common.auth.BasicCredentials)4 CustomSessionCredentialsProvider (com.aliyun.oss.common.auth.CustomSessionCredentialsProvider)4 InvalidCredentialsException (com.aliyun.oss.common.auth.InvalidCredentialsException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 OSSClient (com.aliyun.oss.OSSClient)3 DefaultCredentialProvider (com.aliyun.oss.common.auth.DefaultCredentialProvider)3 EnvironmentVariableCredentialsProvider (com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider)3 ProfileConfigFile (com.aliyun.oss.common.auth.ProfileConfigFile)3 SystemPropertiesCredentialsProvider (com.aliyun.oss.common.auth.SystemPropertiesCredentialsProvider)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3