Skip to content

Commit

Permalink
first roundtrip to oauth is done!
Browse files Browse the repository at this point in the history
  • Loading branch information
BlenMiner committed Nov 19, 2023
1 parent 28b9e49 commit 8370d35
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rebuild-server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 6 additions & 13 deletions 3d-pixels-server/OAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}";
}
}
}
41 changes: 25 additions & 16 deletions 3d-pixels-server/Program.cs
Original file line number Diff line number Diff line change
@@ -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)
{
Expand Down Expand Up @@ -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"));
}

Expand All @@ -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)
{
Expand All @@ -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());

Expand Down
6 changes: 6 additions & 0 deletions 3d-pixels-server/ResponseUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 8370d35

Please sign in to comment.