Skip to content

Commit

Permalink
trying to setup oauth
Browse files Browse the repository at this point in the history
  • Loading branch information
BlenMiner committed Nov 19, 2023
1 parent 21bbdd8 commit 28b9e49
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rebuild-server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
git reset --hard @{u}
git clean -df
git pull
killall -9 3d-pixels-server
sudo killall -9 3d-pixels-server
dotnet build --property:Configuration=Release
sudo nohup ./bin/Release/net6.0/3d-pixels-server > /dev/null 2>&1 &
exit
4 changes: 2 additions & 2 deletions 3d-pixels-client/Assets/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -1176,12 +1176,12 @@ PrefabInstance:
- target: {fileID: 2530163867034958643, guid: 84488ffec075cd0498e2642533b000e1,
type: 3}
propertyPath: m_ssl
value: 0
value: 1
objectReference: {fileID: 0}
- target: {fileID: 2530163867034958643, guid: 84488ffec075cd0498e2642533b000e1,
type: 3}
propertyPath: m_host
value: 52.90.117.251
value: 3dpixels.riten.dev
objectReference: {fileID: 0}
- target: {fileID: 2530163867034958643, guid: 84488ffec075cd0498e2642533b000e1,
type: 3}
Expand Down
29 changes: 29 additions & 0 deletions 3d-pixels-server/OAuth.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PixelsServer
{
public static class OAuth
{
public static string GetOAuthUrl(string clientId, string redirectUri, string scope, string state)
{
return $"https://accounts.google.com/o/oauth2/v2/auth?" +
// $"scope=https%3A//www.googleapis.com/auth/drive.metadata.readonly&" +
$"response_type=code&" +
$"redirect_uri={redirectUri}&" +
$"client_id=client_id";
}

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";
}
}
}
37 changes: 37 additions & 0 deletions 3d-pixels-server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Runtime.CompilerServices;
using System.Security.AccessControl;

namespace PixelsServer
{
Expand Down Expand Up @@ -42,6 +44,37 @@ protected override void OnError(SocketError error)
{
Console.WriteLine($"Chat WebSocket session caught an error with code {error}");
}

protected override void OnReceivedRequest(HttpRequest request)
{
var host = request.GetHeader("Host");
if (host == null) return;

var isLocalHost = host.StartsWith("localhost");
var rootUrl = $"{(isLocalHost ? "http" : "https")}://{host}";

var unescapedPathAndQuery = Uri.UnescapeDataString(request.Url).Split('?');

string urlPath = unescapedPathAndQuery[0];
string urlQuery = unescapedPathAndQuery.Length > 1 ? unescapedPathAndQuery[1] : string.Empty;

switch (request.Method)
{
case "GET":

if (urlPath == "/login")
{
SendResponseAsync(Response.MakeRedirectResponse("https://www.google.com/"));
}

if (urlPath == "/oauth")
{
SendResponseAsync(Response.MakeGetResponse("OAUTH page " + rootUrl, "text/html; charset=UTF-8"));
}

break;
}
}
}

class ChatServer : WsServer
Expand Down Expand Up @@ -91,6 +124,10 @@ static void Main(string[] args)

server.AddStaticContent(GetWWWPath());

var homepage = server.Cache.Find("/index.html");
if (homepage.Item1)
server.Cache.Add("/", homepage.Item2);

// Start the server
Console.Write("Server starting...");
server.Start();
Expand Down
30 changes: 30 additions & 0 deletions 3d-pixels-server/ResponseUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using NetCoreServer;

namespace PixelsServer
{
public static class ResponseUtils
{
public static HttpResponse MakeRedirectResponse(this HttpResponse response, string url)
{
response.Clear();
response.SetBegin(301);
response.SetHeader("Location", url);
response.SetContentType("text/html; charset=UTF-8");
response.SetBody("Redirecting you...");
return response;
}

public static string? GetHeader(this HttpRequest request, string keyValue)
{
for (int i = 0; i < request.Headers; ++i)
{
(string key, string val) = request.Header(i);

if (key == keyValue)
return val;
}

return null;
}
}
}
2 changes: 1 addition & 1 deletion www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
<title>Document</title>
</head>
<body>
Hello World
Hello World !!
</body>
</html>

0 comments on commit 28b9e49

Please sign in to comment.