Skip to content

Commit 72fb837

Browse files
Ticket #771 : Add some sample projects for the OPENID federation
1 parent a8e4827 commit 72fb837

35 files changed

Lines changed: 1455 additions & 1091 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Microsoft.AspNetCore.Authentication;
2+
using Microsoft.AspNetCore.Authorization;
3+
using Microsoft.AspNetCore.Mvc;
4+
using SimpleIdServer.Federation.Rp.Startup.ViewModels;
5+
6+
namespace SimpleIdServer.Federation.Rp.Startup.Controllers;
7+
8+
public class ClaimsController : Controller
9+
{
10+
[Authorize]
11+
public async Task<IActionResult> Index()
12+
{
13+
var accessToken = await HttpContext.GetTokenAsync("access_token");
14+
return View(new ClaimsViewModel
15+
{
16+
AccessToken = accessToken
17+
});
18+
}
19+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using SimpleIdServer.Federation.Rp.Startup.ViewModels;
3+
using System.Diagnostics;
4+
5+
namespace SimpleIdServer.Federation.Rp.Startup.Controllers
6+
{
7+
public class HomeController : Controller
8+
{
9+
private readonly ILogger<HomeController> _logger;
10+
11+
public HomeController(ILogger<HomeController> logger)
12+
{
13+
_logger = logger;
14+
}
15+
16+
public IActionResult Index()
17+
{
18+
return View();
19+
}
20+
21+
public IActionResult Privacy()
22+
{
23+
return View();
24+
}
25+
26+
public IActionResult Logout()
27+
{
28+
return SignOut(new[] { "sid", "Cookies" });
29+
}
30+
31+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
32+
public IActionResult Error()
33+
{
34+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
35+
}
36+
}
37+
}
Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,93 @@
1-
var builder = WebApplication.CreateBuilder(args);
1+
using Microsoft.IdentityModel.Tokens;
2+
using SimpleIdServer.Federation.Rp.Startup;
3+
using SimpleIdServer.IdServer.Domains;
4+
using SimpleIdServer.OpenidFederation.Store.EF;
5+
using System.Security.Cryptography;
6+
7+
var builder = WebApplication.CreateBuilder(args);
8+
var signatureCredentials = new SigningCredentials(new RsaSecurityKey(RSA.Create()) { KeyId = "raId" }, SecurityAlgorithms.RsaSha256);
9+
10+
var jsonWebKey = signatureCredentials.SerializePublicJWK();
11+
jsonWebKey.Alg = SecurityAlgorithms.RsaSha256;
12+
jsonWebKey.Use = "sig";
13+
14+
builder.Services.AddDistributedMemoryCache();
215
builder.Services.AddRpFederation(r =>
316
{
417
r.Client = new SimpleIdServer.IdServer.Domains.Client
518
{
6-
19+
ClientId = "http://localhost:7001",
20+
RedirectionUrls = new List<string>
21+
{
22+
"http://localhost:7001/signin-oidc"
23+
},
24+
ClientRegistrationTypesSupported = new List<string>
25+
{
26+
"automatic"
27+
},
28+
RequestObjectSigningAlg = SecurityAlgorithms.RsaSha256,
29+
Scopes = new List<Scope>
30+
{
31+
new Scope
32+
{
33+
Name = "openid"
34+
},
35+
new Scope
36+
{
37+
Name = "profile"
38+
}
39+
},
40+
ResponseTypes = new List<string>
41+
{
42+
"code"
43+
},
44+
GrantTypes = new List<string>
45+
{
46+
"authorization_code"
47+
},
48+
TokenEndPointAuthMethod = "private_key_jwt"
749
};
8-
});
50+
r.Client.Add(jsonWebKey.Kid, jsonWebKey, "sig", SecurityKeyTypes.RSA);
51+
r.SigningCredentials = signatureCredentials;
52+
});
53+
builder.Services.AddOpenidFederationStore();
54+
builder.Services.AddControllersWithViews();
55+
builder.Services.AddAuthentication(options =>
56+
{
57+
options.DefaultScheme = "Cookies";
58+
options.DefaultChallengeScheme = "sid";
59+
})
60+
.AddCookie("Cookies")
61+
.AddCustomOpenIdConnect("sid", options =>
62+
{
63+
64+
options.SignInScheme = "Cookies";
65+
options.ResponseType = "code";
66+
options.Authority = "https://localhost:5001/master";
67+
options.RequireHttpsMetadata = false;
68+
options.ClientId = "http://localhost:7001";
69+
options.GetClaimsFromUserInfoEndpoint = true;
70+
options.SaveTokens = true;
71+
options.UseFederationAutomaticRegistration(signatureCredentials);
72+
});
73+
74+
var app = builder.Build();
75+
AddTrustedEntities(app.Services);
76+
app.UseHttpsRedirection();
77+
app.UseStaticFiles();
78+
app.UseRouting();
79+
app.UseAuthorization();
80+
app.MapControllerRoute(
81+
name: "default",
82+
pattern: "{controller=Home}/{action=Index}/{id?}");
83+
84+
app.Run();
85+
static void AddTrustedEntities(IServiceProvider services)
86+
{
87+
using (var scope = services.CreateScope())
88+
{
89+
var dbContext = scope.ServiceProvider.GetRequiredService<OpenidFederationDbContext>();
90+
dbContext.FederationEntities.AddRange(RpConfiguration.FederationEntities);
91+
dbContext.SaveChanges();
92+
}
93+
}

