Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Nullinside.Api.TwitchBot/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public static class Constants {
/// The twitch id for the bot account.
/// </summary>
public const string BOT_ID = "640082552";

/// <summary>
/// The amount of time a token is valid for.
/// </summary>
public static readonly TimeSpan OAUTH_TOKEN_TIME_LIMIT = TimeSpan.FromHours(1);

// TODO: This should be dynamic but I need to find a source of "good bots" lists. Might have to cheap out and just do a database table with data entry. Let users of the bot submit suggestions that we approve manually.
/// <summary>
Expand Down
13 changes: 9 additions & 4 deletions src/Nullinside.Api.TwitchBot/Controllers/LoginController.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System.Text;

using log4net;

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

using Newtonsoft.Json;

using Nullinside.Api.Common.Twitch;
using Nullinside.Api.Common.Twitch.Support;
using Nullinside.Api.Model;
Expand Down Expand Up @@ -75,12 +79,13 @@ public async Task<IActionResult> TwitchLogin([FromQuery] string code, [FromServi
return Redirect($"{siteUrl}/twitch/bot/config?error={TwitchBotLoginErrors.INTERNAL_ERROR}");
}

string? bearerToken = await UserHelpers.GenerateTokenAndSaveToDatabase(_dbContext, email, token, api.OAuth?.AccessToken,
api.OAuth?.RefreshToken, api.OAuth?.ExpiresUtc, user.Login, user.Id).ConfigureAwait(false);
if (string.IsNullOrWhiteSpace(bearerToken)) {
var bearerToken = await UserHelpers.GenerateTokenAndSaveToDatabase(_dbContext, email, Constants.OAUTH_TOKEN_TIME_LIMIT, api.OAuth?.AccessToken,
api.OAuth?.RefreshToken, api.OAuth?.ExpiresUtc, user.Login, user.Id, token).ConfigureAwait(false);
if (null == bearerToken) {
return Redirect($"{siteUrl}/twitch/bot/config?error={TwitchBotLoginErrors.INTERNAL_ERROR}");
}

return Redirect($"{siteUrl}/twitch/bot/config?token={bearerToken}");
var json = JsonConvert.SerializeObject(bearerToken);
return Redirect($"{siteUrl}/twitch/bot/config?token={Convert.ToBase64String(Encoding.UTF8.GetBytes(json))}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.EntityFrameworkCore.Storage;

using Nullinside.Api.Common;
using Nullinside.Api.Common.Auth;
using Nullinside.Api.Common.Twitch;
using Nullinside.Api.Model;
using Nullinside.Api.Model.Ddl;
Expand Down Expand Up @@ -34,7 +35,7 @@ public static class NullinsideContextExtensions {
/// <param name="api">The twitch api object currently in use.</param>
/// <returns>The twitch api.</returns>
public static void Configure(this ITwitchApiProxy api, User user) {
api.OAuth = new TwitchAccessToken {
api.OAuth = new OAuthToken {
AccessToken = user.TwitchToken,
RefreshToken = user.TwitchRefreshToken,
ExpiresUtc = user.TwitchTokenExpiration
Expand Down Expand Up @@ -89,7 +90,7 @@ public static void Configure(this ITwitchApiProxy api, User user) {
}

// Refresh the token with the Twitch API.
TwitchAccessToken? newToken = await api.RefreshAccessToken(stoppingToken).ConfigureAwait(false);
OAuthToken? newToken = await api.RefreshAccessToken(stoppingToken).ConfigureAwait(false);
if (null == newToken) {
return null;
}
Expand Down Expand Up @@ -122,7 +123,7 @@ public static void Configure(this ITwitchApiProxy api, User user) {
/// <param name="stoppingToken">The stopping token.</param>
/// <returns>The number of state entries written to the database.</returns>
private static async Task<int> UpdateOAuthInDatabase(this INullinsideContext db, int userId,
TwitchAccessToken oAuth, CancellationToken stoppingToken = new()) {
OAuthToken oAuth, CancellationToken stoppingToken = new()) {
User? row = await db.Users.FirstOrDefaultAsync(u => u.Id == userId && !u.IsBanned, stoppingToken).ConfigureAwait(false);
if (null == row) {
return -1;
Expand Down