From a743159595e02b3010d3991d6983203407da158b Mon Sep 17 00:00:00 2001 From: icedream Date: Wed, 7 May 2014 11:21:46 -0400 Subject: [PATCH] Added AuthenticationHelper which handles remauth.php and /authenticate style token retrieval etc. --- libnpsharp.userprefs | 8 +- .../Authentication/AuthenticationHelper.cs | 109 ++++++++++++++++++ src/libnpsharp/libnpsharp.csproj | 1 + 3 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 src/libnpsharp/Authentication/AuthenticationHelper.cs diff --git a/libnpsharp.userprefs b/libnpsharp.userprefs index 337b019..db7fb2f 100644 --- a/libnpsharp.userprefs +++ b/libnpsharp.userprefs @@ -1,9 +1,11 @@  - + - - + + + + diff --git a/src/libnpsharp/Authentication/AuthenticationHelper.cs b/src/libnpsharp/Authentication/AuthenticationHelper.cs new file mode 100644 index 0000000..5d48b69 --- /dev/null +++ b/src/libnpsharp/Authentication/AuthenticationHelper.cs @@ -0,0 +1,109 @@ +using System; +using System.Net; +using System.IO; +using System.Text; +using System.Text.RegularExpressions; + +namespace NPSharp.Authentication +{ + public class AuthenticationHelper + { + private string _path; + private ushort _port; + private string _host; + + /// + /// Gets the username. + /// + /// The username. + public string Username { get; private set; } + + /// + /// Gets the user's e-mail address. + /// + /// The user's e-mail address. + public string UserEMail { get; private set; } + + /// + /// Gets the session token. + /// + /// The session token. + public string SessionToken { get; private set; } + + /// + /// Gets the user identifier. + /// + /// The user identifier. + public uint UserId { get; private set; } + + /// + /// Initializes a new instance of the class. + /// + /// Hostname of the authentication endpoint. + /// Port of the authentication endpoint. + /// Path of the authentication endpoint. + public AuthenticationHelper (string host, ushort port = 12003, string path = "/authenticate") + { + _host = host; + _port = port; + _path = path; + } + + /// + /// Authenticate the specified username and password. + /// + /// The username to use for authentication. + /// The password to use for authentication. + public void Authenticate (string username, string password) + { + var post = string.Format ("{0}&&{1}", username, password); + + var uri = new UriBuilder { + Scheme = "http", + Port = _port, + Host = _host, + Path = _path + }.Uri; + + var req = (HttpWebRequest)WebRequest.Create (uri); + using (var reqStream = req.GetRequestStream()) { + var buffer = Encoding.UTF8.GetBytes (post); + reqStream.Write (buffer, 0, post.Length); + reqStream.Flush (); + } + req.Method = "POST"; + //req.ContentType = "application/x-www-urlencodedform"; + req.AllowAutoRedirect = true; + + // Response will be in this syntax: + // (ok|fail)#text#userid#username#email#sessiontoken + var rx = new Regex ("^(?Pok|fail)#(?P.+)#(?P[0-9]+)#(?P.+)#(?P.+)#(?P.+)$"); + var resp = (HttpWebResponse)req.GetResponse (); + using (var respStream = resp.GetResponseStream()) { + using (var respReader = new StreamReader(respStream)) { + while (!respReader.EndOfStream) { + var line = respReader.ReadLine (); + + // DW response line found? + if (!rx.IsMatch (line)) + continue; + + // This is a DW response line, analyze + var rxm = rx.Match (line); + + // Login succeeded? + if (rxm.Groups ["status"].Value != "ok") + throw new Exception (rxm.Groups ["text"].Value); + + // Store all data + Username = rxm.Groups ["username"].Value; + UserEMail = rxm.Groups ["usermail"].Value; + SessionToken = rxm.Groups ["sessiontoken"].Value; + UserId = uint.Parse (rxm.Groups ["userid"].Value); + } + } + } + } + } +} + diff --git a/src/libnpsharp/libnpsharp.csproj b/src/libnpsharp/libnpsharp.csproj index d953e3a..58d5493 100644 --- a/src/libnpsharp/libnpsharp.csproj +++ b/src/libnpsharp/libnpsharp.csproj @@ -71,6 +71,7 @@ +