public void Run() { ThreadPool.QueueUserWorkItem((o) => { Console.WriteLine("Webserver running..."); try { while (_listener.IsListening) { ThreadPool.QueueUserWorkItem((c) => { var ctx = c as HttpListenerContext; if (!actions.Keys.Contains(ctx.Request.Url.LocalPath)) { ctx.Response.StatusCode = 404; ctx.Response.StatusDescription = "Error, page not found"; SendResponse(ctx.Request, ctx.Response, "Error, page not found"); return; } try { HTTPSession session; Cookie sessionIdCookie = ctx.Request.Cookies["ss-id"]; if (sessionIdCookie == null || !sessions.Keys.Contains(sessionIdCookie.Value as string)) { string sessionid = new Random().Next().ToString(); session = new HTTPSession(); sessions.Add(sessionid, session); sessionIdCookie = new Cookie("ss-id", sessionid); ctx.Response.Cookies.Add(sessionIdCookie); } else { session = sessions[sessionIdCookie.Value as string]; } actions[ctx.Request.Url.LocalPath](ctx.Request, ctx.Response, session); } catch (Exception e) { string message = "Internal server error: " + e.Message; ctx.Response.StatusCode = 500; Console.WriteLine(message + "\n" + e.StackTrace); ctx.Response.StatusDescription = message; SendResponse(ctx.Request, ctx.Response, message); } }, _listener.GetContext()); } } catch { // suppress any exceptions } }); }
public void authenticate(HttpListenerRequest req, HttpListenerResponse res, HTTPSession session) { // use the session object to store state between requests session["nonce"] = RandomString(); session["state"] = RandomString(); // TODO make authentication request // TODO insert the redirect URL string login_url = null; res.Redirect(login_url); res.Close(); }
public void implicitFlowCallback(HttpListenerRequest req, HttpListenerResponse res, HTTPSession session) { // Callback redirect URI //String url = req.url() + "#" + req.queryParams("url_fragment"); // TODO parse authentication response from url // TODO validate the ID Token according to the OpenID Connect spec (sec 3.2.2.11.) // TODO set the appropriate values string authCode = null; string accessToken = null; OIDCIdToken idToken = null; OIDCUserInfoResponseMessage userInfoResponse = null; }
public void codeFlowCallback(HttpListenerRequest req, HttpListenerResponse res, HTTPSession session) { // Callback redirect URI String queryString = req.Url.Query; // TODO parse authentication response from url // TODO make token request // TODO validate the ID Token according to the OpenID Connect spec (sec 3.1.3.7.) // TODO make userinfo request // TODO set the appropriate values string authCode = null; string accessToken = null; OIDCIdToken idToken = null; OIDCUserInfoResponseMessage userInfoResponse = null; string responsePage = WebServer.successPage(authCode, accessToken, idToken, userInfoResponse); WebServer.SendResponse(req, res, responsePage); }
public static void ReadFromFile(HttpListenerRequest request, HttpListenerResponse response, HTTPSession session) { string fileName = null; switch (request.Url.LocalPath) { case "/": fileName = "index.html"; break; case "/implicit_flow_callback": fileName = "repost_fragment.html"; break; default: throw new Exception("Wrong filename."); } SendResponse(request, response, File.ReadAllText(Path.Combine(Client.ROOT_PATH, fileName))); }