Skip to content

Commit ea878af

Browse files
CADBIMDeveloperabatishchev
authored andcommitted
polish - remove code duplications
1 parent 564d4e1 commit ea878af

File tree

1 file changed

+10
-25
lines changed

1 file changed

+10
-25
lines changed

src/JWT/Jwk/JwtJsonWebKeyAlgorithmFactory.cs

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public IJwtAlgorithm Create(JwtDecoderContext context)
3333

3434
private IJwtAlgorithm CreateRSAAlgorithm(JwtDecoderContext context)
3535
{
36-
var publicKey = CreateRSAPublicKey();
36+
var publicKey = CreateRSAKey(false);
3737

38-
var privateKey = CreateRSAPrivateKey();
38+
var privateKey = CreateRSAKey(true);
3939

4040
var algorithmFactory = privateKey == null
4141
? new RSAlgorithmFactory(publicKey)
@@ -80,38 +80,23 @@ private IJwtAlgorithm CreateHMACSHAAlgorithm(JwtDecoderContext context)
8080
return algorithmFactory.Create(context);
8181
}
8282

83-
private RSA CreateRSAPublicKey()
84-
{
85-
var rsaParameters = new RSAParameters
86-
{
87-
Modulus = JwtWebKeyPropertyValuesEncoder.Base64UrlDecode(_key.Modulus),
88-
Exponent = JwtWebKeyPropertyValuesEncoder.Base64UrlDecode(_key.Exponent)
89-
};
90-
91-
var key = RSA.Create();
92-
93-
key.ImportParameters(rsaParameters);
94-
95-
return key;
96-
}
97-
98-
private RSA CreateRSAPrivateKey()
83+
private RSA CreateRSAKey(bool privateKey)
9984
{
10085
var firstPrimeFactor = JwtWebKeyPropertyValuesEncoder.Base64UrlDecode(_key.FirstPrimeFactor);
10186

102-
if (firstPrimeFactor == null)
87+
if (privateKey && firstPrimeFactor == null)
10388
return null;
10489

10590
var rsaParameters = new RSAParameters
10691
{
10792
Modulus = JwtWebKeyPropertyValuesEncoder.Base64UrlDecode(_key.Modulus),
10893
Exponent = JwtWebKeyPropertyValuesEncoder.Base64UrlDecode(_key.Exponent),
109-
P = firstPrimeFactor,
110-
Q = JwtWebKeyPropertyValuesEncoder.Base64UrlDecode(_key.SecondPrimeFactor),
111-
D = JwtWebKeyPropertyValuesEncoder.Base64UrlDecode(_key.D),
112-
DP = JwtWebKeyPropertyValuesEncoder.Base64UrlDecode(_key.FirstFactorCRTExponent),
113-
DQ = JwtWebKeyPropertyValuesEncoder.Base64UrlDecode(_key.SecondFactorCRTExponent),
114-
InverseQ = JwtWebKeyPropertyValuesEncoder.Base64UrlDecode(_key.FirstCRTCoefficient)
94+
P = privateKey ? firstPrimeFactor : null,
95+
Q = privateKey ? JwtWebKeyPropertyValuesEncoder.Base64UrlDecode(_key.SecondPrimeFactor) : null,
96+
D = privateKey ? JwtWebKeyPropertyValuesEncoder.Base64UrlDecode(_key.D) : null,
97+
DP = privateKey ? JwtWebKeyPropertyValuesEncoder.Base64UrlDecode(_key.FirstFactorCRTExponent) : null,
98+
DQ = privateKey ? JwtWebKeyPropertyValuesEncoder.Base64UrlDecode(_key.SecondFactorCRTExponent) : null,
99+
InverseQ = privateKey ? JwtWebKeyPropertyValuesEncoder.Base64UrlDecode(_key.FirstCRTCoefficient) : null
115100
};
116101

117102
var key = RSA.Create();

0 commit comments

Comments
 (0)