Search in sources :

Example 1 with KinesisVideoCredentials

use of com.amazonaws.kinesisvideo.auth.KinesisVideoCredentials in project aws-sdk-android by aws-amplify.

the class DefaultServiceCallbacksImpl method getStreamingToken.

@Override
public void getStreamingToken(@NonNull final String streamName, final long callAfter, final long timeout, @Nullable final byte[] authData, final int authType, final long streamHandle, final KinesisVideoProducerStream stream) throws ProducerException {
    Preconditions.checkState(isInitialized(), "Service callbacks object should be initialized first");
    final long delay = calculateRelativeServiceCallAfter(callAfter);
    final Runnable task = new Runnable() {

        @Override
        public void run() {
            // Currently, we have no support for getting a streaming token. We will refresh the credentials
            // and return a credential from the credentials provider we got initially.
            final KinesisVideoCredentialsProvider credentialsProvider = configuration.getCredentialsProvider();
            // Stores the serialized credentials as a streaming token
            byte[] serializedCredentials = null;
            long expiration = 0;
            final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            try {
                final KinesisVideoCredentials credentials = credentialsProvider.getUpdatedCredentials();
                // Serialize the credentials
                expiration = credentials.getExpiration().getTime() * Time.HUNDREDS_OF_NANOS_IN_A_MILLISECOND;
                // Serialize the credentials as streaming token
                final ObjectOutput outputStream = new ObjectOutputStream(byteArrayOutputStream);
                outputStream.writeObject(credentials);
                outputStream.flush();
                serializedCredentials = byteArrayOutputStream.toByteArray();
                outputStream.close();
            } catch (final IOException e) {
                log.exception(e);
            } catch (final KinesisVideoException e) {
                log.exception(e);
            } finally {
                try {
                    byteArrayOutputStream.close();
                } catch (final IOException ex) {
                // Do nothing
                }
            }
            final int statusCode = HTTP_OK;
            try {
                kinesisVideoProducer.getStreamingTokenResult(stream, streamHandle, serializedCredentials, expiration, statusCode);
            } catch (final ProducerException e) {
                throw new RuntimeException(e);
            }
        }
    };
    executor.schedule(task, delay, TimeUnit.NANOSECONDS);
}
Also used : ProducerException(com.amazonaws.kinesisvideo.producer.ProducerException) KinesisVideoCredentialsProvider(com.amazonaws.kinesisvideo.auth.KinesisVideoCredentialsProvider) KinesisVideoCredentials(com.amazonaws.kinesisvideo.auth.KinesisVideoCredentials) KinesisVideoException(com.amazonaws.kinesisvideo.common.exception.KinesisVideoException)

Example 2 with KinesisVideoCredentials

use of com.amazonaws.kinesisvideo.auth.KinesisVideoCredentials in project aws-sdk-android by aws-amplify.

the class KinesisVideoAndroidServiceClient method createAwsCredentials.

private static AWSCredentials createAwsCredentials(@NonNull final KinesisVideoCredentialsProvider credentialsProvider) throws KinesisVideoException {
    Preconditions.checkNotNull(credentialsProvider);
    final KinesisVideoCredentials kinesisVideoCredentials = credentialsProvider.getCredentials();
    final AWSCredentials credentials = new AWSSessionCredentials() {

        @Override
        public String getSessionToken() {
            return kinesisVideoCredentials.getSessionToken();
        }

        @Override
        public String getAWSAccessKeyId() {
            return kinesisVideoCredentials.getAccessKey();
        }

        @Override
        public String getAWSSecretKey() {
            return kinesisVideoCredentials.getSecretKey();
        }
    };
    return credentials;
}
Also used : KinesisVideoCredentials(com.amazonaws.kinesisvideo.auth.KinesisVideoCredentials) AWSSessionCredentials(com.amazonaws.auth.AWSSessionCredentials) AWSCredentials(com.amazonaws.auth.AWSCredentials)

Example 3 with KinesisVideoCredentials

use of com.amazonaws.kinesisvideo.auth.KinesisVideoCredentials in project aws-sdk-android by aws-amplify.

the class KinesisVideoCredentialsProviderImpl method updateCredentials.

