Skip to content

Commit f62ed72

Browse files
committed
all packages upgraded and tests passing
1 parent 17b0555 commit f62ed72

15 files changed

+1998
-1942
lines changed

docs/features/administration.rst

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,22 @@ This will bring down everything needed by the admin API.
1212
Providing your own IdentityServer
1313
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1414

15-
16-
1715
All you need to do to hook into your own IdentityServer is add the following to your ConfigureServices method.
1816

1917
.. code-block:: csharp
2018
2119
public virtual void ConfigureServices(IServiceCollection services)
2220
{
23-
Action<IdentityServerAuthenticationOptions> options = o => {
24-
// o.Authority = ;
25-
// o.ApiName = ;
26-
// etc....
21+
Action<JwtBearerOptions> options = o =>
22+
{
23+
o.Authority = identityServerRootUrl;
24+
o.RequireHttpsMetadata = false;
25+
o.TokenValidationParameters = new TokenValidationParameters
26+
{
27+
ValidateAudience = false,
2728
};
29+
// etc....
30+
};
2831
2932
services
3033
.AddOcelot()

docs/features/authentication.rst

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,14 @@ In order to use IdentityServer bearer tokens, register your IdentityServer servi
9797
public void ConfigureServices(IServiceCollection services)
9898
{
9999
var authenticationProviderKey = "TestKey";
100-
Action<IdentityServerAuthenticationOptions> options = o =>
100+
Action<JwtBearerOptions> options = o =>
101101
{
102102
o.Authority = "https://whereyouridentityserverlives.com";
103-
o.ApiName = "api";
104-
o.SupportedTokens = SupportedTokens.Both;
105-
o.ApiSecret = "secret";
103+
// etc
106104
};
107105
108106
services.AddAuthentication()
109-
.AddIdentityServerAuthentication(authenticationProviderKey, options);
107+
.AddJwtBearer(authenticationProviderKey, options);
110108
111109
services.AddOcelot();
112110
}

src/Ocelot.Administration/Ocelot.Administration.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<PrivateAssets>all</PrivateAssets>
3232
</PackageReference>
3333
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="3.0.1" />
34-
<PackageReference Include="IdentityServer4" Version="3.1.1" />
34+
<PackageReference Include="IdentityServer4" Version="4.1.1" />
3535
</ItemGroup>
3636
<ItemGroup>
3737
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="1.0.0" />

src/Ocelot.Administration/OcelotBuilderExtensions.cs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using Ocelot.DependencyInjection;
22
using IdentityServer4.AccessTokenValidation;
33
using IdentityServer4.Models;
4-
using Microsoft.AspNetCore.Builder;
54
using Microsoft.Extensions.Configuration;
65
using Microsoft.Extensions.DependencyInjection;
76
using Microsoft.Extensions.DependencyInjection.Extensions;
@@ -10,6 +9,9 @@
109
using System.Collections.Generic;
1110
using System.IdentityModel.Tokens.Jwt;
1211
using System.Security.Cryptography.X509Certificates;
12+
using System.Linq;
13+
using Microsoft.IdentityModel.Tokens;
14+
using Microsoft.AspNetCore.Authentication.JwtBearer;
1315

1416
namespace Ocelot.Administration
1517
{
@@ -18,6 +20,7 @@ public static class OcelotBuilderExtensions
1820
public static IOcelotAdministrationBuilder AddAdministration(this IOcelotBuilder builder, string path, string secret)
1921
{
2022
var administrationPath = new AdministrationPath(path);
23+
2124
builder.Services.AddSingleton<OcelotMiddlewareConfigurationDelegate>(IdentityServerMiddlewareConfigurationProvider.Get);
2225

2326
//add identity server for admin area
@@ -32,7 +35,7 @@ public static IOcelotAdministrationBuilder AddAdministration(this IOcelotBuilder
3235
return new OcelotAdministrationBuilder(builder.Services, builder.Configuration);
3336
}
3437

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)
3639
{
3740
var administrationPath = new AdministrationPath(path);
3841
builder.Services.AddSingleton<OcelotMiddlewareConfigurationDelegate>(IdentityServerMiddlewareConfigurationProvider.Get);
@@ -46,11 +49,11 @@ public static IOcelotAdministrationBuilder AddAdministration(this IOcelotBuilder
4649
return new OcelotAdministrationBuilder(builder.Services, builder.Configuration);
4750
}
4851

49-
private static void AddIdentityServer(Action<IdentityServerAuthenticationOptions> configOptions, IOcelotBuilder builder)
52+
private static void AddIdentityServer(Action<JwtBearerOptions> configOptions, IOcelotBuilder builder)
5053
{
5154
builder.Services
5255
.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
53-
.AddIdentityServerAuthentication(configOptions);
56+
.AddJwtBearer("Bearer", configOptions);
5457
}
5558

5659
private static void AddIdentityServer(IIdentityServerConfiguration identityServerConfiguration, IAdministrationPath adminPath, IOcelotBuilder builder, IConfiguration configuration)
@@ -60,22 +63,27 @@ private static void AddIdentityServer(IIdentityServerConfiguration identityServe
6063
.AddIdentityServer(o =>
6164
{
6265
o.IssuerUri = "Ocelot";
66+
o.EmitStaticAudienceClaim = true;
6367
})
68+
.AddInMemoryApiScopes(ApiScopes(identityServerConfiguration))
6469
.AddInMemoryApiResources(Resources(identityServerConfiguration))
6570
.AddInMemoryClients(Client(identityServerConfiguration));
6671

6772
var urlFinder = new BaseUrlFinder(configuration);
6873
var baseSchemeUrlAndPort = urlFinder.Find();
6974
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
7075

71-
builder.Services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
72-
.AddIdentityServerAuthentication(o =>
76+
builder.Services
77+
.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
78+
.AddJwtBearer("Bearer", options =>
7379
{
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+
};
7987
});
8088

8189
//todo - refactor naming..
@@ -91,6 +99,11 @@ private static void AddIdentityServer(IIdentityServerConfiguration identityServe
9199
}
92100
}
93101

102+
private static IEnumerable<ApiScope> ApiScopes(IIdentityServerConfiguration identityServerConfiguration)
103+
{
104+
return identityServerConfiguration.AllowedScopes.Select(s => new ApiScope(s));
105+
}
106+
94107
private static List<ApiResource> Resources(IIdentityServerConfiguration identityServerConfiguration)
95108
{
96109
return new List<ApiResource>
@@ -101,9 +114,9 @@ private static List<ApiResource> Resources(IIdentityServerConfiguration identity
101114
{
102115
new Secret
103116
{
104-
Value = identityServerConfiguration.ApiSecret.Sha256()
105-
}
106-
}
117+
Value = identityServerConfiguration.ApiSecret.Sha256(),
118+
},
119+
},
107120
},
108121
};
109122
}
@@ -117,8 +130,8 @@ private static List<Client> Client(IIdentityServerConfiguration identityServerCo
117130
ClientId = identityServerConfiguration.ApiName,
118131
AllowedGrantTypes = GrantTypes.ClientCredentials,
119132
ClientSecrets = new List<Secret> {new Secret(identityServerConfiguration.ApiSecret.Sha256())},
120-
AllowedScopes = { identityServerConfiguration.ApiName }
121-
}
133+
AllowedScopes = identityServerConfiguration.AllowedScopes,
134+
},
122135
};
123136
}
124137
}

