Skip to content
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

add ApiResources to consent screen #95

Merged
merged 3 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<PackageReference Update="Microsoft.AspNetCore.Identity" Version="$(FrameworkVersion)" />
<PackageReference Update="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="$(FrameworkVersion)" />
<PackageReference Update="Microsoft.AspNetCore.Authentication.Certificate" Version="$(FrameworkVersion)"/>
<PackageReference Update="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="$(FrameworkVersion)"/>

<!--microsoft entity framework -->
<PackageReference Update="Microsoft.EntityFrameworkCore.Relational" Version="$(EntityFrameworkVersion)" />
Expand Down
1 change: 1 addition & 0 deletions clients/src/MvcCode/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public void ConfigureServices(IServiceCollection services)
options.Scope.Add("profile");
options.Scope.Add("email");
options.Scope.Add("resource1.scope1");
options.Scope.Add("resource2.scope1");
//options.Scope.Add("transaction:123");
//options.Scope.Add("transaction");
options.Scope.Add("offline_access");
Expand Down
2 changes: 2 additions & 0 deletions hosts/Configuration/Resources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ public class Resources
{
new ApiResource("urn:resource1", "Resource 1")
{
Description = "Something very long and descriptive",
ApiSecrets = { new Secret("secret".Sha256()) },

Scopes = { "resource1.scope1", "resource1.scope2", "shared.scope" }
},

new ApiResource("urn:resource2", "Resource 2")
{
Description = "Something very long and descriptive",
ApiSecrets = { new Secret("secret".Sha256()) },

// additional claims to put into access token
Expand Down
7 changes: 4 additions & 3 deletions hosts/main/Host.Main.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net5.0</TargetFrameworks>
Expand All @@ -18,8 +18,9 @@
<PackageReference Include="Microsoft.AspNetCore.Authentication.Certificate" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" PrivateAssets="All" />

<PackageReference Include="Serilog.AspNetCore" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" />

<PackageReference Include="Serilog.AspNetCore" />
</ItemGroup>

<ItemGroup>
Expand Down
18 changes: 16 additions & 2 deletions hosts/main/Quickstart/Consent/ConsentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Duende.IdentityServer.Models;
using Duende.IdentityServer.Services;
using Duende.IdentityServer.Validation;
using IdentityModel;

namespace IdentityServerHost.Quickstart.UI
{
Expand Down Expand Up @@ -194,15 +195,26 @@ private ConsentViewModel CreateConsentViewModel(
AllowRememberConsent = request.Client.AllowRememberConsent
};

vm.IdentityScopes = request.ValidatedResources.Resources.IdentityResources.Select(x => CreateScopeViewModel(x, vm.ScopesConsented.Contains(x.Name) || model == null)).ToArray();
vm.IdentityScopes = request.ValidatedResources.Resources.IdentityResources
.Select(x => CreateScopeViewModel(x, vm.ScopesConsented.Contains(x.Name) || model == null))
.ToArray();

var resourceIndicators = request.Parameters.GetValues(OidcConstants.AuthorizeRequest.Resource);
var apiResources = request.ValidatedResources.Resources.ApiResources.Where(x => resourceIndicators.Contains(x.Name));

var apiScopes = new List<ScopeViewModel>();
foreach(var parsedScope in request.ValidatedResources.ParsedScopes)
foreach (var parsedScope in request.ValidatedResources.ParsedScopes)
{
var apiScope = request.ValidatedResources.Resources.FindApiScope(parsedScope.ParsedName);
if (apiScope != null)
{
var scopeVm = CreateScopeViewModel(parsedScope, apiScope, vm.ScopesConsented.Contains(parsedScope.RawValue) || model == null);
scopeVm.Resources = apiResources.Where(x => x.Scopes.Contains(parsedScope.ParsedName))
.Select(x=> new ResourceViewModel
{
Name = x.Name,
DisplayName = x.DisplayName ?? x.Name,
}).ToArray();
apiScopes.Add(scopeVm);
}
}
Expand All @@ -219,6 +231,7 @@ private ScopeViewModel CreateScopeViewModel(IdentityResource identity, bool chec
{
return new ScopeViewModel
{
Name = identity.Name,
Value = identity.Name,
DisplayName = identity.DisplayName ?? identity.Name,
Description = identity.Description,
Expand All @@ -238,6 +251,7 @@ public ScopeViewModel CreateScopeViewModel(ParsedScopeValue parsedScopeValue, Ap

return new ScopeViewModel
{
Name = parsedScopeValue.ParsedName,
Value = parsedScopeValue.RawValue,
DisplayName = displayName,
Description = apiScope.Description,
Expand Down
2 changes: 1 addition & 1 deletion hosts/main/Quickstart/Consent/ConsentViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Duende Software. All rights reserved.
// Copyright (c) Duende Software. All rights reserved.
// See LICENSE in the project root for license information.


Expand Down
14 changes: 14 additions & 0 deletions hosts/main/Quickstart/Consent/ResourceViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Duende Software. All rights reserved.
// See LICENSE in the project root for license information.


using System.Collections.Generic;

namespace IdentityServerHost.Quickstart.UI
{
public class ResourceViewModel
{
public string Name { get; set; }
public string DisplayName { get; set; }
}
}
6 changes: 5 additions & 1 deletion hosts/main/Quickstart/Consent/ScopeViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
// Copyright (c) Duende Software. All rights reserved.
// Copyright (c) Duende Software. All rights reserved.
// See LICENSE in the project root for license information.


using System.Collections.Generic;

namespace IdentityServerHost.Quickstart.UI
{
public class ScopeViewModel
{
public string Name { get; set; }
public string Value { get; set; }
public string DisplayName { get; set; }
public string Description { get; set; }
public bool Emphasize { get; set; }
public bool Required { get; set; }
public bool Checked { get; set; }
public IEnumerable<ResourceViewModel> Resources { get; set; }
}
}
13 changes: 10 additions & 3 deletions hosts/main/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,30 @@
using Microsoft.AspNetCore.HttpOverrides;
using IdentityServerHost.Quickstart.UI;
using Duende.IdentityServer.Configuration;
using Microsoft.Extensions.Hosting;

namespace IdentityServerHost
{
public class Startup
{
private readonly IConfiguration _config;
private readonly IHostEnvironment _environment;

public Startup(IConfiguration config)
public Startup(IConfiguration config, IHostEnvironment environment)
{
_config = config;
_environment = environment;

IdentityModelEventSource.ShowPII = true;
}

public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
var mvc = services.AddControllersWithViews();
if (_environment.IsDevelopment())
{
mvc.AddRazorRuntimeCompilation();
}

// cookie policy to deal with temporary browser incompatibilities
services.AddSameSiteCookiePolicy();
Expand Down Expand Up @@ -102,7 +109,7 @@ public void Configure(IApplicationBuilder app)
app.UseStaticFiles();

app.UseRouting();
app.UseIdentityServer();
app.UseIdentityServer();

app.UseAuthorization();

Expand Down
4 changes: 2 additions & 2 deletions hosts/main/Views/Consent/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@
</div>
</div>
}

<div class="form-group">
<div class="card">
<div class="card-header">
<span class="glyphicon glyphicon-tasks"></span>
<span class="glyphicon glyphicon-pencil"></span>
Description
</div>
<div class="card-body">
Expand Down
3 changes: 2 additions & 1 deletion hosts/main/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
Expand All @@ -11,6 +11,7 @@
<link rel="shortcut icon" type="image/x-icon" href="~/favicon.ico" />

<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/lib/bootstrap4-glyphicons/css/bootstrap-glyphicons.min.css" />
<link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
Expand Down
14 changes: 13 additions & 1 deletion hosts/main/Views/Shared/_ScopeListItem.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@model ScopeViewModel
@model ScopeViewModel

<li class="list-group-item">
<label>
Expand Down Expand Up @@ -31,4 +31,16 @@
<label for="scopes_@Model.Value">@Model.Description</label>
</div>
}
@if (Model.Resources?.Any() == true)
{
<div class="consent-description">
<label>Will be available to these resource servers:</label>
<ul>
@foreach (var resource in Model.Resources)
{
<li>@resource.DisplayName</li>
}
</ul>
</div>
}
</li>
2 changes: 1 addition & 1 deletion hosts/main/Views/_ViewStart.cshtml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
@{
@{
Layout = "_Layout";
}
Loading