Feature: Google oauth
This commit is contained in:
@@ -32,7 +32,7 @@ public class UserService(UserManager<ApplicationUser> userManager) : IUserServic
|
||||
|
||||
public async Task CreateUserAsync(Userinfo userInfo)
|
||||
{
|
||||
await CreateUserAsync(userInfo.Email, userInfo.GivenName, userInfo.GivenName, userInfo.FamilyName, GeneratePassword(24));
|
||||
await CreateUserAsync(userInfo.Email, userInfo.GivenName, userInfo.GivenName, userInfo.FamilyName, RandomGenerator.RandomString(24));
|
||||
}
|
||||
|
||||
public async Task<UserModel?> FindUserByIdAsync(string id)
|
||||
@@ -82,23 +82,55 @@ public class UserService(UserManager<ApplicationUser> userManager) : IUserServic
|
||||
|
||||
return userModel;
|
||||
}
|
||||
|
||||
private const string Characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
private const string SpecialCharacters = "!@#$%^&*()_+";
|
||||
|
||||
private String GeneratePassword(int length)
|
||||
{
|
||||
// Using a string builder has additional overhead, maybe we can find something else
|
||||
var password = new StringBuilder();
|
||||
|
||||
for (var i = 0; i < length; i++)
|
||||
{
|
||||
password.Append(Characters[_random.Next(Characters.Length)]);
|
||||
}
|
||||
|
||||
password.Append(SpecialCharacters[_random.Next(SpecialCharacters.Length)]);
|
||||
|
||||
return password.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public class RandomGenerator
|
||||
{
|
||||
private const string LetterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
+ "0123456789"
|
||||
+ "!@#$%^&*()_+"
|
||||
+ "-=[];',./`~{}|:\"<>?";
|
||||
private const int LetterIdxBits = 6;
|
||||
private const int LetterIdxMask = 1 << LetterIdxBits;
|
||||
private const int LetterIdxMax = 64 / LetterIdxBits;
|
||||
|
||||
private static readonly Random Src = new();
|
||||
|
||||
public static byte[] RandBytesMaskSrc(int n)
|
||||
{
|
||||
var b = new byte[n];
|
||||
|
||||
for (var i = n - 1; i >= 0;)
|
||||
{
|
||||
long cache = Src.NextInt64();
|
||||
int remain = LetterIdxMax;
|
||||
|
||||
while (remain != 0)
|
||||
{
|
||||
if (i < 0)
|
||||
break;
|
||||
|
||||
if (cache == 0)
|
||||
cache = Src.NextInt64();
|
||||
|
||||
var idx = (int)(cache & LetterIdxMask);
|
||||
if (idx < LetterBytes.Length)
|
||||
{
|
||||
b[i] = (byte)LetterBytes[idx];
|
||||
i--;
|
||||
}
|
||||
|
||||
cache >>= LetterIdxBits;
|
||||
remain--;
|
||||
}
|
||||
}
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
public static string RandomString(int length)
|
||||
{
|
||||
var bytes = RandBytesMaskSrc(length);
|
||||
return Encoding.UTF8.GetString(bytes); // Equivalent for *(string*)(&bytes[0])
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user