Skip to content

Commit f5f0cd2

Browse files
Fix OAuth Callbacks (#734)
1 parent 785c0b3 commit f5f0cd2

2 files changed

Lines changed: 28 additions & 24 deletions

File tree

EssentialCSharp.Web/Controllers/HomeController.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ namespace EssentialCSharp.Web.Controllers;
99

1010
public class HomeController(ILogger<HomeController> logger, IWebHostEnvironment hostingEnvironment, ISiteMappingService siteMappingService, IHttpContextAccessor httpContextAccessor) : Controller
1111
{
12-
public IActionResult Index(string key)
12+
public IActionResult Index()
1313
{
14+
string? key = Request.Path.Value?.TrimStart('/');
15+
1416
// if no key (default case), then load up home page
1517
SiteMapping? siteMapping = siteMappingService.SiteMappings.Find(key);
1618

EssentialCSharp.Web/Program.cs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,20 @@ public partial class Program
1717
{
1818
private static void Main(string[] args)
1919
{
20-
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
20+
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
21+
22+
builder.Services.Configure<ForwardedHeadersOptions>(options =>
23+
{
24+
options.ForwardedHeaders =
25+
ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
26+
27+
// Only loopback proxies are allowed by default.
28+
// Clear that restriction because forwarders are enabled by explicit
29+
// configuration.
30+
options.KnownNetworks.Clear();
31+
options.KnownProxies.Clear();
32+
});
33+
2134
ConfigurationManager configuration = builder.Configuration;
2235
string connectionString = builder.Configuration.GetConnectionString("EssentialCSharpWebContextConnection") ?? throw new InvalidOperationException("Connection string 'EssentialCSharpWebContextConnection' not found.");
2336

@@ -126,38 +139,35 @@ private static void Main(string[] args)
126139
{
127140
microsoftoptions.ClientId = configuration["authentication:microsoft:clientid"] ?? throw new InvalidOperationException("authentication:microsoft:clientid unexpectedly null");
128141
microsoftoptions.ClientSecret = configuration["authentication:microsoft:clientsecret"] ?? throw new InvalidOperationException("authentication:microsoft:clientsecret unexpectedly null");
129-
microsoftoptions.CallbackPath = "/signin-microsoft";
130142
})
131143
.AddGitHub(o =>
132144
{
133145
o.ClientId = configuration["authentication:github:clientId"] ?? throw new InvalidOperationException("github:clientId unexpectedly null");
134146
o.ClientSecret = configuration["authentication:github:clientSecret"] ?? throw new InvalidOperationException("github:clientSecret unexpectedly null");
135-
o.CallbackPath = "/signin-github";
136147

137148
// Grants access to read a user's profile data.
138149
// https://docs.github.com/en/developers/apps/building-oauth-apps/scopes-for-oauth-apps
139150
o.Scope.Add("read:user");
140151
});
141152
}
142153

143-
builder.Services.Configure<ForwardedHeadersOptions>(options =>
144-
{
145-
options.ForwardedHeaders =
146-
ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
147-
});
148-
149-
WebApplication app = builder.Build();
150154

151-
app.UseForwardedHeaders();
152155

156+
WebApplication app = builder.Build();
153157
// Configure the HTTP request pipeline.
154158
if (!app.Environment.IsDevelopment())
155159
{
156160
app.UseExceptionHandler("/Error");
161+
app.UseForwardedHeaders();
157162
app.UseHsts();
158163
app.UseSecurityHeadersMiddleware(new SecurityHeadersBuilder()
159164
.AddDefaultSecurePolicy());
160165
}
166+
else
167+
{
168+
app.UseDeveloperExceptionPage();
169+
app.UseForwardedHeaders();
170+
}
161171

162172
app.MapHealthChecks("/healthz");
163173

@@ -169,20 +179,12 @@ private static void Main(string[] args)
169179
app.UseAuthentication();
170180
app.UseAuthorization();
171181
app.UseMiddleware<ReferralMiddleware>();
172-
173-
app.Use((context, next) =>
174-
{
175-
context.Request.Scheme = "https";
176-
return next(context);
177-
});
178182

179-
app.MapDefaultControllerRoute();
180-
app.MapRazorPages();
181183

182-
app.MapControllerRoute(
183-
name: "slug",
184-
pattern: "{*key}",
185-
defaults: new { controller = "Home", action = "Index" });
184+
app.MapRazorPages();
185+
app.MapDefaultControllerRoute();
186+
187+
app.MapFallbackToController("Index", "Home");
186188

187189
app.Run();
188190
}

0 commit comments

Comments
 (0)