From 8370d355fc4d0ba81ee1cf78f6855007a6f0882e Mon Sep 17 00:00:00 2001 From: Valentin Bordeianu Date: Sun, 19 Nov 2023 23:19:57 +0100 Subject: [PATCH] first roundtrip to oauth is done! --- .github/workflows/rebuild-server.yml | 2 +- 3d-pixels-server/OAuth.cs | 19 ++++--------- 3d-pixels-server/Program.cs | 41 +++++++++++++++++----------- 3d-pixels-server/ResponseUtils.cs | 6 ++++ 4 files changed, 38 insertions(+), 30 deletions(-) diff --git a/.github/workflows/rebuild-server.yml b/.github/workflows/rebuild-server.yml index ac9f2f5..69ee275 100644 --- a/.github/workflows/rebuild-server.yml +++ b/.github/workflows/rebuild-server.yml @@ -25,5 +25,5 @@ jobs: git pull sudo killall -9 3d-pixels-server dotnet build --property:Configuration=Release - sudo nohup ./bin/Release/net6.0/3d-pixels-server > /dev/null 2>&1 & + sudo nohup ./bin/Release/net6.0/3d-pixels-server ${{ secrets.OAUTH_CLIENT_ID }} > /dev/null 2>&1 & exit \ No newline at end of file diff --git a/3d-pixels-server/OAuth.cs b/3d-pixels-server/OAuth.cs index b420b5f..be5563b 100644 --- a/3d-pixels-server/OAuth.cs +++ b/3d-pixels-server/OAuth.cs @@ -8,22 +8,15 @@ namespace PixelsServer { public static class OAuth { - public static string GetOAuthUrl(string clientId, string redirectUri, string scope, string state) + public static string GetOAuthUrl(string clientId, string redirectUri) { - return $"https://accounts.google.com/o/oauth2/v2/auth?" + - // $"scope=https%3A//www.googleapis.com/auth/drive.metadata.readonly&" + + var query = + $"scope={Uri.EscapeDataString("https://www.googleapis.com/auth/userinfo.email")}&" + $"response_type=code&" + - $"redirect_uri={redirectUri}&" + - $"client_id=client_id"; - } + $"redirect_uri={Uri.EscapeDataString(redirectUri)}&" + + $"client_id={Uri.EscapeDataString(clientId)}"; - static string GenerateGoogleAuthUrl() - { - // Replace with your client ID and redirect URI - string clientId = "YOUR_CLIENT_ID"; - string redirectUri = "http://localhost:8080/oauth2callback"; - string scopes = "openid%20email"; - return $"https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id={clientId}&redirect_uri={redirectUri}&scope={scopes}&access_type=offline"; + return $"https://accounts.google.com/o/oauth2/v2/auth?{query}"; } } } diff --git a/3d-pixels-server/Program.cs b/3d-pixels-server/Program.cs index 9a22ecc..97f1bc0 100644 --- a/3d-pixels-server/Program.cs +++ b/3d-pixels-server/Program.cs @@ -1,16 +1,18 @@ using NetCoreServer; -using System; using System.Net.Sockets; using System.Net; using System.Text; -using System.Runtime.CompilerServices; -using System.Security.AccessControl; namespace PixelsServer { - class ChatSession : WsSession + class PixelsServerSession : WsSession { - public ChatSession(WsServer server) : base(server) { } + private readonly string m_clientId; + + public PixelsServerSession(WsServer server, string oauthClientID) : base(server) + { + m_clientId = oauthClientID; + } public override void OnWsConnected(HttpRequest request) { @@ -64,11 +66,14 @@ protected override void OnReceivedRequest(HttpRequest request) if (urlPath == "/login") { - SendResponseAsync(Response.MakeRedirectResponse("https://www.google.com/")); + var redirectAt = $"{rootUrl}/oauth"; + var oauthUrl = OAuth.GetOAuthUrl(m_clientId, redirectAt); + SendResponseAsync(Response.MakeRedirectResponse(oauthUrl)); } if (urlPath == "/oauth") { + // http://localhost:8080/oauth?code=4%2F0AfJohXkIxhgVtfvJOxEuC5xArAiMFMlM_g-GOw1mpmYh9vZGXLAVvhaCo6Y7E5IAs24bvg&scope=email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+openid&authuser=0&prompt=consent SendResponseAsync(Response.MakeGetResponse("OAUTH page " + rootUrl, "text/html; charset=UTF-8")); } @@ -77,11 +82,16 @@ protected override void OnReceivedRequest(HttpRequest request) } } - class ChatServer : WsServer + class PixelsServer : WsServer { - public ChatServer(IPAddress address, int port) : base(address, port) { } + readonly string m_clientId; + + public PixelsServer(string oauth_clientId, IPAddress address, int port) : base(address, port) + { + m_clientId = oauth_clientId; + } - protected override TcpSession CreateSession() { return new ChatSession(this); } + protected override TcpSession CreateSession() { return new PixelsServerSession(this, m_clientId); } protected override void OnError(SocketError error) { @@ -108,19 +118,18 @@ static string GetWWWPath(string currentDirectory = ".") static void Main(string[] args) { - // WebSocket server port - int port = 8080; + const int PORT = 8080; - if (args.Length > 0) - port = int.Parse(args[0]); + // WebSocket server port + string oauthClientId = args.Length > 0 ? args[0] : "1046003701952-57on8uhpj7ba89afgo30ott3no9vgobj.apps.googleusercontent.com"; - Console.WriteLine($"WebSocket server port: {port}"); - Console.WriteLine($"WebSocket server website: http://localhost:{port}/index.html"); + Console.WriteLine($"WebSocket server port: {PORT}"); + Console.WriteLine($"WebSocket server website: http://localhost:{PORT}/index.html"); Console.WriteLine(); // Create a new WebSocket server - var server = new ChatServer(IPAddress.Any, port); + var server = new PixelsServer(oauthClientId, IPAddress.Any, PORT); server.AddStaticContent(GetWWWPath()); diff --git a/3d-pixels-server/ResponseUtils.cs b/3d-pixels-server/ResponseUtils.cs index 53aff32..8629f0e 100644 --- a/3d-pixels-server/ResponseUtils.cs +++ b/3d-pixels-server/ResponseUtils.cs @@ -8,7 +8,13 @@ public static HttpResponse MakeRedirectResponse(this HttpResponse response, stri { response.Clear(); response.SetBegin(301); + + response.SetHeader("Cache-Control", "no-cache, no-store, must-revalidate"); + response.SetHeader("Pragma", "no-cache"); + response.SetHeader("Expires", "0"); + response.SetHeader("Location", url); + response.SetContentType("text/html; charset=UTF-8"); response.SetBody("Redirecting you..."); return response;