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

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

Switched to use SpaceWizards.HttpListener

File size: 3.5 KB
Line 
1using System;
2using System.IO;
3using System.Net;
4using System.Text;
5using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest;
6using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse;
7
8namespace AllocsFixes.NetConnections.Servers.Web.Handlers {
9 public class SessionHandler : AbsHandler {
10 private const string pageBasePath = "/";
11 private const string steamOpenIdVerifyUrl = "verifysteamopenid";
12 private const string steamLoginUrl = "loginsteam";
13
14 private readonly string footer = "";
15 private readonly string header = "";
16
17 private readonly ConnectionHandler connectionHandler;
18
19 public SessionHandler (string _dataFolder, ConnectionHandler _connectionHandler) : base (null) {
20 connectionHandler = _connectionHandler;
21
22 if (File.Exists (_dataFolder + "/sessionheader.tmpl")) {
23 header = File.ReadAllText (_dataFolder + "/sessionheader.tmpl");
24 }
25
26 if (File.Exists (_dataFolder + "/sessionfooter.tmpl")) {
27 footer = File.ReadAllText (_dataFolder + "/sessionfooter.tmpl");
28 }
29 }
30
31 public override void HandleRequest (string _requestPath, HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _con,
32 int _permissionLevel) {
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);
41
42 StringBuilder result = new StringBuilder ();
43 result.Append (header);
44
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);
82 return;
83 }
84
85 result.Append ($"<h1>Not logged in, <a href=\"{pageBasePath}\">click to return to main page</a>.</h1>");
86 } else if (subpath.StartsWith (steamLoginUrl)) {
87 string host = (Web.IsSslRedirected (_req) ? "https://" : "http://") + _req.UserHostName;
88 string url = OpenID.GetOpenIdLoginUrl (host, host + urlBasePath + steamOpenIdVerifyUrl);
89 _resp.Redirect (url);
90 return;
91 } else {
92 result.Append ($"<h1>Unknown command, <a href=\"{pageBasePath}\">click to return to main page</a>.</h1>");
93 }
94
95 result.Append (footer);
96
97 _resp.ContentType = MimeType.GetMimeType (".html");
98 _resp.ContentEncoding = Encoding.UTF8;
99 byte[] buf = Encoding.UTF8.GetBytes (result.ToString ());
100 _resp.ContentLength64 = buf.Length;
101 _resp.OutputStream.Write (buf, 0, buf.Length);
102 }
103 }
104}
Note: See TracBrowser for help on using the repository browser.