-
Notifications
You must be signed in to change notification settings - Fork 1
/
IzendaTokenAuthorization.cs
executable file
·140 lines (118 loc) · 5.22 KB
/
IzendaTokenAuthorization.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using Mvc5StarterKit.Models;
using System;
using System.Configuration;
namespace Mvc5StarterKit.IzendaBoundary
{
public class IzendaTokenAuthorization
{
#region Constants
private const string KEY = "THISISKEY1234567"; //must be at least 16 characters long (128 bits)
private readonly static string IzendaAdminUserName = "IzendaAdmin@system.com";
#endregion
#region Methods
/// <summary>
/// Generate token from UserInfo. Userinfo will be encrypted before sending to Izenda.
/// </summary>
public static string GetToken(UserInfo user)
{
// remove tenant property when sending token to Izenda, if Tenant is System.
if (user.TenantUniqueName == "System")
user.TenantUniqueName = null;
var serializedObject = Newtonsoft.Json.JsonConvert.SerializeObject(user);
var token = StringCipher.Encrypt(serializedObject, KEY);
return token;
}
/// <summary>
/// Get User info from token. Token, which recieved from Izenda, will be decrypted to get user info.
/// </summary>
public static UserInfo GetUserInfo(string token)
{
var serializedObject = StringCipher.Decrypt(token, KEY);
var user = Newtonsoft.Json.JsonConvert.DeserializeObject<UserInfo>(serializedObject);
return user;
}
/// <summary>
/// Get the token for IzendaAdmin user, to communicate with Izenda to process when user has not been logged in.
/// </summary>
public static string GetIzendaAdminToken() => GetToken(new UserInfo { UserName = IzendaAdminUserName });
public static UserInfo DecryptIzendaAuthenticationMessage(string encryptedMessage)
{
var rsaPrivateKey = ConfigurationManager.AppSettings["RSAPrivateKey"];
var cipher = new System.Security.Cryptography.RSACryptoServiceProvider();
//Decrypt using RSA private key in PEM format.
var rsaParam = ConvertPemToXmlFormat(rsaPrivateKey);
cipher.ImportParameters(rsaParam);
//End
var resultBytes = Convert.FromBase64String(encryptedMessage);
var decryptedBytes = cipher.Decrypt(resultBytes, false);
var decryptedData = System.Text.Encoding.UTF8.GetString(decryptedBytes);
var result = Newtonsoft.Json.JsonConvert.DeserializeObject<UserInfo>(decryptedData);
return result;
}
//Support to convert RSA key from PEM to XML, currently RSACryptoServiceProvider only support XML format.
private static System.Security.Cryptography.RSAParameters ConvertPemToXmlFormat(string privateKey)
{
var privateKeyBits = System.Convert.FromBase64String(privateKey);
var rsaParams = new System.Security.Cryptography.RSAParameters();
using (var binr = new System.IO.BinaryReader(new System.IO.MemoryStream(privateKeyBits)))
{
byte bt = 0;
ushort twobytes = 0;
twobytes = binr.ReadUInt16();
if (twobytes == 0x8130)
binr.ReadByte();
else if (twobytes == 0x8230)
binr.ReadInt16();
else
throw new Exception("Unexpected value read binr.ReadUInt16()");
twobytes = binr.ReadUInt16();
if (twobytes != 0x0102)
throw new Exception("Unexpected version");
bt = binr.ReadByte();
if (bt != 0x00)
throw new Exception("Unexpected value read binr.ReadByte()");
rsaParams.Modulus = binr.ReadBytes(GetIntegerSize(binr));
rsaParams.Exponent = binr.ReadBytes(GetIntegerSize(binr));
rsaParams.D = binr.ReadBytes(GetIntegerSize(binr));
rsaParams.P = binr.ReadBytes(GetIntegerSize(binr));
rsaParams.Q = binr.ReadBytes(GetIntegerSize(binr));
rsaParams.DP = binr.ReadBytes(GetIntegerSize(binr));
rsaParams.DQ = binr.ReadBytes(GetIntegerSize(binr));
rsaParams.InverseQ = binr.ReadBytes(GetIntegerSize(binr));
}
return rsaParams;
}
private static int GetIntegerSize(System.IO.BinaryReader binr)
{
byte bt = 0;
byte lowbyte = 0x00;
byte highbyte = 0x00;
int count = 0;
bt = binr.ReadByte();
if (bt != 0x02)
return 0;
bt = binr.ReadByte();
if (bt == 0x81)
count = binr.ReadByte();
else
if (bt == 0x82)
{
highbyte = binr.ReadByte();
lowbyte = binr.ReadByte();
byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };
count = BitConverter.ToInt32(modint, 0);
}
else
{
count = bt;
}
while (binr.ReadByte() == 0x00)
{
count -= 1;
}
binr.BaseStream.Seek(-1, System.IO.SeekOrigin.Current);
return count;
}
#endregion
}
}