Skip to content

Commit

Permalink
Merge pull request #1505 from DuendeSoftware/dom/update-par
Browse files Browse the repository at this point in the history
Remove DiscoveryCache from PAR sample
  • Loading branch information
josephdecock committed Jan 4, 2024
2 parents aa6dd24 + f1137d2 commit 4cf05a2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
22 changes: 8 additions & 14 deletions clients/src/MvcPar/ParOidcEvents.cs
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
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

0 comments on commit 4cf05a2

Please sign in to comment.