use of com.azure.core.credential.TokenRequestContext in project azure-iot-sdk-java by Azure.
the class TokenCredentialCacheTest method tokenCredentialRenewsExpiredToken.
@Test
public void tokenCredentialRenewsExpiredToken() {
TokenCredentialCache cache = new TokenCredentialCache(mockTokenCredential);
new Expectations() {
{
mockTokenCredential.getToken((TokenRequestContext) any).block();
result = mockAccessToken;
mockAccessToken.getExpiresAt();
result = OffsetDateTime.MAX;
}
};
AccessToken accessToken = cache.getAccessToken();
// Token expired one minute ago
final long milliseconds = System.currentTimeMillis() - (60 * 1000);
new Expectations() {
{
mockAccessToken.getExpiresAt();
result = Instant.ofEpochMilli(milliseconds).atOffset(ZoneOffset.UTC);
mockTokenCredential.getToken((TokenRequestContext) any).block();
result = mockAccessToken2;
}
};
// act
AccessToken accessToken2 = cache.getAccessToken();
// assert
assertEquals(mockAccessToken, accessToken);
assertEquals(mockAccessToken2, accessToken2);
}
use of com.azure.core.credential.TokenRequestContext in project azure-iot-sdk-java by Azure.
the class TokenCredentialCacheTest method tokenCredentialDoesNotRenewTooProactively.
@Test
public void tokenCredentialDoesNotRenewTooProactively() {
TokenCredentialCache cache = new TokenCredentialCache(mockTokenCredential);
new Expectations() {
{
mockTokenCredential.getToken((TokenRequestContext) any).block();
result = mockAccessToken;
mockAccessToken.getExpiresAt();
result = OffsetDateTime.MAX;
}
};
AccessToken accessToken = cache.getAccessToken();
// 12 minutes from the current time, should not fit within the proactive renewal range, so the cached token shouldn't be renewed
final long milliseconds = System.currentTimeMillis() + (12 * 60 * 1000);
new Expectations() {
{
mockAccessToken.getExpiresAt();
result = Instant.ofEpochMilli(milliseconds).atOffset(ZoneOffset.UTC);
}
};
// act
AccessToken accessToken2 = cache.getAccessToken();
// assert
assertEquals(mockAccessToken, accessToken);
assertEquals(mockAccessToken, accessToken2);
}
use of com.azure.core.credential.TokenRequestContext in project azure-iot-sdk-java by Azure.
the class TokenCredentialCacheTest method tokenCredentialProactivelyRenewsToken.
@Test
public void tokenCredentialProactivelyRenewsToken() {
TokenCredentialCache cache = new TokenCredentialCache(mockTokenCredential);
new Expectations() {
{
mockTokenCredential.getToken((TokenRequestContext) any).block();
result = mockAccessToken;
mockAccessToken.getExpiresAt();
result = OffsetDateTime.MAX;
}
};
AccessToken accessToken = cache.getAccessToken();
// 8 minutes from the current time, should fit within the proactive renewal range
final long milliseconds = System.currentTimeMillis() + (8 * 60 * 1000);
new Expectations() {
{
mockAccessToken.getExpiresAt();
result = Instant.ofEpochMilli(milliseconds).atOffset(ZoneOffset.UTC);
mockTokenCredential.getToken((TokenRequestContext) any).block();
result = mockAccessToken2;
}
};
// act
AccessToken accessToken2 = cache.getAccessToken();
// assert
assertEquals(mockAccessToken, accessToken);
assertEquals(mockAccessToken2, accessToken2);
}
use of com.azure.core.credential.TokenRequestContext in project azure-iot-sdk-java by Azure.
the class TokenCredentialCacheTest method tokenCredentialCachesToken.
@Test
public void tokenCredentialCachesToken() {
TokenCredentialCache cache = new TokenCredentialCache(mockTokenCredential);
new Expectations() {
{
mockTokenCredential.getToken((TokenRequestContext) any).block();
result = mockAccessToken;
mockAccessToken.getExpiresAt();
result = OffsetDateTime.MAX;
}
};
AccessToken accessToken = cache.getAccessToken();
AccessToken accessToken2 = cache.getAccessToken();
assertEquals(mockAccessToken, accessToken);
assertEquals(mockAccessToken, accessToken2);
}
use of com.azure.core.credential.TokenRequestContext in project azure-iot-sdk-java by Azure.
the class CbsSenderLinkHandler method sendAuthenticationMessage.
public int sendAuthenticationMessage(UUID authenticationMessageCorrelationId) {
MessageImpl outgoingMessage = (MessageImpl) Proton.message();
Properties properties = new Properties();
// Note that setting "messageId = correlationId" is intentional.
// IotHub only responds correctly if this correlation id is set this way
properties.setMessageId(authenticationMessageCorrelationId);
properties.setTo(CBS_TO);
properties.setReplyTo(CBS_REPLY);
outgoingMessage.setProperties(properties);
Map<String, Object> applicationProperties = new HashMap<>();
applicationProperties.put(PUT_TOKEN_OPERATION, PUT_TOKEN_OPERATION_VALUE);
if (credential != null) {
TokenRequestContext context = new TokenRequestContext().addScopes(IOTHUB_PUBLIC_SCOPE);
this.currentAccessToken = credential.getToken(context).block();
if (this.currentAccessToken == null) {
log.error("The AccessToken supplied by the TokenCredential for the CbsSenderLinkHandler was null.");
return -1;
}
applicationProperties.put(PUT_TOKEN_EXPIRY, Date.from(this.currentAccessToken.getExpiresAt().toInstant()));
applicationProperties.put(PUT_TOKEN_TYPE, BEARER);
Section section = new AmqpValue("Bearer " + this.currentAccessToken.getToken());
outgoingMessage.setBody(section);
} else if (this.sasTokenProvider != null) {
String sasToken = this.sasTokenProvider.getSignature();
this.currentAccessToken = getAccessTokenFromSasToken(sasToken);
applicationProperties.put(PUT_TOKEN_TYPE, SAS_TOKEN);
Section section = new AmqpValue(sasToken);
outgoingMessage.setBody(section);
} else {
this.currentAccessToken = getAccessTokenFromSasToken(this.sasToken);
applicationProperties.put(PUT_TOKEN_TYPE, SAS_TOKEN);
Section section = new AmqpValue(this.sasToken);
outgoingMessage.setBody(section);
}
applicationProperties.put(PUT_TOKEN_AUDIENCE, this.senderLink.getSession().getConnection().getHostname());
outgoingMessage.setApplicationProperties(new ApplicationProperties(applicationProperties));
return this.sendMessageAndGetDeliveryTag(outgoingMessage);
}
Aggregations