Skip to content
This repository was archived by the owner on Aug 1, 2021. It is now read-only.

Commit ffd0ba2

Browse files
committed
Ceredentials
1 parent a8a5032 commit ffd0ba2

10 files changed

Lines changed: 515 additions & 16 deletions

File tree

keys/JPProjectOAuth.cer

791 Bytes
Binary file not shown.

keys/JpProjectOAuth.pvk

1.18 KB
Binary file not shown.

src/Backend/Jp.UserManagement/jpProject_sso_log.txt

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,3 +619,78 @@ WHERE [e].[Id] = @__get_Item_0
619619
2018-09-03 01:53:29.258 -03:00 [INF] Request starting HTTP/1.1 GET http://localhost:5003/swagger/v1/swagger.json
620620
2018-09-03 01:53:29.260 -03:00 [DBG] AuthenticationScheme: Bearer was not authenticated.
621621
2018-09-03 01:53:29.461 -03:00 [INF] Request finished in 202.9468ms 200 application/json;charset=utf-8
622+
2018-09-03 14:52:14.639 -03:00 [INF] User profile is available. Using 'C:\Users\bruno.brito\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
623+
2018-09-03 14:52:15.712 -03:00 [INF] Request starting HTTP/1.1 GET http://localhost:5003/swagger/index.html
624+
2018-09-03 14:52:15.742 -03:00 [DBG] AuthenticationScheme: Bearer was not authenticated.
625+
2018-09-03 14:52:15.783 -03:00 [INF] Request finished in 72.4872ms 200 text/html
626+
2018-09-03 14:52:15.870 -03:00 [INF] Request starting HTTP/1.1 GET http://localhost:5003/swagger/swagger-ui.css
627+
2018-09-03 14:52:15.870 -03:00 [INF] Request starting HTTP/1.1 GET http://localhost:5003/swagger/swagger-ui-standalone-preset.js
628+
2018-09-03 14:52:15.870 -03:00 [INF] Request starting HTTP/1.1 GET http://localhost:5003/swagger/swagger-ui-bundle.js
629+
2018-09-03 14:52:15.872 -03:00 [DBG] AuthenticationScheme: Bearer was not authenticated.
630+
2018-09-03 14:52:15.872 -03:00 [DBG] AuthenticationScheme: Bearer was not authenticated.
631+
2018-09-03 14:52:15.873 -03:00 [DBG] AuthenticationScheme: Bearer was not authenticated.
632+
2018-09-03 14:52:15.927 -03:00 [INF] Sending file. Request path: '/swagger-ui.css'. Physical path: 'N/A'
633+
2018-09-03 14:52:15.927 -03:00 [INF] Sending file. Request path: '/swagger-ui-standalone-preset.js'. Physical path: 'N/A'
634+
2018-09-03 14:52:15.934 -03:00 [INF] Request finished in 63.254ms 200 text/css
635+
2018-09-03 14:52:15.934 -03:00 [INF] Request finished in 63.254ms 200 application/javascript
636+
2018-09-03 14:52:16.033 -03:00 [INF] Sending file. Request path: '/swagger-ui-bundle.js'. Physical path: 'N/A'
637+
2018-09-03 14:52:16.035 -03:00 [INF] Request finished in 164.7783ms 200 application/javascript
638+
2018-09-03 14:52:16.326 -03:00 [INF] Request starting HTTP/1.1 GET http://localhost:5003/swagger/v1/swagger.json
639+
2018-09-03 14:52:16.327 -03:00 [DBG] AuthenticationScheme: Bearer was not authenticated.
640+
2018-09-03 14:52:16.359 -03:00 [INF] Request starting HTTP/1.1 GET http://localhost:5003/swagger/favicon-32x32.png
641+
2018-09-03 14:52:16.360 -03:00 [DBG] AuthenticationScheme: Bearer was not authenticated.
642+
2018-09-03 14:52:16.363 -03:00 [INF] Sending file. Request path: '/favicon-32x32.png'. Physical path: 'N/A'
643+
2018-09-03 14:52:16.365 -03:00 [INF] Request finished in 6.4668ms 200 image/png
644+
2018-09-03 14:52:16.370 -03:00 [INF] Request starting HTTP/1.1 GET http://localhost:5003/swagger/favicon-16x16.png
645+
2018-09-03 14:52:16.371 -03:00 [DBG] AuthenticationScheme: Bearer was not authenticated.
646+
2018-09-03 14:52:16.373 -03:00 [INF] Sending file. Request path: '/favicon-16x16.png'. Physical path: 'N/A'
647+
2018-09-03 14:52:16.376 -03:00 [INF] Request finished in 5.9045ms 200 image/png
648+
2018-09-03 14:52:16.493 -03:00 [INF] Request finished in 166.3778ms 200 application/json;charset=utf-8
649+
2018-09-03 14:53:44.168 -03:00 [INF] User profile is available. Using 'C:\Users\bruno.brito\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
650+
2018-09-03 14:53:45.125 -03:00 [INF] Request starting HTTP/1.1 GET http://localhost:5003/swagger/index.html
651+
2018-09-03 14:53:45.211 -03:00 [DBG] AuthenticationScheme: Bearer was not authenticated.
652+
2018-09-03 14:53:45.229 -03:00 [INF] Request finished in 105.718ms 200 text/html
653+
2018-09-03 14:53:45.772 -03:00 [INF] Request starting HTTP/1.1 GET http://localhost:5003/swagger/v1/swagger.json
654+
2018-09-03 14:53:45.774 -03:00 [DBG] AuthenticationScheme: Bearer was not authenticated.
655+
2018-09-03 14:53:45.924 -03:00 [INF] Request finished in 151.6338ms 200 application/json;charset=utf-8
656+
2018-09-03 14:54:13.996 -03:00 [INF] Request starting HTTP/1.1 GET http://localhost:5003/swagger/oauth2-redirect.html
657+
2018-09-03 14:54:13.998 -03:00 [DBG] AuthenticationScheme: Bearer was not authenticated.
658+
2018-09-03 14:54:14.011 -03:00 [INF] Sending file. Request path: '/oauth2-redirect.html'. Physical path: 'N/A'
659+
2018-09-03 14:54:14.014 -03:00 [INF] Request finished in 18.2516ms 200 text/html
660+
2018-09-03 14:54:20.112 -03:00 [INF] Request starting HTTP/1.1 GET http://localhost:5003/Management/user-data
661+
2018-09-03 14:54:20.355 -03:00 [INF] Successfully validated the token.
662+
2018-09-03 14:54:20.360 -03:00 [DBG] AuthenticationScheme: Bearer was successfully authenticated.
663+
2018-09-03 14:54:20.389 -03:00 [INF] Route matched with {action = "UserData", controller = "Management"}. Executing action Jp.UserManagement.Controllers.ManagementController.UserData (Jp.UserManagement)
664+
2018-09-03 14:54:20.404 -03:00 [INF] Authorization was successful.
665+
2018-09-03 14:54:20.516 -03:00 [INF] Executing action method Jp.UserManagement.Controllers.ManagementController.UserData (Jp.UserManagement) - Validation state: "Valid"
666+
2018-09-03 14:54:45.995 -03:00 [INF] Entity Framework Core 2.1.2-rtm-30932 initialized 'ApplicationDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
667+
2018-09-03 14:54:46.324 -03:00 [INF] Executed DbCommand (10ms) [Parameters=[@__get_Item_0='?' (DbType = Guid)], CommandType='"Text"', CommandTimeout='30']
668+
SELECT TOP(1) [e].[Id], [e].[AccessFailedCount], [e].[Bio], [e].[Company], [e].[ConcurrencyStamp], [e].[Email], [e].[EmailConfirmed], [e].[JobTitle], [e].[LockoutEnabled], [e].[LockoutEnd], [e].[Name], [e].[NormalizedEmail], [e].[NormalizedUserName], [e].[PasswordHash], [e].[PhoneNumber], [e].[PhoneNumberConfirmed], [e].[Picture], [e].[SecurityStamp], [e].[TwoFactorEnabled], [e].[Url], [e].[UserName]
669+
FROM [Users] AS [e]
670+
WHERE [e].[Id] = @__get_Item_0
671+
2018-09-03 14:54:46.429 -03:00 [INF] Executed action method Jp.UserManagement.Controllers.ManagementController.UserData (Jp.UserManagement), returned result Microsoft.AspNetCore.Mvc.OkObjectResult in 25905.5463ms.
672+
2018-09-03 14:54:46.438 -03:00 [INF] Executing ObjectResult, writing value of type 'Jp.Infra.CrossCutting.Tools.Model.DefaultResponse`1[[Jp.Application.ViewModels.ProfileViewModel, Jp.Application, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'.
673+
2018-09-03 14:54:46.450 -03:00 [INF] Executed action Jp.UserManagement.Controllers.ManagementController.UserData (Jp.UserManagement) in 26056.407300000003ms
674+
2018-09-03 14:54:46.457 -03:00 [INF] Request finished in 26344.8842ms 200 application/json; charset=utf-8
675+
2018-09-03 15:32:26.606 -03:00 [INF] User profile is available. Using 'C:\Users\bruno.brito\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
676+
2018-09-03 15:32:27.495 -03:00 [INF] Request starting HTTP/1.1 GET http://localhost:5003/swagger/index.html
677+
2018-09-03 15:32:27.530 -03:00 [DBG] AuthenticationScheme: Bearer was not authenticated.
678+
2018-09-03 15:32:27.549 -03:00 [INF] Request finished in 58.2322ms 200 text/html
679+
2018-09-03 15:32:27.929 -03:00 [INF] Request starting HTTP/1.1 GET http://localhost:5003/swagger/v1/swagger.json
680+
2018-09-03 15:32:27.931 -03:00 [DBG] AuthenticationScheme: Bearer was not authenticated.
681+
2018-09-03 15:32:28.088 -03:00 [INF] Request finished in 158.9564ms 200 application/json;charset=utf-8
682+
2018-09-03 15:32:45.281 -03:00 [INF] Request starting HTTP/1.1 GET http://localhost:5003/Management/user-data
683+
2018-09-03 15:32:45.530 -03:00 [INF] Successfully validated the token.
684+
2018-09-03 15:32:45.535 -03:00 [DBG] AuthenticationScheme: Bearer was successfully authenticated.
685+
2018-09-03 15:32:45.574 -03:00 [INF] Route matched with {action = "UserData", controller = "Management"}. Executing action Jp.UserManagement.Controllers.ManagementController.UserData (Jp.UserManagement)
686+
2018-09-03 15:32:45.587 -03:00 [INF] Authorization was successful.
687+
2018-09-03 15:32:45.694 -03:00 [INF] Executing action method Jp.UserManagement.Controllers.ManagementController.UserData (Jp.UserManagement) - Validation state: "Valid"
688+
2018-09-03 15:32:46.138 -03:00 [INF] Entity Framework Core 2.1.2-rtm-30932 initialized 'ApplicationDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
689+
2018-09-03 15:32:46.469 -03:00 [INF] Executed DbCommand (9ms) [Parameters=[@__get_Item_0='?' (DbType = Guid)], CommandType='"Text"', CommandTimeout='30']
690+
SELECT TOP(1) [e].[Id], [e].[AccessFailedCount], [e].[Bio], [e].[Company], [e].[ConcurrencyStamp], [e].[Email], [e].[EmailConfirmed], [e].[JobTitle], [e].[LockoutEnabled], [e].[LockoutEnd], [e].[Name], [e].[NormalizedEmail], [e].[NormalizedUserName], [e].[PasswordHash], [e].[PhoneNumber], [e].[PhoneNumberConfirmed], [e].[Picture], [e].[SecurityStamp], [e].[TwoFactorEnabled], [e].[Url], [e].[UserName]
691+
FROM [Users] AS [e]
692+
WHERE [e].[Id] = @__get_Item_0
693+
2018-09-03 15:32:46.575 -03:00 [INF] Executed action method Jp.UserManagement.Controllers.ManagementController.UserData (Jp.UserManagement), returned result Microsoft.AspNetCore.Mvc.OkObjectResult in 873.77150000000006ms.
694+
2018-09-03 15:32:46.584 -03:00 [INF] Executing ObjectResult, writing value of type 'Jp.Infra.CrossCutting.Tools.Model.DefaultResponse`1[[Jp.Application.ViewModels.ProfileViewModel, Jp.Application, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'.
695+
2018-09-03 15:32:46.594 -03:00 [INF] Executed action Jp.UserManagement.Controllers.ManagementController.UserData (Jp.UserManagement) in 1017.3105ms
696+
2018-09-03 15:32:46.607 -03:00 [INF] Request finished in 1325.4112ms 200 application/json; charset=utf-8

