- Timestamp:
- Feb 16, 2023, 3:50:53 PM (21 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements2/WebServer/src/UrlHandlers/SessionHandler.cs
r402 r404 5 5 using Platform.Steam; 6 6 using Utf8Json; 7 using Webserver.Permissions; 7 8 8 9 namespace Webserver.UrlHandlers { … … 10 11 private const string pageBasePath = "/app"; 11 12 private const string pageErrorPath = "/app/error/"; 12 13 13 14 private const string steamOpenIdVerifyUrl = "verifysteamopenid"; 14 15 private const string steamLoginUrl = "loginsteam"; 16 private const string steamLoginFailedPage = "SteamLoginFailed"; 17 15 18 private const string userPassLoginUrl = "login"; 16 19 … … 29 32 string subpath = _context.RequestPath.Remove (0, urlBasePath.Length); 30 33 34 string remoteEndpointString = _context.Request.RemoteEndPoint!.ToString (); 35 31 36 if (subpath.StartsWith (steamOpenIdVerifyUrl)) { 32 HandleSteamVerification (_context );37 HandleSteamVerification (_context, remoteEndpointString); 33 38 return; 34 39 } … … 43 48 return; 44 49 } 45 50 46 51 if (subpath.StartsWith (userPassLoginUrl)) { 47 HandleUserPassLogin (_context );52 HandleUserPassLogin (_context, remoteEndpointString); 48 53 return; 49 54 } … … 52 57 } 53 58 54 private void HandleUserPassLogin (RequestContext _context ) {59 private void HandleUserPassLogin (RequestContext _context, string _remoteEndpointString) { 55 60 if (!_context.Request.HasEntityBody) { 56 61 _context.Response.Redirect (pageErrorPath + "NoLoginData"); … … 83 88 } 84 89 85 // TODO: Apply login90 AdminWebUsers.WebUser? webUser = AdminWebUsers.Instance.GetUser (username, password); 86 91 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 if (!webUser.HasValue) { 93 Log.Out ($"[Web] User/pass login failed from {_remoteEndpointString}"); 92 94 _context.Response.Redirect (pageErrorPath + "UserPassInvalid"); 93 95 return; 94 96 } 95 96 try {97 // TODO: Match username/password to UserIdentifierAbs / serveradmins.xml98 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 97 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"); 98 HandleUserIdLogin (_context, _remoteEndpointString, "user/pass", "UserPassLoginFailed", webUser.Value.Name, webUser.Value.PlatformUser, webUser.Value.CrossPlatformUser); 119 99 } 120 100 … … 140 120 } 141 121 142 private void HandleSteamVerification (RequestContext _context) { 143 string remoteEndpointString = _context.Request.RemoteEndPoint!.ToString (); 144 122 private void HandleSteamVerification (RequestContext _context, string _remoteEndpointString) { 123 ulong id; 145 124 try { 146 ulong id = OpenID.Validate (_context.Request); 147 if (id > 0) { 148 WebConnection con = connectionHandler.LogIn (new UserIdentifierSteam (id), _context.Request.RemoteEndPoint.Address); 149 int level = GameManager.Instance.adminTools.GetUserPermissionLevel (con.UserId); 150 Log.Out ($"[Web] Steam OpenID login from {remoteEndpointString} with ID {con.UserId}, permission level {level}"); 151 152 Cookie cookie = new Cookie ("sid", con.SessionID, "/") { 153 Expired = false, 154 Expires = DateTime.MinValue, 155 HttpOnly = true, 156 Secure = false 157 }; 158 _context.Response.AppendCookie (cookie); 159 _context.Response.Redirect (pageBasePath); 160 161 return; 162 } 125 id = OpenID.Validate (_context.Request); 163 126 } catch (Exception e) { 164 Log.Error ( "[Web] Error validating Steam login:");127 Log.Error ($"[Web] Error validating Steam login from {_remoteEndpointString}:"); 165 128 Log.Exception (e); 129 _context.Response.Redirect (pageErrorPath + steamLoginFailedPage); 130 return; 166 131 } 167 132 168 Log.Out ($"[Web] Steam OpenID login failed from {remoteEndpointString}"); 169 _context.Response.Redirect (pageErrorPath + "SteamLoginFailed"); 133 if (id <= 0) { 134 Log.Out ($"[Web] Steam OpenID login failed (invalid ID) from {_remoteEndpointString}"); 135 _context.Response.Redirect (pageErrorPath + steamLoginFailedPage); 136 return; 137 } 138 139 UserIdentifierSteam userId = new UserIdentifierSteam (id); 140 HandleUserIdLogin (_context, _remoteEndpointString, "Steam OpenID", steamLoginFailedPage, userId.ToString (), userId); 170 141 } 171 142 143 private void HandleUserIdLogin (RequestContext _context, string _remoteEndpointString, string _loginName, string _errorPage, string _username, 144 PlatformUserIdentifierAbs _userId, PlatformUserIdentifierAbs _crossUserId = null) { 145 try { 146 WebConnection con = connectionHandler.LogIn (_context.Request.RemoteEndPoint!.Address, _username, _userId, _crossUserId); 147 148 int level1 = GameManager.Instance.adminTools.Users.GetUserPermissionLevel (_userId); 149 int level2 = int.MaxValue; 150 if (_crossUserId != null) { 151 level2 = GameManager.Instance.adminTools.Users.GetUserPermissionLevel (_crossUserId); 152 } 153 154 int higherLevel = Math.Min (level1, level2); 155 156 Log.Out ($"[Web] {_loginName} login from {_remoteEndpointString}, name {_username} with ID {_userId}, CID {(_crossUserId != null ? _crossUserId : "none")}, permission level {higherLevel}"); 157 Cookie cookie = new Cookie ("sid", con.SessionID, "/") { 158 Expired = false, 159 Expires = DateTime.MinValue, 160 HttpOnly = true, 161 Secure = false 162 }; 163 _context.Response.AppendCookie (cookie); 164 _context.Response.Redirect (pageBasePath); 165 } catch (Exception e) { 166 Log.Error ($"[Web] Error during {_loginName} login:"); 167 Log.Exception (e); 168 _context.Response.Redirect (pageErrorPath + _errorPage); 169 } 170 } 172 171 } 173 172 }
Note:
See TracChangeset
for help on using the changeset viewer.