Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AddAbpDeveloperSigningCredential method. #4325

Merged
merged 1 commit into from
Jun 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
using System;
using System;
using System.IdentityModel.Tokens.Jwt;
using System.IO;
using System.Security.Cryptography;
using IdentityModel;
using IdentityServer4;
using IdentityServer4.Configuration;
using IdentityServer4.Services;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Newtonsoft.Json.Linq;
using Volo.Abp.Identity;
using Volo.Abp.IdentityServer.AspNetIdentity;
using Volo.Abp.Security.Claims;
Expand Down Expand Up @@ -49,5 +54,77 @@ public static IIdentityServerBuilder AddAbpIdentityServer(

return builder;
}

public static IIdentityServerBuilder AddAbpDeveloperSigningCredential(
this IIdentityServerBuilder builder,
bool persistKey = true,
string filename = null,
IdentityServerConstants.RsaSigningAlgorithm signingAlgorithm = IdentityServerConstants.RsaSigningAlgorithm.RS256)
{
if (filename == null)
{
filename = Path.Combine(Directory.GetCurrentDirectory(), "tempkey.rsa");
}

if (File.Exists(filename))
{
var keyFile = File.ReadAllText(filename);

var json = JObject.Parse(keyFile);
var keyId = json.GetValue("KeyId").Value<string>();
var jsonParameters = json.GetValue("Parameters");
RSAParameters rsaParameters;
rsaParameters.D = Convert.FromBase64String(jsonParameters["D"].Value<string>());
rsaParameters.DP = Convert.FromBase64String(jsonParameters["DP"].Value<string>());
rsaParameters.DQ = Convert.FromBase64String(jsonParameters["DQ"].Value<string>());
rsaParameters.Exponent = Convert.FromBase64String(jsonParameters["Exponent"].Value<string>());
rsaParameters.InverseQ = Convert.FromBase64String(jsonParameters["InverseQ"].Value<string>());
rsaParameters.Modulus = Convert.FromBase64String(jsonParameters["Modulus"].Value<string>());
rsaParameters.P = Convert.FromBase64String(jsonParameters["P"].Value<string>());
rsaParameters.Q = Convert.FromBase64String(jsonParameters["Q"].Value<string>());

return builder.AddSigningCredential(CryptoHelper.CreateRsaSecurityKey(rsaParameters, keyId), signingAlgorithm);
}
else
{
var key = CryptoHelper.CreateRsaSecurityKey();

RSAParameters parameters;

if (key.Rsa != null)
{
parameters = key.Rsa.ExportParameters(includePrivateParameters: true);
}
else
{
parameters = key.Parameters;
}

var jObject = new JObject
{
{
"KeyId", key.KeyId
},
{
"Parameters", new JObject
{
{"D", Convert.ToBase64String(parameters.D)},
{"DP", Convert.ToBase64String(parameters.DP)},
{"DQ", Convert.ToBase64String(parameters.DQ)},
{"Exponent", Convert.ToBase64String(parameters.Exponent)},
{"Modulus", Convert.ToBase64String(parameters.Modulus)},
{"P", Convert.ToBase64String(parameters.P)},
{"Q", Convert.ToBase64String(parameters.Q)}
}
}
};

if (persistKey)
{
File.WriteAllText(filename, jObject.ToString());
}
return builder.AddSigningCredential(key, signingAlgorithm);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private static void AddIdentityServer(IServiceCollection services)

if (builderOptions.AddDeveloperSigningCredential)
{
identityServerBuilder = identityServerBuilder.AddDeveloperSigningCredential();
identityServerBuilder = identityServerBuilder.AddAbpDeveloperSigningCredential();
}

identityServerBuilder.AddAbpIdentityServer(builderOptions);
Expand Down