|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 |
|
4 | 4 | using Azure.Core; |
| 5 | +using Azure.Core.Pipeline; |
5 | 6 | using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Linq; |
6 | 9 |
|
7 | 10 | namespace Microsoft.DotNet.Helix.Client |
8 | 11 | { |
| 12 | + public enum HelixApiAuthenticationMode |
| 13 | + { |
| 14 | + Anonymous, |
| 15 | + PersonalAccessToken, |
| 16 | + EntraId, |
| 17 | + } |
| 18 | + |
9 | 19 | partial class HelixApiOptions |
10 | 20 | { |
| 21 | + public const string ProductionScope = "api://eb70c40b-c265-44f7-842e-1a568f035f33/.default"; |
| 22 | + public const string StagingScope = "api://f45b17a4-149b-4f89-91bc-e6331af8d0e8/.default"; |
| 23 | + |
11 | 24 | // See https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/core/Azure.Core/src/RetryOptions.cs for values this overrides |
12 | 25 | public const int DefaultRetryDelaySeconds = 10; |
13 | 26 | public const int DefaultMaxRetryCount = 5; |
14 | 27 |
|
| 28 | + public HelixApiOptions(Uri baseUri, TokenCredential credentials, IEnumerable<string> scopes) |
| 29 | + { |
| 30 | + BaseUri = ValidateBaseUri(baseUri); |
| 31 | + Credentials = credentials ?? throw new ArgumentNullException(nameof(credentials)); |
| 32 | + if (credentials is HelixApiTokenCredential) |
| 33 | + { |
| 34 | + throw new ArgumentException( |
| 35 | + "Explicit scopes are only supported for Entra credentials. " + |
| 36 | + "For PAT authentication, pass HelixApiTokenCredential without explicit scopes.", |
| 37 | + nameof(credentials)); |
| 38 | + } |
| 39 | + |
| 40 | + string[] tokenScopes = scopes?.ToArray() ?? throw new ArgumentNullException(nameof(scopes)); |
| 41 | + if (tokenScopes.Length == 0 || tokenScopes.Any(string.IsNullOrWhiteSpace)) |
| 42 | + { |
| 43 | + throw new ArgumentException("At least one non-empty token scope is required.", nameof(scopes)); |
| 44 | + } |
| 45 | + TokenScopes = Array.AsReadOnly(tokenScopes); |
| 46 | + |
| 47 | + InitializeOptions(); |
| 48 | + } |
| 49 | + |
| 50 | + public HelixApiAuthenticationMode AuthenticationMode { get; private set; } |
| 51 | + |
| 52 | + public IReadOnlyList<string> TokenScopes { get; private set; } = Array.Empty<string>(); |
| 53 | + |
15 | 54 | partial void InitializeOptions() |
16 | 55 | { |
17 | | - if (Credentials != null) |
| 56 | + if (Credentials == null) |
18 | 57 | { |
| 58 | + AuthenticationMode = HelixApiAuthenticationMode.Anonymous; |
| 59 | + } |
| 60 | + else if (Credentials is HelixApiTokenCredential) |
| 61 | + { |
| 62 | + AuthenticationMode = HelixApiAuthenticationMode.PersonalAccessToken; |
| 63 | + TokenScopes = Array.Empty<string>(); |
19 | 64 | AddPolicy(new HelixApiTokenAuthenticationPolicy(Credentials), HttpPipelinePosition.PerCall); |
20 | 65 | } |
| 66 | + else |
| 67 | + { |
| 68 | + AuthenticationMode = HelixApiAuthenticationMode.EntraId; |
| 69 | + if (TokenScopes.Count == 0) |
| 70 | + { |
| 71 | + TokenScopes = Array.AsReadOnly(new[] { GetDefaultScope(BaseUri) }); |
| 72 | + } |
| 73 | + |
| 74 | + AddPolicy( |
| 75 | + new BearerTokenAuthenticationPolicy(Credentials, TokenScopes.ToArray()), |
| 76 | + HttpPipelinePosition.PerRetry); |
| 77 | + } |
21 | 78 |
|
22 | 79 | // Users should not generally need to modify these but can do so after creating a HelixApi object if needed |
23 | 80 | Retry.Delay = TimeSpan.FromSeconds(DefaultRetryDelaySeconds); |
24 | 81 | Retry.MaxRetries = DefaultMaxRetryCount; |
25 | 82 | } |
| 83 | + |
| 84 | + private static string GetDefaultScope(Uri baseUri) |
| 85 | + { |
| 86 | + baseUri = ValidateBaseUri(baseUri); |
| 87 | + |
| 88 | + if (baseUri.Host.Equals("helix.dot.net", StringComparison.OrdinalIgnoreCase)) |
| 89 | + { |
| 90 | + return ProductionScope; |
| 91 | + } |
| 92 | + |
| 93 | + if (baseUri.Host.Equals("helix.int-dot.net", StringComparison.OrdinalIgnoreCase)) |
| 94 | + { |
| 95 | + return StagingScope; |
| 96 | + } |
| 97 | + |
| 98 | + throw new ArgumentException( |
| 99 | + $"No default Entra scope is known for Helix API host '{baseUri.Host}'. " + |
| 100 | + "Use the HelixApiOptions constructor that accepts explicit scopes.", |
| 101 | + nameof(baseUri)); |
| 102 | + } |
| 103 | + |
| 104 | + private static Uri ValidateBaseUri(Uri baseUri) |
| 105 | + { |
| 106 | + if (baseUri == null) |
| 107 | + { |
| 108 | + throw new ArgumentNullException(nameof(baseUri)); |
| 109 | + } |
| 110 | + |
| 111 | + if (!baseUri.IsAbsoluteUri) |
| 112 | + { |
| 113 | + throw new ArgumentException("The Helix API base URI must be absolute.", nameof(baseUri)); |
| 114 | + } |
| 115 | + |
| 116 | + return baseUri; |
| 117 | + } |
26 | 118 | } |
27 | 119 | } |
0 commit comments