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

Remove DiscoveryCache from PAR sample #1505

Merged
merged 1 commit into from
Jan 4, 2024
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
22 changes: 8 additions & 14 deletions clients/src/MvcPar/ParOidcEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Net.Http.Json;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using IdentityModel.Client;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
Expand All @@ -11,12 +12,8 @@

namespace MvcPar
{
public class ParOidcEvents(HttpClient httpClient, IDiscoveryCache discoveryCache, ILogger<ParOidcEvents> logger) : OpenIdConnectEvents
public class ParOidcEvents(HttpClient httpClient, ILogger<ParOidcEvents> logger) : OpenIdConnectEvents
{
private readonly HttpClient _httpClient = httpClient;
private readonly IDiscoveryCache _discoveryCache = discoveryCache;
private readonly ILogger<ParOidcEvents> _logger = logger;

public override async Task RedirectToIdentityProvider(RedirectContext context)
{
var clientId = context.ProtocolMessage.ClientId;
Expand Down Expand Up @@ -59,7 +56,7 @@ private async Task RedirectToAuthorizeEndpoint(RedirectContext context, OpenIdCo
var redirectUri = message.CreateAuthenticationRequestUrl();
if (!Uri.IsWellFormedUriString(redirectUri, UriKind.Absolute))
{
_logger.LogWarning("The redirect URI is not well-formed. The URI is: '{AuthenticationRequestUrl}'.", redirectUri);
logger.LogWarning("The redirect URI is not well-formed. The URI is: '{AuthenticationRequestUrl}'.", redirectUri);
}

context.Response.Redirect(redirectUri);
Expand Down Expand Up @@ -89,15 +86,12 @@ private async Task<ParResponse> PushAuthorizationParameters(RedirectContext cont
{
// Send our PAR request
var requestBody = new FormUrlEncodedContent(context.ProtocolMessage.Parameters);
_httpClient.SetBasicAuthentication(clientId, "secret");
httpClient.SetBasicAuthentication(clientId, "secret");

var disco = await _discoveryCache.GetAsync();
if (disco.IsError)
{
throw new Exception(disco.Error);
}
var parEndpoint = disco.TryGetValue("pushed_authorization_request_endpoint").GetString();
var response = await _httpClient.PostAsync(parEndpoint, requestBody);
var config = await context.Options.ConfigurationManager!.GetConfigurationAsync(context.HttpContext.RequestAborted);
var parEndpoint = config.AdditionalData["pushed_authorization_request_endpoint"].ToString();

var response = await httpClient.PostAsync(parEndpoint, requestBody);
if (!response.IsSuccessStatusCode)
{
throw new Exception("PAR failure");
Expand Down
3 changes: 1 addition & 2 deletions clients/src/MvcPar/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public Startup(IConfiguration configuration)
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<ParOidcEvents>();
services.AddSingleton<IDiscoveryCache>(_ => new DiscoveryCache(Constants.Authority));


// add MVC
services.AddControllersWithViews();

Expand Down