refact
This commit is contained in:
parent
ec5bd7ca1f
commit
1b2d850522
33 changed files with 49 additions and 88 deletions
20
Models/Account/AccountOptions.cs
Normal file
20
Models/Account/AccountOptions.cs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
|
||||
|
||||
|
||||
using System;
|
||||
|
||||
namespace IdentityServerHost.Quickstart.UI
|
||||
{
|
||||
public class AccountOptions
|
||||
{
|
||||
public static bool AllowLocalLogin = true;
|
||||
public static bool AllowRememberLogin = true;
|
||||
public static TimeSpan RememberMeLoginDuration = TimeSpan.FromDays(30);
|
||||
|
||||
public static bool ShowLogoutPrompt = true;
|
||||
public static bool AutomaticRedirectAfterSignOut = false;
|
||||
|
||||
public static string InvalidCredentialsErrorMessage = "Invalid username or password";
|
||||
}
|
||||
}
|
||||
12
Models/Account/ExternalProvider.cs
Normal file
12
Models/Account/ExternalProvider.cs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
|
||||
|
||||
|
||||
namespace IdentityServerHost.Quickstart.UI
|
||||
{
|
||||
public class ExternalProvider
|
||||
{
|
||||
public string DisplayName { get; set; }
|
||||
public string AuthenticationScheme { get; set; }
|
||||
}
|
||||
}
|
||||
19
Models/Account/LoggedOutViewModel.cs
Normal file
19
Models/Account/LoggedOutViewModel.cs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
|
||||
|
||||
|
||||
namespace IdentityServerHost.Quickstart.UI
|
||||
{
|
||||
public class LoggedOutViewModel
|
||||
{
|
||||
public string PostLogoutRedirectUri { get; set; }
|
||||
public string ClientName { get; set; }
|
||||
public string SignOutIframeUrl { get; set; }
|
||||
|
||||
public bool AutomaticRedirectAfterSignOut { get; set; }
|
||||
|
||||
public string LogoutId { get; set; }
|
||||
public bool TriggerExternalSignout => ExternalAuthenticationScheme != null;
|
||||
public string ExternalAuthenticationScheme { get; set; }
|
||||
}
|
||||
}
|
||||
18
Models/Account/LoginInputModel.cs
Normal file
18
Models/Account/LoginInputModel.cs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
|
||||
|
||||
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace IdentityServerHost.Quickstart.UI
|
||||
{
|
||||
public class LoginInputModel
|
||||
{
|
||||
[Required]
|
||||
public string Username { get; set; }
|
||||
[Required]
|
||||
public string Password { get; set; }
|
||||
public bool RememberLogin { get; set; }
|
||||
public string ReturnUrl { get; set; }
|
||||
}
|
||||
}
|
||||
22
Models/Account/LoginViewModel.cs
Normal file
22
Models/Account/LoginViewModel.cs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
|
||||
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace IdentityServerHost.Quickstart.UI
|
||||
{
|
||||
public class LoginViewModel : LoginInputModel
|
||||
{
|
||||
public bool AllowRememberLogin { get; set; } = true;
|
||||
public bool EnableLocalLogin { get; set; } = true;
|
||||
|
||||
public IEnumerable<ExternalProvider> ExternalProviders { get; set; } = Enumerable.Empty<ExternalProvider>();
|
||||
public IEnumerable<ExternalProvider> VisibleExternalProviders => ExternalProviders.Where(x => !String.IsNullOrWhiteSpace(x.DisplayName));
|
||||
|
||||
public bool IsExternalLoginOnly => EnableLocalLogin == false && ExternalProviders?.Count() == 1;
|
||||
public string ExternalLoginScheme => IsExternalLoginOnly ? ExternalProviders?.SingleOrDefault()?.AuthenticationScheme : null;
|
||||
}
|
||||
}
|
||||
11
Models/Account/LogoutInputModel.cs
Normal file
11
Models/Account/LogoutInputModel.cs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
|
||||
|
||||
|
||||
namespace IdentityServerHost.Quickstart.UI
|
||||
{
|
||||
public class LogoutInputModel
|
||||
{
|
||||
public string LogoutId { get; set; }
|
||||
}
|
||||
}
|
||||
11
Models/Account/LogoutViewModel.cs
Normal file
11
Models/Account/LogoutViewModel.cs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
|
||||
|
||||
|
||||
namespace IdentityServerHost.Quickstart.UI
|
||||
{
|
||||
public class LogoutViewModel : LogoutInputModel
|
||||
{
|
||||
public bool ShowLogoutPrompt { get; set; } = true;
|
||||
}
|
||||
}
|
||||
12
Models/Account/RedirectViewModel.cs
Normal file
12
Models/Account/RedirectViewModel.cs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
|
||||
|
||||
|
||||
|
||||
namespace IdentityServerHost.Quickstart.UI
|
||||
{
|
||||
public class RedirectViewModel
|
||||
{
|
||||
public string RedirectUrl { get; set; }
|
||||
}
|
||||
}
|
||||
24
Models/Consent/ConsentInputModel.cs
Normal file
24
Models/Consent/ConsentInputModel.cs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
|
||||
|
||||
|
||||
using System.Collections.Generic;
|
||||
using IdentityServer4.Models;
|
||||
|
||||
namespace IdentityServerHost.Quickstart.UI
|
||||
{
|
||||
public class ConsentInputModel
|
||||
{
|
||||
public string Button { get; set; }
|
||||
public IEnumerable<string> ScopesConsented { get; set; }
|
||||
public bool RememberConsent { get; set; }
|
||||
public string ReturnUrl { get; set; }
|
||||
public string Description { get; set; }
|
||||
public string ClientName { get; internal set; }
|
||||
public string ClientUri { get; internal set; }
|
||||
public string LogoUri { get; internal set; }
|
||||
public bool AllowRememberConsent { get; internal set; }
|
||||
public ValidatedResources ValidatedResources { get; internal set; }
|
||||
public Client Client { get; internal set; }
|
||||
}
|
||||
}
|
||||
16
Models/Consent/ConsentOptions.cs
Normal file
16
Models/Consent/ConsentOptions.cs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
|
||||
|
||||
|
||||
namespace IdentityServerHost.Quickstart.UI
|
||||
{
|
||||
public class ConsentOptions
|
||||
{
|
||||
public static bool EnableOfflineAccess = true;
|
||||
public static string OfflineAccessDisplayName = "Offline Access";
|
||||
public static string OfflineAccessDescription = "Access to your applications and resources, even when you are offline";
|
||||
|
||||
public static readonly string MustChooseOneErrorMessage = "You must pick at least one permission";
|
||||
public static readonly string InvalidSelectionErrorMessage = "Invalid selection";
|
||||
}
|
||||
}
|
||||
20
Models/Consent/ConsentViewModel.cs
Normal file
20
Models/Consent/ConsentViewModel.cs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
|
||||
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace IdentityServerHost.Quickstart.UI
|
||||
{
|
||||
public class ConsentViewModel : ConsentInputModel
|
||||
{
|
||||
public string ClientLogoUrl { get; set; }
|
||||
public string ClientUrl { get; set; }
|
||||
|
||||
public IEnumerable<ScopeViewModel> ApiScopes { get; set; }
|
||||
|
||||
public IEnumerable<ScopeViewModel> IdentityScopes { get; set; }
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
29
Models/Consent/ParsedScopes.cs
Normal file
29
Models/Consent/ParsedScopes.cs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
|
||||
|
||||
|
||||
using System.Collections.Generic;
|
||||
using IdentityServer4.Models;
|
||||
|
||||
namespace IdentityServerHost.Quickstart.UI
|
||||
{
|
||||
public class ParsedScopes
|
||||
{
|
||||
public ParsedScopes(ParsedSecret secret)
|
||||
{
|
||||
RawValue = secret.Properties.ContainsKey(KEY_SCOPES) ? null : secret.Properties[KEY_SCOPES];
|
||||
Emphasize = secret.Properties.ContainsKey(KEY_OL);
|
||||
if (secret.Properties.ContainsKey(KEY_SCOPES)) Scopes = secret.Properties[KEY_SCOPES].Split(',');
|
||||
}
|
||||
|
||||
public const string KEY_SCOPES = "scopes";
|
||||
public const string KEY_OL = "ol";
|
||||
public string RawValue {
|
||||
get ;
|
||||
}
|
||||
|
||||
|
||||
public string[] Scopes { get ; protected set; }
|
||||
public bool Emphasize { get; }
|
||||
}
|
||||
}
|
||||
21
Models/Consent/ProcessConsentResult.cs
Normal file
21
Models/Consent/ProcessConsentResult.cs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
|
||||
|
||||
|
||||
using IdentityServer4.Models;
|
||||
|
||||
namespace IdentityServerHost.Quickstart.UI
|
||||
{
|
||||
public class ProcessConsentResult
|
||||
{
|
||||
public bool IsRedirect => RedirectUri != null;
|
||||
public string RedirectUri { get; set; }
|
||||
public Client Client { get; set; }
|
||||
|
||||
public bool ShowView => ViewModel != null;
|
||||
public ConsentViewModel ViewModel { get; set; }
|
||||
|
||||
public bool HasValidationError => ValidationError != null;
|
||||
public string ValidationError { get; set; }
|
||||
}
|
||||
}
|
||||
30
Models/Consent/QSAuthorizationRequest.cs
Normal file
30
Models/Consent/QSAuthorizationRequest.cs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
|
||||
|
||||
|
||||
using IdentityServer4.Models;
|
||||
using IdentityServer4.Validation;
|
||||
using NuGet.Packaging;
|
||||
|
||||
namespace nuget_host.OAuth
|
||||
{
|
||||
internal class NHAuthorizationRequest : AuthorizationRequest
|
||||
{
|
||||
internal NHAuthorizationRequest(ValidatedAuthorizeRequest request) : base()
|
||||
{
|
||||
ClientId = request.ClientId;
|
||||
RedirectUri = request.RedirectUri;
|
||||
DisplayMode = request.DisplayMode;
|
||||
UiLocales = request.UiLocales;
|
||||
IdP = request.GetIdP();
|
||||
Tenant = request.GetTenant();
|
||||
LoginHint = request.LoginHint;
|
||||
PromptMode = request.PromptMode;
|
||||
AcrValues = request.GetAcrValues();
|
||||
ScopesRequested = request.RequestedScopes;
|
||||
Parameters.Add(request.Raw);
|
||||
RequestObjectValues.AddRange(request.RequestObjectValues);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
16
Models/Consent/ScopeViewModel.cs
Normal file
16
Models/Consent/ScopeViewModel.cs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
|
||||
|
||||
|
||||
namespace IdentityServerHost.Quickstart.UI
|
||||
{
|
||||
public class ScopeViewModel
|
||||
{
|
||||
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; }
|
||||
}
|
||||
}
|
||||
17
Models/Consent/ValidatedResource.cs
Normal file
17
Models/Consent/ValidatedResource.cs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
|
||||
|
||||
|
||||
using IdentityServer4.Models;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace IdentityServerHost.Quickstart.UI
|
||||
{
|
||||
public class ValidatedResources
|
||||
{
|
||||
public Resources Resources { get; set; }
|
||||
public ParsedScopes ParsedScopes { get; internal set; }
|
||||
public bool OfflineAccess { get; internal set; }
|
||||
|
||||
}
|
||||
}
|
||||
13
Models/Device/DeviceAuthorizationInputModel.cs
Normal file
13
Models/Device/DeviceAuthorizationInputModel.cs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
|
||||
|
||||
|
||||
namespace IdentityServerHost.Quickstart.UI
|
||||
{
|
||||
|
||||
public class DeviceAuthorizationInputModel : ConsentInputModel
|
||||
{
|
||||
public string UserCode { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
12
Models/Device/DeviceAuthorizationViewModel.cs
Normal file
12
Models/Device/DeviceAuthorizationViewModel.cs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
|
||||
|
||||
|
||||
namespace IdentityServerHost.Quickstart.UI
|
||||
{
|
||||
public class DeviceAuthorizationViewModel : ConsentViewModel
|
||||
{
|
||||
public string UserCode { get; set; }
|
||||
public bool ConfirmUserCode { get; set; }
|
||||
}
|
||||
}
|
||||
255
Models/Device/DeviceController.cs
Normal file
255
Models/Device/DeviceController.cs
Normal file
|
|
@ -0,0 +1,255 @@
|
|||
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
|
||||
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using IdentityModel.Client;
|
||||
using IdentityServer4.Configuration;
|
||||
using IdentityServer4.Events;
|
||||
using IdentityServer4.Extensions;
|
||||
using IdentityServer4.Models;
|
||||
using IdentityServer4.Services;
|
||||
using IdentityServer4.Validation;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using nuget_host.OAuth;
|
||||
|
||||
namespace IdentityServerHost.Quickstart.UI
|
||||
{
|
||||
[Authorize]
|
||||
[SecurityHeaders]
|
||||
public class DeviceController : Controller
|
||||
{
|
||||
private readonly IDeviceFlowInteractionService _interaction;
|
||||
private readonly IEventService _events;
|
||||
private readonly IOptions<IdentityServerOptions> _options;
|
||||
private readonly ILogger<DeviceController> _logger;
|
||||
|
||||
public DeviceController(
|
||||
IDeviceFlowInteractionService interaction,
|
||||
IEventService eventService,
|
||||
IOptions<IdentityServerOptions> options,
|
||||
ILogger<DeviceController> logger)
|
||||
{
|
||||
_interaction = interaction;
|
||||
_events = eventService;
|
||||
_options = options;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Index()
|
||||
{
|
||||
string userCodeParamName = _options.Value.UserInteraction.DeviceVerificationUserCodeParameter;
|
||||
string userCode = Request.Query[userCodeParamName];
|
||||
if (string.IsNullOrWhiteSpace(userCode)) return View("UserCodeCapture");
|
||||
|
||||
var vm = await BuildViewModelAsync(userCode);
|
||||
if (vm == null) return View("Error");
|
||||
|
||||
vm.ConfirmUserCode = true;
|
||||
return View("UserCodeConfirmation", vm);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> UserCodeCapture(string userCode)
|
||||
{
|
||||
var vm = await BuildViewModelAsync(userCode);
|
||||
if (vm == null) return View("Error");
|
||||
|
||||
return View("UserCodeConfirmation", vm);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Callback(DeviceAuthorizationInputModel model)
|
||||
{
|
||||
if (model == null) throw new ArgumentNullException(nameof(model));
|
||||
|
||||
var result = await ProcessConsent(model);
|
||||
if (result.HasValidationError) return View("Error");
|
||||
|
||||
return View("Success");
|
||||
}
|
||||
|
||||
private async Task<ProcessConsentResult> ProcessConsent(DeviceAuthorizationInputModel model)
|
||||
{
|
||||
var result = new ProcessConsentResult();
|
||||
|
||||
var request = await _interaction.GetAuthorizationContextAsync(model.UserCode);
|
||||
if (request == null) return result;
|
||||
|
||||
ConsentResponse grantedConsent = null;
|
||||
|
||||
// user clicked 'no' - send back the standard 'access_denied' response
|
||||
if (model.Button == "no")
|
||||
{
|
||||
grantedConsent = ConsentResponse.Denied;
|
||||
|
||||
// emit event
|
||||
await _events.RaiseAsync(new ConsentDeniedEvent(User.GetSubjectId(), request.ClientId, request.ScopesRequested));
|
||||
}
|
||||
// user clicked 'yes' - validate the data
|
||||
else if (model.Button == "yes")
|
||||
{
|
||||
// if the user consented to some scope, build the response model
|
||||
if (model.ScopesConsented != null && model.ScopesConsented.Any())
|
||||
{
|
||||
var scopes = model.ScopesConsented;
|
||||
if (ConsentOptions.EnableOfflineAccess == false)
|
||||
{
|
||||
scopes = scopes.Where(x => x != IdentityServer4.IdentityServerConstants.StandardScopes.OfflineAccess);
|
||||
}
|
||||
|
||||
grantedConsent = new ConsentResponse
|
||||
{
|
||||
RememberConsent = model.RememberConsent,
|
||||
ScopesConsented = scopes.ToArray()
|
||||
};
|
||||
|
||||
// emit event
|
||||
await _events.RaiseAsync(new ConsentGrantedEvent(User.GetSubjectId(), request.ClientId, request.ScopesRequested, grantedConsent.ScopesConsented, grantedConsent.RememberConsent));
|
||||
}
|
||||
else
|
||||
{
|
||||
result.ValidationError = ConsentOptions.MustChooseOneErrorMessage;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result.ValidationError = ConsentOptions.InvalidSelectionErrorMessage;
|
||||
}
|
||||
|
||||
if (grantedConsent != null)
|
||||
{
|
||||
// communicate outcome of consent back to identityserver
|
||||
await _interaction.HandleRequestAsync(model.UserCode, grantedConsent);
|
||||
|
||||
// indicate that's it ok to redirect back to authorization endpoint
|
||||
result.RedirectUri = model.ReturnUrl;
|
||||
result.Client = model.Client;
|
||||
}
|
||||
else
|
||||
{
|
||||
// we need to redisplay the consent UI
|
||||
result.ViewModel = await BuildViewModelAsync(model.UserCode, model);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private async Task<DeviceAuthorizationViewModel> BuildViewModelAsync(string userCode, DeviceAuthorizationInputModel model = null)
|
||||
{
|
||||
var request = await _interaction.GetAuthorizationContextAsync(userCode);
|
||||
if (request != null)
|
||||
{
|
||||
return CreateConsentViewModel(userCode, model, request);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private DeviceAuthorizationViewModel CreateConsentViewModel(
|
||||
string userCode, DeviceAuthorizationInputModel model,
|
||||
DeviceFlowAuthorizationRequest request)
|
||||
{
|
||||
var vm = new DeviceAuthorizationViewModel
|
||||
{
|
||||
UserCode = userCode,
|
||||
Description = model?.Description,
|
||||
|
||||
RememberConsent = model?.RememberConsent ?? true,
|
||||
ScopesConsented = model?.ScopesConsented ?? Enumerable.Empty<string>(),
|
||||
|
||||
ClientName = model?.ClientName,
|
||||
ClientUrl = model?.ClientUri,
|
||||
ClientLogoUrl = model?.LogoUri,
|
||||
AllowRememberConsent = model != null && model.AllowRememberConsent
|
||||
};
|
||||
|
||||
vm.IdentityScopes = model.ValidatedResources.Resources.IdentityResources.Select(x => CreateScopeViewModel(x, vm.ScopesConsented.Contains(x.Name) || model == null)).ToArray();
|
||||
|
||||
var apiScopes = new List<ScopeViewModel>();
|
||||
foreach (var parsedScope in model.ValidatedResources.ParsedScopes.Scopes)
|
||||
{
|
||||
var apiScope = model.ValidatedResources.Resources.FindApiScope(parsedScope);
|
||||
if (apiScope != null)
|
||||
{
|
||||
var vreq = CreateValidatedRequest(request, apiScope);
|
||||
|
||||
var scopeVm = CreateScopeViewModel(apiScope,vreq);
|
||||
apiScopes.Add(scopeVm);
|
||||
}
|
||||
}
|
||||
if (ConsentOptions.EnableOfflineAccess && model.ValidatedResources.Resources.OfflineAccess)
|
||||
{
|
||||
apiScopes.Add(GetOfflineAccessScope(vm.ScopesConsented.Contains(IdentityServer4.IdentityServerConstants.StandardScopes.OfflineAccess) || model == null));
|
||||
}
|
||||
vm.ApiScopes = apiScopes;
|
||||
|
||||
return vm;
|
||||
}
|
||||
|
||||
private ValidatedAuthorizeRequest CreateValidatedRequest(DeviceFlowAuthorizationRequest request, Scope apiScope)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private ScopeViewModel CreateScopeViewModel(Scope scope, ValidatedAuthorizeRequest req)
|
||||
{
|
||||
return new ScopeViewModel
|
||||
{
|
||||
Value = scope.Name,
|
||||
DisplayName = scope.DisplayName ?? scope.Name,
|
||||
Description = scope.Description,
|
||||
Emphasize = scope.Emphasize,
|
||||
Required = scope.Required,
|
||||
Checked = req.Client != null
|
||||
};
|
||||
}
|
||||
|
||||
private ScopeViewModel CreateScopeViewModel(IdentityResource identity, bool check)
|
||||
{
|
||||
return new ScopeViewModel
|
||||
{
|
||||
Value = identity.Name,
|
||||
DisplayName = identity.DisplayName ?? identity.Name,
|
||||
Description = identity.Description,
|
||||
Emphasize = identity.Emphasize,
|
||||
Required = identity.Required,
|
||||
Checked = check || identity.Required
|
||||
};
|
||||
}
|
||||
|
||||
public ScopeViewModel CreateScopeViewModel(ParsedScopes parsedScopeValue, Scope apiScope, bool check)
|
||||
{
|
||||
return new ScopeViewModel
|
||||
{
|
||||
Value = parsedScopeValue.RawValue,
|
||||
// todo: use the parsed scope value in the display?
|
||||
DisplayName = apiScope.DisplayName,
|
||||
Description = apiScope.Description,
|
||||
Emphasize = parsedScopeValue.Emphasize,
|
||||
Required = apiScope.Required,
|
||||
Checked = check || apiScope.Required
|
||||
};
|
||||
}
|
||||
private ScopeViewModel GetOfflineAccessScope(bool check)
|
||||
{
|
||||
return new ScopeViewModel
|
||||
{
|
||||
Value = IdentityServer4.IdentityServerConstants.StandardScopes.OfflineAccess,
|
||||
DisplayName = ConsentOptions.OfflineAccessDisplayName,
|
||||
Description = ConsentOptions.OfflineAccessDescription,
|
||||
Emphasize = true,
|
||||
Checked = check
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
32
Models/Diagnostic/DiagnosticsViewModel.cs
Normal file
32
Models/Diagnostic/DiagnosticsViewModel.cs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
|
||||
|
||||
|
||||
using IdentityModel;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Newtonsoft.Json;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace IdentityServerHost.Quickstart.UI
|
||||
{
|
||||
public class DiagnosticsViewModel
|
||||
{
|
||||
public DiagnosticsViewModel(AuthenticateResult result)
|
||||
{
|
||||
AuthenticateResult = result;
|
||||
|
||||
if (result.Properties.Items.ContainsKey("client_list"))
|
||||
{
|
||||
var encoded = result.Properties.Items["client_list"];
|
||||
var bytes = Base64Url.Decode(encoded);
|
||||
var value = Encoding.UTF8.GetString(bytes);
|
||||
|
||||
Clients = JsonConvert.DeserializeObject<string[]>(value);
|
||||
}
|
||||
}
|
||||
|
||||
public AuthenticateResult AuthenticateResult { get; }
|
||||
public IEnumerable<string> Clients { get; } = new List<string>();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using IdentityServer4.Models;
|
||||
|
||||
namespace nuget_host.Models
|
||||
{
|
||||
|
|
@ -7,5 +8,18 @@ namespace nuget_host.Models
|
|||
public string RequestId { get; set; }
|
||||
|
||||
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
|
||||
|
||||
|
||||
public ErrorViewModel()
|
||||
{
|
||||
}
|
||||
|
||||
public ErrorViewModel(string error)
|
||||
{
|
||||
Error = new ErrorMessage { Error = error };
|
||||
}
|
||||
|
||||
public ErrorMessage Error { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
27
Models/Grants/GrantsViewModel.cs
Normal file
27
Models/Grants/GrantsViewModel.cs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
|
||||
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace IdentityServerHost.Quickstart.UI
|
||||
{
|
||||
public class GrantsViewModel
|
||||
{
|
||||
public IEnumerable<GrantViewModel> Grants { get; set; }
|
||||
}
|
||||
|
||||
public class GrantViewModel
|
||||
{
|
||||
public string ClientId { get; set; }
|
||||
public string ClientName { get; set; }
|
||||
public string ClientUrl { get; set; }
|
||||
public string ClientLogoUrl { get; set; }
|
||||
public string Description { get; set; }
|
||||
public DateTime Created { get; set; }
|
||||
public DateTime? Expires { get; set; }
|
||||
public IEnumerable<string> IdentityGrantNames { get; set; }
|
||||
public IEnumerable<string> ApiGrantNames { get; set; }
|
||||
}
|
||||
}
|
||||
56
Models/SecurityHeadersAttribute.cs
Normal file
56
Models/SecurityHeadersAttribute.cs
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
|
||||
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
|
||||
namespace IdentityServerHost.Quickstart.UI
|
||||
{
|
||||
public class SecurityHeadersAttribute : ActionFilterAttribute
|
||||
{
|
||||
public override void OnResultExecuting(ResultExecutingContext context)
|
||||
{
|
||||
var result = context.Result;
|
||||
if (result is ViewResult)
|
||||
{
|
||||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options
|
||||
if (!context.HttpContext.Response.Headers.ContainsKey("X-Content-Type-Options"))
|
||||
{
|
||||
context.HttpContext.Response.Headers.Add("X-Content-Type-Options", "nosniff");
|
||||
}
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options
|
||||
if (!context.HttpContext.Response.Headers.ContainsKey("X-Frame-Options"))
|
||||
{
|
||||
context.HttpContext.Response.Headers.Add("X-Frame-Options", "SAMEORIGIN");
|
||||
}
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy
|
||||
var csp = "default-src 'self'; object-src 'none'; frame-ancestors 'none'; sandbox allow-forms allow-same-origin allow-scripts; base-uri 'self';";
|
||||
// also consider adding upgrade-insecure-requests once you have HTTPS in place for production
|
||||
//csp += "upgrade-insecure-requests;";
|
||||
// also an example if you need client images to be displayed from twitter
|
||||
// csp += "img-src 'self' https://pbs.twimg.com;";
|
||||
|
||||
// once for standards compliant browsers
|
||||
if (!context.HttpContext.Response.Headers.ContainsKey("Content-Security-Policy"))
|
||||
{
|
||||
context.HttpContext.Response.Headers.Add("Content-Security-Policy", csp);
|
||||
}
|
||||
// and once again for IE
|
||||
if (!context.HttpContext.Response.Headers.ContainsKey("X-Content-Security-Policy"))
|
||||
{
|
||||
context.HttpContext.Response.Headers.Add("X-Content-Security-Policy", csp);
|
||||
}
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy
|
||||
var referrer_policy = "no-referrer";
|
||||
if (!context.HttpContext.Response.Headers.ContainsKey("Referrer-Policy"))
|
||||
{
|
||||
context.HttpContext.Response.Headers.Add("Referrer-Policy", referrer_policy);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
66
Models/TestUsers.cs
Normal file
66
Models/TestUsers.cs
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
|
||||
|
||||
|
||||
using IdentityModel;
|
||||
using IdentityServer4.Test;
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Claims;
|
||||
using IdentityServer4;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace IdentityServerHost.Quickstart.UI
|
||||
{
|
||||
public class TestUsers
|
||||
{
|
||||
public static List<TestUser> Users
|
||||
{
|
||||
get
|
||||
{
|
||||
var address = new
|
||||
{
|
||||
street_address = "One Hacker Way",
|
||||
locality = "Heidelberg",
|
||||
postal_code = 69118,
|
||||
country = "Germany"
|
||||
};
|
||||
|
||||
return new List<TestUser>
|
||||
{
|
||||
new TestUser
|
||||
{
|
||||
SubjectId = "818727",
|
||||
Username = "alice",
|
||||
Password = "alice",
|
||||
Claims =
|
||||
{
|
||||
new Claim(JwtClaimTypes.Name, "Alice Smith"),
|
||||
new Claim(JwtClaimTypes.GivenName, "Alice"),
|
||||
new Claim(JwtClaimTypes.FamilyName, "Smith"),
|
||||
new Claim(JwtClaimTypes.Email, "AliceSmith@email.com"),
|
||||
new Claim(JwtClaimTypes.EmailVerified, "true", ClaimValueTypes.Boolean),
|
||||
new Claim(JwtClaimTypes.WebSite, "http://alice.com"),
|
||||
new Claim(JwtClaimTypes.Address, JsonConvert.SerializeObject(address), IdentityServerConstants.ClaimValueTypes.Json)
|
||||
}
|
||||
},
|
||||
new TestUser
|
||||
{
|
||||
SubjectId = "88421113",
|
||||
Username = "bob",
|
||||
Password = "bob",
|
||||
Claims =
|
||||
{
|
||||
new Claim(JwtClaimTypes.Name, "Bob Smith"),
|
||||
new Claim(JwtClaimTypes.GivenName, "Bob"),
|
||||
new Claim(JwtClaimTypes.FamilyName, "Smith"),
|
||||
new Claim(JwtClaimTypes.Email, "BobSmith@email.com"),
|
||||
new Claim(JwtClaimTypes.EmailVerified, "true", ClaimValueTypes.Boolean),
|
||||
new Claim(JwtClaimTypes.WebSite, "http://bob.com"),
|
||||
new Claim(JwtClaimTypes.Address, JsonConvert.SerializeObject(address), IdentityServerConstants.ClaimValueTypes.Json)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue