483 lines
14 KiB
C#
483 lines
14 KiB
C#
using System;
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
using System.Net.Http;
|
|
using System.Security.Cryptography;
|
|
using KGdev.BI3D.Revit.Common;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
namespace KGdev.BI3D.Revit.Implementations
|
|
{
|
|
internal class DefaultAuthProvider : IAuthProvider
|
|
{
|
|
public DefaultAuthProvider(
|
|
string productId,
|
|
IMuidProvider muidProvider,
|
|
IGlobalStore globalStore
|
|
)
|
|
{
|
|
this.productId = productId;
|
|
this.muidProvider = muidProvider;
|
|
this.globalStore = globalStore;
|
|
}
|
|
|
|
public bool IsTrial(string token)
|
|
{
|
|
JwtSecurityToken securityToken = this.GetSecurityToken(token);
|
|
bool flag = !securityToken.Payload.ContainsKey("META_LICENSE_TYPE");
|
|
bool flag2;
|
|
if (flag)
|
|
{
|
|
flag2 = false;
|
|
}
|
|
else
|
|
{
|
|
string text = securityToken.Payload["META_LICENSE_TYPE"].ToString();
|
|
flag2 = text == "TRIAL";
|
|
}
|
|
return flag2;
|
|
}
|
|
|
|
public int LicenseTypeInt(string token)
|
|
{
|
|
int num = 1;
|
|
JwtSecurityToken securityToken = this.GetSecurityToken(token);
|
|
bool flag = securityToken.Payload.ContainsKey("META_LICENSE_TYPE");
|
|
if (flag)
|
|
{
|
|
string text = securityToken.Payload["META_LICENSE_TYPE"].ToString();
|
|
bool flag2 = text == "TRIAL";
|
|
if (flag2)
|
|
{
|
|
num = 0;
|
|
}
|
|
else
|
|
{
|
|
bool flag3 = text == "PERSONAL";
|
|
if (flag3)
|
|
{
|
|
num = 1;
|
|
}
|
|
else
|
|
{
|
|
bool flag4 = text == "REVIEW";
|
|
if (flag4)
|
|
{
|
|
num = 2;
|
|
}
|
|
else
|
|
{
|
|
bool flag5 = text == "ENTERPRISE";
|
|
if (flag5)
|
|
{
|
|
num = 3;
|
|
}
|
|
else
|
|
{
|
|
bool flag6 = text == "ROYALTY_FREE";
|
|
if (flag6)
|
|
{
|
|
num = 4;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return num;
|
|
}
|
|
|
|
public bool TryUpdateToken(string key)
|
|
{
|
|
bool flag2;
|
|
try
|
|
{
|
|
string muid = this.muidProvider.GetMuid();
|
|
string text = string.Concat(
|
|
new string[]
|
|
{
|
|
"https://consumer.licensing.kg-dev.be/api/LicenseToken?key=",
|
|
key,
|
|
"&client=",
|
|
muid,
|
|
"&productreference=",
|
|
this.productId
|
|
}
|
|
);
|
|
HttpClient httpClient = new HttpClient();
|
|
HttpResponseMessage result = httpClient.GetAsync(text).Result;
|
|
string result2 = result.Content.ReadAsStringAsync().Result;
|
|
JObject jobject = null;
|
|
try
|
|
{
|
|
jobject = JObject.Parse(result2);
|
|
}
|
|
catch
|
|
{
|
|
throw new Exception("Failed to parse: " + result2);
|
|
}
|
|
JToken jtoken = null;
|
|
jobject.TryGetValue("token", out jtoken);
|
|
bool flag = jtoken == null;
|
|
if (flag)
|
|
{
|
|
throw new Exception("Response does not contain a token.");
|
|
}
|
|
string text2 = jtoken.Value<string>();
|
|
this.globalStore.SetToken(text2);
|
|
flag2 = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
string token = this.globalStore.GetToken();
|
|
bool flag3 = this.ValidateToken(token);
|
|
bool flag4 = !flag3;
|
|
if (flag4)
|
|
{
|
|
this.globalStore.SetToken(
|
|
(ex.InnerException != null) ? ex.InnerException.Message : ex.Message
|
|
);
|
|
}
|
|
flag2 = false;
|
|
}
|
|
return flag2;
|
|
}
|
|
|
|
public bool ValidateToken(string token)
|
|
{
|
|
bool flag3;
|
|
try
|
|
{
|
|
JwtSecurityToken securityToken = this.GetSecurityToken(token);
|
|
bool flag = !securityToken.Payload.ContainsKey("certserialnumber");
|
|
if (flag)
|
|
{
|
|
throw new Exception("No certserialnumber given.");
|
|
}
|
|
string text = securityToken.Payload["certserialnumber"].ToString();
|
|
string muid = this.muidProvider.GetMuid();
|
|
bool flag2 =
|
|
string.IsNullOrWhiteSpace(muid)
|
|
|| string.IsNullOrWhiteSpace(text)
|
|
|| muid != text;
|
|
if (flag2)
|
|
{
|
|
throw new Exception("Not Licensed.");
|
|
}
|
|
flag3 = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
flag3 = false;
|
|
}
|
|
return flag3;
|
|
}
|
|
|
|
private JwtSecurityToken GetSecurityToken(string token)
|
|
{
|
|
RSAParameters rsaparameters = new RSAParameters
|
|
{
|
|
Modulus = new byte[]
|
|
{
|
|
158,
|
|
172,
|
|
231,
|
|
226,
|
|
231,
|
|
91,
|
|
212,
|
|
107,
|
|
240,
|
|
26,
|
|
173,
|
|
150,
|
|
233,
|
|
20,
|
|
187,
|
|
54,
|
|
239,
|
|
143,
|
|
195,
|
|
143,
|
|
77,
|
|
126,
|
|
138,
|
|
22,
|
|
181,
|
|
236,
|
|
24,
|
|
20,
|
|
3,
|
|
224,
|
|
189,
|
|
11,
|
|
236,
|
|
47,
|
|
58,
|
|
114,
|
|
141,
|
|
189,
|
|
239,
|
|
40,
|
|
208,
|
|
181,
|
|
43,
|
|
209,
|
|
40,
|
|
144,
|
|
218,
|
|
84,
|
|
117,
|
|
207,
|
|
2,
|
|
190,
|
|
103,
|
|
137,
|
|
213,
|
|
129,
|
|
224,
|
|
241,
|
|
173,
|
|
144,
|
|
242,
|
|
6,
|
|
86,
|
|
156,
|
|
183,
|
|
160,
|
|
59,
|
|
192,
|
|
96,
|
|
122,
|
|
33,
|
|
215,
|
|
20,
|
|
8,
|
|
55,
|
|
189,
|
|
92,
|
|
8,
|
|
165,
|
|
48,
|
|
126,
|
|
0,
|
|
64,
|
|
161,
|
|
240,
|
|
3,
|
|
79,
|
|
196,
|
|
238,
|
|
226,
|
|
193,
|
|
246,
|
|
133,
|
|
181,
|
|
43,
|
|
214,
|
|
147,
|
|
31,
|
|
132,
|
|
157,
|
|
19,
|
|
22,
|
|
43,
|
|
246,
|
|
5,
|
|
40,
|
|
172,
|
|
79,
|
|
98,
|
|
121,
|
|
66,
|
|
46,
|
|
99,
|
|
251,
|
|
194,
|
|
237,
|
|
108,
|
|
235,
|
|
179,
|
|
75,
|
|
225,
|
|
166,
|
|
206,
|
|
215,
|
|
115,
|
|
17,
|
|
145,
|
|
188,
|
|
36,
|
|
129,
|
|
49,
|
|
216,
|
|
240,
|
|
177,
|
|
91,
|
|
216,
|
|
59,
|
|
200,
|
|
166,
|
|
108,
|
|
195,
|
|
105,
|
|
248,
|
|
137,
|
|
61,
|
|
159,
|
|
167,
|
|
40,
|
|
18,
|
|
154,
|
|
190,
|
|
40,
|
|
136,
|
|
32,
|
|
59,
|
|
239,
|
|
40,
|
|
239,
|
|
130,
|
|
32,
|
|
223,
|
|
81,
|
|
167,
|
|
30,
|
|
201,
|
|
151,
|
|
86,
|
|
95,
|
|
128,
|
|
35,
|
|
1,
|
|
215,
|
|
128,
|
|
178,
|
|
21,
|
|
169,
|
|
28,
|
|
71,
|
|
154,
|
|
139,
|
|
91,
|
|
52,
|
|
27,
|
|
87,
|
|
31,
|
|
58,
|
|
21,
|
|
169,
|
|
159,
|
|
156,
|
|
189,
|
|
84,
|
|
79,
|
|
185,
|
|
120,
|
|
109,
|
|
24,
|
|
83,
|
|
220,
|
|
122,
|
|
68,
|
|
213,
|
|
220,
|
|
50,
|
|
56,
|
|
188,
|
|
176,
|
|
52,
|
|
140,
|
|
54,
|
|
64,
|
|
161,
|
|
31,
|
|
55,
|
|
246,
|
|
32,
|
|
246,
|
|
141,
|
|
186,
|
|
183,
|
|
107,
|
|
102,
|
|
235,
|
|
139,
|
|
147,
|
|
81,
|
|
15,
|
|
231,
|
|
44,
|
|
193,
|
|
7,
|
|
117,
|
|
251,
|
|
byte.MaxValue,
|
|
53,
|
|
98,
|
|
187,
|
|
41,
|
|
213,
|
|
157,
|
|
92,
|
|
byte.MaxValue,
|
|
222,
|
|
71,
|
|
7,
|
|
69,
|
|
85,
|
|
225,
|
|
237,
|
|
117,
|
|
142,
|
|
164,
|
|
48,
|
|
96,
|
|
220,
|
|
191
|
|
},
|
|
Exponent = new byte[] { 1, 0, 1 }
|
|
};
|
|
RSACryptoServiceProvider rsacryptoServiceProvider = new RSACryptoServiceProvider(2048);
|
|
rsacryptoServiceProvider.ImportParameters(rsaparameters);
|
|
RsaSecurityKey rsaSecurityKey = new RsaSecurityKey(rsacryptoServiceProvider);
|
|
TokenValidationParameters tokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
IssuerSigningKey = rsaSecurityKey,
|
|
RequireSignedTokens = true,
|
|
RequireExpirationTime = true,
|
|
ValidateLifetime = true,
|
|
ValidateAudience = true,
|
|
ValidAudiences = new string[] { this.productId },
|
|
ValidateIssuer = false
|
|
};
|
|
JwtSecurityTokenHandler jwtSecurityTokenHandler = new JwtSecurityTokenHandler();
|
|
SecurityToken securityToken;
|
|
jwtSecurityTokenHandler.ValidateToken(
|
|
token,
|
|
tokenValidationParameters,
|
|
out securityToken
|
|
);
|
|
return (JwtSecurityToken)securityToken;
|
|
}
|
|
|
|
private const string LICENSE_TYPE_CLAIM_NAME = "META_LICENSE_TYPE";
|
|
|
|
private const string TRIAL = "TRIAL";
|
|
|
|
private const string PERSONAL = "PERSONAL";
|
|
|
|
private const string REVIEW = "REVIEW";
|
|
|
|
private const string ENTERPRISE = "ENTERPRISE";
|
|
|
|
private const string ROYALTY_FREE = "ROYALTY_FREE";
|
|
|
|
private readonly string productId;
|
|
|
|
private readonly IMuidProvider muidProvider;
|
|
|
|
private readonly IGlobalStore globalStore;
|
|
}
|
|
}
|