source: binary-improvements2/WebServer/src/UrlHandlers/SessionHandler.cs@ 391

Last change on this file since 391 was 391, checked in by alloc, 2 years ago

Major refactoring/cleanup

File size: 3.3 KB
Line 
1using System;
2using System.IO;
3using System.Net;
4using System.Text;
5
6namespace Webserver.UrlHandlers {
7 public class SessionHandler : AbsHandler {
8 private const string pageBasePath = "/";
9 private const string steamOpenIdVerifyUrl = "verifysteamopenid";
10 private const string steamLoginUrl = "loginsteam";
11
12 private readonly string footer = "";
13 private readonly string header = "";
14
15 private readonly ConnectionHandler connectionHandler;
16
17 public SessionHandler (string _dataFolder, ConnectionHandler _connectionHandler) : base (null) {
18 connectionHandler = _connectionHandler;
19
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
29 public override void HandleRequest (RequestContext _context) {
30
31 IPEndPoint reqRemoteEndPoint = _context.Request.RemoteEndPoint;
32 if (reqRemoteEndPoint == null) {
33 _context.Response.Redirect (pageBasePath);
34 return;
35 }
36
37 string subpath = _context.RequestPath.Remove (0, urlBasePath.Length);
38
39 StringBuilder result = new StringBuilder ();
40 result.Append (header);
41
42 if (subpath.StartsWith (steamOpenIdVerifyUrl)) {
43 string remoteEndpointString = reqRemoteEndPoint.ToString ();
44
45 try {
46 ulong id = OpenID.Validate (_context.Request);
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 };
59 _context.Response.AppendCookie (cookie);
60 _context.Response.Redirect (pageBasePath);
61
62 return;
63 }
64 } catch (Exception e) {
65 Log.Error ("Error validating login:");
66 Log.Exception (e);
67 }
68
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>");
71 } else if (subpath.StartsWith ("logout")) {
72 if (_context.Connection != null) {
73 connectionHandler.LogOut (_context.Connection.SessionID);
74 Cookie cookie = new Cookie ("sid", "", "/") {
75 Expired = true
76 };
77 _context.Response.AppendCookie (cookie);
78 _context.Response.Redirect (pageBasePath);
79 return;
80 }
81
82 result.Append ($"<h1>Not logged in, <a href=\"{pageBasePath}\">click to return to main page</a>.</h1>");
83 } else if (subpath.StartsWith (steamLoginUrl)) {
84 string host = (WebUtils.IsSslRedirected (_context.Request) ? "https://" : "http://") + _context.Request.UserHostName;
85 string url = OpenID.GetOpenIdLoginUrl (host, host + urlBasePath + steamOpenIdVerifyUrl);
86 _context.Response.Redirect (url);
87 return;
88 } else {
89 result.Append ($"<h1>Unknown command, <a href=\"{pageBasePath}\">click to return to main page</a>.</h1>");
90 }
91
92 result.Append (footer);
93
94 WebUtils.WriteText (_context.Response, result.ToString (), _mimeType: WebUtils.MimeHtml);
95 }
96 }
97}
Note: See TracBrowser for help on using the repository browser.