| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Linq; |
| | 7 | | using System.Text; |
| | 8 | | using System.Text.RegularExpressions; |
| | 9 | | using System.Threading.Tasks; |
| | 10 | |
|
| | 11 | | namespace Azure.Identity |
| | 12 | | { |
| | 13 | | internal static class ScopeUtilities |
| | 14 | | { |
| | 15 | | private const string DefaultSuffix = "/.default"; |
| | 16 | | private const string ScopePattern = "^[0-9a-zA-Z-.:/]+$"; |
| | 17 | |
|
| | 18 | | private const string InvalidScopeMessage = "The specified scope is not in expected format. Only alphanumeric cha |
| 2 | 19 | | private static readonly Regex scopeRegex = new Regex(ScopePattern); |
| | 20 | |
|
| | 21 | | public static string ScopesToResource(string[] scopes) |
| | 22 | | { |
| 192 | 23 | | if (scopes == null) |
| | 24 | | { |
| 0 | 25 | | throw new ArgumentNullException(nameof(scopes)); |
| | 26 | | } |
| | 27 | |
|
| 192 | 28 | | if (scopes.Length != 1) |
| | 29 | | { |
| 0 | 30 | | throw new ArgumentException("To convert to a resource string the specified array must be exactly length |
| | 31 | | } |
| | 32 | |
|
| 192 | 33 | | if (!scopes[0].EndsWith(DefaultSuffix, StringComparison.Ordinal)) |
| | 34 | | { |
| 36 | 35 | | return scopes[0]; |
| | 36 | | } |
| | 37 | |
|
| 156 | 38 | | return scopes[0].Remove(scopes[0].LastIndexOf(DefaultSuffix, StringComparison.Ordinal)); |
| | 39 | | } |
| | 40 | |
|
| | 41 | | public static string[] ResourceToScopes(string resource) |
| | 42 | | { |
| 0 | 43 | | return new string[] { resource + "/.default" }; |
| | 44 | | } |
| | 45 | |
|
| | 46 | | public static void ValidateScope(string scope) |
| | 47 | | { |
| 104 | 48 | | bool isScopeMatch = scopeRegex.IsMatch(scope); |
| | 49 | |
|
| 104 | 50 | | if (!isScopeMatch) |
| | 51 | | { |
| 0 | 52 | | throw new ArgumentException(InvalidScopeMessage, nameof(scope)); |
| | 53 | | } |
| 104 | 54 | | } |
| | 55 | | } |
| | 56 | | } |