-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changing UMLS/VSAC API authentication
- Loading branch information
1 parent
c4a1ce9
commit 65d1eb0
Showing
22 changed files
with
281 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace Trifolia.DB.Migrations | ||
{ | ||
using System; | ||
using System.Data.Entity.Migrations; | ||
|
||
public partial class umls : DbMigration | ||
{ | ||
public override void Up() | ||
{ | ||
AddColumn("dbo.user", "umlsApiKey", c => c.String(maxLength: 255)); | ||
DropColumn("dbo.user", "umlsUsername"); | ||
DropColumn("dbo.user", "umlsPassword"); | ||
} | ||
|
||
public override void Down() | ||
{ | ||
AddColumn("dbo.user", "umlsPassword", c => c.String(maxLength: 255)); | ||
AddColumn("dbo.user", "umlsUsername", c => c.String(maxLength: 255)); | ||
DropColumn("dbo.user", "umlsApiKey"); | ||
} | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,113 +1,77 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Xml; | ||
using Trifolia.Config; | ||
using Trifolia.Logging; | ||
|
||
namespace Trifolia.Shared | ||
{ | ||
public class UmlsHelper | ||
{ | ||
private const string TGT_URL = "https://vsac.nlm.nih.gov/vsac/ws/Ticket"; | ||
private const string TGT_BODY_FORMAT = "username={0}&password={1}"; | ||
|
||
/// <summary> | ||
/// Authenticates the user with the VSAC using the credentials specified. | ||
/// </summary> | ||
/// <param name="username">The VSAC username</param> | ||
/// <param name="password">The VSAC password</param> | ||
/// <returns>True if authenticated, otherwise false.</returns> | ||
public static string Authenticate(string username, string password) | ||
public static string GetTicketGrantingTicket(string apiKey) | ||
{ | ||
HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(TGT_URL); | ||
string body = string.Format(TGT_BODY_FORMAT, username, password); | ||
byte[] rawBody = Encoding.UTF8.GetBytes(body); | ||
webRequest.Method = "POST"; | ||
webRequest.ContentType = "text/plain"; | ||
webRequest.ContentLength = rawBody.Length; | ||
|
||
using (var sw = webRequest.GetRequestStream()) | ||
{ | ||
sw.Write(rawBody, 0, rawBody.Length); | ||
} | ||
string url = AppSettings.UMLSTicketGrantingTicketURL; | ||
HttpWebRequest tgtRequest = (HttpWebRequest)HttpWebRequest.Create(url); | ||
tgtRequest.Method = "POST"; | ||
tgtRequest.ContentType = "application/x-www-form-urlencoded"; | ||
tgtRequest.Accept = "application/xml"; | ||
|
||
try | ||
{ | ||
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse(); | ||
|
||
if (response.StatusCode == HttpStatusCode.OK) | ||
using (StreamWriter sw = new StreamWriter(tgtRequest.GetRequestStream())) | ||
{ | ||
using (StreamReader sr = new StreamReader(response.GetResponseStream())) | ||
{ | ||
return sr.ReadToEnd(); | ||
} | ||
sw.Write("apikey=" + apiKey); | ||
} | ||
} | ||
catch (WebException wex) | ||
{ | ||
Log.For(typeof(UmlsHelper)).Error("Error authenticating with UMLS", wex); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static bool ValidateCredentials(string username, string password) | ||
{ | ||
string ticketGrantingTicket = Authenticate(username, password); | ||
return !string.IsNullOrEmpty(ticketGrantingTicket); | ||
} | ||
HttpWebResponse tgtResponse = (HttpWebResponse)tgtRequest.GetResponse(); | ||
|
||
public static bool ValidateLicense(string username, string password) | ||
{ | ||
string licenseCode = AppSettings.UmlsLicenseCode; | ||
string[] query = new string[] { | ||
"user=" + Uri.EscapeDataString(username), | ||
"password=" + Uri.EscapeDataString(password), | ||
"licenseCode=" + Uri.EscapeDataString(licenseCode) | ||
}; | ||
string url = AppSettings.UmlsValidateUrl + "?" + string.Join("&", query); | ||
HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(url); | ||
webRequest.Method = "POST"; | ||
webRequest.ContentType = "x-www-form-urlencoded"; | ||
webRequest.Accept = "application/xml"; | ||
if (tgtResponse.StatusCode != HttpStatusCode.Created) | ||
return null; | ||
|
||
var response = (HttpWebResponse) webRequest.GetResponse(); | ||
string location = tgtResponse.GetResponseHeader("Location"); | ||
|
||
if (response.StatusCode != HttpStatusCode.OK) | ||
return false; | ||
if (string.IsNullOrEmpty(location) || location.IndexOf("TGT") < 0) | ||
return null; | ||
|
||
using (StreamReader sr = new StreamReader(response.GetResponseStream())) | ||
return location.Substring(location.IndexOf("TGT")); | ||
} | ||
catch | ||
{ | ||
var responseContent = sr.ReadToEnd(); | ||
bool isValid = false; | ||
return null; | ||
} | ||
} | ||
|
||
if (responseContent.StartsWith("\"") && responseContent.EndsWith("\"")) | ||
responseContent = responseContent.Substring(0, responseContent.Length - 2); | ||
public static string GetServiceTicket(string tgt) | ||
{ | ||
HttpWebRequest serviceTicketRequest = (HttpWebRequest) HttpWebRequest.Create("https://utslogin.nlm.nih.gov/cas/v1/tickets/" + tgt); | ||
serviceTicketRequest.Method = "POST"; | ||
serviceTicketRequest.ContentType = "application/x-www-form-urlencoded"; | ||
|
||
try | ||
try | ||
{ | ||
using (StreamWriter sw = new StreamWriter(serviceTicketRequest.GetRequestStream())) | ||
{ | ||
XmlDocument doc = new XmlDocument(); | ||
doc.LoadXml(responseContent); | ||
sw.Write("service=http://umlsks.nlm.nih.gov"); | ||
} | ||
|
||
if (doc.DocumentElement.Name != "Result" || !Boolean.TryParse(doc.DocumentElement.InnerText, out isValid)) | ||
{ | ||
Log.For(typeof(UmlsHelper)).Error("Unexpected response from UMLS validation service: {0}", responseContent); | ||
return false; | ||
} | ||
HttpWebResponse stResponse = (HttpWebResponse)serviceTicketRequest.GetResponse(); | ||
|
||
return isValid; | ||
} | ||
catch (Exception ex) | ||
using (StreamReader sr = new StreamReader(stResponse.GetResponseStream())) | ||
{ | ||
Log.For(typeof(UmlsHelper)).Error("Error validation UMLS license for {0}", ex, username); | ||
return false; | ||
return sr.ReadToEnd(); | ||
} | ||
} | ||
catch | ||
{ | ||
return null; | ||
} | ||
} | ||
|
||
public static bool ValidateLicense(string apiKey) | ||
{ | ||
string tgt = GetTicketGrantingTicket(apiKey); | ||
if (string.IsNullOrEmpty(tgt)) return false; | ||
string serviceTicket = GetServiceTicket(tgt); | ||
return !string.IsNullOrEmpty(serviceTicket); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.