Skip to content

Commit

Permalink
properly connect
Browse files Browse the repository at this point in the history
  • Loading branch information
BlenMiner committed Nov 20, 2023
1 parent 34e434e commit 660960e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
1 change: 1 addition & 0 deletions 3d-pixels-server/3d-pixels-server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ItemGroup>
<PackageReference Include="NetCoreServer" Version="6.7.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

</Project>
16 changes: 12 additions & 4 deletions 3d-pixels-server/OAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,16 @@ public static OAuthSecrets GetSecrets(string[] args, string rootPath)

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

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

if (webRoot == null)
{
Console.WriteLine($"Could not find web root in oauth.json");
return secrets;
}

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

if (clientId == null || clientSecret == null)
{
Expand All @@ -38,7 +46,7 @@ public static OAuthSecrets GetSecrets(string[] args, string rootPath)
}

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

return secrets;
}
Expand All @@ -52,7 +60,7 @@ public static OAuthSecrets GetSecrets(string[] args, string rootPath)
public static string GetOAuthUrl(string clientId, string redirectUri)
{
var query =
$"scope={Uri.EscapeDataString("https://www.googleapis.com/auth/userinfo.email")}&" +
$"scope=https://www.googleapis.com/auth/userinfo.email openid&" +
$"response_type=code&" +
$"redirect_uri={Uri.EscapeDataString(redirectUri)}&" +
$"client_id={Uri.EscapeDataString(clientId)}";
Expand Down
37 changes: 34 additions & 3 deletions 3d-pixels-server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
using System.Net.Sockets;
using System.Net;
using System.Text;
using Newtonsoft.Json.Linq;
using System.Net.Http.Headers;

namespace PixelsServer
{
class PixelsServerSession : WsSession
{
private readonly OAuthSecrets m_secrets;

private readonly System.Net.Http.HttpClient m_client = new System.Net.Http.HttpClient();

public PixelsServerSession(WsServer server, OAuthSecrets oauthSecrets) : base(server)
{
m_secrets = oauthSecrets;
Expand Down Expand Up @@ -47,7 +51,7 @@ protected override void OnError(SocketError error)
Console.WriteLine($"Chat WebSocket session caught an error with code {error}");
}

protected override void OnReceivedRequest(HttpRequest request)
protected override async void OnReceivedRequest(HttpRequest request)
{
var host = request.GetHeader("Host");
if (host == null) return;
Expand All @@ -73,8 +77,35 @@ protected override void OnReceivedRequest(HttpRequest request)

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"));
var end = urlQuery.IndexOf('&');
var code = urlQuery[5..end];

var values = new Dictionary<string, string>
{
{ "code", code },
{ "client_id", m_secrets.ClientID },
{ "client_secret", m_secrets.ClientSecret },
{ "redirect_uri", rootUrl + "/oauth" },
{ "grant_type", "authorization_code" }
};

var content = new FormUrlEncodedContent(values);

var response = await m_client.PostAsync("https://oauth2.googleapis.com/token", content);

var json = JObject.Parse(await response.Content.ReadAsStringAsync());
var accessToken = json["access_token"]?.ToString();

if (accessToken == null)
{
SendResponseAsync(Response.MakeGetResponse("OAUTH: ERROOORRRRRRRRRR", "text/html; charset=UTF-8"));
break;
}

var userInfo = await m_client.GetAsync($"https://www.googleapis.com/oauth2/v2/userinfo?access_token={accessToken}");
var userInfoData = await userInfo.Content.ReadAsStringAsync();

SendResponseAsync(Response.MakeGetResponse("OAUTH: " + userInfoData, "text/html; charset=UTF-8"));
}

break;
Expand Down

0 comments on commit 660960e

Please sign in to comment.