[391] | 1 | using System;
|
---|
| 2 | using System.Net;
|
---|
| 3 |
|
---|
| 4 | namespace Webserver.UrlHandlers {
|
---|
| 5 | public class SessionHandler : AbsHandler {
|
---|
[394] | 6 | private const string pageBasePath = "/app";
|
---|
| 7 | private const string pageErrorPath = "/app/error/";
|
---|
[391] | 8 | private const string steamOpenIdVerifyUrl = "verifysteamopenid";
|
---|
| 9 | private const string steamLoginUrl = "loginsteam";
|
---|
| 10 |
|
---|
| 11 | private readonly ConnectionHandler connectionHandler;
|
---|
| 12 |
|
---|
[394] | 13 | public SessionHandler (ConnectionHandler _connectionHandler) : base (null) {
|
---|
[391] | 14 | connectionHandler = _connectionHandler;
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 | public override void HandleRequest (RequestContext _context) {
|
---|
[394] | 18 | if (_context.Request.RemoteEndPoint == null) {
|
---|
| 19 | _context.Response.Redirect (pageErrorPath + "NoRemoteEndpoint");
|
---|
[391] | 20 | return;
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | string subpath = _context.RequestPath.Remove (0, urlBasePath.Length);
|
---|
| 24 |
|
---|
| 25 | if (subpath.StartsWith (steamOpenIdVerifyUrl)) {
|
---|
[394] | 26 | HandleSteamVerification (_context);
|
---|
| 27 | return;
|
---|
| 28 | }
|
---|
[391] | 29 |
|
---|
[394] | 30 | if (subpath.StartsWith ("logout")) {
|
---|
| 31 | HandleLogout (_context);
|
---|
| 32 | return;
|
---|
| 33 | }
|
---|
[391] | 34 |
|
---|
[394] | 35 | if (subpath.StartsWith (steamLoginUrl)) {
|
---|
| 36 | HandleSteamLogin (_context);
|
---|
| 37 | return;
|
---|
| 38 | }
|
---|
[391] | 39 |
|
---|
[394] | 40 | _context.Response.Redirect (pageErrorPath + "InvalidSessionsCommand");
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | 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);
|
---|
| 46 | _context.Response.Redirect (url);
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | private void HandleLogout (RequestContext _context) {
|
---|
| 50 | Cookie cookie = new Cookie ("sid", "", "/") {
|
---|
| 51 | Expired = true
|
---|
| 52 | };
|
---|
| 53 | _context.Response.AppendCookie (cookie);
|
---|
| 54 |
|
---|
| 55 | if (_context.Connection == null) {
|
---|
| 56 | _context.Response.Redirect (pageErrorPath + "NotLoggedIn");
|
---|
| 57 | return;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | connectionHandler.LogOut (_context.Connection.SessionID);
|
---|
| 61 | _context.Response.Redirect (pageBasePath);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | private void HandleSteamVerification (RequestContext _context) {
|
---|
| 65 | string remoteEndpointString = _context.Request.RemoteEndPoint!.ToString ();
|
---|
| 66 |
|
---|
| 67 | try {
|
---|
| 68 | ulong id = OpenID.Validate (_context.Request);
|
---|
| 69 | if (id > 0) {
|
---|
| 70 | WebConnection con = connectionHandler.LogIn (id, _context.Request.RemoteEndPoint.Address);
|
---|
| 71 | int level = GameManager.Instance.adminTools.GetUserPermissionLevel (con.UserId);
|
---|
[399] | 72 | Log.Out ("[Web] Steam OpenID login from {0} with ID {1}, permission level {2}",
|
---|
[394] | 73 | remoteEndpointString, con.UserId, level);
|
---|
| 74 |
|
---|
| 75 | Cookie cookie = new Cookie ("sid", con.SessionID, "/") {
|
---|
| 76 | Expired = false,
|
---|
| 77 | Expires = DateTime.MinValue,
|
---|
| 78 | HttpOnly = true,
|
---|
| 79 | Secure = false
|
---|
[391] | 80 | };
|
---|
| 81 | _context.Response.AppendCookie (cookie);
|
---|
| 82 | _context.Response.Redirect (pageBasePath);
|
---|
[394] | 83 |
|
---|
[391] | 84 | return;
|
---|
| 85 | }
|
---|
[394] | 86 | } catch (Exception e) {
|
---|
[399] | 87 | Log.Error ("[Web] Error validating Steam login:");
|
---|
[394] | 88 | Log.Exception (e);
|
---|
[391] | 89 | }
|
---|
| 90 |
|
---|
[399] | 91 | Log.Out ($"[Web] Steam OpenID login failed from {remoteEndpointString}");
|
---|
[394] | 92 | _context.Response.Redirect (pageErrorPath + "SteamLoginFailed");
|
---|
| 93 | }
|
---|
[391] | 94 |
|
---|
| 95 | }
|
---|
| 96 | }
|
---|