コード例 #1
0
        public async Task <IActionResult> Edit(EditOpenIdScopeViewModel model, string returnUrl = null)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageScopes))
            {
                return(Forbid());
            }

            var scope = await _scopeManager.FindByPhysicalIdAsync(model.Id);

            if (scope == null)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                var other = await _scopeManager.FindByNameAsync(model.Name);

                if (other != null && !string.Equals(
                        await _scopeManager.GetIdAsync(other),
                        await _scopeManager.GetIdAsync(scope)))
                {
                    ModelState.AddModelError(nameof(model.Name), S["The name is already taken by another scope."]);
                }
            }

            if (!ModelState.IsValid)
            {
                ViewData["ReturnUrl"] = returnUrl;
                return(View(model));
            }

            var descriptor = new OpenIdScopeDescriptor();
            await _scopeManager.PopulateAsync(descriptor, scope);

            descriptor.Description = model.Description;
            descriptor.DisplayName = model.DisplayName;
            descriptor.Name        = model.Name;

            descriptor.Resources.Clear();

            if (!string.IsNullOrEmpty(model.Resources))
            {
                descriptor.Resources.UnionWith(model.Resources.Split(' ', StringSplitOptions.RemoveEmptyEntries));
            }

            descriptor.Resources.UnionWith(model.Tenants
                                           .Where(tenant => tenant.Selected)
                                           .Where(tenant => !string.Equals(tenant.Name, _shellSettings.Name))
                                           .Select(tenant => OpenIdConstants.Prefixes.Tenant + tenant.Name));

            await _scopeManager.UpdateAsync(scope, descriptor);

            if (string.IsNullOrEmpty(returnUrl))
            {
                return(RedirectToAction("Index"));
            }

            return(LocalRedirect(returnUrl));
        }
コード例 #2
0
        public async Task OpenIdScopeCanBeParsed()
        {
            // Arrange

            // Match expected with scope-recipe.json
            var expected = CreateScopeDescriptor(
                "test_scope", "A", "res1", "res2", "res3");
            OpenIdScopeDescriptor actual = null;
            var scopeManagerMock         = new Mock <IOpenIdScopeManager>(MockBehavior.Strict);

            scopeManagerMock.Setup(m =>
                                   m.FindByNameAsync(
                                       It.IsAny <string>(),
                                       It.IsAny <CancellationToken>()))
            .Returns(
                new ValueTask <object>(actual));

            scopeManagerMock.Setup(m =>
                                   m.CreateAsync(
                                       It.IsAny <OpenIdScopeDescriptor>(),
                                       It.IsAny <CancellationToken>()))
            .Callback <OpenIddictScopeDescriptor, CancellationToken>((s, c) =>
                                                                     actual = (OpenIdScopeDescriptor)s)
            .Returns(
                new ValueTask <object>());

            var step    = new OpenIdScopeStep(scopeManagerMock.Object);
            var recipe  = JObject.Parse(GetRecipeFileContent("scope-recipe"));
            var context = new RecipeExecutionContext
            {
                Name = recipe.Property("steps").Value.First.Value <string>("name"),
                Step = (JObject)recipe.Property("steps").Value.First,
            };

            // Act
            await step.ExecuteAsync(context);

            // Assert
            scopeManagerMock.Verify(m =>
                                    m.FindByNameAsync(
                                        It.Is <string>(v => v == expected.Name),
                                        It.IsAny <CancellationToken>()));

            scopeManagerMock.Verify(m =>
                                    m.CreateAsync(
                                        It.IsAny <OpenIdScopeDescriptor>(),
                                        It.IsAny <CancellationToken>()));

            Assert.Equal(expected.Name, actual.Name);
            Assert.Equal(expected.DisplayName, actual.DisplayName);
            Assert.Equal(expected.Description, actual.Description);
            Assert.Equal(expected.Resources.ToArray(), actual.Resources.ToArray());
        }
コード例 #3
0
        private static OpenIdScopeDescriptor CreateScopeDescriptor(string name, string suffix, params string[] resources)
        {
            var scope = new OpenIdScopeDescriptor
            {
                Name        = name,
                DisplayName = $"Test Scope {suffix}",
                Description = $"Unit test scope {suffix}."
            };

            scope.Resources.UnionWith(resources);
            return(scope);
        }
コード例 #4
0
        public async Task <IActionResult> Create(CreateOpenIdScopeViewModel model, string returnUrl = null)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageScopes))
            {
                return(Unauthorized());
            }

            if (await _scopeManager.FindByNameAsync(model.Name) != null)
            {
                ModelState.AddModelError(nameof(model.Name), T["The name is already taken by another scope."]);
            }

            if (!ModelState.IsValid)
            {
                ViewData["ReturnUrl"] = returnUrl;
                return(View(model));
            }

            var descriptor = new OpenIdScopeDescriptor
            {
                Description = model.Description,
                DisplayName = model.DisplayName,
                Name        = model.Name
            };

            if (!string.IsNullOrEmpty(model.Resources))
            {
                descriptor.Resources.UnionWith(model.Resources.Split(' ', StringSplitOptions.RemoveEmptyEntries));
            }

            descriptor.Resources.UnionWith(model.Tenants
                                           .Where(tenant => tenant.Selected)
                                           .Where(tenant => !string.Equals(tenant.Name, _shellSettings.Name))
                                           .Select(tenant => OpenIdConstants.Prefixes.Tenant + tenant.Name));

            await _scopeManager.CreateAsync(descriptor);

            if (string.IsNullOrEmpty(returnUrl))
            {
                return(RedirectToAction("Index"));
            }

            return(LocalRedirect(returnUrl));
        }
コード例 #5
0
        public async Task ExecuteAsync(RecipeExecutionContext context)
        {
            if (!string.Equals(context.Name, "OpenIdScope", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            var model = context.Step.ToObject <OpenIdScopeStepModel>();
            var scope = await _scopeManager.FindByNameAsync(model.ScopeName);

            var descriptor = new OpenIdScopeDescriptor();
            var isNew      = true;

            if (scope != null)
            {
                isNew = false;
                await _scopeManager.PopulateAsync(scope, descriptor);
            }

            descriptor.Description = model.Description;
            descriptor.Name        = model.ScopeName;
            descriptor.DisplayName = model.DisplayName;

            if (!string.IsNullOrEmpty(model.Resources))
            {
                descriptor.Resources.Clear();
                descriptor.Resources.UnionWith(
                    model.Resources
                    .Split(' ', StringSplitOptions.RemoveEmptyEntries));
            }

            if (isNew)
            {
                await _scopeManager.CreateAsync(descriptor);
            }
            else
            {
                await _scopeManager.UpdateAsync(scope, descriptor);
            }
        }
コード例 #6
0
        public async Task ExecuteAsync(RecipeExecutionContext context)
        {
            if (!string.Equals(context.Name, "OpenIdScope", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            var model      = context.Step.ToObject <OpenIdScopeStepViewModel>();
            var descriptor = new OpenIdScopeDescriptor
            {
                Description = model.Description,
                Name        = model.ScopeName,
                DisplayName = model.DisplayName
            };

            if (!string.IsNullOrEmpty(model.Resources))
            {
                descriptor.Resources.UnionWith(model.Resources.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries));
            }

            await _scopeManager.CreateAsync(descriptor);
        }