Skip to content

Commit e64259c

Browse files
committed
chore(auth): integrate auth service
1 parent 448657f commit e64259c

File tree

9 files changed

+93
-10
lines changed

9 files changed

+93
-10
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace Micro.AppRegistration.Api.Auth.Exceptions
4+
{
5+
public class KeyNotFoundException : Exception
6+
{
7+
public KeyNotFoundException(string message) : base(message)
8+
{
9+
}
10+
}
11+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Threading.Tasks;
2+
using Fossapps.Micro.KeyStore;
3+
using Fossapps.Micro.KeyStore.Models;
4+
using Micro.AppRegistration.Api.Auth.Exceptions;
5+
6+
namespace Micro.AppRegistration.Api.Auth
7+
{
8+
public interface IKeyResolver
9+
{
10+
Task<string> ResolveKey(string keyId);
11+
}
12+
public class KeyResolver : IKeyResolver
13+
{
14+
private readonly IKeyStoreClient _keyStoreClient;
15+
16+
public KeyResolver(IKeyStoreClient keyStoreClient)
17+
{
18+
_keyStoreClient = keyStoreClient;
19+
}
20+
21+
public async Task<string> ResolveKey(string keyId)
22+
{
23+
var response = await _keyStoreClient.Keys.GetAsync(keyId);
24+
return response switch
25+
{
26+
KeyCreatedResponse keyCreatedResponse => keyCreatedResponse.Body,
27+
_ => throw new KeyNotFoundException($"key: '{keyId}' not found")
28+
};
29+
}
30+
}
31+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Micro.AppRegistration.Api.Configs
2+
{
3+
public class Services
4+
{
5+
public KeyStoreConfig KeyStore { set; get; }
6+
}
7+
8+
public class KeyStoreConfig
9+
{
10+
public string Url { set; get; }
11+
}
12+
}

Micro.AppRegistration.Api/Micro.AppRegistration.Api.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
<PackageReference Include="App.Metrics.AspNetCore.Mvc" Version="3.2.0-dev0002" />
1010
<PackageReference Include="App.Metrics.Extensions.Configuration" Version="3.2.0-dev0002" />
1111
<PackageReference Include="App.Metrics.Reporting.InfluxDB" Version="3.2.0-dev0002" />
12+
<PackageReference Include="Fossapps.Micro.KeyStore" Version="1.10.0" />
1213
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.0.3" />
1314
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.0.0-preview8.19405.11" />
1415
<PackageReference Include="Microsoft.Extensions.Logging.Slack" Version="1.1.0" />
1516
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.0.0-preview8" />
1617
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.Design" Version="1.1.1" />
1718
<None Include="./appsettings.ci.json" CopyToPublishDirectory="Always" />
19+
<PackageReference Include="PemUtils" Version="3.0.0.82" />
1820
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc2" />
1921
</ItemGroup>
2022

Micro.AppRegistration.Api/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public void ConfigureServices(IServiceCollection services)
2424
{
2525
services.AddConfiguration(Configuration);
2626
services.AddMetrics();
27-
services.ConfigureRequiredDependencies();
27+
services.ConfigureRequiredDependencies(Configuration);
2828
services.ConfigureHealthChecks();
2929
services.AddControllers();
3030
services.ConfigureSwagger();

Micro.AppRegistration.Api/StartupExtensions/Configuration.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public static void AddConfiguration(this IServiceCollection services, IConfigura
1010
{
1111
services.Configure<DatabaseConfig>(configuration.GetSection("DatabaseConfig"));
1212
services.Configure<SlackLoggingConfig>(configuration.GetSection("Logging").GetSection("Slack"));
13+
services.Configure<Services>(configuration.GetSection("Services"));
1314
}
1415
}
1516
}
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
1+
using System;
2+
using Fossapps.Micro.KeyStore;
3+
using Micro.AppRegistration.Api.Auth;
4+
using Micro.AppRegistration.Api.Configs;
15
using Micro.AppRegistration.Api.Models;
26
using Micro.AppRegistration.Api.Uuid;
7+
using Microsoft.Extensions.Configuration;
38
using Microsoft.Extensions.DependencyInjection;
49

510
namespace Micro.AppRegistration.Api.StartupExtensions
611
{
712
public static class DependencyInjection
813
{
9-
public static void ConfigureRequiredDependencies(this IServiceCollection services)
14+
public static void ConfigureRequiredDependencies(this IServiceCollection services, IConfiguration configuration)
1015
{
1116
services.AddDbContext<ApplicationContext>();
1217
services.AddSingleton<IUuidService, UuidService>();
18+
services.AddSingleton<IKeyResolver, KeyResolver>();
19+
services.AddSingleton(SetupKeyStoreHttpClient(configuration.GetSection("Services").Get<Services>().KeyStore));
20+
}
21+
22+
private static IKeyStoreClient SetupKeyStoreHttpClient(KeyStoreConfig config)
23+
{
24+
return new KeyStoreClient
25+
{
26+
BaseUri = new Uri(config.Url)
27+
};
1328
}
1429
}
15-
}
30+
}

Micro.AppRegistration.Api/StartupExtensions/Identity.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
2+
using System.IO;
3+
using Micro.AppRegistration.Api.Auth;
24
using Microsoft.AspNetCore.Authentication.JwtBearer;
3-
using Microsoft.AspNetCore.Identity;
45
using Microsoft.Extensions.Configuration;
56
using Microsoft.Extensions.DependencyInjection;
67
using Microsoft.IdentityModel.Tokens;
8+
using PemUtils;
79

810
namespace Micro.AppRegistration.Api.StartupExtensions
911
{
@@ -36,12 +38,16 @@ private static void ConfigureJwtBearer(IServiceCollection services, JwtBearerOpt
3638
ClockSkew = TimeSpan.Zero,
3739
IssuerSigningKeyResolver = (token, secToken, kid, parameters) =>
3840
{
39-
// todo: first get keystore to generate a sdk and publish to nuget automatically
40-
// add that package as a dependency
41-
// and finally copy logic from micro.auth
42-
throw new NotImplementedException();
43-
}
41+
// todo: I know this .Result is a very bad idea (converting from async to sync)
42+
// however there's no other way to do this, signing key resolver doesn't have a
43+
// async version of this method, they are looking into it though
44+
// https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet/issues/468
45+
var key = services.BuildServiceProvider().GetRequiredService<IKeyResolver>()
46+
.ResolveKey(kid).Result;
47+
var pemReader = new PemReader(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(key)));
48+
var publicKeyParameters = pemReader.ReadRsaKey();
49+
return new []{new RsaSecurityKey(publicKeyParameters)}; }
4450
};
4551
}
4652
}
47-
}
53+
}

Micro.AppRegistration.Api/appsettings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,10 @@
2323
"Database": "monitoring"
2424
}
2525
},
26+
"Services": {
27+
"KeyStore": {
28+
"Url": "http://localhost:15000"
29+
}
30+
},
2631
"AllowedHosts": "*"
2732
}

0 commit comments

Comments
 (0)