src/Frontend/Jp.UI.SSO/Configuration/IdentityServerConfig.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
using System;
22
using System.Reflection;
33
using Jp.Infra.CrossCutting.Identity.Entities.Identity;
4+
using Jp.UI.SSO.Util;
45
using Microsoft.AspNetCore.Hosting;
56
using Microsoft.EntityFrameworkCore;
67
using Microsoft.Extensions.Configuration;
78
using Microsoft.Extensions.DependencyInjection;
9+
using Microsoft.Extensions.Logging;
810

911
namespace Jp.UI.SSO.Configuration
1012
{
1113
public static class IdentityServerConfig
1214
{
13-
public static IServiceCollection AddIdentityServer(this IServiceCollection services, IConfiguration configuration, IHostingEnvironment environment)
15+
public static IServiceCollection AddIdentityServer(this IServiceCollection services,
16+
IConfiguration configuration, IHostingEnvironment environment, ILogger logger)
1417
{
1518
var connectionString = Environment.GetEnvironmentVariable("SQLSERVER_CONNECTION") ?? configuration.GetConnectionString("SSOConnection");
1619

@@ -24,7 +27,7 @@ public static IServiceCollection AddIdentityServer(this IServiceCollection servi
2427
options.Events.RaiseFailureEvents = true;
2528
options.Events.RaiseSuccessEvents = true;
2629
options.IssuerUri = Environment.GetEnvironmentVariable("ISSUER_URI");
27-
options.PublicOrigin = Environment.GetEnvironmentVariable("PUBLIC_ORIGIN");
30+
options.PublicOrigin = Environment.GetEnvironmentVariable("PUBLIC_URI");
2831
})
2932
.AddAspNetIdentity<UserIdentity>()
3033
// this adds the config data from DB (clients, resources)
@@ -43,16 +46,19 @@ public static IServiceCollection AddIdentityServer(this IServiceCollection servi
4346
//options.EnableTokenCleanup = true;
4447
//options.TokenCleanupInterval = 15; // frequency in seconds to cleanup stale grants. 15 is useful during debugging
4548
});
46-
if (environment.IsDevelopment())
47-
{
48-
builder.AddDeveloperSigningCredential(false);
49-
}
50-
else
51-
{
52-
throw new Exception("need to configure key material");
53-
}
49+
50+
builder.AddSigninCredentialFromConfig(configuration.GetSection("CertificateOptions"), logger);
51+
//if (environment.IsDevelopment())
52+
//{
53+
// builder.AddDeveloperSigningCredential(false);
54+
//}
55+
//else
56+
//{
57+
// builder.AddSigninCredentialFromConfig(configuration.GetSection("CertificateOptions"), logger);
58+
//}
5459

5560
return services;
5661
}
62+
5763
}
5864
}

