|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using IdentityServer4; |
| 5 | +using IdentityServer4.Models; |
| 6 | +using IdentityServer4.Services; |
| 7 | +using IdentityServer4.Stores; |
| 8 | +using Microsoft.AspNetCore.Mvc; |
| 9 | +using ThemeTestingProjectDemo.Web.Controllers; |
| 10 | +using ThemeTestingProjectDemo.Web.Models.Consent; |
| 11 | + |
| 12 | +namespace ThemeTestingProjectDemo.Web.Host.Controllers |
| 13 | +{ |
| 14 | + public class ConsentController : ThemeTestingProjectDemoControllerBase |
| 15 | + { |
| 16 | + private readonly IIdentityServerInteractionService _interaction; |
| 17 | + private readonly IResourceStore _resourceStore; |
| 18 | + private readonly IClientStore _clientStore; |
| 19 | + |
| 20 | + public ConsentController( |
| 21 | + IIdentityServerInteractionService interaction, |
| 22 | + IClientStore clientStore, |
| 23 | + IResourceStore resourceStore |
| 24 | + ) |
| 25 | + { |
| 26 | + _interaction = interaction; |
| 27 | + _clientStore = clientStore; |
| 28 | + _resourceStore = resourceStore; |
| 29 | + } |
| 30 | + |
| 31 | + public async Task<IActionResult> Index(string returnUrl) |
| 32 | + { |
| 33 | + var model = await BuildViewModelAsync(returnUrl); |
| 34 | + return View(model); |
| 35 | + } |
| 36 | + |
| 37 | + [HttpPost] |
| 38 | + [ValidateAntiForgeryToken] |
| 39 | + public async Task<IActionResult> Index(ConsentInputModel model) |
| 40 | + { |
| 41 | + var result = await ProcessConsent(model); |
| 42 | + |
| 43 | + if (result.IsRedirect) |
| 44 | + { |
| 45 | + return Redirect(result.RedirectUri); |
| 46 | + } |
| 47 | + |
| 48 | + if (result.HasValidationError) |
| 49 | + { |
| 50 | + ModelState.AddModelError("", result.ValidationError); |
| 51 | + } |
| 52 | + |
| 53 | + if (result.ShowView) |
| 54 | + { |
| 55 | + return View("Index", result.ViewModel); |
| 56 | + } |
| 57 | + |
| 58 | + throw new Exception("Unexpected model/result!"); |
| 59 | + } |
| 60 | + |
| 61 | + public async Task<ProcessConsentResult> ProcessConsent(ConsentInputModel model) |
| 62 | + { |
| 63 | + var result = new ProcessConsentResult(); |
| 64 | + |
| 65 | + ConsentResponse grantedConsent = null; |
| 66 | + |
| 67 | + if (model.Button == "no") |
| 68 | + { |
| 69 | + grantedConsent = ConsentResponse.Denied; |
| 70 | + } |
| 71 | + else if (model.Button == "yes") |
| 72 | + { |
| 73 | + if (model.ScopesConsented != null && model.ScopesConsented.Any()) |
| 74 | + { |
| 75 | + var scopes = model.ScopesConsented; |
| 76 | + if (ConsentOptions.EnableOfflineAccess == false) |
| 77 | + { |
| 78 | + scopes = scopes.Where(x => x != IdentityServerConstants.StandardScopes.OfflineAccess); |
| 79 | + } |
| 80 | + |
| 81 | + grantedConsent = new ConsentResponse |
| 82 | + { |
| 83 | + RememberConsent = model.RememberConsent, |
| 84 | + ScopesConsented = scopes.ToArray() |
| 85 | + }; |
| 86 | + } |
| 87 | + else |
| 88 | + { |
| 89 | + result.ValidationError = ConsentOptions.MuchChooseOneErrorMessage; |
| 90 | + } |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + result.ValidationError = ConsentOptions.InvalidSelectionErrorMessage; |
| 95 | + } |
| 96 | + |
| 97 | + if (grantedConsent != null) |
| 98 | + { |
| 99 | + var request = await _interaction.GetAuthorizationContextAsync(model.ReturnUrl); |
| 100 | + if (request == null) |
| 101 | + { |
| 102 | + return result; |
| 103 | + } |
| 104 | + |
| 105 | + await _interaction.GrantConsentAsync(request, grantedConsent); |
| 106 | + |
| 107 | + result.RedirectUri = model.ReturnUrl; |
| 108 | + } |
| 109 | + else |
| 110 | + { |
| 111 | + result.ViewModel = await BuildViewModelAsync(model.ReturnUrl, model); |
| 112 | + } |
| 113 | + |
| 114 | + return result; |
| 115 | + } |
| 116 | + |
| 117 | + private async Task<ConsentViewModel> BuildViewModelAsync(string returnUrl, ConsentInputModel model = null) |
| 118 | + { |
| 119 | + var request = await _interaction.GetAuthorizationContextAsync(returnUrl); |
| 120 | + if (request == null) |
| 121 | + { |
| 122 | + return null; |
| 123 | + } |
| 124 | + |
| 125 | + var client = await _clientStore.FindEnabledClientByIdAsync(request.ClientId); |
| 126 | + if (client == null) |
| 127 | + { |
| 128 | + return null; |
| 129 | + } |
| 130 | + |
| 131 | + var resources = await _resourceStore.FindEnabledResourcesByScopeAsync(request.ScopesRequested); |
| 132 | + if (resources != null && (resources.IdentityResources.Any() || resources.ApiResources.Any())) |
| 133 | + { |
| 134 | + return CreateConsentViewModel(returnUrl, client, resources, model); |
| 135 | + } |
| 136 | + |
| 137 | + return null; |
| 138 | + } |
| 139 | + |
| 140 | + private ConsentViewModel CreateConsentViewModel( |
| 141 | + string returnUrl, |
| 142 | + Client client, |
| 143 | + Resources resources, |
| 144 | + ConsentInputModel model = null) |
| 145 | + { |
| 146 | + var vm = new ConsentViewModel |
| 147 | + { |
| 148 | + RememberConsent = model?.RememberConsent ?? true, |
| 149 | + ScopesConsented = model?.ScopesConsented ?? Enumerable.Empty<string>(), |
| 150 | + ReturnUrl = returnUrl, |
| 151 | + ClientName = client.ClientName, |
| 152 | + ClientUrl = client.ClientUri, |
| 153 | + ClientLogoUrl = client.LogoUri, |
| 154 | + AllowRememberConsent = client.AllowRememberConsent |
| 155 | + }; |
| 156 | + |
| 157 | + vm.IdentityScopes = resources.IdentityResources.Select(x => CreateScopeViewModel(x, vm.ScopesConsented.Contains(x.Name) || model == null)).ToArray(); |
| 158 | + vm.ResourceScopes = resources.ApiResources.SelectMany(x => x.Scopes).Select(x => CreateScopeViewModel(x, vm.ScopesConsented.Contains(x.Name) || model == null)).ToArray(); |
| 159 | + if (ConsentOptions.EnableOfflineAccess && resources.OfflineAccess) |
| 160 | + { |
| 161 | + vm.ResourceScopes = vm.ResourceScopes.Union(new [] { |
| 162 | + GetOfflineAccessScope(vm.ScopesConsented.Contains(IdentityServerConstants.StandardScopes.OfflineAccess) || model == null) |
| 163 | + }); |
| 164 | + } |
| 165 | + |
| 166 | + return vm; |
| 167 | + } |
| 168 | + |
| 169 | + public ScopeViewModel CreateScopeViewModel(IdentityResource identity, bool check) |
| 170 | + { |
| 171 | + return new ScopeViewModel |
| 172 | + { |
| 173 | + Name = identity.Name, |
| 174 | + DisplayName = identity.DisplayName, |
| 175 | + Description = identity.Description, |
| 176 | + Emphasize = identity.Emphasize, |
| 177 | + Required = identity.Required, |
| 178 | + Checked = check || identity.Required, |
| 179 | + }; |
| 180 | + } |
| 181 | + |
| 182 | + public ScopeViewModel CreateScopeViewModel(Scope scope, bool check) |
| 183 | + { |
| 184 | + return new ScopeViewModel |
| 185 | + { |
| 186 | + Name = scope.Name, |
| 187 | + DisplayName = scope.DisplayName, |
| 188 | + Description = scope.Description, |
| 189 | + Emphasize = scope.Emphasize, |
| 190 | + Required = scope.Required, |
| 191 | + Checked = check || scope.Required, |
| 192 | + }; |
| 193 | + } |
| 194 | + |
| 195 | + private ScopeViewModel GetOfflineAccessScope(bool check) |
| 196 | + { |
| 197 | + return new ScopeViewModel |
| 198 | + { |
| 199 | + Name = IdentityServerConstants.StandardScopes.OfflineAccess, |
| 200 | + DisplayName = ConsentOptions.OfflineAccessDisplayName, |
| 201 | + Description = ConsentOptions.OfflineAccessDescription, |
| 202 | + Emphasize = true, |
| 203 | + Checked = check |
| 204 | + }; |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments