1
1
using Ocelot . DependencyInjection ;
2
2
using IdentityServer4 . AccessTokenValidation ;
3
3
using IdentityServer4 . Models ;
4
- using Microsoft . AspNetCore . Builder ;
5
4
using Microsoft . Extensions . Configuration ;
6
5
using Microsoft . Extensions . DependencyInjection ;
7
6
using Microsoft . Extensions . DependencyInjection . Extensions ;
10
9
using System . Collections . Generic ;
11
10
using System . IdentityModel . Tokens . Jwt ;
12
11
using System . Security . Cryptography . X509Certificates ;
12
+ using System . Linq ;
13
+ using Microsoft . IdentityModel . Tokens ;
14
+ using Microsoft . AspNetCore . Authentication . JwtBearer ;
13
15
14
16
namespace Ocelot . Administration
15
17
{
@@ -18,6 +20,7 @@ public static class OcelotBuilderExtensions
18
20
public static IOcelotAdministrationBuilder AddAdministration ( this IOcelotBuilder builder , string path , string secret )
19
21
{
20
22
var administrationPath = new AdministrationPath ( path ) ;
23
+
21
24
builder . Services . AddSingleton < OcelotMiddlewareConfigurationDelegate > ( IdentityServerMiddlewareConfigurationProvider . Get ) ;
22
25
23
26
//add identity server for admin area
@@ -32,7 +35,7 @@ public static IOcelotAdministrationBuilder AddAdministration(this IOcelotBuilder
32
35
return new OcelotAdministrationBuilder ( builder . Services , builder . Configuration ) ;
33
36
}
34
37
35
- public static IOcelotAdministrationBuilder AddAdministration ( this IOcelotBuilder builder , string path , Action < IdentityServerAuthenticationOptions > configureOptions )
38
+ public static IOcelotAdministrationBuilder AddAdministration ( this IOcelotBuilder builder , string path , Action < JwtBearerOptions > configureOptions )
36
39
{
37
40
var administrationPath = new AdministrationPath ( path ) ;
38
41
builder . Services . AddSingleton < OcelotMiddlewareConfigurationDelegate > ( IdentityServerMiddlewareConfigurationProvider . Get ) ;
@@ -46,11 +49,11 @@ public static IOcelotAdministrationBuilder AddAdministration(this IOcelotBuilder
46
49
return new OcelotAdministrationBuilder ( builder . Services , builder . Configuration ) ;
47
50
}
48
51
49
- private static void AddIdentityServer ( Action < IdentityServerAuthenticationOptions > configOptions , IOcelotBuilder builder )
52
+ private static void AddIdentityServer ( Action < JwtBearerOptions > configOptions , IOcelotBuilder builder )
50
53
{
51
54
builder . Services
52
55
. AddAuthentication ( IdentityServerAuthenticationDefaults . AuthenticationScheme )
53
- . AddIdentityServerAuthentication ( configOptions ) ;
56
+ . AddJwtBearer ( "Bearer" , configOptions ) ;
54
57
}
55
58
56
59
private static void AddIdentityServer ( IIdentityServerConfiguration identityServerConfiguration , IAdministrationPath adminPath , IOcelotBuilder builder , IConfiguration configuration )
@@ -60,22 +63,27 @@ private static void AddIdentityServer(IIdentityServerConfiguration identityServe
60
63
. AddIdentityServer ( o =>
61
64
{
62
65
o . IssuerUri = "Ocelot" ;
66
+ o . EmitStaticAudienceClaim = true ;
63
67
} )
68
+ . AddInMemoryApiScopes ( ApiScopes ( identityServerConfiguration ) )
64
69
. AddInMemoryApiResources ( Resources ( identityServerConfiguration ) )
65
70
. AddInMemoryClients ( Client ( identityServerConfiguration ) ) ;
66
71
67
72
var urlFinder = new BaseUrlFinder ( configuration ) ;
68
73
var baseSchemeUrlAndPort = urlFinder . Find ( ) ;
69
74
JwtSecurityTokenHandler . DefaultInboundClaimTypeMap . Clear ( ) ;
70
75
71
- builder . Services . AddAuthentication ( IdentityServerAuthenticationDefaults . AuthenticationScheme )
72
- . AddIdentityServerAuthentication ( o =>
76
+ builder . Services
77
+ . AddAuthentication ( IdentityServerAuthenticationDefaults . AuthenticationScheme )
78
+ . AddJwtBearer ( "Bearer" , options =>
73
79
{
74
- o . Authority = baseSchemeUrlAndPort + adminPath . Path ;
75
- o . ApiName = identityServerConfiguration . ApiName ;
76
- o . RequireHttpsMetadata = identityServerConfiguration . RequireHttps ;
77
- o . SupportedTokens = SupportedTokens . Both ;
78
- o . ApiSecret = identityServerConfiguration . ApiSecret ;
80
+ options . Authority = baseSchemeUrlAndPort + adminPath . Path ;
81
+ options . RequireHttpsMetadata = identityServerConfiguration . RequireHttps ;
82
+
83
+ options . TokenValidationParameters = new TokenValidationParameters
84
+ {
85
+ ValidateAudience = false ,
86
+ } ;
79
87
} ) ;
80
88
81
89
//todo - refactor naming..
@@ -91,6 +99,11 @@ private static void AddIdentityServer(IIdentityServerConfiguration identityServe
91
99
}
92
100
}
93
101
102
+ private static IEnumerable < ApiScope > ApiScopes ( IIdentityServerConfiguration identityServerConfiguration )
103
+ {
104
+ return identityServerConfiguration . AllowedScopes . Select ( s => new ApiScope ( s ) ) ;
105
+ }
106
+
94
107
private static List < ApiResource > Resources ( IIdentityServerConfiguration identityServerConfiguration )
95
108
{
96
109
return new List < ApiResource >
@@ -101,9 +114,9 @@ private static List<ApiResource> Resources(IIdentityServerConfiguration identity
101
114
{
102
115
new Secret
103
116
{
104
- Value = identityServerConfiguration . ApiSecret . Sha256 ( )
105
- }
106
- }
117
+ Value = identityServerConfiguration . ApiSecret . Sha256 ( ) ,
118
+ } ,
119
+ } ,
107
120
} ,
108
121
} ;
109
122
}
@@ -117,8 +130,8 @@ private static List<Client> Client(IIdentityServerConfiguration identityServerCo
117
130
ClientId = identityServerConfiguration . ApiName ,
118
131
AllowedGrantTypes = GrantTypes . ClientCredentials ,
119
132
ClientSecrets = new List < Secret > { new Secret ( identityServerConfiguration . ApiSecret . Sha256 ( ) ) } ,
120
- AllowedScopes = { identityServerConfiguration . ApiName }
121
- }
133
+ AllowedScopes = identityServerConfiguration . AllowedScopes ,
134
+ } ,
122
135
} ;
123
136
}
124
137
}
0 commit comments