-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
StaticTokenCredential #22955
Merged
christothes
merged 9 commits into
Azure:main
from
christothes:chriss/StaticTokenCredential
Sep 3, 2021
Merged
StaticTokenCredential #22955
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bf09002
StaticTokenCredential
christothes 4e693a2
export and tweak sample
christothes c7008c4
fix test
christothes 94ee44d
Refactor to static factory method on TokenCredential
christothes 7fb0589
export
christothes 34894e2
use GetAwaiter.GetResult
christothes 2ec5dbc
snippet updates
christothes 0f0d413
merge
christothes 534f3b9
Update DefiningCustomCredentialTypes.md
christothes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.Core.Tests | ||
{ | ||
public class StaticTokenCredentialTests | ||
{ | ||
private static string[] scopes = { "https://default.mock.auth.scope/.default" }; | ||
private static CancellationToken ctx = new CancellationTokenSource(TimeSpan.FromMinutes(5)).Token; | ||
private static string expectedToken = "token"; | ||
private static DateTimeOffset expires = DateTimeOffset.UtcNow; | ||
private static AccessToken staticToken; | ||
private static Func<TokenRequestContext, CancellationToken, AccessToken> getToken; | ||
private static Func<TokenRequestContext, CancellationToken, ValueTask<AccessToken>> getTokenAsync; | ||
|
||
private static IEnumerable<object[]> Credentials() | ||
{ | ||
staticToken = new AccessToken(expectedToken, expires); | ||
getToken = (context, token) => | ||
{ | ||
Assert.AreEqual(scopes, context.Scopes); | ||
Assert.AreEqual(ctx, token); | ||
return staticToken; | ||
}; | ||
getTokenAsync = async (context, token) => | ||
{ | ||
Assert.AreEqual(scopes, context.Scopes); | ||
Assert.AreEqual(ctx, token); | ||
await Task.Yield(); | ||
return staticToken; | ||
}; | ||
yield return new object[] { TokenCredential.Create(getTokenAsync) }; | ||
yield return new object[] { TokenCredential.Create(getToken) }; | ||
yield return new object[] { TokenCredential.Create(getToken, getTokenAsync) }; | ||
} | ||
|
||
[TestCaseSource(nameof(Credentials))] | ||
public async Task CreateGetTokenAsyncCallsDelegate(TokenCredential credential) | ||
{ | ||
AccessToken actualToken = await credential.GetTokenAsync(new TokenRequestContext(scopes), ctx); | ||
|
||
Assert.AreEqual(expectedToken, actualToken.Token); | ||
Assert.AreEqual(expires, actualToken.ExpiresOn); | ||
} | ||
|
||
[TestCaseSource(nameof(Credentials))] | ||
public void CreateGetTokenCallsDelegate(TokenCredential credential) | ||
{ | ||
AccessToken actualToken = credential.GetToken(new TokenRequestContext(scopes), ctx); | ||
|
||
Assert.AreEqual(expectedToken, actualToken.Token); | ||
Assert.AreEqual(expires, actualToken.ExpiresOn); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the func need to return
ValueTask
? If not I wonder ifTask<AccessToken>
would be easier for users.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to keep it 1:1 with GetToken's signature, unless you feel strongly otherwise
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't feel strongly I just wanted to pose the question to others.