Skip to content

Commit

Permalink
load secrets from file during dev, load from args during not dev
Browse files Browse the repository at this point in the history
  • Loading branch information
BlenMiner committed Nov 20, 2023
1 parent c8599e8 commit 34e434e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 20 deletions.
51 changes: 46 additions & 5 deletions 3d-pixels-server/OAuth.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

namespace PixelsServer
{
public struct OAuthSecrets
{
public string ClientID;
public string ClientSecret;
}

public static class OAuth
{
public static OAuthSecrets GetSecrets(string[] args, string rootPath)
{
var secrets = new OAuthSecrets();

if (args.Length < 2)
{
// Load from file

string path = Path.Combine(rootPath, "oauth.json");

if (!File.Exists(path))
{
Console.WriteLine($"Could not find oauth.json at {path}");
return secrets;
}

var json = JObject.Parse(File.ReadAllText(path));

var clientId = json["client_id"];
var clientSecret = json["client_secret"];

if (clientId == null || clientSecret == null)
{
Console.WriteLine($"Could not find client_id or client_secret in oauth.json");
return secrets;
}

secrets.ClientID = clientId.ToString();
secrets.ClientSecret = clientId.ToString();

return secrets;
}

secrets.ClientID = args[0];
secrets.ClientSecret = args[1];

return secrets;
}

public static string GetOAuthUrl(string clientId, string redirectUri)
{
var query =
Expand Down
31 changes: 16 additions & 15 deletions 3d-pixels-server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ namespace PixelsServer
{
class PixelsServerSession : WsSession
{
private readonly string m_clientId;
private readonly OAuthSecrets m_secrets;

public PixelsServerSession(WsServer server, string oauthClientID) : base(server)
public PixelsServerSession(WsServer server, OAuthSecrets oauthSecrets) : base(server)
{
m_clientId = oauthClientID;
m_secrets = oauthSecrets;
}

public override void OnWsConnected(HttpRequest request)
Expand Down Expand Up @@ -67,7 +67,7 @@ protected override void OnReceivedRequest(HttpRequest request)
if (urlPath == "/login")
{
var redirectAt = $"{rootUrl}/oauth";
var oauthUrl = OAuth.GetOAuthUrl(m_clientId, redirectAt);
var oauthUrl = OAuth.GetOAuthUrl(m_secrets.ClientID, redirectAt);
SendResponseAsync(Response.MakeRedirectResponse(oauthUrl));
}

Expand All @@ -84,14 +84,14 @@ protected override void OnReceivedRequest(HttpRequest request)

class PixelsServer : WsServer
{
readonly string m_clientId;
readonly OAuthSecrets m_oauth;

public PixelsServer(string oauth_clientId, IPAddress address, int port) : base(address, port)
public PixelsServer(OAuthSecrets oauth, IPAddress address, int port) : base(address, port)
{
m_clientId = oauth_clientId;
m_oauth = oauth;
}

protected override TcpSession CreateSession() { return new PixelsServerSession(this, m_clientId); }
protected override TcpSession CreateSession() { return new PixelsServerSession(this, m_oauth); }

protected override void OnError(SocketError error)
{
Expand All @@ -101,37 +101,38 @@ protected override void OnError(SocketError error)

class Program
{
static string GetWWWPath(string currentDirectory = ".")
static string GetRootPath(string currentDirectory = ".")
{
var directoryInfo = new DirectoryInfo(currentDirectory);

if (directoryInfo.Name == "web-3d-pixels")
return Path.Combine(directoryInfo.FullName, "www");
return directoryInfo.FullName;

var parent = directoryInfo.Parent;

if (parent == null)
return string.Empty;

return GetWWWPath(parent.FullName);
return GetRootPath(parent.FullName);
}

static void Main(string[] args)
{
const int PORT = 8080;

// WebSocket server port
string oauthClientId = args.Length > 0 ? args[0] : "1046003701952-57on8uhpj7ba89afgo30ott3no9vgobj.apps.googleusercontent.com";
var rootPath = GetRootPath();
var wwwPath = Path.Combine(rootPath, "www");
var oauthSecrets = OAuth.GetSecrets(args, rootPath);

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 PixelsServer(oauthClientId, IPAddress.Any, PORT);
var server = new PixelsServer(oauthSecrets, IPAddress.Any, PORT);

server.AddStaticContent(GetWWWPath());
server.AddStaticContent(wwwPath);

var homepage = server.Cache.Find("/index.html");
if (homepage.Item1)
Expand Down

0 comments on commit 34e434e

Please sign in to comment.