2018-10-26 07:22:53 +02:00
|
|
|
|
using IdentityServer4.Services;
|
|
|
|
|
using IdentityServer4.Stores;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Teknik.IdentityServer.Security;
|
|
|
|
|
using Teknik.IdentityServer.ViewModels;
|
|
|
|
|
using Teknik.Logging;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Teknik.Configuration;
|
|
|
|
|
|
|
|
|
|
namespace Teknik.IdentityServer.Controllers
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This sample controller allows a user to revoke grants given to clients
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Authorize(AuthenticationSchemes = "Identity.Application")]
|
|
|
|
|
public class GrantsController : DefaultController
|
|
|
|
|
{
|
|
|
|
|
private readonly IIdentityServerInteractionService _interaction;
|
|
|
|
|
private readonly IClientStore _clients;
|
|
|
|
|
private readonly IResourceStore _resources;
|
|
|
|
|
|
|
|
|
|
public GrantsController(
|
|
|
|
|
ILogger<Logger> logger,
|
|
|
|
|
Config config,
|
|
|
|
|
IIdentityServerInteractionService interaction,
|
|
|
|
|
IClientStore clients,
|
|
|
|
|
IResourceStore resources) : base(logger, config)
|
|
|
|
|
{
|
|
|
|
|
_interaction = interaction;
|
|
|
|
|
_clients = clients;
|
|
|
|
|
_resources = resources;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Show list of grants
|
|
|
|
|
/// </summary>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public async Task<IActionResult> Index()
|
|
|
|
|
{
|
2019-01-26 06:16:01 +01:00
|
|
|
|
ViewBag.Title = "Granted Applications";
|
2018-10-26 07:22:53 +02:00
|
|
|
|
return View("Index", await BuildViewModelAsync());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handle postback to revoke a client
|
|
|
|
|
/// </summary>
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[ValidateAntiForgeryToken]
|
|
|
|
|
public async Task<IActionResult> Revoke(string clientId)
|
|
|
|
|
{
|
|
|
|
|
await _interaction.RevokeUserConsentAsync(clientId);
|
|
|
|
|
return RedirectToAction("Index");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<GrantsViewModel> BuildViewModelAsync()
|
|
|
|
|
{
|
2021-07-01 06:56:12 +02:00
|
|
|
|
var grants = await _interaction.GetAllUserGrantsAsync();
|
2018-10-26 07:22:53 +02:00
|
|
|
|
|
|
|
|
|
var list = new List<GrantViewModel>();
|
|
|
|
|
foreach(var grant in grants)
|
|
|
|
|
{
|
|
|
|
|
var client = await _clients.FindClientByIdAsync(grant.ClientId);
|
|
|
|
|
if (client != null)
|
|
|
|
|
{
|
|
|
|
|
var resources = await _resources.FindResourcesByScopeAsync(grant.Scopes);
|
|
|
|
|
|
|
|
|
|
var item = new GrantViewModel()
|
|
|
|
|
{
|
|
|
|
|
ClientId = client.ClientId,
|
|
|
|
|
ClientName = client.ClientName ?? client.ClientId,
|
|
|
|
|
ClientLogoUrl = client.LogoUri,
|
|
|
|
|
ClientUrl = client.ClientUri,
|
|
|
|
|
Created = grant.CreationTime,
|
|
|
|
|
Expires = grant.Expiration,
|
|
|
|
|
IdentityGrantNames = resources.IdentityResources.Select(x => x.DisplayName ?? x.Name).ToArray(),
|
|
|
|
|
ApiGrantNames = resources.ApiResources.Select(x => x.DisplayName ?? x.Name).ToArray()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
list.Add(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new GrantsViewModel
|
|
|
|
|
{
|
|
|
|
|
Grants = list
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|