- Timestamp:
- Aug 1, 2022, 12:54:31 PM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements2/MapRendering/Web/Handlers/SessionHandler.cs
r367 r382 1 using System; 1 2 using System.IO; 2 3 using System.Net; 3 4 using System.Text; 5 using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest; 6 using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse; 4 7 5 8 namespace AllocsFixes.NetConnections.Servers.Web.Handlers { 6 public class SessionHandler : PathHandler { 9 public class SessionHandler : AbsHandler { 10 private const string pageBasePath = "/"; 11 private const string steamOpenIdVerifyUrl = "verifysteamopenid"; 12 private const string steamLoginUrl = "loginsteam"; 13 7 14 private readonly string footer = ""; 8 15 private readonly string header = ""; 9 16 10 public SessionHandler (string _dataFolder, string _moduleName = null) : base (_moduleName) { 17 private readonly ConnectionHandler connectionHandler; 18 19 public SessionHandler (string _dataFolder, ConnectionHandler _connectionHandler) : base (null) { 20 connectionHandler = _connectionHandler; 21 11 22 if (File.Exists (_dataFolder + "/sessionheader.tmpl")) { 12 23 header = File.ReadAllText (_dataFolder + "/sessionheader.tmpl"); … … 18 29 } 19 30 20 public override void HandleRequest ( HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,31 public override void HandleRequest (string _requestPath, HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _con, 21 32 int _permissionLevel) { 22 string subpath = _req.Url.AbsolutePath.Remove (0, urlBasePath.Length); 33 34 IPEndPoint reqRemoteEndPoint = _req.RemoteEndPoint; 35 if (reqRemoteEndPoint == null) { 36 _resp.Redirect (pageBasePath); 37 return; 38 } 39 40 string subpath = _requestPath.Remove (0, urlBasePath.Length); 23 41 24 42 StringBuilder result = new StringBuilder (); 25 43 result.Append (header); 26 44 27 if (subpath.StartsWith ("verify")) { 28 if (_user != null) { 29 _resp.Redirect ("/static/index.html"); 45 if (subpath.StartsWith (steamOpenIdVerifyUrl)) { 46 string remoteEndpointString = reqRemoteEndPoint.ToString (); 47 48 try { 49 ulong id = OpenID.Validate (_req); 50 if (id > 0) { 51 WebConnection con = connectionHandler.LogIn (id, reqRemoteEndPoint.Address); 52 int level = GameManager.Instance.adminTools.GetUserPermissionLevel (con.UserId); 53 Log.Out ("Steam OpenID login from {0} with ID {1}, permission level {2}", 54 remoteEndpointString, con.UserId, level); 55 56 Cookie cookie = new Cookie ("sid", con.SessionID, "/") { 57 Expired = false, 58 Expires = DateTime.MinValue, 59 HttpOnly = true, 60 Secure = false 61 }; 62 _resp.AppendCookie (cookie); 63 _resp.Redirect (pageBasePath); 64 65 return; 66 } 67 } catch (Exception e) { 68 Log.Error ("Error validating login:"); 69 Log.Exception (e); 70 } 71 72 Log.Out ($"Steam OpenID login failed from {remoteEndpointString}"); 73 result.Append ($"<h1>Login failed, <a href=\"{pageBasePath}\">click to return to main page</a>.</h1>"); 74 } else if (subpath.StartsWith ("logout")) { 75 if (_con != null) { 76 connectionHandler.LogOut (_con.SessionID); 77 Cookie cookie = new Cookie ("sid", "", "/") { 78 Expired = true 79 }; 80 _resp.AppendCookie (cookie); 81 _resp.Redirect (pageBasePath); 30 82 return; 31 83 } 32 84 33 result.Append ( 34 "<h1>Login failed, <a href=\"/static/index.html\">click to return to main page</a>.</h1>"); 35 } else if (subpath.StartsWith ("logout")) { 36 if (_user != null) { 37 parent.connectionHandler.LogOut (_user.SessionID); 38 Cookie cookie = new Cookie ("sid", "", "/"); 39 cookie.Expired = true; 40 _resp.AppendCookie (cookie); 41 _resp.Redirect ("/static/index.html"); 42 return; 43 } 44 45 result.Append ( 46 "<h1>Not logged in, <a href=\"/static/index.html\">click to return to main page</a>.</h1>"); 47 } else if (subpath.StartsWith ("login")) { 85 result.Append ($"<h1>Not logged in, <a href=\"{pageBasePath}\">click to return to main page</a>.</h1>"); 86 } else if (subpath.StartsWith (steamLoginUrl)) { 48 87 string host = (Web.IsSslRedirected (_req) ? "https://" : "http://") + _req.UserHostName; 49 string url = OpenID.GetOpenIdLoginUrl (host, host + "/session/verify");88 string url = OpenID.GetOpenIdLoginUrl (host, host + urlBasePath + steamOpenIdVerifyUrl); 50 89 _resp.Redirect (url); 51 90 return; 52 91 } else { 53 result.Append ( 54 "<h1>Unknown command, <a href=\"/static/index.html\">click to return to main page</a>.</h1>"); 92 result.Append ($"<h1>Unknown command, <a href=\"{pageBasePath}\">click to return to main page</a>.</h1>"); 55 93 } 56 94
Note:
See TracChangeset
for help on using the changeset viewer.