This repository has been archived by the owner on Nov 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for resource indicators (#173)
* update IdM * update signatures * update user store * Re-org samples * re-org samples 2 * miminmal working sample * add support for handlers per resource, fix storage
- Loading branch information
1 parent
ecd37ce
commit 19ec3e6
Showing
171 changed files
with
39,904 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net5.0</TargetFramework> | ||
<RootNamespace>TokenManagement5</RootNamespace> | ||
</PropertyGroup> | ||
|
||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="5.0.0" /> | ||
|
||
<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\IdentityModel.AspNetCore.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Newtonsoft.Json.Linq; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Authentication; | ||
|
||
namespace MvcCode.Controllers | ||
{ | ||
public class HomeController : Controller | ||
{ | ||
private readonly IHttpClientFactory _httpClientFactory; | ||
|
||
public HomeController(IHttpClientFactory httpClientFactory) | ||
{ | ||
_httpClientFactory = httpClientFactory; | ||
} | ||
|
||
[AllowAnonymous] | ||
public IActionResult Index() => View(); | ||
|
||
public IActionResult Secure() => View(); | ||
|
||
public IActionResult Logout() => SignOut("cookie", "oidc"); | ||
|
||
public async Task<IActionResult> CallApiAsUser() | ||
{ | ||
var client = _httpClientFactory.CreateClient("user_client"); | ||
|
||
var response = await client.GetStringAsync("test"); | ||
ViewBag.Json = JArray.Parse(response).ToString(); | ||
|
||
return View("CallApi"); | ||
} | ||
|
||
public async Task<IActionResult> CallApiAsUserResource3() | ||
{ | ||
var client = _httpClientFactory.CreateClient("user_client_resource3"); | ||
|
||
var response = await client.GetStringAsync("test"); | ||
ViewBag.Json = JArray.Parse(response).ToString(); | ||
|
||
return View("CallApi"); | ||
} | ||
|
||
public async Task<IActionResult> CallApiAsUserTyped([FromServices] TypedUserClient client) | ||
{ | ||
var response = await client.CallApi(); | ||
ViewBag.Json = JArray.Parse(response).ToString(); | ||
|
||
return View("CallApi"); | ||
} | ||
|
||
[AllowAnonymous] | ||
public async Task<IActionResult> CallApiAsClient() | ||
{ | ||
var client = _httpClientFactory.CreateClient("client"); | ||
|
||
var response = await client.GetStringAsync("test"); | ||
ViewBag.Json = JArray.Parse(response).ToString(); | ||
|
||
return View("CallApi"); | ||
} | ||
|
||
[AllowAnonymous] | ||
public async Task<IActionResult> CallApiAsClientTyped([FromServices] TypedClientClient client) | ||
{ | ||
var response = await client.CallApi(); | ||
ViewBag.Json = JArray.Parse(response).ToString(); | ||
|
||
return View("CallApi"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Hosting; | ||
using Serilog; | ||
using Serilog.Events; | ||
using Serilog.Sinks.SystemConsole.Themes; | ||
|
||
namespace MvcCode | ||
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
Log.Logger = new LoggerConfiguration() | ||
.MinimumLevel.Debug() | ||
.MinimumLevel.Override("System", LogEventLevel.Error) | ||
.MinimumLevel.Override("Microsoft", LogEventLevel.Error) | ||
.MinimumLevel.Override("System.Net.Http", LogEventLevel.Information) | ||
.MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information) | ||
.WriteTo.Console(theme: AnsiConsoleTheme.Code) | ||
.CreateLogger(); | ||
|
||
CreateHostBuilder(args).Build().Run(); | ||
} | ||
|
||
public static IHostBuilder CreateHostBuilder(string[] args) => | ||
Host.CreateDefaultBuilder(args) | ||
.UseSerilog() | ||
.ConfigureWebHostDefaults(webBuilder => | ||
{ | ||
webBuilder.UseStartup<Startup>(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"profiles": { | ||
"MvcCode": { | ||
"commandName": "Project", | ||
"launchBrowser": true, | ||
"applicationUrl": "https://localhost:5001;http://localhost:5000", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.IdentityModel.Tokens; | ||
using Polly; | ||
using System; | ||
using System.IdentityModel.Tokens.Jwt; | ||
using System.Threading.Tasks; | ||
|
||
namespace MvcCode | ||
{ | ||
public class Startup | ||
{ | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
JwtSecurityTokenHandler.DefaultMapInboundClaims = false; | ||
|
||
services.AddControllersWithViews(); | ||
|
||
services.AddAuthentication(options => | ||
{ | ||
options.DefaultScheme = "cookie"; | ||
options.DefaultChallengeScheme = "oidc"; | ||
}) | ||
.AddCookie("cookie", options => | ||
{ | ||
options.Cookie.Name = "mvccode"; | ||
|
||
options.Events.OnSigningOut = async e => | ||
{ | ||
await e.HttpContext.RevokeUserRefreshTokenAsync(); | ||
}; | ||
}) | ||
.AddOpenIdConnect("oidc", options => | ||
{ | ||
options.Authority = "https://demo.duendesoftware.com"; | ||
|
||
options.ClientId = "interactive.confidential.short"; | ||
options.ClientSecret = "secret"; | ||
|
||
options.ResponseType = "code"; | ||
options.ResponseMode = "query"; | ||
|
||
options.Scope.Clear(); | ||
options.Scope.Add("openid"); | ||
options.Scope.Add("profile"); | ||
options.Scope.Add("email"); | ||
options.Scope.Add("offline_access"); | ||
|
||
options.Scope.Add("resource1.scope1"); | ||
options.Scope.Add("resource2.scope1"); | ||
options.Scope.Add("resource3.scope1"); | ||
options.Scope.Add("scope3"); | ||
options.Scope.Add("scope4"); | ||
|
||
// keeps id_token smaller | ||
options.GetClaimsFromUserInfoEndpoint = true; | ||
options.SaveTokens = true; | ||
|
||
options.TokenValidationParameters = new TokenValidationParameters | ||
{ | ||
NameClaimType = "name", | ||
RoleClaimType = "role" | ||
}; | ||
|
||
options.Events.OnRedirectToIdentityProvider = e => | ||
{ | ||
// prepare token requests, so a resource specific token can be requested | ||
e.ProtocolMessage.Resource = "urn:resource3"; | ||
|
||
return Task.CompletedTask; | ||
}; | ||
}); | ||
|
||
// adds user and client access token management | ||
services.AddAccessTokenManagement(options => | ||
{ | ||
// ask for a token for a specific resource | ||
//options.Client.Resource = "urn:resource3"; | ||
|
||
// ask for a specific scope | ||
//options.Client.Scope = "shared.scope"; | ||
}) | ||
.ConfigureBackchannelHttpClient() | ||
.AddTransientHttpErrorPolicy(policy => policy.WaitAndRetryAsync(new[] | ||
{ | ||
TimeSpan.FromSeconds(1), | ||
TimeSpan.FromSeconds(2), | ||
TimeSpan.FromSeconds(3) | ||
})); | ||
|
||
// registers HTTP client that uses the managed user access token | ||
services.AddUserAccessTokenClient("user_client", configureClient: client => | ||
{ | ||
client.BaseAddress = new Uri("https://demo.duendesoftware.com/api/"); | ||
}); | ||
|
||
// registers HTTP client that uses the managed user access token for a specific resource | ||
services.AddUserAccessTokenClient("user_client_resource3", "urn:resource3", configureClient: client => | ||
{ | ||
client.BaseAddress = new Uri("https://demo.duendesoftware.com/api/"); | ||
}); | ||
|
||
// registers HTTP client that uses the managed client access token | ||
services.AddClientAccessTokenClient("client", configureClient: client => | ||
{ | ||
client.BaseAddress = new Uri("https://demo.duendesoftware.com/api/"); | ||
}); | ||
|
||
// registers a typed HTTP client with token management support | ||
services.AddHttpClient<TypedUserClient>(client => | ||
{ | ||
client.BaseAddress = new Uri("https://demo.duendesoftware.com/api/"); | ||
}) | ||
.AddUserAccessTokenHandler(); | ||
|
||
services.AddHttpClient<TypedClientClient>(client => | ||
{ | ||
client.BaseAddress = new Uri("https://demo.duendesoftware.com/api/"); | ||
}) | ||
.AddClientAccessTokenHandler(); | ||
} | ||
|
||
public void Configure(IApplicationBuilder app) | ||
{ | ||
app.UseDeveloperExceptionPage(); | ||
app.UseHttpsRedirection(); | ||
app.UseStaticFiles(); | ||
|
||
app.UseRouting(); | ||
|
||
app.UseAuthentication(); | ||
app.UseAuthorization(); | ||
|
||
app.UseEndpoints(endpoints => | ||
{ | ||
endpoints.MapDefaultControllerRoute() | ||
.RequireAuthorization(); | ||
}); | ||
} | ||
} | ||
} |
Oops, something went wrong.