1010using StsServerIdentity . Filters ;
1111using StsServerIdentity . Models ;
1212
13- namespace StsServerIdentity . Controllers
13+ namespace StsServerIdentity . Controllers ;
14+
15+ /// <summary>
16+ /// This sample controller allows a user to revoke grants given to clients
17+ /// </summary>
18+ [ SecurityHeaders ]
19+ [ Authorize ]
20+ public class GrantsController : Controller
1421{
22+ private readonly IIdentityServerInteractionService _interaction ;
23+ private readonly IClientStore _clients ;
24+ private readonly IResourceStore _resources ;
25+ private readonly IEventService _events ;
26+
27+ public GrantsController ( IIdentityServerInteractionService interaction ,
28+ IClientStore clients ,
29+ IResourceStore resources ,
30+ IEventService events )
31+ {
32+ _interaction = interaction ;
33+ _clients = clients ;
34+ _resources = resources ;
35+ _events = events ;
36+ }
37+
1538 /// <summary>
16- /// This sample controller allows a user to revoke grants given to clients
39+ /// Show list of grants
1740 /// </summary>
18- [ SecurityHeaders ]
19- [ Authorize ]
20- public class GrantsController : Controller
41+ [ HttpGet ]
42+ public async Task < IActionResult > Index ( )
2143 {
22- private readonly IIdentityServerInteractionService _interaction ;
23- private readonly IClientStore _clients ;
24- private readonly IResourceStore _resources ;
25- private readonly IEventService _events ;
26-
27- public GrantsController ( IIdentityServerInteractionService interaction ,
28- IClientStore clients ,
29- IResourceStore resources ,
30- IEventService events )
31- {
32- _interaction = interaction ;
33- _clients = clients ;
34- _resources = resources ;
35- _events = events ;
36- }
44+ return View ( "Index" , await BuildViewModelAsync ( ) ) ;
45+ }
3746
38- /// <summary>
39- /// Show list of grants
40- /// </summary>
41- [ HttpGet ]
42- public async Task < IActionResult > Index ( )
43- {
44- return View ( "Index" , await BuildViewModelAsync ( ) ) ;
45- }
47+ /// <summary>
48+ /// Handle postback to revoke a client
49+ /// </summary>
50+ [ HttpPost ]
51+ [ ValidateAntiForgeryToken ]
52+ public async Task < IActionResult > Revoke ( string clientId )
53+ {
54+ await _interaction . RevokeUserConsentAsync ( clientId ) ;
55+ await _events . RaiseAsync ( new GrantsRevokedEvent ( User . GetSubjectId ( ) , clientId ) ) ;
4656
47- /// <summary>
48- /// Handle postback to revoke a client
49- /// </summary>
50- [ HttpPost ]
51- [ ValidateAntiForgeryToken ]
52- public async Task < IActionResult > Revoke ( string clientId )
53- {
54- await _interaction . RevokeUserConsentAsync ( clientId ) ;
55- await _events . RaiseAsync ( new GrantsRevokedEvent ( User . GetSubjectId ( ) , clientId ) ) ;
57+ return RedirectToAction ( "Index" ) ;
58+ }
5659
57- return RedirectToAction ( "Index" ) ;
58- }
60+ private async Task < GrantsViewModel > BuildViewModelAsync ( )
61+ {
62+ var grants = await _interaction . GetAllUserGrantsAsync ( ) ;
5963
60- private async Task < GrantsViewModel > BuildViewModelAsync ( )
64+ var list = new List < GrantViewModel > ( ) ;
65+ foreach ( var grant in grants )
6166 {
62- var grants = await _interaction . GetAllUserGrantsAsync ( ) ;
63-
64- var list = new List < GrantViewModel > ( ) ;
65- foreach ( var grant in grants )
67+ var client = await _clients . FindClientByIdAsync ( grant . ClientId ) ;
68+ if ( client != null )
6669 {
67- var client = await _clients . FindClientByIdAsync ( grant . ClientId ) ;
68- if ( client != null )
69- {
70- var resources = await _resources . FindResourcesByScopeAsync ( grant . Scopes ) ;
70+ var resources = await _resources . FindResourcesByScopeAsync ( grant . Scopes ) ;
7171
72- var item = new GrantViewModel ( )
73- {
74- ClientId = client . ClientId ,
75- ClientName = client . ClientName ?? client . ClientId ,
76- ClientLogoUrl = client . LogoUri ,
77- ClientUrl = client . ClientUri ,
78- Description = grant . Description ,
79- Created = grant . CreationTime ,
80- Expires = grant . Expiration ,
81- IdentityGrantNames = resources . IdentityResources . Select ( x => x . DisplayName ?? x . Name ) . ToArray ( ) ,
82- ApiGrantNames = resources . ApiScopes . Select ( x => x . DisplayName ?? x . Name ) . ToArray ( )
83- } ;
72+ var item = new GrantViewModel ( )
73+ {
74+ ClientId = client . ClientId ,
75+ ClientName = client . ClientName ?? client . ClientId ,
76+ ClientLogoUrl = client . LogoUri ,
77+ ClientUrl = client . ClientUri ,
78+ Description = grant . Description ,
79+ Created = grant . CreationTime ,
80+ Expires = grant . Expiration ,
81+ IdentityGrantNames = resources . IdentityResources . Select ( x => x . DisplayName ?? x . Name ) . ToArray ( ) ,
82+ ApiGrantNames = resources . ApiScopes . Select ( x => x . DisplayName ?? x . Name ) . ToArray ( )
83+ } ;
8484
85- list . Add ( item ) ;
86- }
85+ list . Add ( item ) ;
8786 }
88-
89- return new GrantsViewModel
90- {
91- Grants = list
92- } ;
9387 }
88+
89+ return new GrantsViewModel
90+ {
91+ Grants = list
92+ } ;
9493 }
9594}
0 commit comments