src/Frontend/Jp.UI.SSO/Startup.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using IdentityServer4.Services;
1+
using IdentityServer4.Configuration;
2+
using IdentityServer4.Services;
23
using Jp.Infra.CrossCutting.IoC;
34
using Jp.UI.SSO.Configuration;
45
using MediatR;
@@ -12,11 +13,13 @@ namespace Jp.UI.SSO
1213
{
1314
public class Startup
1415
{
16+
private readonly ILogger _logger;
1517
public IConfiguration Configuration { get; }
1618
public IHostingEnvironment Environment { get; }
1719

18-
public Startup(IHostingEnvironment environment, ILogger<DefaultCorsPolicyService> logger)
20+
public Startup(IHostingEnvironment environment, ILogger<Startup> logger)
1921
{
22+
_logger = logger;
2023
var builder = new ConfigurationBuilder()
2124
.SetBasePath(environment.ContentRootPath)
2225
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
@@ -46,7 +49,7 @@ public void ConfigureServices(IServiceCollection services)
4649
});
4750

4851
// Configure identity server
49-
services.AddIdentityServer(Configuration, Environment);
52+
services.AddIdentityServer(Configuration, Environment, _logger);
5053

5154
// Configure authentication and external logins
5255
services.AddSocialIntegration(Configuration);
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using System.IO;
2+
using System.Security.Cryptography.X509Certificates;
3+
using Microsoft.Extensions.Configuration;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.Extensions.Logging;
6+
7+
namespace Jp.UI.SSO.Util
8+
{
9+
/// <summary>
10+
/// Impl of adding a signin key for identity server 4,
11+
/// with an appsetting.json configuration look similar to:
12+
/// "SigninKeyCredentials": {
13+
/// "KeyType": "KeyFile",
14+
/// "KeyFilePath": "C:\\certificates\\idsv4.pfx",
15+
/// "KeyStorePath": ""
16+
/// }
17+
/// </summary>
18+
public static class SigninCredentialExtension
19+
{
20+
private const string KeyType = "KeyType";
21+
private const string KeyTypeKeyFile = "File";
22+
private const string KeyTypeKeyStore = "Store";
23+
private const string KeyTypeTemporary = "Temporary";
24+
private const string KeyFilePath = "FilePath";
25+
private const string KeyFilePassword = "FilePassword";
26+
private const string KeyStoreIssuer = "KeyStoreIssuer";
27+
28+
public static IIdentityServerBuilder AddSigninCredentialFromConfig(
29+
this IIdentityServerBuilder builder, IConfigurationSection options, ILogger logger)
30+
{
31+
string keyType = options.GetValue<string>(KeyType);
32+
logger.LogDebug($"SigninCredentialExtension keyType is {keyType}");
33+
34+
switch (keyType)
35+
{
36+
case KeyTypeTemporary:
37+
logger.LogDebug($"SigninCredentialExtension adding Temporary Signing Credential");
38+
builder.AddDeveloperSigningCredential(true);
39+
break;
40+
41+
case KeyTypeKeyFile:
42+
AddCertificateFromFile(builder, options, logger);
43+
break;
44+
45+
case KeyTypeKeyStore:
46+
AddCertificateFromStore(builder, options, logger);
47+
break;
48+
}
49+
50+
return builder;
51+
}
52+
53+
private static void AddCertificateFromStore(IIdentityServerBuilder builder,
54+
IConfigurationSection options, ILogger logger)
55+
{
56+
var keyIssuer = options.GetValue<string>(KeyStoreIssuer);
57+
logger.LogDebug($"SigninCredentialExtension adding key from store by {keyIssuer}");
58+
59+
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
60+
store.Open(OpenFlags.ReadOnly);
61+
62+
var certificates = store.Certificates.Find(X509FindType.FindByIssuerName, keyIssuer, true);
63+
64+
if (certificates.Count > 0)
65+
builder.AddSigningCredential(certificates[0]);
66+
else
67+
logger.LogError("A matching key couldn't be found in the store");
68+
}
69+
70+
private static void AddCertificateFromFile(IIdentityServerBuilder builder,
71+
IConfigurationSection options, ILogger logger)
72+
{
73+
var keyFilePath = options.GetValue<string>(KeyFilePath);
74+
var keyFilePassword = options.GetValue<string>(KeyFilePassword);
75+
76+
if (File.Exists(keyFilePath))
77+
{
78+
logger.LogDebug($"SigninCredentialExtension adding key from file {keyFilePath}");
79+
builder.AddSigningCredential(new X509Certificate2(keyFilePath, keyFilePassword, X509KeyStorageFlags.MachineKeySet));
80+
}
81+
else
82+
{
83+
logger.LogError($"SigninCredentialExtension cannot find key file {keyFilePath}");
84+
}
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)