mirror of https://github.com/icedream/npsharp.git
				
				
				
			Fixing session authentication stuff.
							parent
							
								
									331b357a64
								
							
						
					
					
						commit
						df9c59a19e
					
				| 
						 | 
					@ -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.
 | 
				
			||||||
| 
						 | 
					@ -105,7 +105,7 @@ 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())
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
| 
						 | 
					@ -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);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
| 
						 | 
					@ -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();
 | 
				
			||||||
| 
						 | 
					@ -169,13 +172,15 @@ namespace NPSharp.Authentication
 | 
				
			||||||
        /// </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);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue