Skip to content

Commit a0ae36b

Browse files
authored
Merge pull request #1056 from GekySan/master
Implemented RSA signing for JWT
2 parents 5123dac + a91922c commit a0ae36b

File tree

2 files changed

+59
-13
lines changed

2 files changed

+59
-13
lines changed

RuriLib/Blocks/Functions/Crypto/Methods.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public static string AESDecryptString(BotData data, byte[] cipherText, byte[] ke
217217
}
218218

219219
[Block("Generates a JSON Web Token using a secret key, payload, optional extra headers and specified algorithm type",
220-
name = "JWT Encode", extraInfo = "The header already contains the selected algorithm and token type (JWT) by default")]
220+
name = "JWT Encode", extraInfo = "The header already contains the selected algorithm and token type (JWT) by default. For JWTs using asymmetric key signatures, the secret must be provided in PEM format.")]
221221
public static string JwtEncode(BotData data, JwtAlgorithmName algorithm, string secret, string extraHeaders = "{}", string payload = "{}")
222222
{
223223
var extraHeadersDictionary = JsonConvert.DeserializeObject<Dictionary<string, object>>(extraHeaders);

RuriLib/Functions/Crypto/Crypto.cs

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -626,19 +626,65 @@ private static byte[] PerformCryptography(byte[] data, ICryptoTransform cryptoTr
626626
#region JWT
627627
public static string JwtEncode(JwtAlgorithmName algorithmName, string secret, IDictionary<string, object> extraHeaders, IDictionary<string, object> payload)
628628
{
629-
IJwtAlgorithm algorithm = algorithmName switch
629+
IJwtAlgorithm algorithm = null;
630+
RSA rsa = null;
631+
try
630632
{
631-
JwtAlgorithmName.HS256 => new HMACSHA256Algorithm(),
632-
JwtAlgorithmName.HS384 => new HMACSHA384Algorithm(),
633-
JwtAlgorithmName.HS512 => new HMACSHA512Algorithm(),
634-
_ => throw new NotSupportedException("This algorithm is not supported at the moment")
635-
};
636-
637-
var jsonSerializer = new JsonNetSerializer();
638-
var urlEncoder = new JwtBase64UrlEncoder();
639-
var jwtEncoder = new JwtEncoder(algorithm, jsonSerializer, urlEncoder);
640-
641-
return jwtEncoder.Encode(extraHeaders, payload, secret);
633+
switch (algorithmName)
634+
{
635+
case JwtAlgorithmName.HS256:
636+
algorithm = new HMACSHA256Algorithm();
637+
break;
638+
case JwtAlgorithmName.HS384:
639+
algorithm = new HMACSHA384Algorithm();
640+
break;
641+
case JwtAlgorithmName.HS512:
642+
algorithm = new HMACSHA512Algorithm();
643+
break;
644+
case JwtAlgorithmName.RS256:
645+
rsa = RSA.Create();
646+
rsa.ImportFromPem(secret.ToCharArray());
647+
algorithm = new RS256Algorithm(rsa, rsa);
648+
break;
649+
case JwtAlgorithmName.RS384:
650+
rsa = RSA.Create();
651+
rsa.ImportFromPem(secret.ToCharArray());
652+
algorithm = new RS384Algorithm(rsa, rsa);
653+
break;
654+
case JwtAlgorithmName.RS512:
655+
rsa = RSA.Create();
656+
rsa.ImportFromPem(secret.ToCharArray());
657+
algorithm = new RS512Algorithm(rsa, rsa);
658+
break;
659+
case JwtAlgorithmName.RS1024:
660+
rsa = RSA.Create();
661+
rsa.ImportFromPem(secret.ToCharArray());
662+
algorithm = new RS1024Algorithm(rsa, rsa);
663+
break;
664+
case JwtAlgorithmName.RS2048:
665+
rsa = RSA.Create();
666+
rsa.ImportFromPem(secret.ToCharArray());
667+
algorithm = new RS2048Algorithm(rsa, rsa);
668+
break;
669+
case JwtAlgorithmName.RS4096:
670+
rsa = RSA.Create();
671+
rsa.ImportFromPem(secret.ToCharArray());
672+
algorithm = new RS4096Algorithm(rsa, rsa);
673+
break;
674+
default:
675+
throw new NotSupportedException("This algorithm is not supported at the moment");
676+
}
677+
678+
var jsonSerializer = new JsonNetSerializer();
679+
var urlEncoder = new JwtBase64UrlEncoder();
680+
var jwtEncoder = new JwtEncoder(algorithm, jsonSerializer, urlEncoder);
681+
682+
return jwtEncoder.Encode(extraHeaders, payload, secret);
683+
}
684+
finally
685+
{
686+
rsa?.Dispose();
687+
}
642688
}
643689
#endregion
644690

0 commit comments

Comments
 (0)