|
1 | | -// Copyright (c) .NET Foundation. All rights reserved. |
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
2 | 2 | // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
3 | 3 |
|
4 | 4 | using System; |
@@ -1025,8 +1025,8 @@ public virtual async Task<JsonResult> GenerateApiKey(string description, string |
1025 | 1025 | return Json(Strings.UserNotFound); |
1026 | 1026 | } |
1027 | 1027 |
|
1028 | | - var resolvedScopes = BuildScopes(scopeOwner, scopes, subjects); |
1029 | | - if (!VerifyScopes(resolvedScopes)) |
| 1028 | + var resolvedScopes = _credentialBuilder.BuildScopes(scopeOwner, scopes, subjects); |
| 1029 | + if (!_credentialBuilder.VerifyScopes(GetCurrentUser(), resolvedScopes)) |
1030 | 1030 | { |
1031 | 1031 | Response.StatusCode = (int)HttpStatusCode.BadRequest; |
1032 | 1032 | return Json(Strings.ApiKeyScopesNotAllowed); |
@@ -1079,7 +1079,7 @@ public virtual async Task<JsonResult> EditCredential(string credentialType, int? |
1079 | 1079 |
|
1080 | 1080 | var scopeOwner = cred.Scopes.GetOwnerScope(); |
1081 | 1081 | var scopes = cred.Scopes.Select(x => x.AllowedAction).Distinct().ToArray(); |
1082 | | - var newScopes = BuildScopes(scopeOwner, scopes, subjects); |
| 1082 | + var newScopes = _credentialBuilder.BuildScopes(scopeOwner, scopes, subjects); |
1083 | 1083 |
|
1084 | 1084 | await AuthenticationService.EditCredentialScopes(user, cred, newScopes); |
1085 | 1085 |
|
@@ -1111,86 +1111,6 @@ private async Task<CredentialViewModel> GenerateApiKeyInternal(string descriptio |
1111 | 1111 | return credentialViewModel; |
1112 | 1112 | } |
1113 | 1113 |
|
1114 | | - private static IDictionary<string, IActionRequiringEntityPermissions[]> AllowedActionToActionRequiringEntityPermissionsMap = new Dictionary<string, IActionRequiringEntityPermissions[]> |
1115 | | - { |
1116 | | - { NuGetScopes.PackagePush, new IActionRequiringEntityPermissions[] { ActionsRequiringPermissions.UploadNewPackageId, ActionsRequiringPermissions.UploadNewPackageVersion } }, |
1117 | | - { NuGetScopes.PackagePushVersion, new [] { ActionsRequiringPermissions.UploadNewPackageVersion } }, |
1118 | | - { NuGetScopes.PackageUnlist, new [] { ActionsRequiringPermissions.UnlistOrRelistPackage } }, |
1119 | | - { NuGetScopes.PackageVerify, new [] { ActionsRequiringPermissions.VerifyPackage } }, |
1120 | | - }; |
1121 | | - |
1122 | | - private bool VerifyScopes(IEnumerable<Scope> scopes) |
1123 | | - { |
1124 | | - if (!scopes.Any()) |
1125 | | - { |
1126 | | - // All API keys must have at least one scope. |
1127 | | - return false; |
1128 | | - } |
1129 | | - |
1130 | | - foreach (var scope in scopes) |
1131 | | - { |
1132 | | - if (string.IsNullOrEmpty(scope.AllowedAction)) |
1133 | | - { |
1134 | | - // All scopes must have an allowed action. |
1135 | | - return false; |
1136 | | - } |
1137 | | - |
1138 | | - // Get the list of actions allowed by this scope. |
1139 | | - var actions = new List<IActionRequiringEntityPermissions>(); |
1140 | | - foreach (var allowedAction in AllowedActionToActionRequiringEntityPermissionsMap.Keys) |
1141 | | - { |
1142 | | - if (scope.AllowsActions(allowedAction)) |
1143 | | - { |
1144 | | - actions.AddRange(AllowedActionToActionRequiringEntityPermissionsMap[allowedAction]); |
1145 | | - } |
1146 | | - } |
1147 | | - |
1148 | | - if (!actions.Any()) |
1149 | | - { |
1150 | | - // A scope should allow at least one action. |
1151 | | - return false; |
1152 | | - } |
1153 | | - |
1154 | | - foreach (var action in actions) |
1155 | | - { |
1156 | | - if (!action.IsAllowedOnBehalfOfAccount(GetCurrentUser(), scope.Owner)) |
1157 | | - { |
1158 | | - // The user must be able to perform the actions allowed by the scope on behalf of the scope's owner. |
1159 | | - return false; |
1160 | | - } |
1161 | | - } |
1162 | | - } |
1163 | | - |
1164 | | - return true; |
1165 | | - } |
1166 | | - |
1167 | | - private IList<Scope> BuildScopes(User scopeOwner, string[] scopes, string[] subjects) |
1168 | | - { |
1169 | | - var result = new List<Scope>(); |
1170 | | - |
1171 | | - var subjectsList = subjects?.Where(s => !string.IsNullOrWhiteSpace(s)).ToList() ?? new List<string>(); |
1172 | | - |
1173 | | - // No package filtering information was provided. So allow any pattern. |
1174 | | - if (!subjectsList.Any()) |
1175 | | - { |
1176 | | - subjectsList.Add(NuGetPackagePattern.AllInclusivePattern); |
1177 | | - } |
1178 | | - |
1179 | | - if (scopes != null) |
1180 | | - { |
1181 | | - foreach (var scope in scopes) |
1182 | | - { |
1183 | | - result.AddRange(subjectsList.Select(subject => new Scope(scopeOwner, subject, scope))); |
1184 | | - } |
1185 | | - } |
1186 | | - else |
1187 | | - { |
1188 | | - result.AddRange(subjectsList.Select(subject => new Scope(scopeOwner, subject, NuGetScopes.All))); |
1189 | | - } |
1190 | | - |
1191 | | - return result; |
1192 | | - } |
1193 | | - |
1194 | 1114 | private static IList<Scope> BuildScopes(IEnumerable<Scope> scopes) |
1195 | 1115 | { |
1196 | 1116 | return scopes.Select(scope => new Scope(scope.Owner, scope.Subject, scope.AllowedAction)).ToList(); |
|
0 commit comments