@Override
protected KinesisVideoCredentials updateCredentials() throws KinesisVideoException {
    // Refresh the token first
    log.debug("Refreshing credentials");
    credentialsProvider.refresh();
    // Get the AWS credentials and create Kinesis Video Credentials
    final AWSCredentials awsCredentials = credentialsProvider.getCredentials();
    String sessionToken = null;
    if (awsCredentials instanceof AWSSessionCredentials) {
        final AWSSessionCredentials sessionCredentials = (AWSSessionCredentials) awsCredentials;
        sessionToken = sessionCredentials.getSessionToken();
    }
    Date expiration = KinesisVideoCredentials.CREDENTIALS_NEVER_EXPIRE;
    if (credentialsProvider instanceof CognitoCredentialsProvider) {
        final CognitoCredentialsProvider cognitoCredentialsProvider = (CognitoCredentialsProvider) credentialsProvider;
        expiration = cognitoCredentialsProvider.getSessionCredentialsExpiration();
        log.debug("Refreshed token expiration is %s", expiration);
    } else if (credentialsProvider instanceof AWSMobileClient) {
        AWSMobileClient awsMobileClient = (AWSMobileClient) credentialsProvider;
        try {
            expiration = awsMobileClient.getTokens().getAccessToken().getExpiration();
            log.debug("Refreshed token expiration is %s", expiration);
        } catch (Exception e) {
            throw new KinesisVideoException("Failed to refresh! " + e.getMessage());
        }
    }
    log.debug("Returning %scredentials with expiration %s", sessionToken == null ? "" : "session ", expiration);
    return new KinesisVideoCredentials(awsCredentials.getAWSAccessKeyId(), awsCredentials.getAWSSecretKey(), sessionToken, expiration);
}
Also used : CognitoCredentialsProvider(com.amazonaws.auth.CognitoCredentialsProvider) KinesisVideoCredentials(com.amazonaws.kinesisvideo.auth.KinesisVideoCredentials) AWSSessionCredentials(com.amazonaws.auth.AWSSessionCredentials) AWSMobileClient(com.amazonaws.mobile.client.AWSMobileClient) AWSCredentials(com.amazonaws.auth.AWSCredentials) KinesisVideoException(com.amazonaws.kinesisvideo.common.exception.KinesisVideoException) Date(java.util.Date) KinesisVideoException(com.amazonaws.kinesisvideo.common.exception.KinesisVideoException)

Example 4 with KinesisVideoCredentials

use of com.amazonaws.kinesisvideo.auth.KinesisVideoCredentials in project aws-sdk-android by aws-amplify.

the class DefaultServiceCallbacksImpl method getCredentialsProvider.

@Nullable
protected static KinesisVideoCredentialsProvider getCredentialsProvider(@Nullable final byte[] authData, @NonNull final Log log) {
    if (null == authData) {
        log.warn("NULL credentials have been returned by the credentials provider.");
        return null;
    }
    // De-serialize the bytes into AWSCredentials object
    final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(authData);
    KinesisVideoCredentials credentials = null;
    try {
        final ObjectInput objectInput = new ObjectInputStream(byteArrayInputStream);
        credentials = (KinesisVideoCredentials) objectInput.readObject();
        objectInput.close();
    } catch (final IOException e) {
        log.exception(e);
        return null;
    } catch (final ClassNotFoundException e) {
        log.exception(e);
        return null;
    } finally {
        try {
            byteArrayInputStream.close();
        } catch (final IOException e) {
            log.exception(e);
        }
    }
    // Create a static credentials provider
    return new StaticCredentialsProvider(credentials);
}
Also used : StaticCredentialsProvider(com.amazonaws.kinesisvideo.auth.StaticCredentialsProvider) KinesisVideoCredentials(com.amazonaws.kinesisvideo.auth.KinesisVideoCredentials) Nullable(androidx.annotation.Nullable)

Aggregations

KinesisVideoCredentials (com.amazonaws.kinesisvideo.auth.KinesisVideoCredentials)4 AWSCredentials (com.amazonaws.auth.AWSCredentials)2 AWSSessionCredentials (com.amazonaws.auth.AWSSessionCredentials)2 KinesisVideoException (com.amazonaws.kinesisvideo.common.exception.KinesisVideoException)2 Nullable (androidx.annotation.Nullable)1 CognitoCredentialsProvider (com.amazonaws.auth.CognitoCredentialsProvider)1 KinesisVideoCredentialsProvider (com.amazonaws.kinesisvideo.auth.KinesisVideoCredentialsProvider)1 StaticCredentialsProvider (com.amazonaws.kinesisvideo.auth.StaticCredentialsProvider)1 ProducerException (com.amazonaws.kinesisvideo.producer.ProducerException)1 AWSMobileClient (com.amazonaws.mobile.client.AWSMobileClient)1 Date (java.util.Date)1