- Timestamp:
- Jan 27, 2023, 7:28:00 PM (22 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements2/WebServer/src/UrlHandlers/SessionHandler.cs
r399 r402 1 1 using System; 2 using System.Collections.Generic; 3 using System.IO; 2 4 using System.Net; 5 using Platform.Steam; 6 using Utf8Json; 3 7 4 8 namespace Webserver.UrlHandlers { … … 6 10 private const string pageBasePath = "/app"; 7 11 private const string pageErrorPath = "/app/error/"; 12 8 13 private const string steamOpenIdVerifyUrl = "verifysteamopenid"; 9 14 private const string steamLoginUrl = "loginsteam"; 15 private const string userPassLoginUrl = "login"; 10 16 11 17 private readonly ConnectionHandler connectionHandler; … … 37 43 return; 38 44 } 45 46 if (subpath.StartsWith (userPassLoginUrl)) { 47 HandleUserPassLogin (_context); 48 return; 49 } 39 50 40 51 _context.Response.Redirect (pageErrorPath + "InvalidSessionsCommand"); 41 52 } 42 53 54 private void HandleUserPassLogin (RequestContext _context) { 55 if (!_context.Request.HasEntityBody) { 56 _context.Response.Redirect (pageErrorPath + "NoLoginData"); 57 return; 58 } 59 60 Stream requestInputStream = _context.Request.InputStream; 61 62 byte[] jsonInputData = new byte[_context.Request.ContentLength64]; 63 requestInputStream.Read (jsonInputData, 0, (int)_context.Request.ContentLength64); 64 65 IDictionary<string, object> inputJson; 66 try { 67 inputJson = JsonSerializer.Deserialize<IDictionary<string, object>> (jsonInputData); 68 } catch (Exception e) { 69 Log.Error ("Error deserializing JSON from user/password login:"); 70 Log.Exception (e); 71 _context.Response.Redirect (pageErrorPath + "InvalidLoginJson"); 72 return; 73 } 74 75 if (!inputJson.TryGetValue ("username", out object fieldNode) || fieldNode is not string username) { 76 _context.Response.Redirect (pageErrorPath + "InvalidLoginJson"); 77 return; 78 } 79 80 if (!inputJson.TryGetValue ("password", out fieldNode) || fieldNode is not string password) { 81 _context.Response.Redirect (pageErrorPath + "InvalidLoginJson"); 82 return; 83 } 84 85 // TODO: Apply login 86 87 string remoteEndpointString = _context.Request.RemoteEndPoint!.ToString (); 88 89 if (username != "test" || password != "123") { 90 // TODO: failed login 91 Log.Out ($"[Web] User/pass login failed from {remoteEndpointString}"); 92 _context.Response.Redirect (pageErrorPath + "UserPassInvalid"); 93 return; 94 } 95 96 try { 97 // TODO: Match username/password to UserIdentifierAbs / serveradmins.xml 98 99 WebConnection con = connectionHandler.LogIn (new UserIdentifierSteam (76561198066968172ul), _context.Request.RemoteEndPoint.Address); 100 int level = GameManager.Instance.adminTools.GetUserPermissionLevel (con.UserId); 101 Log.Out ($"[Web] User/pass login from {remoteEndpointString} with ID {con.UserId}, permission level {level}"); 102 103 Cookie cookie = new Cookie ("sid", con.SessionID, "/") { 104 Expired = false, 105 Expires = DateTime.MinValue, 106 HttpOnly = true, 107 Secure = false 108 }; 109 _context.Response.AppendCookie (cookie); 110 _context.Response.Redirect (pageBasePath); 111 112 return; 113 } catch (Exception e) { 114 Log.Error ("[Web] Error during user/pass login:"); 115 Log.Exception (e); 116 } 117 118 _context.Response.Redirect (pageErrorPath + "UserPassLoginFailed"); 119 } 120 43 121 private void HandleSteamLogin (RequestContext _context) { 44 string host = (WebUtils.IsSslRedirected (_context.Request) ? "https://" : "http://") + _context.Request.UserHostName;45 string url = OpenID.GetOpenIdLoginUrl (host, host + urlBasePath + steamOpenIdVerifyUrl);122 string host = $"{(WebUtils.IsSslRedirected (_context.Request) ? "https://" : "http://")}{_context.Request.UserHostName}"; 123 string url = OpenID.GetOpenIdLoginUrl (host, $"{host}{urlBasePath}{steamOpenIdVerifyUrl}"); 46 124 _context.Response.Redirect (url); 47 125 } … … 68 146 ulong id = OpenID.Validate (_context.Request); 69 147 if (id > 0) { 70 WebConnection con = connectionHandler.LogIn ( id, _context.Request.RemoteEndPoint.Address);148 WebConnection con = connectionHandler.LogIn (new UserIdentifierSteam (id), _context.Request.RemoteEndPoint.Address); 71 149 int level = GameManager.Instance.adminTools.GetUserPermissionLevel (con.UserId); 72 Log.Out ("[Web] Steam OpenID login from {0} with ID {1}, permission level {2}", 73 remoteEndpointString, con.UserId, level); 150 Log.Out ($"[Web] Steam OpenID login from {remoteEndpointString} with ID {con.UserId}, permission level {level}"); 74 151 75 152 Cookie cookie = new Cookie ("sid", con.SessionID, "/") {
Note:
See TracChangeset
for help on using the changeset viewer.