Skip to content

Commit 2f9156b

Browse files
Generalizing location of code not specific to database
1 parent 516d4ea commit 2f9156b

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Security.Cryptography;
2+
3+
namespace Nullinside.Api.Common.Auth;
4+
5+
/// <summary>
6+
/// Random utilities for authentication.
7+
/// </summary>
8+
public static class AuthUtils {
9+
/// <summary>
10+
/// Generates a new unique bearer token.
11+
/// </summary>
12+
/// <returns>A bearer token.</returns>
13+
public static string GenerateBearerToken() {
14+
// This method is trash but it doesn't matter. We should be doing real OAuth tokens with expirations and
15+
// renewals. Right now nothing that exists on the site requires this level of sophistication.
16+
string allowed = "ABCDEFGHIJKLMONOPQRSTUVWXYZabcdefghijklmonopqrstuvwxyz0123456789";
17+
int strlen = 255; // Or whatever
18+
char[] randomChars = new char[strlen];
19+
20+
for (int i = 0; i < strlen; i++) {
21+
randomChars[i] = allowed[RandomNumberGenerator.GetInt32(0, allowed.Length)];
22+
}
23+
24+
return new string(randomChars);
25+
}
26+
}

src/Nullinside.Api.Model/Shared/UserHelpers.cs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.EntityFrameworkCore;
44

55
using Nullinside.Api.Common;
6+
using Nullinside.Api.Common.Auth;
67
using Nullinside.Api.Model.Ddl;
78

89
namespace Nullinside.Api.Model.Shared;
@@ -26,10 +27,9 @@ public static class UserHelpers {
2627
public static async Task<string?> GetTokenAndSaveToDatabase(INullinsideContext dbContext, string email,
2728
CancellationToken token = new(), string? authToken = null, string? refreshToken = null, DateTime? expires = null,
2829
string? twitchUsername = null, string? twitchId = null) {
29-
string bearerToken = GenerateBearerToken();
30+
string bearerToken = AuthUtils.GenerateBearerToken();
3031
try {
31-
// We prevent banned users from logging into the site.
32-
User? existing = await dbContext.Users.FirstOrDefaultAsync(u => u.Email == email && !u.IsBanned, token);
32+
User? existing = await dbContext.Users.FirstOrDefaultAsync(u => u.Email == email, token);
3333
if (null == existing) {
3434
dbContext.Users.Add(new User {
3535
Email = email,
@@ -73,22 +73,4 @@ public static class UserHelpers {
7373
return null;
7474
}
7575
}
76-
77-
/// <summary>
78-
/// Generates a new unique bearer token.
79-
/// </summary>
80-
/// <returns>A bearer token.</returns>
81-
public static string GenerateBearerToken() {
82-
// This method is trash but it doesn't matter. We should be doing real OAuth tokens with expirations and
83-
// renewals. Right now nothing that exists on the site requires this level of sophistication.
84-
string allowed = "ABCDEFGHIJKLMONOPQRSTUVWXYZabcdefghijklmonopqrstuvwxyz0123456789";
85-
int strlen = 255; // Or whatever
86-
char[] randomChars = new char[strlen];
87-
88-
for (int i = 0; i < strlen; i++) {
89-
randomChars[i] = allowed[RandomNumberGenerator.GetInt32(0, allowed.Length)];
90-
}
91-
92-
return new string(randomChars);
93-
}
9476
}

0 commit comments

Comments
 (0)