Skip to content
Closed
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ riderModule.iml
/*.sln.DotSettings.user

*/appsettings.Secret.yml
*/tempkey.jwk
*/tempkey.jwk
/is4-rsa.pem
20 changes: 19 additions & 1 deletion OAuthTest/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
Expand All @@ -25,9 +26,26 @@ public IActionResult Index()
return View();
}

[HttpPost]
public IActionResult LogoutLocal()
{
return SignOut(new AuthenticationProperties {RedirectUri = Url.Action(nameof(LoggedOut))}, "Cookies");
}

[HttpPost]
public IActionResult LogoutProvider()
{
return SignOut(new AuthenticationProperties {RedirectUri = Url.Action(nameof(LoggedOut))}, "Cookies", "oidc");
}

public IActionResult LoggedOut()
{
return View();
}

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel {RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier});
}
}
}
5 changes: 3 additions & 2 deletions OAuthTest/OAuthTest.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="6.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="10.0.0" />
<PackageReference Include="NetEscapades.Configuration.Yaml" Version="2.1.0" />
</ItemGroup>
</Project>
14 changes: 8 additions & 6 deletions OAuthTest/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
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 OAuthTest;

Expand All @@ -18,5 +13,12 @@ public static void Main(string[] args)

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, builder) =>
{
var env = context.HostingEnvironment;
builder.AddYamlFile("appsettings.yml", false, true);
builder.AddYamlFile($"appsettings.{env.EnvironmentName}.yml", true, true);
builder.AddYamlFile("appsettings.Secret.yml", true, true);
})
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
}
}
5 changes: 2 additions & 3 deletions OAuthTest/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
{
"profiles": {
"OAuthTest": {
"commandName": "Project",
"dotnetRunMessages": "true",
"applicationUrl": "https://localhost:5002",
"dotnetRunMessages": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
Expand Down
66 changes: 44 additions & 22 deletions OAuthTest/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using System.IdentityModel.Tokens.Jwt;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpLogging;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;

namespace OAuthTest;

Expand All @@ -21,13 +21,14 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpLogging(logging =>
{
logging.LoggingFields = HttpLoggingFields.All;
//Write your code to configure the HttpLogging middleware here
});
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

var settings = Configuration.GetSection("OAuthTest").Get<OAuthTestSettings>()
?? throw new InvalidOperationException("Missing OAuthTest config section");

services.AddHttpLogging(logging => { logging.LoggingFields = settings.HttpLoggingFields; });

if (!settings.MapInboundClaims)
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

services.AddControllersWithViews();

services.AddAuthentication(options =>
Expand All @@ -39,19 +40,22 @@ public void ConfigureServices(IServiceCollection services)
.AddOpenIdConnect("oidc", options =>
{
options.SignInScheme = "Cookies";

options.Authority = "https://localhost:5003";
options.ClientId = "A";
options.ClientSecret = "A";

options.GetClaimsFromUserInfoEndpoint = true;
options.ResponseType = OpenIdConnectResponseType.Code;
//options.SaveTokens = true;

options.Scope.Add("profile");
options.Scope.Add("email");
options.ResponseType = "code";
options.UsePkce = true;

options.Authority = settings.Authority;
options.ClientId = settings.ClientId;
options.ClientSecret = settings.ClientSecret;

options.GetClaimsFromUserInfoEndpoint = settings.GetClaimsFromUserInfoEndpoint;
options.ResponseType = settings.ResponseType;
options.SaveTokens = settings.SaveTokens;
options.UsePkce = settings.UsePkce;
options.RequireHttpsMetadata = settings.RequireHttpsMetadata;
options.MapInboundClaims = settings.MapInboundClaims;

foreach (var scope in settings.Scopes)
{
options.Scope.Add(scope);
}
});
}

Expand Down Expand Up @@ -85,4 +89,22 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}

/// <summary>
/// Bound from the "OAuthTest" section of appsettings
/// </summary>
public sealed class OAuthTestSettings
{
public string Authority { get; set; } = "";
public string ClientId { get; set; } = "";
public string ClientSecret { get; set; } = "";
public string ResponseType { get; set; } = "code";
public bool UsePkce { get; set; } = true;
public bool GetClaimsFromUserInfoEndpoint { get; set; } = true;
public bool SaveTokens { get; set; }
public bool RequireHttpsMetadata { get; set; } = true;
public bool MapInboundClaims { get; set; }
public HttpLoggingFields HttpLoggingFields { get; set; } = HttpLoggingFields.All;
public string[] Scopes { get; set; } = Array.Empty<string>();
}
34 changes: 30 additions & 4 deletions OAuthTest/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
@using Microsoft.AspNetCore.Authentication
@using Microsoft.AspNetCore.Authentication
@{
ViewData["Title"] = "Home Page";

var tfa = User.FindFirst("two_factor_enabled")?.Value;
var amr = User.FindAll("amr").Select(c => c.Value).ToArray();
}

<div class="text-center">
<form method="post" style="display: inline">
<button type="submit" formaction="@Url.Action("LogoutLocal")">Log out (this app only)</button>
<button type="submit" formaction="@Url.Action("LogoutProvider")">Log out (also at provider)</button>
</form>

<h2>Security scope</h2>

<dl>
<dt>two_factor_enabled</dt>
<dd>
@if (tfa == null)
{
<span class="text-danger">not present</span>
}
else
{
<strong>@tfa</strong>
}
</dd>
<dt>amr</dt>
<dd>@(amr.Length == 0 ? "not present" : string.Join(", ", amr))</dd>
</dl>

<h2>Claims</h2>

<dl>
Expand All @@ -13,14 +39,14 @@
<dd>@claim.Value</dd>
}
</dl>

<h2>Properties</h2>

<dl>
@foreach (var prop in (await Context.AuthenticateAsync()).Properties.Items)
{
<dt>@prop.Key</dt>
<dd>@prop.Value</dd>
}
</dl>
</div>
</div>
8 changes: 8 additions & 0 deletions OAuthTest/Views/Home/LoggedOut.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@{
ViewData["Title"] = "Logged out";
}

<div class="text-center">
<h2>Logged out</h2>
<p><a href="@Url.Action("Index")">Log in again</a></p>
</div>
9 changes: 0 additions & 9 deletions OAuthTest/appsettings.Development.json

This file was deleted.

10 changes: 0 additions & 10 deletions OAuthTest/appsettings.json

This file was deleted.

28 changes: 28 additions & 0 deletions OAuthTest/appsettings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Logging:
LogLevel:
Default: Information
Microsoft: Information
Microsoft.Hosting.Lifetime: Information

AllowedHosts: "*"

# Address this test app listens on (the OAuth callback is <this>/signin-oidc).
Urls: https://localhost:5002

OAuthTest:
Authority: https://localhost:5003
ClientId: A
ClientSecret: A
ResponseType: code
UsePkce: true
GetClaimsFromUserInfoEndpoint: true
SaveTokens: false
RequireHttpsMetadata: true
# false = keep claim types exactly as sent by the server (no mapping to ClaimTypes.*).
MapInboundClaims: false
# Dump full HTTP request/response logging. One of the HttpLoggingFields values.
HttpLoggingFields: All
Scopes:
- profile
- email
- security
6 changes: 6 additions & 0 deletions SS14.Auth.Shared/Data/AuthHash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ public class AuthHash
public long? HwidId { get; set; }
[CanBeNull] public Hwid Hwid { get; set; }

/// <summary>
/// If true, the client granted the server it is connecting to permission to see security info about the user,
/// such as whether 2FA is enabled. Only released through hasJoined for this single connection attempt.
/// </summary>
public bool RequestSecurity { get; set; }

public DateTimeOffset Expires { get; set; }

[Required]
Expand Down
Loading
Loading