public async Task CredentialThrows()
        {
            var cred1    = new SimpleMockTokenCredential("scopeA", "tokenA");
            var cred2    = new ExceptionalMockTokenCredential();
            var cred3    = new SimpleMockTokenCredential("scopeB", "tokenB");
            var provider = new ChainedTokenCredential(cred1, cred2, cred3);

            Assert.AreEqual("tokenA", (await provider.GetTokenAsync(new TokenRequestContext(new string[] { "scopeA" }))).Token);
            Assert.CatchAsync <AuthenticationFailedException>(async() => await provider.GetTokenAsync(new TokenRequestContext(new string[] { "ScopeB" })));
            Assert.CatchAsync <AuthenticationFailedException>(async() => await provider.GetTokenAsync(new TokenRequestContext(new string[] { "ScopeC" })));
        }
        public async Task CredentialSequenceValid()
        {
            var cred1    = new SimpleMockTokenCredential("scopeA", "tokenA");
            var cred2    = new SimpleMockTokenCredential("scopeB", "tokenB");
            var cred3    = new SimpleMockTokenCredential("scopeB", "NotToBeReturned");
            var cred4    = new SimpleMockTokenCredential("scopeC", "tokenC");
            var provider = new ChainedTokenCredential(cred1, cred2, cred3, cred4);

            Assert.AreEqual("tokenA", (await provider.GetTokenAsync(new string[] { "scopeA" })).Token);
            Assert.AreEqual("tokenB", (await provider.GetTokenAsync(new string[] { "scopeB" })).Token);
            Assert.AreEqual("tokenC", (await provider.GetTokenAsync(new string[] { "scopeC" })).Token);
            Assert.IsNull((await provider.GetTokenAsync(new string[] { "scopeD" })).Token);
        }
        public async Task CredentialSequenceValid()
        {
            var cred1    = new SimpleMockTokenCredential("scopeA", "tokenA");
            var cred2    = new SimpleMockTokenCredential("scopeB", "tokenB");
            var cred3    = new SimpleMockTokenCredential("scopeB", "NotToBeReturned");
            var cred4    = new SimpleMockTokenCredential("scopeC", "tokenC");
            var provider = new ChainedTokenCredential(cred1, cred2, cred3, cred4);

            Assert.AreEqual("tokenA", (await provider.GetTokenAsync(new TokenRequestContext(new string[] { "scopeA" }))).Token);
            Assert.AreEqual("tokenB", (await provider.GetTokenAsync(new TokenRequestContext(new string[] { "scopeB" }))).Token);
            Assert.AreEqual("tokenC", (await provider.GetTokenAsync(new TokenRequestContext(new string[] { "scopeC" }))).Token);
            var ex = Assert.CatchAsync <AuthenticationFailedException>(async() => await provider.GetTokenAsync(new TokenRequestContext(new string[] { "ScopeD" })));

            Assert.IsInstanceOf(typeof(AggregateException), ex.InnerException);

            CollectionAssert.AllItemsAreInstancesOfType(((AggregateException)ex.InnerException).InnerExceptions, typeof(CredentialUnavailableException));
        }
        protected async Task <TokenCredentials> GetServiceClientCredentials()
        {
            var cts   = new CancellationTokenSource();
            var creds = new ChainedTokenCredential(new[] { new AzureServiceTokenProviderCredential(TokenCredentialProvider.MsftAdTenantId) });

            AccessToken token = await creds.GetTokenAsync(new TokenRequestContext(new[]
            {
                "https://management.azure.com/.default",
            }), cts.Token);

            return(new TokenCredentials(token.Token));
        }
Example #5
0
        public static async Task <GraphServiceClient> GetGraphApiClient()
        {
            var credential  = new ChainedTokenCredential(new ManagedIdentityCredential(), new AzureCliCredential());
            var accessToken = await credential.GetTokenAsync(new
                                                             TokenRequestContext(scopes : new [] { "https://graph.microsoft.com/.default" }, parentRequestId : null), default);

            var graphServiceClient = new GraphServiceClient(
                new DelegateAuthenticationProvider((requestMessage) =>
            {
                requestMessage
                .Headers
                .Authorization = new AuthenticationHeaderValue("bearer", accessToken.Token);

                return(Task.CompletedTask);
            }));

            return(graphServiceClient);
        }
        public async Task AllCredentialSkipped()
        {
            var cred1 = new UnavailbleCredential();
            var cred2 = new UnavailbleCredential();

            var chain = new ChainedTokenCredential(cred1, cred2);

            var ex = Assert.CatchAsync <AuthenticationFailedException>(async() => await chain.GetTokenAsync(new TokenRequestContext(MockScopes.Default)));

            Assert.IsInstanceOf(typeof(AggregateException), ex.InnerException);

            CollectionAssert.AllItemsAreInstancesOfType(((AggregateException)ex.InnerException).InnerExceptions, typeof(CredentialUnavailableException));

            await Task.CompletedTask;
        }
Example #7
0
 protected override async Task <AccessToken> GetAccessTokenFromProviderAsync(CancellationToken cancellationToken)
 {
     return(await _credential.GetTokenAsync(new TokenRequestContext(_authenticationTokenScopes), cancellationToken));
 }