Skip to content

Commit 1596f6d

Browse files
Moving twitch login code to our service
The API was handling this for us because they already had all the logic anyway for getting the email. We've sufficiently moved things around enough that we can do it correctly by allowing the twitch bot service to handle its own logins.
1 parent b2a04ee commit 1596f6d

File tree

4 files changed

+12
-97
lines changed

4 files changed

+12
-97
lines changed
File renamed without changes.

src/Nullinside.Api.Common/Twitch/TwitchApiProxy.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using TwitchLib.Api.Helix.Models.Moderation.GetModerators;
1515
using TwitchLib.Api.Helix.Models.Streams.GetStreams;
1616
using TwitchLib.Api.Helix.Models.Users.GetUsers;
17+
using TwitchLib.Api.Interfaces;
1718

1819
using Stream = TwitchLib.Api.Helix.Models.Streams.GetStreams.Stream;
1920

@@ -73,7 +74,7 @@ public TwitchApiProxy(string token, string refreshToken, DateTime tokenExpires)
7374

7475
/// <inheritdoc />
7576
public async Task<TwitchAccessToken?> CreateAccessToken(string code, CancellationToken token = new()) {
76-
TwitchAPI api = GetApi();
77+
ITwitchAPI api = GetApi();
7778
AuthCodeResponse? response = await api.Auth.GetAccessTokenFromCodeAsync(code, ClientSecret, ClientRedirect);
7879
if (null == response) {
7980
return null;
@@ -90,7 +91,7 @@ public TwitchApiProxy(string token, string refreshToken, DateTime tokenExpires)
9091
/// <inheritdoc />
9192
public async Task<TwitchAccessToken?> RefreshAccessToken(CancellationToken token = new()) {
9293
try {
93-
TwitchAPI api = GetApi();
94+
ITwitchAPI api = GetApi();
9495
RefreshResponse? response = await api.Auth.RefreshAuthTokenAsync(OAuth?.RefreshToken, ClientSecret, ClientId);
9596
if (null == response) {
9697
return null;
@@ -116,7 +117,7 @@ public TwitchApiProxy(string token, string refreshToken, DateTime tokenExpires)
116117
/// <inheritdoc />
117118
public async Task<(string? id, string? username)> GetUser(CancellationToken token = new()) {
118119
return await Retry.Execute(async () => {
119-
TwitchAPI api = GetApi();
120+
ITwitchAPI api = GetApi();
120121
GetUsersResponse? response = await api.Helix.Users.GetUsersAsync();
121122
if (null == response) {
122123
return (null, null);
@@ -130,7 +131,7 @@ public TwitchApiProxy(string token, string refreshToken, DateTime tokenExpires)
130131
/// <inheritdoc />
131132
public async Task<string?> GetUserEmail(CancellationToken token = new()) {
132133
return await Retry.Execute(async () => {
133-
TwitchAPI api = GetApi();
134+
ITwitchAPI api = GetApi();
134135
GetUsersResponse? response = await api.Helix.Users.GetUsersAsync();
135136
if (null == response) {
136137
return null;
@@ -175,7 +176,7 @@ public async Task<IEnumerable<TwitchModeratedChannel>> GetUserModChannels(string
175176
public async Task<IEnumerable<BannedUser>> BanChannelUsers(string channelId, string botId,
176177
IEnumerable<(string Id, string Username)> users, string reason, CancellationToken token = new()) {
177178
return await Retry.Execute(async () => {
178-
TwitchAPI api = GetApi();
179+
ITwitchAPI api = GetApi();
179180

180181
var bannedUsers = new List<BannedUser>();
181182
foreach ((string Id, string Username) user in users) {
@@ -209,7 +210,7 @@ public async Task<IEnumerable<BannedUser>> BanChannelUsers(string channelId, str
209210
public async Task<IEnumerable<Chatter>> GetChannelUsers(string channelId, string botId,
210211
CancellationToken token = new()) {
211212
return await Retry.Execute(async () => {
212-
TwitchAPI api = GetApi();
213+
ITwitchAPI api = GetApi();
213214
var chatters = new List<Chatter>();
214215
string? cursor = null;
215216
int total = 0;
@@ -231,7 +232,7 @@ public async Task<IEnumerable<Chatter>> GetChannelUsers(string channelId, string
231232

232233
/// <inheritdoc />
233234
public async Task<IEnumerable<string>> GetChannelsLive(IEnumerable<string> userIds) {
234-
TwitchAPI api = GetApi();
235+
ITwitchAPI api = GetApi();
235236

236237
// We can only query 100 at a time, so throttle the search.
237238
var liveUsers = new List<Stream>();
@@ -256,7 +257,7 @@ public async Task<IEnumerable<string>> GetChannelsLive(IEnumerable<string> userI
256257
/// <inheritdoc />
257258
public async Task<IEnumerable<Moderator>> GetChannelMods(string channelId, CancellationToken token = new()) {
258259
return await Retry.Execute(async () => {
259-
TwitchAPI api = GetApi();
260+
ITwitchAPI api = GetApi();
260261

261262
var results = new List<Moderator>();
262263
GetModeratorsResponse? response = null;
@@ -283,7 +284,7 @@ public async Task<IEnumerable<string>> GetChannelsLive(IEnumerable<string> userI
283284
/// <inheritdoc />
284285
public async Task<bool> AddChannelMod(string channelId, string userId, CancellationToken token = new()) {
285286
return await Retry.Execute(async () => {
286-
TwitchAPI api = GetApi();
287+
ITwitchAPI api = GetApi();
287288
await api.Helix.Moderation.AddChannelModeratorAsync(channelId, userId);
288289
return true;
289290
}, Retries, token);
@@ -293,7 +294,7 @@ public async Task<IEnumerable<string>> GetChannelsLive(IEnumerable<string> userI
293294
/// Gets a new instance of the <see cref="TwitchAPI" />.
294295
/// </summary>
295296
/// <returns>A new instance of the <see cref="TwitchAPI" />.</returns>
296-
private TwitchAPI GetApi() {
297+
protected ITwitchAPI GetApi() {
297298
var api = new TwitchAPI {
298299
Settings = {
299300
ClientId = ClientId,

src/Nullinside.Api/Controllers/TwitchBotController.cs

Lines changed: 0 additions & 86 deletions
This file was deleted.

src/Nullinside.Api/Controllers/UserController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public async Task<IActionResult> Login([FromForm] GoogleOpenIdToken creds, Cance
9696
[HttpGet]
9797
[Route("twitch-login")]
9898
public async Task<IActionResult> TwitchLogin([FromQuery] string code, [FromServices] ITwitchApiProxy api,
99-
CancellationToken token) {
99+
CancellationToken token = new()) {
100100
string? siteUrl = _configuration.GetValue<string>("Api:SiteUrl");
101101
if (null == await api.CreateAccessToken(code, token)) {
102102
return Redirect($"{siteUrl}/user/login?error=3");

0 commit comments

Comments
 (0)