Пример #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);
            }
        }
Пример #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);
        }
Пример #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);
        }
Пример #4
0
        public void CommunicationTokenCredential_StaticTokenReturnsExpiredToken()
        {
            var userCredential = new CommunicationUserCredential(ExpiredToken);
            var communicationTokenCredential = new CommunicationTokenCredential(userCredential);

            var accessToken = communicationTokenCredential.GetToken(MockTokenRequestContext(), CancellationToken.None);

            Assert.AreEqual(ExpiredToken, accessToken.Token);
        }
Пример #5
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);
        }
Пример #6
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());
        }
Пример #7
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);
            }
        }
 public CommunicationBearerTokenCredential(CommunicationTokenCredential credential) => _credential = credential;