-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathIdentityServerHost.cs
More file actions
122 lines (100 loc) · 4.07 KB
/
IdentityServerHost.cs
File metadata and controls
122 lines (100 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (c) Duende Software. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
using Duende.IdentityServer.Models;
using Duende.IdentityServer.Services;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Security.Claims;
using Microsoft.IdentityModel.JsonWebTokens;
using Microsoft.IdentityModel.Tokens;
namespace Duende.AccessTokenManagement.Tests;
public class IdentityServerHost : GenericHost
{
public IdentityServerHost(WriteTestOutput writeTestOutput, string baseAddress = "https://identityserver")
: base(writeTestOutput, baseAddress)
{
OnConfigureServices += ConfigureServices;
OnConfigure += Configure;
}
public List<Client> Clients { get; set; } = new List<Client>();
public List<IdentityResource> IdentityResources { get; set; } = new List<IdentityResource>()
{
new IdentityResources.OpenId(),
new IdentityResources.Profile(),
new IdentityResources.Email(),
};
public List<ApiScope> ApiScopes { get; set; } = new();
public List<ApiResource> ApiResources { get; set; } = new()
{
new ApiResource("urn:api1"),
new ApiResource("urn:api2")
};
private void ConfigureServices(IServiceCollection services)
{
services.AddRouting();
services.AddAuthorization();
services.AddLogging(logging => {
logging.AddFilter("Duende", LogLevel.Debug);
});
services.AddIdentityServer(options=>
{
options.EmitStaticAudienceClaim = true;
// Artificially low durations to force retries
options.DPoP.ServerClockSkew = TimeSpan.Zero;
options.DPoP.ProofTokenValidityDuration = TimeSpan.FromSeconds(1);
// Disable PAR (this keeps test setup simple, and we don't need to integration test PAR here - it is covered by IdentityServer itself)
options.Endpoints.EnablePushedAuthorizationEndpoint = false;
})
.AddInMemoryClients(Clients)
.AddInMemoryIdentityResources(IdentityResources)
.AddInMemoryApiResources(ApiResources)
.AddInMemoryApiScopes(ApiScopes);
}
private void Configure(IApplicationBuilder app)
{
app.UseRouting();
app.UseIdentityServer();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/account/login", context =>
{
return Task.CompletedTask;
});
endpoints.MapGet("/account/logout", async context =>
{
// signout as if the user were prompted
await context.SignOutAsync();
var logoutId = context.Request.Query["logoutId"];
var interaction = context.RequestServices.GetRequiredService<IIdentityServerInteractionService>();
var signOutContext = await interaction.GetLogoutContextAsync(logoutId);
context.Response.Redirect(signOutContext.PostLogoutRedirectUri ?? "/");
});
});
}
public async Task CreateIdentityServerSessionCookieAsync(string sub, string? sid = null)
{
var props = new AuthenticationProperties();
if (!String.IsNullOrWhiteSpace(sid))
{
props.Items.Add("session_id", sid);
}
await IssueSessionCookieAsync(props, new Claim("sub", sub));
}
public string CreateIdToken(string sub, string clientId)
{
var descriptor = new SecurityTokenDescriptor
{
Issuer = BaseAddress,
Audience = clientId,
Claims = new Dictionary<string, object>
{
{ "sub", sub }
}
};
var handler = new JsonWebTokenHandler();
return handler.CreateToken(descriptor);
}
}