Added user invite system (#351)

* Added user invite system

* Added user invite system

* Revert recent migrations and try again

* stopped implicitly assigning token variables

* Added correct context to migrations

* Apply suggestions from code review

Some grammar changes, etc.

Co-authored-by: Jayden <jvyden@jvyden.xyz>

* Updated the API key page

* Removed enabled field from APIKey

* Removed reference to APIKey.Enabled

* Add creation guide text

* Fix this.Forbid() usage

Causes an exception on my machine for some reason, always has.

* Fix more forbid usages

* Return 404 if trying to generate token when private registration is disabled

* Capture authentication schema more cleanly

Co-authored-by: Jayden <jvyden@jvyden.xyz>
This commit is contained in:
Zaprit 2022-07-24 03:43:00 +01:00 committed by GitHub
parent c231af0936
commit ce0fe9edee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 408 additions and 19 deletions

View file

@ -1,6 +1,7 @@
#nullable enable
using LBPUnion.ProjectLighthouse.PlayerData.Profiles;
using LBPUnion.ProjectLighthouse.Types;
using LBPUnion.ProjectLighthouse.PlayerData;
using LBPUnion.ProjectLighthouse.Helpers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
@ -54,4 +55,33 @@ public class UserEndpoints : ApiEndpointController
return this.Ok(userStatus);
}
[HttpPost("user/inviteToken")]
public async Task<IActionResult> CreateUserInviteToken()
{
if (Configuration.ServerConfiguration.Instance.Authentication.PrivateRegistration ||
Configuration.ServerConfiguration.Instance.Authentication.RegistrationEnabled)
{
string authHeader = this.Request.Headers["Authorization"];
if (!string.IsNullOrWhiteSpace(authHeader))
{
string authToken = authHeader.Substring(authHeader.IndexOf(' ') + 1);
APIKey? apiKey = await this.database.APIKeys.FirstOrDefaultAsync(k => k.Key == authToken);
if (apiKey == null) return this.StatusCode(403, null);
RegistrationToken token = new();
token.Created = DateTime.Now;
token.Token = CryptoHelper.GenerateAuthToken();
this.database.RegistrationTokens.Add(token);
await this.database.SaveChangesAsync();
return Ok(token.Token);
}
}
return this.NotFound();
}
}