|
1 | 1 | using System; |
2 | | -using System.Collections.Generic; |
3 | 2 | using System.IO; |
4 | | -using System.Linq; |
5 | 3 | using System.Net; |
6 | | -using System.Text; |
7 | | -using System.Threading.Tasks; |
8 | | -using System.Xml; |
9 | 4 | using Trifolia.Config; |
10 | | -using Trifolia.Logging; |
11 | 5 |
|
12 | 6 | namespace Trifolia.Shared |
13 | 7 | { |
14 | 8 | public class UmlsHelper |
15 | 9 | { |
16 | | - private const string TGT_URL = "https://vsac.nlm.nih.gov/vsac/ws/Ticket"; |
17 | | - private const string TGT_BODY_FORMAT = "username={0}&password={1}"; |
18 | | - |
19 | | - /// <summary> |
20 | | - /// Authenticates the user with the VSAC using the credentials specified. |
21 | | - /// </summary> |
22 | | - /// <param name="username">The VSAC username</param> |
23 | | - /// <param name="password">The VSAC password</param> |
24 | | - /// <returns>True if authenticated, otherwise false.</returns> |
25 | | - public static string Authenticate(string username, string password) |
| 10 | + public static string GetTicketGrantingTicket(string apiKey) |
26 | 11 | { |
27 | | - HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(TGT_URL); |
28 | | - string body = string.Format(TGT_BODY_FORMAT, username, password); |
29 | | - byte[] rawBody = Encoding.UTF8.GetBytes(body); |
30 | | - webRequest.Method = "POST"; |
31 | | - webRequest.ContentType = "text/plain"; |
32 | | - webRequest.ContentLength = rawBody.Length; |
33 | | - |
34 | | - using (var sw = webRequest.GetRequestStream()) |
35 | | - { |
36 | | - sw.Write(rawBody, 0, rawBody.Length); |
37 | | - } |
| 12 | + string url = AppSettings.UMLSTicketGrantingTicketURL; |
| 13 | + HttpWebRequest tgtRequest = (HttpWebRequest)HttpWebRequest.Create(url); |
| 14 | + tgtRequest.Method = "POST"; |
| 15 | + tgtRequest.ContentType = "application/x-www-form-urlencoded"; |
| 16 | + tgtRequest.Accept = "application/xml"; |
38 | 17 |
|
39 | 18 | try |
40 | 19 | { |
41 | | - HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse(); |
42 | | - |
43 | | - if (response.StatusCode == HttpStatusCode.OK) |
| 20 | + using (StreamWriter sw = new StreamWriter(tgtRequest.GetRequestStream())) |
44 | 21 | { |
45 | | - using (StreamReader sr = new StreamReader(response.GetResponseStream())) |
46 | | - { |
47 | | - return sr.ReadToEnd(); |
48 | | - } |
| 22 | + sw.Write("apikey=" + apiKey); |
49 | 23 | } |
50 | | - } |
51 | | - catch (WebException wex) |
52 | | - { |
53 | | - Log.For(typeof(UmlsHelper)).Error("Error authenticating with UMLS", wex); |
54 | | - } |
55 | | - |
56 | | - return null; |
57 | | - } |
58 | 24 |
|
59 | | - public static bool ValidateCredentials(string username, string password) |
60 | | - { |
61 | | - string ticketGrantingTicket = Authenticate(username, password); |
62 | | - return !string.IsNullOrEmpty(ticketGrantingTicket); |
63 | | - } |
| 25 | + HttpWebResponse tgtResponse = (HttpWebResponse)tgtRequest.GetResponse(); |
64 | 26 |
|
65 | | - public static bool ValidateLicense(string username, string password) |
66 | | - { |
67 | | - string licenseCode = AppSettings.UmlsLicenseCode; |
68 | | - string[] query = new string[] { |
69 | | - "user=" + Uri.EscapeDataString(username), |
70 | | - "password=" + Uri.EscapeDataString(password), |
71 | | - "licenseCode=" + Uri.EscapeDataString(licenseCode) |
72 | | - }; |
73 | | - string url = AppSettings.UmlsValidateUrl + "?" + string.Join("&", query); |
74 | | - HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(url); |
75 | | - webRequest.Method = "POST"; |
76 | | - webRequest.ContentType = "x-www-form-urlencoded"; |
77 | | - webRequest.Accept = "application/xml"; |
| 27 | + if (tgtResponse.StatusCode != HttpStatusCode.Created) |
| 28 | + return null; |
78 | 29 |
|
79 | | - var response = (HttpWebResponse) webRequest.GetResponse(); |
| 30 | + string location = tgtResponse.GetResponseHeader("Location"); |
80 | 31 |
|
81 | | - if (response.StatusCode != HttpStatusCode.OK) |
82 | | - return false; |
| 32 | + if (string.IsNullOrEmpty(location) || location.IndexOf("TGT") < 0) |
| 33 | + return null; |
83 | 34 |
|
84 | | - using (StreamReader sr = new StreamReader(response.GetResponseStream())) |
| 35 | + return location.Substring(location.IndexOf("TGT")); |
| 36 | + } |
| 37 | + catch |
85 | 38 | { |
86 | | - var responseContent = sr.ReadToEnd(); |
87 | | - bool isValid = false; |
| 39 | + return null; |
| 40 | + } |
| 41 | + } |
88 | 42 |
|
89 | | - if (responseContent.StartsWith("\"") && responseContent.EndsWith("\"")) |
90 | | - responseContent = responseContent.Substring(0, responseContent.Length - 2); |
| 43 | + public static string GetServiceTicket(string tgt) |
| 44 | + { |
| 45 | + HttpWebRequest serviceTicketRequest = (HttpWebRequest) HttpWebRequest.Create("https://utslogin.nlm.nih.gov/cas/v1/tickets/" + tgt); |
| 46 | + serviceTicketRequest.Method = "POST"; |
| 47 | + serviceTicketRequest.ContentType = "application/x-www-form-urlencoded"; |
91 | 48 |
|
92 | | - try |
| 49 | + try |
| 50 | + { |
| 51 | + using (StreamWriter sw = new StreamWriter(serviceTicketRequest.GetRequestStream())) |
93 | 52 | { |
94 | | - XmlDocument doc = new XmlDocument(); |
95 | | - doc.LoadXml(responseContent); |
| 53 | + sw.Write("service=http://umlsks.nlm.nih.gov"); |
| 54 | + } |
96 | 55 |
|
97 | | - if (doc.DocumentElement.Name != "Result" || !Boolean.TryParse(doc.DocumentElement.InnerText, out isValid)) |
98 | | - { |
99 | | - Log.For(typeof(UmlsHelper)).Error("Unexpected response from UMLS validation service: {0}", responseContent); |
100 | | - return false; |
101 | | - } |
| 56 | + HttpWebResponse stResponse = (HttpWebResponse)serviceTicketRequest.GetResponse(); |
102 | 57 |
|
103 | | - return isValid; |
104 | | - } |
105 | | - catch (Exception ex) |
| 58 | + using (StreamReader sr = new StreamReader(stResponse.GetResponseStream())) |
106 | 59 | { |
107 | | - Log.For(typeof(UmlsHelper)).Error("Error validation UMLS license for {0}", ex, username); |
108 | | - return false; |
| 60 | + return sr.ReadToEnd(); |
109 | 61 | } |
110 | 62 | } |
| 63 | + catch |
| 64 | + { |
| 65 | + return null; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public static bool ValidateLicense(string apiKey) |
| 70 | + { |
| 71 | + string tgt = GetTicketGrantingTicket(apiKey); |
| 72 | + if (string.IsNullOrEmpty(tgt)) return false; |
| 73 | + string serviceTicket = GetServiceTicket(tgt); |
| 74 | + return !string.IsNullOrEmpty(serviceTicket); |
111 | 75 | } |
112 | 76 | } |
113 | 77 | } |
0 commit comments