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
{
private readonly string _host;
private readonly ILog _log;
private readonly string _path;
private readonly ushort _port;
private readonly ILog _log;
/// <summary>
/// Initializes a new instance of the <see cref="NPSharp.Authentication.SessionAuthenticationClient" /> class.
@ -105,7 +105,7 @@ namespace NPSharp.Authentication
// (ok|fail)#text#userid#username#email#sessiontoken
var rx =
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();
using (var respStream = resp.GetResponseStream())
{
@ -116,6 +116,7 @@ namespace NPSharp.Authentication
while (!respReader.EndOfStream)
{
var line = respReader.ReadLine();
_log.DebugFormat("Received authentication response: {0}", line);
// No answer?
if (string.IsNullOrEmpty(line))
@ -123,7 +124,10 @@ namespace NPSharp.Authentication
// DW response line found?
if (!rx.IsMatch(line))
{
_log.WarnFormat("Extra data in authentication response: {0}", line);
continue;
}
// This is a DW response line, analyze
var rxm = rx.Match(line);

View File

@ -13,7 +13,8 @@ using uhttpsharp.RequestProviders;
namespace NPSharp.Authentication
{
/// <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>
public class SessionAuthenticationServer
{
@ -125,6 +126,8 @@ namespace NPSharp.Authentication
sar = new SessionAuthenticationResult {Reason = @"Internal server error"};
}
_authServer._log.DebugFormat("/authenticate reply is {0}", sar);
context.Response = new HttpResponse(HttpResponseCode.Ok, sar.ToString(),
!sar.Success && context.Request.Headers.KeepAliveConnection());
return Task.Factory.GetCompleted();
@ -169,13 +172,15 @@ namespace NPSharp.Authentication
/// </summary>
public override string ToString()
{
// Response will be in this syntax:
// (ok|fail)#text#userid#username#email#sessiontoken
return String.Join("#",
Success ? "ok" : "fail",
String.IsNullOrEmpty(Reason) ? (Success ? "Success" : "Unknown error") : Reason,
UserID,
UserName,
UserMail,
SessionToken,
string.IsNullOrEmpty(UserName) ? "Anonymous" : UserName,
string.IsNullOrEmpty(UserMail) ? "anonymous@localhost" : UserMail,
string.IsNullOrEmpty(SessionToken) ? "0" : SessionToken,
String.Empty);
}
}