source: binary-improvements2/MapRendering/Web/Handlers/SessionHandler.cs@ 388

Last change on this file since 388 was 387, checked in by alloc, 3 years ago

Big refactoring in Web to pass around a Context instead of a bunch of individual arguments all the time

File size: 3.3 KB
RevLine 
[382]1using System;
[244]2using System.IO;
3using System.Net;
4using System.Text;
5
[325]6namespace AllocsFixes.NetConnections.Servers.Web.Handlers {
[382]7 public class SessionHandler : AbsHandler {
8 private const string pageBasePath = "/";
9 private const string steamOpenIdVerifyUrl = "verifysteamopenid";
10 private const string steamLoginUrl = "loginsteam";
11
[325]12 private readonly string footer = "";
13 private readonly string header = "";
[244]14
[382]15 private readonly ConnectionHandler connectionHandler;
16
17 public SessionHandler (string _dataFolder, ConnectionHandler _connectionHandler) : base (null) {
18 connectionHandler = _connectionHandler;
19
[244]20 if (File.Exists (_dataFolder + "/sessionheader.tmpl")) {
21 header = File.ReadAllText (_dataFolder + "/sessionheader.tmpl");
22 }
23
24 if (File.Exists (_dataFolder + "/sessionfooter.tmpl")) {
25 footer = File.ReadAllText (_dataFolder + "/sessionfooter.tmpl");
26 }
27 }
28
[387]29 public override void HandleRequest (RequestContext _context) {
[382]30
[387]31 IPEndPoint reqRemoteEndPoint = _context.Request.RemoteEndPoint;
[382]32 if (reqRemoteEndPoint == null) {
[387]33 _context.Response.Redirect (pageBasePath);
[382]34 return;
35 }
[244]36
[387]37 string subpath = _context.RequestPath.Remove (0, urlBasePath.Length);
[382]38
[244]39 StringBuilder result = new StringBuilder ();
40 result.Append (header);
41
[382]42 if (subpath.StartsWith (steamOpenIdVerifyUrl)) {
43 string remoteEndpointString = reqRemoteEndPoint.ToString ();
44
45 try {
[387]46 ulong id = OpenID.Validate (_context.Request);
[382]47 if (id > 0) {
48 WebConnection con = connectionHandler.LogIn (id, reqRemoteEndPoint.Address);
49 int level = GameManager.Instance.adminTools.GetUserPermissionLevel (con.UserId);
50 Log.Out ("Steam OpenID login from {0} with ID {1}, permission level {2}",
51 remoteEndpointString, con.UserId, level);
52
53 Cookie cookie = new Cookie ("sid", con.SessionID, "/") {
54 Expired = false,
55 Expires = DateTime.MinValue,
56 HttpOnly = true,
57 Secure = false
58 };
[387]59 _context.Response.AppendCookie (cookie);
60 _context.Response.Redirect (pageBasePath);
[382]61
62 return;
63 }
64 } catch (Exception e) {
65 Log.Error ("Error validating login:");
66 Log.Exception (e);
[244]67 }
[325]68
[382]69 Log.Out ($"Steam OpenID login failed from {remoteEndpointString}");
70 result.Append ($"<h1>Login failed, <a href=\"{pageBasePath}\">click to return to main page</a>.</h1>");
[244]71 } else if (subpath.StartsWith ("logout")) {
[387]72 if (_context.Connection != null) {
73 connectionHandler.LogOut (_context.Connection.SessionID);
[382]74 Cookie cookie = new Cookie ("sid", "", "/") {
75 Expired = true
76 };
[387]77 _context.Response.AppendCookie (cookie);
78 _context.Response.Redirect (pageBasePath);
[244]79 return;
80 }
[325]81
[382]82 result.Append ($"<h1>Not logged in, <a href=\"{pageBasePath}\">click to return to main page</a>.</h1>");
83 } else if (subpath.StartsWith (steamLoginUrl)) {
[387]84 string host = (Web.IsSslRedirected (_context.Request) ? "https://" : "http://") + _context.Request.UserHostName;
[382]85 string url = OpenID.GetOpenIdLoginUrl (host, host + urlBasePath + steamOpenIdVerifyUrl);
[387]86 _context.Response.Redirect (url);
[244]87 return;
88 } else {
[382]89 result.Append ($"<h1>Unknown command, <a href=\"{pageBasePath}\">click to return to main page</a>.</h1>");
[244]90 }
91
92 result.Append (footer);
93
[387]94 WebUtils.WriteText (_context.Response, result.ToString (), _mimeType: WebUtils.MimeHtml);
[244]95 }
96 }
[325]97}
Note: See TracBrowser for help on using the repository browser.