-
Notifications
You must be signed in to change notification settings - Fork 395
Add Entra authentication to the Helix API client #17366
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
Open
missymessa
wants to merge
6
commits into
main
Choose a base branch
from
users/mjanecke/12269-helix-entra-client
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8c7decc
Add Entra authentication to the Helix API client
missymessa 520cbc1
Address Helix Entra client review feedback
missymessa 4970563
Merge branch 'main' into users/mjanecke/12269-helix-entra-client
missymessa 40e1df3
Validate Helix Entra authentication inputs
missymessa 5974b93
Merge main and validate explicit Helix scopes
missymessa a6540a4
Address Helix Entra credential feedback
missymessa 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
182 changes: 182 additions & 0 deletions
182
...ft.DotNet.Helix/Sdk.Tests/Microsoft.DotNet.Helix.Sdk.Tests/HelixApiAuthenticationTests.cs
This file contains hidden or 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,182 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Azure.Core; | ||
| using Microsoft.DotNet.Helix.Client; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.DotNet.Helix.Sdk.Tests | ||
| { | ||
| public class HelixApiAuthenticationTests | ||
| { | ||
| [Fact] | ||
| public void AnonymousOptionsExposeAnonymousMode() | ||
| { | ||
| var options = new HelixApiOptions(); | ||
|
|
||
| Assert.Equal(HelixApiAuthenticationMode.Anonymous, options.AuthenticationMode); | ||
| Assert.Empty(options.TokenScopes); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void PatCredentialPreservesLegacyAuthenticationMode() | ||
| { | ||
| var options = new HelixApiOptions(new HelixApiTokenCredential("legacy-token")); | ||
|
|
||
| Assert.Equal(HelixApiAuthenticationMode.PersonalAccessToken, options.AuthenticationMode); | ||
| Assert.Empty(options.TokenScopes); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ProductionCredentialUsesProductionScope() | ||
| { | ||
| var options = new HelixApiOptions(new TestTokenCredential()); | ||
|
|
||
| Assert.Equal(HelixApiAuthenticationMode.EntraId, options.AuthenticationMode); | ||
| Assert.Equal(new[] { HelixApiOptions.ProductionScope }, options.TokenScopes); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void StagingCredentialUsesStagingScope() | ||
| { | ||
| var options = new HelixApiOptions( | ||
| new Uri("https://helix.int-dot.net/"), | ||
| new TestTokenCredential()); | ||
|
|
||
| Assert.Equal(HelixApiAuthenticationMode.EntraId, options.AuthenticationMode); | ||
| Assert.Equal(new[] { HelixApiOptions.StagingScope }, options.TokenScopes); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CustomHostRequiresExplicitScope() | ||
| { | ||
| var exception = Assert.Throws<ArgumentException>(() => | ||
| new HelixApiOptions(new Uri("https://localhost:5001/"), new TestTokenCredential())); | ||
|
|
||
| Assert.Contains("explicit scopes", exception.Message); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void EntraCredentialRequiresBaseUri() | ||
| { | ||
| Assert.Throws<ArgumentNullException>(() => | ||
| new HelixApiOptions(null, new TestTokenCredential())); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void EntraCredentialRequiresAbsoluteBaseUri() | ||
| { | ||
| Assert.Throws<ArgumentException>(() => | ||
| new HelixApiOptions(new Uri("relative", UriKind.Relative), new TestTokenCredential())); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CustomHostUsesExplicitScope() | ||
| { | ||
| const string scope = "api://custom-helix/.default"; | ||
| var options = new HelixApiOptions( | ||
| new Uri("https://localhost:5001/"), | ||
| new TestTokenCredential(), | ||
| new[] { scope }); | ||
|
|
||
| Assert.Equal(HelixApiAuthenticationMode.EntraId, options.AuthenticationMode); | ||
| Assert.Equal(new[] { scope }, options.TokenScopes); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ExplicitScopeCannotBeEmpty() | ||
| { | ||
| Assert.Throws<ArgumentException>(() => | ||
| new HelixApiOptions( | ||
| new Uri("https://localhost:5001/"), | ||
| new TestTokenCredential(), | ||
| new[] { "" })); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ExplicitScopeRequiresAbsoluteBaseUri() | ||
| { | ||
| Assert.Throws<ArgumentException>(() => | ||
| new HelixApiOptions( | ||
| new Uri("relative", UriKind.Relative), | ||
| new TestTokenCredential(), | ||
| new[] { "api://custom-helix/.default" })); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ExplicitScopeRejectsPatCredential() | ||
| { | ||
| var exception = Assert.Throws<ArgumentException>(() => | ||
| new HelixApiOptions( | ||
| new Uri("https://localhost:5001/"), | ||
| new HelixApiTokenCredential("legacy-token"), | ||
| new[] { "api://custom-helix/.default" })); | ||
|
|
||
| Assert.Contains("without explicit scopes", exception.Message); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void EntraFactoryUsesProductionScope() | ||
| { | ||
| var api = Assert.IsType<HelixApi>( | ||
| ApiFactory.GetAuthenticatedWithEntra(new TestTokenCredential())); | ||
|
|
||
| Assert.Equal(HelixApiAuthenticationMode.EntraId, api.Options.AuthenticationMode); | ||
| Assert.Equal(new[] { HelixApiOptions.ProductionScope }, api.Options.TokenScopes); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void EntraFactoryRequiresCredential() | ||
| { | ||
| Assert.Throws<ArgumentNullException>(() => | ||
| ApiFactory.GetAuthenticatedWithEntra(null)); | ||
| Assert.Throws<ArgumentNullException>(() => | ||
| ApiFactory.GetAuthenticatedWithEntra("https://helix.dot.net/", null)); | ||
| Assert.Throws<ArgumentNullException>(() => | ||
| ApiFactory.GetAuthenticatedWithEntra( | ||
| "https://localhost:5001/", | ||
| null, | ||
| "api://custom-helix/.default")); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void EntraFactoryRejectsPatCredential() | ||
| { | ||
| var credential = new HelixApiTokenCredential("legacy-token"); | ||
|
|
||
| var productionException = Assert.Throws<ArgumentException>(() => | ||
| ApiFactory.GetAuthenticatedWithEntra(credential)); | ||
| var hostException = Assert.Throws<ArgumentException>(() => | ||
| ApiFactory.GetAuthenticatedWithEntra("https://helix.dot.net/", credential)); | ||
| var explicitScopeException = Assert.Throws<ArgumentException>(() => | ||
| ApiFactory.GetAuthenticatedWithEntra( | ||
| "https://localhost:5001/", | ||
| credential, | ||
| "api://custom-helix/.default")); | ||
|
|
||
| Assert.Contains("GetAuthenticated", productionException.Message); | ||
| Assert.Contains("GetAuthenticated", hostException.Message); | ||
| Assert.Contains("GetAuthenticated", explicitScopeException.Message); | ||
| } | ||
|
|
||
| private sealed class TestTokenCredential : TokenCredential | ||
| { | ||
| public override AccessToken GetToken( | ||
| TokenRequestContext requestContext, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| return new AccessToken("test-token", DateTimeOffset.UtcNow.AddMinutes(30)); | ||
| } | ||
|
|
||
| public override ValueTask<AccessToken> GetTokenAsync( | ||
| TokenRequestContext requestContext, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| return new ValueTask<AccessToken>(GetToken(requestContext, cancellationToken)); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.