Search in sources :

Example 1 with TokenRequestContext

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);
}
Also used : Expectations(mockit.Expectations) TokenRequestContext(com.azure.core.credential.TokenRequestContext) AccessToken(com.azure.core.credential.AccessToken) TokenCredentialCache(com.microsoft.azure.sdk.iot.service.auth.TokenCredentialCache) Test(org.junit.Test)

Example 2 with TokenRequestContext

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);
}
Also used : Expectations(mockit.Expectations) TokenRequestContext(com.azure.core.credential.TokenRequestContext) AccessToken(com.azure.core.credential.AccessToken) TokenCredentialCache(com.microsoft.azure.sdk.iot.service.auth.TokenCredentialCache) Test(org.junit.Test)

Example 3 with TokenRequestContext

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);
}
Also used : Expectations(mockit.Expectations) TokenRequestContext(com.azure.core.credential.TokenRequestContext) AccessToken(com.azure.core.credential.AccessToken) TokenCredentialCache(com.microsoft.azure.sdk.iot.service.auth.TokenCredentialCache) Test(org.junit.Test)

Example 4 with TokenRequestContext

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);
}
Also used : Expectations(mockit.Expectations) TokenRequestContext(com.azure.core.credential.TokenRequestContext) AccessToken(com.azure.core.credential.AccessToken) TokenCredentialCache(com.microsoft.azure.sdk.iot.service.auth.TokenCredentialCache) Test(org.junit.Test)

Example 5 with TokenRequestContext

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);
}
Also used : TokenRequestContext(com.azure.core.credential.TokenRequestContext) HashMap(java.util.HashMap) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) Properties(org.apache.qpid.proton.amqp.messaging.Properties) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) MessageImpl(org.apache.qpid.proton.message.impl.MessageImpl) Section(org.apache.qpid.proton.amqp.messaging.Section) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue)

Aggregations

TokenRequestContext (com.azure.core.credential.TokenRequestContext)5 AccessToken (com.azure.core.credential.AccessToken)4 TokenCredentialCache (com.microsoft.azure.sdk.iot.service.auth.TokenCredentialCache)4 Expectations (mockit.Expectations)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)1 AmqpValue (org.apache.qpid.proton.amqp.messaging.AmqpValue)1 ApplicationProperties (org.apache.qpid.proton.amqp.messaging.ApplicationProperties)1 Properties (org.apache.qpid.proton.amqp.messaging.Properties)1 Section (org.apache.qpid.proton.amqp.messaging.Section)1 MessageImpl (org.apache.qpid.proton.message.impl.MessageImpl)1