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);
}
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;
}
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);
}
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);
}
Aggregations