mirror of https://github.com/icedream/npsharp.git
Added AuthenticationHelper which handles remauth.php and /authenticate style token retrieval etc.
parent
e536b3a114
commit
a743159595
|
@ -1,9 +1,11 @@
|
||||||
<Properties>
|
<Properties>
|
||||||
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
|
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
|
||||||
<MonoDevelop.Ide.Workbench ActiveDocument="src/libnpsharp/NpClient.cs">
|
<MonoDevelop.Ide.Workbench ActiveDocument="src/libnpsharp/Authentication/AuthenticationHelper.cs">
|
||||||
<Files>
|
<Files>
|
||||||
<File FileName="src/libnpsharp/Rpc/Packets/AuthenticateExternalStatusMessage.cs" Line="1" Column="1" />
|
<File FileName="src/libnpsharp/Rpc/Packets/AuthenticateExternalStatusMessage.cs" Line="7" Column="8" />
|
||||||
<File FileName="src/libnpsharp/NpClient.cs" Line="31" Column="45" />
|
<File FileName="src/libnpsharp/NpClient.cs" Line="160" Column="16" />
|
||||||
|
<File FileName="src/libnpsharp/Rpc/RpcClientStream.cs" Line="58" Column="36" />
|
||||||
|
<File FileName="src/libnpsharp/Authentication/AuthenticationHelper.cs" Line="77" Column="4" />
|
||||||
</Files>
|
</Files>
|
||||||
</MonoDevelop.Ide.Workbench>
|
</MonoDevelop.Ide.Workbench>
|
||||||
<MonoDevelop.Ide.DebuggingService.Breakpoints>
|
<MonoDevelop.Ide.DebuggingService.Breakpoints>
|
||||||
|
|
|
@ -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;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the username.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>The username.</value>
|
||||||
|
public string Username { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the user's e-mail address.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>The user's e-mail address.</value>
|
||||||
|
public string UserEMail { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the session token.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>The session token.</value>
|
||||||
|
public string SessionToken { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the user identifier.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>The user identifier.</value>
|
||||||
|
public uint UserId { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="NPSharp.Authentication.TokenRetriever"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="host">Hostname of the authentication endpoint.</param>
|
||||||
|
/// <param name="port">Port of the authentication endpoint.</param>
|
||||||
|
/// <param name="path">Path of the authentication endpoint.</param>
|
||||||
|
public AuthenticationHelper (string host, ushort port = 12003, string path = "/authenticate")
|
||||||
|
{
|
||||||
|
_host = host;
|
||||||
|
_port = port;
|
||||||
|
_path = path;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Authenticate the specified username and password.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="username">The username to use for authentication.</param>
|
||||||
|
/// <param name="password">The password to use for authentication.</param>
|
||||||
|
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 ("^(?P<status>ok|fail)#(?P<text>.+)#(?P<userid>[0-9]+)#(?P<username>.+)#(?P<usermail>.+)#(?P<sessiontoken>.+)$");
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -71,6 +71,7 @@
|
||||||
<Compile Include="Rpc\Packets\StorageWriteUserFileMessage.cs" />
|
<Compile Include="Rpc\Packets\StorageWriteUserFileMessage.cs" />
|
||||||
<Compile Include="Rpc\Packets\StorageWriteUserFileResultMessage.cs" />
|
<Compile Include="Rpc\Packets\StorageWriteUserFileResultMessage.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
<Compile Include="Authentication\AuthenticationHelper.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup />
|
<ItemGroup />
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
|
|
Loading…
Reference in New Issue