Added userinfo endpoint usage and api now properly creates necessary claims to start doing database stuff
This commit is contained in:
@@ -10,6 +10,7 @@ namespace WebAPI.Data
|
||||
public static string OIDCIntrospectionEndpoint { get; private set; }
|
||||
public static string OIDCClientId { get; private set; }
|
||||
public static string OIDCClientSecret { get; set; }
|
||||
public static string OIDCUserInfoEndpoint { get; set; }
|
||||
public static void Init(IConfiguration configuration)
|
||||
{
|
||||
var fields = typeof(AppSettings).GetProperties();
|
||||
|
||||
34
WebAPI/Data/Dto/OIDC/OIDCUserInfoResponse.cs
Normal file
34
WebAPI/Data/Dto/OIDC/OIDCUserInfoResponse.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace WebAPI.Data.Dto.OIDC
|
||||
{
|
||||
public class OIDCUserInfoResponse
|
||||
{
|
||||
[JsonProperty("email")]
|
||||
public string Email { get; set; }
|
||||
|
||||
[JsonProperty("email_verified")]
|
||||
public bool EmailVerified { get; set; }
|
||||
|
||||
[JsonProperty("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[JsonProperty("given_name")]
|
||||
public string GivenName { get; set; }
|
||||
|
||||
[JsonProperty("family_name")]
|
||||
public string FamilyName { get; set; }
|
||||
|
||||
[JsonProperty("preferred_username")]
|
||||
public string PreferredUsername { get; set; }
|
||||
|
||||
[JsonProperty("nickname")]
|
||||
public string Nickname { get; set; }
|
||||
|
||||
[JsonProperty("groups")]
|
||||
public string[] Groups { get; set; }
|
||||
|
||||
[JsonProperty("sub")]
|
||||
public string Sub { get; set; }
|
||||
}
|
||||
}
|
||||
16
WebAPI/Data/Dto/PterodactylCreateUserRequest.cs
Normal file
16
WebAPI/Data/Dto/PterodactylCreateUserRequest.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace WebAPI.Data.Dto
|
||||
{
|
||||
public class PterodactylCreateUserRequest
|
||||
{
|
||||
public string Email { get; set; }
|
||||
public string Username { get; set; }
|
||||
[JsonProperty("external_id")]
|
||||
public string ExternalId { get; set; }
|
||||
[JsonProperty("first_name")]
|
||||
public string FirstName { get; set; }
|
||||
[JsonProperty("last_name")]
|
||||
public string LastName { get; set; }
|
||||
}
|
||||
}
|
||||
11
WebAPI/Data/Dto/PterodactylCreateUserResponse.cs
Normal file
11
WebAPI/Data/Dto/PterodactylCreateUserResponse.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace WebAPI.Data.Dto
|
||||
{
|
||||
public class PterodactylCreateUserResponseAttributes
|
||||
{
|
||||
public int Id { get; set; }
|
||||
}
|
||||
public class PterodactylCreateUserResponse
|
||||
{
|
||||
public PterodactylCreateUserResponseAttributes Attributes { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using WebAPI.Data.Dto.OIDC;
|
||||
|
||||
namespace WebAPI.Data
|
||||
{
|
||||
@@ -49,6 +50,21 @@ namespace WebAPI.Data
|
||||
var responsecontent = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<IntrospectionResponse>(responsecontent).Active;
|
||||
}
|
||||
|
||||
public async Task<OIDCUserInfoResponse> GetTokenDetails(string accessToken)
|
||||
{
|
||||
HttpRequestMessage requestMessage =
|
||||
new HttpRequestMessage(HttpMethod.Get, $"https://{AppSettings.OIDCUserInfoEndpoint}");
|
||||
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
|
||||
HttpResponseMessage response = await _httpClient.SendAsync(requestMessage);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var responsecontent = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<OIDCUserInfoResponse>(responsecontent);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using WebAPI.Data.Dto;
|
||||
|
||||
namespace WebAPI.Data
|
||||
{
|
||||
@@ -46,7 +47,7 @@ namespace WebAPI.Data
|
||||
return JsonConvert.DeserializeObject<T>(responsedata);
|
||||
}
|
||||
|
||||
public async Task<T> SendGet<T>(string endpoint, IEnumerable includeParameters, bool client=false)
|
||||
private async Task<T> SendGet<T>(string endpoint, IEnumerable includeParameters, bool client=false)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -61,7 +62,7 @@ namespace WebAPI.Data
|
||||
|
||||
}
|
||||
|
||||
public async Task<T> SendPost<T>(string endpoint, object obj, IEnumerable includeParameters, bool client = false)
|
||||
private async Task<T> SendPost<T>(string endpoint, object obj, IEnumerable includeParameters, bool client = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -75,5 +76,19 @@ namespace WebAPI.Data
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<PterodactylCreateUserResponse> SendPterodactylUserCreate(string username, string email,
|
||||
string firstname, string lastname, string externalId)
|
||||
{
|
||||
var requestObj = new PterodactylCreateUserRequest()
|
||||
{
|
||||
Email = email,
|
||||
ExternalId = externalId,
|
||||
FirstName = firstname,
|
||||
LastName = lastname,
|
||||
Username = username
|
||||
};
|
||||
return await SendPost<PterodactylCreateUserResponse>("users", requestObj, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user