Skip to content

Commit 43f1758

Browse files
committed
refactor and clean up code
1 parent a4ace4f commit 43f1758

File tree

14 files changed

+81
-137
lines changed

14 files changed

+81
-137
lines changed

samples/BiMonetaryApi/Controllers/TickerController.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
42
using System.Threading.Tasks;
53
using Microsoft.AspNetCore.Mvc;
64
using NetCoreKit.Domain;

samples/BiMonetaryApi/appsettings.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,29 @@
2020
"Enabled": true,
2121
"UI": {
2222
"Enabled": true
23+
},
24+
"ApiInfo": {
25+
"Title": "BiMonetary API",
26+
"Description": "An application with Swagger, Swashbuckle, and API versioning.",
27+
"ContactName": "Vietnam Devs",
28+
"ContactEmail": "[email protected]",
29+
"TermOfService": "Shareware",
30+
"LicenseName": "MIT",
31+
"LicenseUrl": "https://github.com/cloudnative-netcore/netcorekit/blob/master/LICENSE"
2332
}
2433
},
34+
"AuthN": {
35+
"Enabled": true,
36+
"ClaimToScopeMap": {
37+
"access_example1_api": "example1_api_scope",
38+
"access_example2_api": "example2_api_scope"
39+
},
40+
"Scopes": {
41+
"example1_api_scope": "Example 1 APIs",
42+
"example2_api_scope": "Example 2 APIs"
43+
},
44+
"Audience": "api"
45+
},
2546
"Logging": {
2647
"LogLevel": {
2748
"Default": "Warning"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Collections.Generic;
2+
using Microsoft.Extensions.Configuration;
3+
using NetCoreKit.Infrastructure.AspNetCore.Miniservice.Options;
4+
5+
namespace NetCoreKit.Infrastructure.AspNetCore.Miniservice
6+
{
7+
public static class ConfigurationExtensions
8+
{
9+
public static Dictionary<string, string> GetScopes(this IConfiguration config)
10+
{
11+
return GetAuthNOptions(config).Scopes;
12+
}
13+
14+
public static Dictionary<string, string> GetClaims(this IConfiguration config)
15+
{
16+
return GetAuthNOptions(config).ClaimToScopeMap;
17+
}
18+
19+
public static string GetAudience(this IConfiguration config)
20+
{
21+
return GetAuthNOptions(config).Audience;
22+
}
23+
24+
private static AuthNOptions GetAuthNOptions(IConfiguration config)
25+
{
26+
var options = new AuthNOptions();
27+
config.GetSection("AuthN").Bind(options);
28+
return options;
29+
}
30+
}
31+
}
32+

src/NetCoreKit.Infrastructure.AspNetCore.Miniservice/Configurator.cs

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/NetCoreKit.Infrastructure.AspNetCore.Miniservice/Constants.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ namespace NetCoreKit.Infrastructure.AspNetCore.Miniservice
22
{
33
public sealed class Constants
44
{
5-
public const string ClaimToScopeMap = "claimToScopeMap";
6-
public const string Scopes = "scopes";
7-
public const string Audience = "audience";
5+
86
}
97
}

src/NetCoreKit.Infrastructure.AspNetCore.Miniservice/Controllers/SysInfoController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public SysInfoController(IConfiguration config)
2020
_config = config;
2121
}
2222

23-
[HttpGet("/system-info")]
23+
[HttpGet("/sysinfo")]
2424
public IActionResult Index()
2525
{
2626
// Source: http://michaco.net/blog/EnvironmentVariablesAndConfigurationInASPNETCoreApps

src/NetCoreKit.Infrastructure.AspNetCore.Miniservice/Mongo.ServiceCollectionExtensions.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.IdentityModel.Tokens.Jwt;
43
using System.Linq;
54
using Microsoft.AspNetCore.Authentication.JwtBearer;
@@ -29,17 +28,13 @@ public static partial class ServiceCollectionExtensions
2928
public static IServiceCollection AddMongoMiniService(
3029
this IServiceCollection services,
3130
Action<IServiceCollection> preScopeAction = null,
32-
Action<IServiceCollection, IServiceProvider> afterDbScopeAction = null,
33-
Func<IEnumerable<KeyValuePair<string, object>>> extendServiceParamsFunc = null)
31+
Action<IServiceCollection, IServiceProvider> afterDbScopeAction = null)
3432
{
35-
services.AddScoped(sp => new ServiceParams().ExtendServiceParams(extendServiceParamsFunc?.Invoke()));
36-
3733
using (var scope = services.BuildServiceProvider().GetService<IServiceScopeFactory>().CreateScope())
3834
{
3935
var svcProvider = scope.ServiceProvider;
4036
var config = svcProvider.GetRequiredService<IConfiguration>();
4137
var env = svcProvider.GetRequiredService<IHostingEnvironment>();
42-
var serviceParams = svcProvider.GetRequiredService<ServiceParams>();
4338

4439
// let registering the database providers or others from the outside
4540
preScopeAction?.Invoke(services);
@@ -127,12 +122,12 @@ public static IServiceCollection AddMongoMiniService(
127122
{
128123
options.Authority = GetAuthUri(config, env);
129124
options.RequireHttpsMetadata = false;
130-
options.Audience = serviceParams.GetAudience();
125+
options.Audience = config.GetAudience();
131126
});
132127

133128
services.AddAuthorization(c =>
134129
{
135-
foreach (var claimToScope in serviceParams.GetClaims())
130+
foreach (var claimToScope in config.GetClaims())
136131
c.AddPolicy(claimToScope.Key, p => p.RequireClaim("scope", claimToScope.Value));
137132
});
138133
}
@@ -165,7 +160,7 @@ public static IServiceCollection AddMongoMiniService(
165160
Flow = "implicit",
166161
AuthorizationUrl = $"{GetExternalAuthUri(config)}/connect/authorize",
167162
TokenUrl = $"{GetExternalAuthUri(config)}/connect/token",
168-
Scopes = serviceParams.GetScopes()
163+
Scopes = config.GetScopes()
169164
});
170165

171166
c.EnableAnnotations();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Collections.Generic;
2+
3+
namespace NetCoreKit.Infrastructure.AspNetCore.Miniservice.Options
4+
{
5+
public class AuthNOptions
6+
{
7+
public Dictionary<string, string> ClaimToScopeMap { get; set; }
8+
public Dictionary<string, string> Scopes { get; set; }
9+
public string Audience { get; set; }
10+
}
11+
}

src/NetCoreKit.Infrastructure.AspNetCore.Miniservice/Options/MiniserviceOptions.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/NetCoreKit.Infrastructure.AspNetCore.Miniservice/Options/PersistenceOptions.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)