Fixing session authentication stuff.

feature-npv2
Icedream 2014-05-15 05:32:21 +02:00
parent 331b357a64
commit df9c59a19e
2 changed files with 31 additions and 22 deletions

View File

@ -15,9 +15,9 @@ namespace NPSharp.Authentication
public class SessionAuthenticationClient public class SessionAuthenticationClient
{ {
private readonly string _host; private readonly string _host;
private readonly ILog _log;
private readonly string _path; private readonly string _path;
private readonly ushort _port; private readonly ushort _port;
private readonly ILog _log;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="NPSharp.Authentication.SessionAuthenticationClient" /> class. /// Initializes a new instance of the <see cref="NPSharp.Authentication.SessionAuthenticationClient" /> class.
@ -89,7 +89,7 @@ namespace NPSharp.Authentication
Path = _path Path = _path
}.Uri; }.Uri;
var req = (HttpWebRequest)WebRequest.Create(uri); var req = (HttpWebRequest) WebRequest.Create(uri);
req.Method = "POST"; req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded"; req.ContentType = "application/x-www-form-urlencoded";
req.AllowAutoRedirect = true; req.AllowAutoRedirect = true;
@ -105,8 +105,8 @@ namespace NPSharp.Authentication
// (ok|fail)#text#userid#username#email#sessiontoken // (ok|fail)#text#userid#username#email#sessiontoken
var rx = var rx =
new Regex( new Regex(
"^(?<status>ok|fail)#(?<text>.+)#(?<userid>[0-9]+)#(?<username>.+)#(?<usermail>.+)#(?<sessiontoken>[^#]+)[#]*$"); "^(?<status>ok|fail)#(?<text>[^#]+)#(?<userid>[0-9]+)#(?<username>[^#]*)#(?<usermail>[^#]*)#(?<sessiontoken>[^#]*)[#]*$");
var resp = (HttpWebResponse)req.GetResponse(); var resp = (HttpWebResponse) req.GetResponse();
using (var respStream = resp.GetResponseStream()) using (var respStream = resp.GetResponseStream())
{ {
if (respStream == null) if (respStream == null)
@ -116,6 +116,7 @@ namespace NPSharp.Authentication
while (!respReader.EndOfStream) while (!respReader.EndOfStream)
{ {
var line = respReader.ReadLine(); var line = respReader.ReadLine();
_log.DebugFormat("Received authentication response: {0}", line);
// No answer? // No answer?
if (string.IsNullOrEmpty(line)) if (string.IsNullOrEmpty(line))
@ -123,7 +124,10 @@ namespace NPSharp.Authentication
// DW response line found? // DW response line found?
if (!rx.IsMatch(line)) if (!rx.IsMatch(line))
{
_log.WarnFormat("Extra data in authentication response: {0}", line);
continue; continue;
}
// This is a DW response line, analyze // This is a DW response line, analyze
var rxm = rx.Match(line); var rxm = rx.Match(line);
@ -160,7 +164,7 @@ namespace NPSharp.Authentication
Path = _path Path = _path
}.Uri; }.Uri;
var req = (HttpWebRequest)WebRequest.Create(uri); var req = (HttpWebRequest) WebRequest.Create(uri);
req.Method = "POST"; req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded"; req.ContentType = "application/x-www-form-urlencoded";
req.AllowAutoRedirect = true; req.AllowAutoRedirect = true;
@ -177,7 +181,7 @@ namespace NPSharp.Authentication
var rx = var rx =
new Regex( new Regex(
"^(?<status>ok|fail)#(?<text>.+)#(?<userid>[0-9]+)#(?<username>.+)#(?<usermail>.+)#(?<sessiontoken>[^#]+)[#]*$"); "^(?<status>ok|fail)#(?<text>.+)#(?<userid>[0-9]+)#(?<username>.+)#(?<usermail>.+)#(?<sessiontoken>[^#]+)[#]*$");
var resp = (HttpWebResponse)req.GetResponse(); var resp = (HttpWebResponse) req.GetResponse();
using (var respStream = resp.GetResponseStream()) using (var respStream = resp.GetResponseStream())
{ {
if (respStream == null) if (respStream == null)

View File

@ -13,7 +13,8 @@ using uhttpsharp.RequestProviders;
namespace NPSharp.Authentication namespace NPSharp.Authentication
{ {
/// <summary> /// <summary>
/// Represents a session authentication server which uses the HTTP protocol to send out session tokens to authenticating NP clients. /// Represents a session authentication server which uses the HTTP protocol to send out session tokens to
/// authenticating NP clients.
/// </summary> /// </summary>
public class SessionAuthenticationServer public class SessionAuthenticationServer
{ {
@ -21,7 +22,7 @@ namespace NPSharp.Authentication
private HttpServer _http; private HttpServer _http;
/// <summary> /// <summary>
/// Constructs a new session authentication server. /// Constructs a new session authentication server.
/// </summary> /// </summary>
public SessionAuthenticationServer() public SessionAuthenticationServer()
{ {
@ -30,12 +31,12 @@ namespace NPSharp.Authentication
} }
/// <summary> /// <summary>
/// Support oldskool "user&amp;&amp;pass" authentication format. /// Support oldskool "user&amp;&amp;pass" authentication format.
/// </summary> /// </summary>
public bool SupportOldAuthentication { get; set; } public bool SupportOldAuthentication { get; set; }
/// <summary> /// <summary>
/// Will be triggered whenever a client tries to authenticate via this server. /// Will be triggered whenever a client tries to authenticate via this server.
/// </summary> /// </summary>
public event Func<string, string, SessionAuthenticationResult> Authenticating; public event Func<string, string, SessionAuthenticationResult> Authenticating;
@ -48,7 +49,7 @@ namespace NPSharp.Authentication
} }
/// <summary> /// <summary>
/// Starts the authentication server. /// Starts the authentication server.
/// </summary> /// </summary>
/// <param name="port">The port on which the authentication server should listen on.</param> /// <param name="port">The port on which the authentication server should listen on.</param>
public void Start(ushort port = 12003) public void Start(ushort port = 12003)
@ -74,7 +75,7 @@ namespace NPSharp.Authentication
} }
/// <summary> /// <summary>
/// Stops the authentication server. /// Stops the authentication server.
/// </summary> /// </summary>
public void Stop() public void Stop()
{ {
@ -125,6 +126,8 @@ namespace NPSharp.Authentication
sar = new SessionAuthenticationResult {Reason = @"Internal server error"}; sar = new SessionAuthenticationResult {Reason = @"Internal server error"};
} }
_authServer._log.DebugFormat("/authenticate reply is {0}", sar);
context.Response = new HttpResponse(HttpResponseCode.Ok, sar.ToString(), context.Response = new HttpResponse(HttpResponseCode.Ok, sar.ToString(),
!sar.Success && context.Request.Headers.KeepAliveConnection()); !sar.Success && context.Request.Headers.KeepAliveConnection());
return Task.Factory.GetCompleted(); return Task.Factory.GetCompleted();
@ -135,47 +138,49 @@ namespace NPSharp.Authentication
public class SessionAuthenticationResult public class SessionAuthenticationResult
{ {
/// <summary> /// <summary>
/// true if authentication was successful, otherwise false. /// true if authentication was successful, otherwise false.
/// </summary> /// </summary>
public bool Success { get; set; } public bool Success { get; set; }
/// <summary> /// <summary>
/// Reason for the given success state. Use this especially in authentication fail cases. /// Reason for the given success state. Use this especially in authentication fail cases.
/// </summary> /// </summary>
public string Reason { get; set; } public string Reason { get; set; }
/// <summary> /// <summary>
/// If authenticated set this to the user's unique ID. /// If authenticated set this to the user's unique ID.
/// </summary> /// </summary>
public uint UserID { get; set; } public uint UserID { get; set; }
/// <summary> /// <summary>
/// If authenticated set this to the user's session token. /// If authenticated set this to the user's session token.
/// </summary> /// </summary>
public string SessionToken { get; set; } public string SessionToken { get; set; }
/// <summary> /// <summary>
/// If authenticated set this to the actual correctly spelled username. /// If authenticated set this to the actual correctly spelled username.
/// </summary> /// </summary>
public string UserName { get; set; } public string UserName { get; set; }
/// <summary> /// <summary>
/// If authenticated set this to the user's e-mail address. /// If authenticated set this to the user's e-mail address.
/// </summary> /// </summary>
public string UserMail { get; set; } public string UserMail { get; set; }
/// <summary> /// <summary>
/// Returns the response line as it should be sent out to the client. /// Returns the response line as it should be sent out to the client.
/// </summary> /// </summary>
public override string ToString() public override string ToString()
{ {
// Response will be in this syntax:
// (ok|fail)#text#userid#username#email#sessiontoken
return String.Join("#", return String.Join("#",
Success ? "ok" : "fail", Success ? "ok" : "fail",
String.IsNullOrEmpty(Reason) ? (Success ? "Success" : "Unknown error") : Reason, String.IsNullOrEmpty(Reason) ? (Success ? "Success" : "Unknown error") : Reason,
UserID, UserID,
UserName, string.IsNullOrEmpty(UserName) ? "Anonymous" : UserName,
UserMail, string.IsNullOrEmpty(UserMail) ? "anonymous@localhost" : UserMail,
SessionToken, string.IsNullOrEmpty(SessionToken) ? "0" : SessionToken,
String.Empty); String.Empty);
} }
} }