Skip to content

Commit 63966ee

Browse files
Merge pull request #119 from nullinside-development-group/feat/oauth
feat: updating to oauth from nullinside-api
2 parents eec797b + 92e122d commit 63966ee

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

src/Nullinside.Api.TwitchBot/Constants.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ public static class Constants {
1818
/// The twitch id for the bot account.
1919
/// </summary>
2020
public const string BOT_ID = "640082552";
21+
22+
/// <summary>
23+
/// The amount of time a token is valid for.
24+
/// </summary>
25+
public static readonly TimeSpan OAUTH_TOKEN_TIME_LIMIT = TimeSpan.FromHours(1);
2126

2227
// 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.
2328
/// <summary>

src/Nullinside.Api.TwitchBot/Controllers/LoginController.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
using System.Text;
2+
13
using log4net;
24

35
using Microsoft.AspNetCore.Authorization;
46
using Microsoft.AspNetCore.Mvc;
57

8+
using Newtonsoft.Json;
9+
610
using Nullinside.Api.Common.Twitch;
711
using Nullinside.Api.Common.Twitch.Support;
812
using Nullinside.Api.Model;
@@ -75,12 +79,13 @@ public async Task<IActionResult> TwitchLogin([FromQuery] string code, [FromServi
7579
return Redirect($"{siteUrl}/twitch/bot/config?error={TwitchBotLoginErrors.INTERNAL_ERROR}");
7680
}
7781

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

84-
return Redirect($"{siteUrl}/twitch/bot/config?token={bearerToken}");
88+
var json = JsonConvert.SerializeObject(bearerToken);
89+
return Redirect($"{siteUrl}/twitch/bot/config?token={Convert.ToBase64String(Encoding.UTF8.GetBytes(json))}");
8590
}
8691
}

src/Nullinside.Api.TwitchBot/Model/NullinsideContextExtensions.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.EntityFrameworkCore.Storage;
88

99
using Nullinside.Api.Common;
10+
using Nullinside.Api.Common.Auth;
1011
using Nullinside.Api.Common.Twitch;
1112
using Nullinside.Api.Model;
1213
using Nullinside.Api.Model.Ddl;
@@ -34,7 +35,7 @@ public static class NullinsideContextExtensions {
3435
/// <param name="api">The twitch api object currently in use.</param>
3536
/// <returns>The twitch api.</returns>
3637
public static void Configure(this ITwitchApiProxy api, User user) {
37-
api.OAuth = new TwitchAccessToken {
38+
api.OAuth = new OAuthToken {
3839
AccessToken = user.TwitchToken,
3940
RefreshToken = user.TwitchRefreshToken,
4041
ExpiresUtc = user.TwitchTokenExpiration
@@ -89,7 +90,7 @@ public static void Configure(this ITwitchApiProxy api, User user) {
8990
}
9091

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

0 commit comments

Comments
 (0)