src/Federation/SimpleIdServer.Federation.Rp.Startup/Properties/launchSettings.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
"profiles": {
33
"SimpleIdServer.Federation.Rp.Startup": {
44
"commandName": "Project",
5-
"launchBrowser": true,
65
"environmentVariables": {
76
"ASPNETCORE_ENVIRONMENT": "Development"
87
},
9-
"applicationUrl": "https://localhost:5425;http://localhost:5426"
8+
"applicationUrl": "http://localhost:7001"
109
}
1110
}
1211
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using SimpleIdServer.OpenidFederation.Domains;
2+
3+
namespace SimpleIdServer.Federation.Rp.Startup;
4+
5+
public class RpConfiguration
6+
{
7+
public static List<FederationEntity> FederationEntities = new List<FederationEntity>
8+
{
9+
new FederationEntity
10+
{
11+
Id = Guid.NewGuid().ToString(),
12+
Sub = "http://localhost:7000",
13+
Realm = string.Empty,
14+
IsSubordinate = false
15+
}
16+
};
17+
}

src/Federation/SimpleIdServer.Federation.Rp.Startup/SimpleIdServer.Federation.Rp.Startup.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
<ImplicitUsings>enable</ImplicitUsings>
66
</PropertyGroup>
77
<ItemGroup>
8+
<ProjectReference Include="..\..\IdServer\SimpleIdServer.OpenIdConnect\SimpleIdServer.OpenIdConnect.csproj" />
9+
<ProjectReference Include="..\..\IdServer\SimpleIdServer.OpenidFederation.Store.EF\SimpleIdServer.OpenidFederation.Store.EF.csproj" />
810
<ProjectReference Include="..\..\IdServer\SimpleIdServer.Rp.Federation\SimpleIdServer.Rp.Federation.csproj" />
911
</ItemGroup>
1012
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace SimpleIdServer.Federation.Rp.Startup.ViewModels;
2+
3+
public class ClaimsViewModel
4+
{
5+
public string AccessToken { get; set; }
6+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace SimpleIdServer.Federation.Rp.Startup.ViewModels;
2+
3+
public class ErrorViewModel
4+
{
5+
public string? RequestId { get; set; }
6+
7+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
8+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@model SimpleIdServer.Federation.Rp.Startup.ViewModels.ClaimsViewModel
2+
3+
<ul>
4+
@foreach (var claim in User.Claims)
5+
{
6+
<li>@claim.Type : @claim.Value</li>
7+
}
8+
</ul>
9+
10+
<div>
11+
<h3>Access token</h3>
12+
<p>@Model.AccessToken</p>
13+
</div>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<ul>
2+
@foreach (var claim in User.Claims)
3+
{
4+
<li>@claim.Type : @claim.Value</li>
5+
}
6+
</ul>

0 commit comments

Comments
 (0)