Skip to content

Commit

Permalink
Merge pull request #3 from EpicOfficer/FEATURE/Wordle
Browse files Browse the repository at this point in the history
Implement Docker argument and update token endpoint
  • Loading branch information
EpicOfficer authored Apr 2, 2024
2 parents f40b194 + da7f586 commit 5b03f9e
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@ jobs:
file: ${{ matrix.dockerfile }}
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
VITE_CLIENT_ID=${{ secrets.DISCORD_CLIENT_ID }}
54 changes: 53 additions & 1 deletion Blink3.API/Controllers/AuthController.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
using System.Net.Http.Headers;
using System.Text.Json;
using AspNet.Security.OAuth.Discord;
using Blink3.API.Models;
using Blink3.Core.Configuration;
using Blink3.Core.DiscordAuth;
using Blink3.Core.DiscordAuth.Extensions;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Swashbuckle.AspNetCore.Annotations;

namespace Blink3.API.Controllers;

[Route("api/[controller]")]
[ApiController]
[SwaggerTag("Authentication related actions")]
public class AuthController(IAuthenticationService authenticationService) : ControllerBase
public class AuthController(IAuthenticationService authenticationService,
IHttpClientFactory httpClientFactory,
IOptions<BlinkConfiguration> config) : ControllerBase
{
/// <summary>
/// Represents the configuration settings for the application.
/// </summary>
private BlinkConfiguration Config => config.Value;

[HttpGet("login")]
[SwaggerOperation(
Summary = "Initiates login via Discord",
Expand Down Expand Up @@ -60,4 +72,44 @@ public IActionResult Status()
{
return Ok(User.GetAuthStatusModel());
}

[HttpPost("token")]
public async Task<IActionResult> Token([FromBody] DiscordTokenRequest tokenRequest)
{
string clientId = Config.Discord.ClientId;
string clientSecret = Config.Discord.ClientSecret;

using HttpClient httpClient = httpClientFactory.CreateClient();
FormUrlEncodedContent requestBody = new(new[]
{
new KeyValuePair<string, string>("client_id", clientId),
new KeyValuePair<string, string>("client_secret", clientSecret),
new KeyValuePair<string, string>("grant_type", "authorization_code"),
new KeyValuePair<string, string>("code", tokenRequest.Code)
});

httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

HttpResponseMessage response;

try
{
response = await httpClient.PostAsync(DiscordAuthenticationDefaults.TokenEndpoint, requestBody);
}
catch (Exception ex)
{
return BadRequest("Request failed - " + ex.Message);
}

if (!response.IsSuccessStatusCode) return BadRequest();

string jsonResponse = await response.Content.ReadAsStringAsync();
Dictionary<string, string>? token = JsonSerializer.Deserialize<Dictionary<string, string>>(jsonResponse);
if (token is null || !token.TryGetValue("access_token", out string? value))
{
return BadRequest("Could not obtain access token");
}

return Ok(new { access_token = value });
}
}
8 changes: 8 additions & 0 deletions Blink3.API/Models/DiscordTokenRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Newtonsoft.Json;

namespace Blink3.API.Models;

public class DiscordTokenRequest
{
public string Code { get; set; } = string.Empty;
}
2 changes: 2 additions & 0 deletions Blink3.Activity/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ FROM nginx:alpine AS base
EXPOSE 80

FROM node:21-alpine AS build
ARG VITE_CLIENT_ID
ENV VITE_CLIENT_ID=$VITE_CLIENT_ID
WORKDIR /app
COPY ["Blink3.Activity/package.json", "."]
RUN npm install
Expand Down
3 changes: 2 additions & 1 deletion Blink3.Activity/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function App() {
});

// Retrieve an access_token from your activity's server
const response = await fetch('/api/token', {
const response = await fetch('/api/auth/token', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down Expand Up @@ -66,6 +66,7 @@ function App() {
</a>
</div>
<h1>Vite + React</h1>
<p>User: auth?.user.username</p>
<div className="card">
<button onClick={() => setCount((count) => count + 1)}>
count is {count}
Expand Down

0 comments on commit 5b03f9e

Please sign in to comment.