Beispiel #1
0
        public async Task CommunicationBearerTokenCredential_PassesAsyncCancelToken(bool refreshProactively)
        {
            var cancellationToken = new CancellationToken();
            CancellationToken?actualCancellationToken = null;

            var tokenCredential = new CommunicationTokenCredential(
                new CommunicationTokenRefreshOptions(
                    refreshProactively,
                    RefreshToken)
            {
                AsyncTokenRefresher = c => new ValueTask <string>(RefreshToken(c)),
                InitialToken        = ExpiredToken
            });

            var communicationBearerTokenCredential = new CommunicationBearerTokenCredential(tokenCredential);
            var accessToken = await communicationBearerTokenCredential.GetTokenAsync(MockTokenRequestContext(), cancellationToken);

            Assert.AreEqual(cancellationToken.GetHashCode(), actualCancellationToken.GetHashCode());

            string RefreshToken(CancellationToken token)
            {
                actualCancellationToken = token;
                return(SampleToken);
            }
        }
Beispiel #2
0
        public async Task CommunicationBearerTokenCredential_StaticTokenAsyncReturnsExpiredToken()
        {
            var tokenCredential = new CommunicationTokenCredential(ExpiredToken);
            var communicationBearerTokenCredential = new CommunicationBearerTokenCredential(tokenCredential);

            var accessToken = await communicationBearerTokenCredential.GetTokenAsync(MockTokenRequestContext(), CancellationToken.None);

            Assert.AreEqual(ExpiredToken, accessToken.Token);
        }
Beispiel #3
0
        public async Task CommunicationBearerTokenCredential_CreateStaticToken()
        {
            var token = ExpiredToken;

            using var tokenCredential = new CommunicationTokenCredential(token);
            var communicationBearerTokenCredential = new CommunicationBearerTokenCredential(tokenCredential);

            await communicationBearerTokenCredential.GetTokenAsync(MockTokenRequestContext(), CancellationToken.None);
        }
Beispiel #4
0
        public async Task CommunicationBearerTokenCredential_CreateRefreshableWithoutInitialToken()
        {
            var tokenCredential = new CommunicationTokenCredential(
                refreshProactively: true, // Indicates if the token should be proactively refreshed in the background or only on-demand
                tokenRefresher: cancellationToken => FetchTokenForUserFromMyServer("*****@*****.**", cancellationToken),
                asyncTokenRefresher: cancellationToken => FetchTokenForUserFromMyServerAsync("*****@*****.**", cancellationToken));
            var communicationBearerTokenCredential = new CommunicationBearerTokenCredential(tokenCredential);

            await communicationBearerTokenCredential.GetTokenAsync(MockTokenRequestContext(), CancellationToken.None);
        }
Beispiel #5
0
        public async Task CommunicationBearerTokenCredential_DecodesToken()
        {
            var initialToken    = SampleToken;
            var tokenCredential = new CommunicationTokenCredential(initialToken);
            var communicationBearerTokenCredential = new CommunicationBearerTokenCredential(tokenCredential);

            var accessToken = await communicationBearerTokenCredential.GetTokenAsync(MockTokenRequestContext(), CancellationToken.None);

            Assert.AreEqual(initialToken, accessToken.Token);
            Assert.AreEqual(SampleTokenExpiry, accessToken.ExpiresOn.ToUnixTimeSeconds());
        }
Beispiel #6
0
        public void CommunicationBearerTokenCredential_PassesCancelToken(bool refreshProactively)
        {
            var cancellationToken = new CancellationToken();
            CancellationToken?actualCancellationToken = null;

            var tokenCredential = new CommunicationTokenCredential(
                refreshProactively,
                RefreshToken,
                c => new ValueTask <string>(RefreshToken(c)),
                ExpiredToken);

            var communicationBearerTokenCredential = new CommunicationBearerTokenCredential(tokenCredential);
            var accessToken = communicationBearerTokenCredential.GetToken(MockTokenRequestContext(), cancellationToken);

            Assert.AreEqual(cancellationToken.GetHashCode(), actualCancellationToken.GetHashCode());

            string RefreshToken(CancellationToken token)
            {
                actualCancellationToken = token;
                return(SampleToken);
            }
        }