test/Ocelot.AcceptanceTests/AuthenticationTests.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,11 @@ private void GivenThereIsAnIdentityServerOn(string url, string apiName, string a
278278
services.AddLogging();
279279
services.AddIdentityServer()
280280
.AddDeveloperSigningCredential()
281+
.AddInMemoryApiScopes(new List<ApiScope>
282+
{
283+
new ApiScope(apiName, "test"),
284+
new ApiScope(api2Name, "test"),
285+
})
281286
.AddInMemoryApiResources(new List<ApiResource>
282287
{
283288
new ApiResource
@@ -286,12 +291,12 @@ private void GivenThereIsAnIdentityServerOn(string url, string apiName, string a
286291
Description = "My API",
287292
Enabled = true,
288293
DisplayName = "test",
289-
Scopes = new List<Scope>()
294+
Scopes = new List<string>()
290295
{
291-
new Scope("api"),
292-
new Scope("api.readOnly"),
293-
new Scope("openid"),
294-
new Scope("offline_access"),
296+
"api",
297+
"api.readOnly",
298+
"openid",
299+
"offline_access",
295300
},
296301
ApiSecrets = new List<Secret>()
297302
{
@@ -311,10 +316,10 @@ private void GivenThereIsAnIdentityServerOn(string url, string apiName, string a
311316
Description = "My second API",
312317
Enabled = true,
313318
DisplayName = "second test",
314-
Scopes = new List<Scope>()
319+
Scopes = new List<string>()
315320
{
316-
new Scope("api2"),
317-
new Scope("api2.readOnly"),
321+
"api2",
322+
"api2.readOnly",
318323
},
319324
ApiSecrets = new List<Secret>()
320325
{

0 commit comments

Comments
 (0)