You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.6 KiB

  1. using Common.Models.DigitalSignature;
  2. using JsonWebToken;
  3. using System;
  4. using System.Configuration;
  5. using System.IO;
  6. namespace DigitalSignature
  7. {
  8. public class Utlis
  9. {
  10. private string publicKeyLocation {get;set;}
  11. private string privateKeyLocation { get; set; }
  12. public Utlis()
  13. {
  14. publicKeyLocation = ReadWebConfig("brac_Publickey", "");
  15. privateKeyLocation = ReadWebConfig("Jme_Privatekey", "");
  16. }
  17. //public static string MapPath(string path)
  18. //{
  19. // return Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
  20. // path);
  21. //}
  22. public static string ReadWebConfig(string key, string defVal)
  23. {
  24. try
  25. {
  26. return ConfigurationManager.AppSettings[key].ToString() == null ? defVal : ConfigurationManager.AppSettings[key].ToString();
  27. }
  28. catch
  29. {
  30. return defVal;
  31. }
  32. }
  33. public JwtResponse CreateToken(string payload)
  34. {
  35. JwtResponse jwtResponse = new JwtResponse();
  36. try
  37. {
  38. // RSA key for signing
  39. string pem = File.ReadAllText(@"D:\Office\SourceCodes\git-repo\JME\apis\ThirdPartyAPI\ThirdPartyAPIs\Keys\Brac\Jme_private_uat.pem");
  40. var signatureKey = Jwk.FromPem(pem);
  41. // RSA key for encryption
  42. //pem = File.ReadAllText(MapPath(publicKeyLocation));
  43. var encryptionKey = Jwk.FromPem(pem);
  44. string issuer = "testissuer";
  45. string subject = "testorgid";
  46. var now = DateTime.Now;
  47. var unixTimeSeconds = new DateTimeOffset(now).ToUnixTimeSeconds();
  48. var jws = new JwsDescriptor()
  49. {
  50. SigningKey = signatureKey,
  51. Algorithm = SignatureAlgorithm.RsaSha256,
  52. ExpirationTime = now.AddMinutes(1440),
  53. IssuedAt = now,
  54. Issuer = issuer,
  55. NotBefore = DateTime.Now,
  56. Subject = subject,
  57. Payload = new JwtObject()
  58. };
  59. jws.Payload.Add("Payload", payload);
  60. jws.AddClaim("iss", issuer);
  61. // Creates a JWE descriptor with all its properties
  62. var descriptor = new JweDescriptor<JwsDescriptor>()
  63. {
  64. EncryptionKey = encryptionKey,
  65. EncryptionAlgorithm = EncryptionAlgorithm.Aes256CbcHmacSha512,
  66. Algorithm = KeyManagementAlgorithm.RsaOaep256,
  67. Payload = jws
  68. };
  69. // Generates the UTF-8 string representation of the JWT
  70. var writer = new JwtWriter();
  71. var token = writer.WriteTokenString(descriptor);
  72. jwtResponse.Token = token;
  73. jwtResponse.ExpiresAt = unixTimeSeconds;
  74. return jwtResponse;
  75. }
  76. catch (Exception ex)
  77. {
  78. // write exception logs to database
  79. return jwtResponse;
  80. }
  81. }
  82. public string ExtractPayloadFromToken(string token)
  83. {
  84. string jwtResponse = string.Empty;
  85. try
  86. {
  87. // RSA key for verification
  88. string pem = File.ReadAllText(publicKeyLocation);
  89. var signatureKey = Jwk.FromPem(pem);
  90. // RSA key for decryption
  91. pem = File.ReadAllText(privateKeyLocation);
  92. var decryptionKey = Jwk.FromPem(pem);
  93. string issuer = "testissuer";
  94. var policy = new TokenValidationPolicyBuilder()
  95. .RequireSignature(signatureKey, SignatureAlgorithm.RsaSha256)
  96. .Build();
  97. var reader = new JwtReader(decryptionKey);
  98. var result = reader.TryReadToken(token, policy);
  99. if (result.Succedeed)
  100. {
  101. JwtProperty jwtProp = new JwtProperty();
  102. var hasPayload = result.Token?.Payload?.TryGetValue("Payload", out jwtProp);
  103. if (hasPayload != null && hasPayload.Value)
  104. {
  105. jwtResponse = jwtProp.Value.ToString();
  106. }
  107. }
  108. return jwtResponse;
  109. }
  110. catch (Exception ex)
  111. {
  112. // write exception logs to database
  113. return jwtResponse;
  114. }
  115. }
  116. }
  117. }