Skip to content
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
7 changes: 6 additions & 1 deletion admin/TwoWeeksReady.Admin/Pages/HazardHunts/List.razor
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ else
protected override async Task OnInitializedAsync()
{

_HazardHunts = await Repository.GetAllHazardHunts();
try {
_HazardHunts = await Repository.GetAllHazardHunts();
} catch (NotImplementedException) {
// Application is still growing, let's not throw an error yet
_HazardHunts = Enumerable.Empty<HazardHunt>();
}

}

Expand Down
1 change: 1 addition & 0 deletions admin/TwoWeeksReady.Admin/Pages/HazardInfos/List.razor
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ else if (_HazardInfos == null)
else
{
<p>No hazard infos defined</p>
<a class="btn btn-primary" href="/HazardInfos/new">Add New Hazard</a>
}

@code {
Expand Down
8 changes: 7 additions & 1 deletion admin/TwoWeeksReady.Admin/Pages/Kits/List.razor
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ else
protected override async Task OnInitializedAsync()
{

BaseKits = await Repository.GetAllBaseKits();
try {
BaseKits = await Repository.GetAllBaseKits();
} catch (NotImplementedException)
{
// do nothing for now... site is still growing
BaseKits = Enumerable.Empty<BaseKit>();
}

}

Expand Down
54 changes: 30 additions & 24 deletions admin/TwoWeeksReady.Admin/Program.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace TwoWeeksReady.Admin
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using TwoWeeksReady.Admin;

var builder = WebApplication.CreateBuilder(args);

// Configure the container.
builder.Services.ConfigureServices(builder.Configuration);

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseCookiePolicy();
app.UseAuthentication();
app.UseAuthorization();

app.MapBlazorHub();
app.MapFallbackToPage("/_Host");

app.Run();
181 changes: 72 additions & 109 deletions admin/TwoWeeksReady.Admin/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,137 +15,100 @@
using System;
using System.Net.Http;

namespace TwoWeeksReady.Admin
namespace TwoWeeksReady.Admin;

public static class StartupExtensions
{
public class Startup
public static IServiceCollection ConfigureServices(this IServiceCollection services, IConfiguration configuration)
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }
services.AddRazorPages();
services.AddServerSideBlazor();

// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
services.Configure<CookiePolicyOptions>(options =>
{
services.AddRazorPages();
services.AddServerSideBlazor();
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});

services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
// Add authentication services
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect("Auth0", options =>
{
options.Authority = $"https://{configuration["Auth0:Domain"]}";

// Add authentication services
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect("Auth0", options =>
{
options.Authority = $"https://{Configuration["Auth0:Domain"]}";
options.ClientId = configuration["Auth0:ClientId"];
options.ClientSecret = configuration["Auth0:ClientSecret"];

options.ClientId = Configuration["Auth0:ClientId"];
options.ClientSecret = Configuration["Auth0:ClientSecret"];
options.ResponseType = OpenIdConnectResponseType.Code;
options.SaveTokens = true;

options.ResponseType = OpenIdConnectResponseType.Code;
options.SaveTokens = true;
options.Scope.Clear();
options.Scope.Add("openid");
options.Scope.Add("profile");

options.Scope.Clear();
options.Scope.Add("openid");
options.Scope.Add("profile");
options.CallbackPath = new PathString("/callback");
options.ClaimsIssuer = "Auth0";

options.CallbackPath = new PathString("/callback");
options.ClaimsIssuer = "Auth0";
options.TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name",
RoleClaimType = "https://schemas.2wradmin.com/roles"
};

options.TokenValidationParameters = new TokenValidationParameters
options.Events = new OpenIdConnectEvents
{
OnRedirectToIdentityProvider = context =>
{
NameClaimType = "name",
RoleClaimType = "https://schemas.2wradmin.com/roles"
};

options.Events = new OpenIdConnectEvents
// The context's ProtocolMessage can be used to pass along additional query parameters
// to Auth0's /authorize endpoint.
//
// Set the audience query parameter to the API identifier to ensure the returned Access Tokens can be used
// to call protected endpoints on the corresponding API.
context.ProtocolMessage.SetParameter("audience", configuration["Auth0:Audience"]);

return Task.FromResult(0);
},
OnRedirectToIdentityProviderForSignOut = (context) =>
{
OnRedirectToIdentityProvider = context =>
{
// The context's ProtocolMessage can be used to pass along additional query parameters
// to Auth0's /authorize endpoint.
//
// Set the audience query parameter to the API identifier to ensure the returned Access Tokens can be used
// to call protected endpoints on the corresponding API.
context.ProtocolMessage.SetParameter("audience", Configuration["Auth0:Audience"]);

return Task.FromResult(0);
},
OnRedirectToIdentityProviderForSignOut = (context) =>
{
var logoutUri = $"https://{Configuration["Auth0:Domain"]}/v2/logout?client_id={Configuration["Auth0:ClientId"]}";
var logoutUri = $"https://{configuration["Auth0:Domain"]}/v2/logout?client_id={configuration ["Auth0:ClientId"]}";

var postLogoutUri = context.Properties.RedirectUri;
var postLogoutUri = context.Properties.RedirectUri;

if (!string.IsNullOrEmpty(postLogoutUri))
if (!string.IsNullOrEmpty(postLogoutUri))
{
if (postLogoutUri.StartsWith("/"))
{
if (postLogoutUri.StartsWith("/"))
{
var request = context.Request;
postLogoutUri = request.Scheme + "://" + request.Host + request.PathBase + postLogoutUri;
}
var request = context.Request;
postLogoutUri = request.Scheme + "://" + request.Host + request.PathBase + postLogoutUri;
}
}

context.Response.Redirect(logoutUri);
context.HandleResponse();

return Task.CompletedTask;
}
};
});
context.Response.Redirect(logoutUri);
context.HandleResponse();

services.AddHttpContextAccessor();
services.AddHttpClient("ApiClient", (HttpClient client) =>
{
client.BaseAddress = new Uri(Configuration["ApiUrl"]);
});

services.AddScoped<TokenProvider>();
//services.AddScoped<IRepository, StubRepository>();
services.AddScoped<IRepository, FunctionsRepository>();
services.AddSingleton<ClientImageService>();
}
return Task.CompletedTask;
}
};
});

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
services.AddHttpContextAccessor();
services.AddHttpClient("ApiClient", (HttpClient client) =>
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();
client.BaseAddress = new Uri(configuration["ApiUrl"]);
});

app.UseCookiePolicy();
app.UseAuthentication();
app.UseAuthorization();
services.AddScoped<TokenProvider>();
//services.AddScoped<IRepository, StubRepository>();
services.AddScoped<IRepository, FunctionsRepository>();
services.AddSingleton<ClientImageService>();

app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
return services;
}

}
5 changes: 3 additions & 2 deletions admin/TwoWeeksReady.Admin/TwoWeeksReady.Admin.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<UserSecretsId>4c82d094-3afe-4274-922b-9c0d8bdda7c5</UserSecretsId>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
Expand All @@ -16,7 +17,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.6" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="6.0.2" />
<PackageReference Include="TinyMCE.Blazor" Version="0.0.8" />
</ItemGroup>

Expand Down