feat(apiv2): group stubs, authentication middleware, /systems/:id endpoint
This commit is contained in:
parent
8a88b23021
commit
57722e035b
@ -29,5 +29,33 @@ namespace PluralKit.API
|
||||
_db = svc.GetRequiredService<IDatabase>();
|
||||
_repo = svc.GetRequiredService<ModelRepository>();
|
||||
}
|
||||
|
||||
protected Task<PKSystem?> ResolveSystem(string systemRef)
|
||||
{
|
||||
if (systemRef == "@me")
|
||||
{
|
||||
HttpContext.Items.TryGetValue("SystemId", out var systemId);
|
||||
if (systemId == null) return null;
|
||||
return _repo.GetSystem((SystemId)systemId);
|
||||
}
|
||||
|
||||
if (Guid.TryParse(systemRef, out var guid))
|
||||
return _repo.GetSystemByGuid(guid);
|
||||
|
||||
if (_snowflakeRegex.IsMatch(systemRef))
|
||||
return _repo.GetSystemByAccount(ulong.Parse(systemRef));
|
||||
|
||||
if (_shortIdRegex.IsMatch(systemRef))
|
||||
return _repo.GetSystemByHid(systemRef);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public LookupContext LookupContextFor(PKSystem target)
|
||||
{
|
||||
HttpContext.Items.TryGetValue("SystemId", out var systemId);
|
||||
if (systemId == null) return LookupContext.ByNonOwner;
|
||||
return target.Id == (SystemId)systemId ? LookupContext.ByOwner : LookupContext.ByNonOwner;
|
||||
}
|
||||
}
|
||||
}
|
66
PluralKit.API/Controllers/v2/GroupControllerV2.cs
Normal file
66
PluralKit.API/Controllers/v2/GroupControllerV2.cs
Normal file
@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
using PluralKit.Core;
|
||||
|
||||
namespace PluralKit.API
|
||||
{
|
||||
[ApiController]
|
||||
[ApiVersion("2.0")]
|
||||
[Route("v{version:apiVersion}")]
|
||||
public class GroupControllerV2: PKControllerBase
|
||||
{
|
||||
public GroupControllerV2(IServiceProvider svc) : base(svc) { }
|
||||
|
||||
[HttpGet("systems/{system_id}/groups")]
|
||||
public async Task<IActionResult> GetSystemGroups(string system_id)
|
||||
{
|
||||
return new ObjectResult("Unimplemented")
|
||||
{
|
||||
StatusCode = 501
|
||||
};
|
||||
}
|
||||
|
||||
[HttpPost("groups")]
|
||||
public async Task<IActionResult> GroupCreate(string group_id)
|
||||
{
|
||||
return new ObjectResult("Unimplemented")
|
||||
{
|
||||
StatusCode = 501
|
||||
};
|
||||
}
|
||||
|
||||
[HttpGet("groups/{group_id}")]
|
||||
public async Task<IActionResult> GroupGet(string group_id)
|
||||
{
|
||||
return new ObjectResult("Unimplemented")
|
||||
{
|
||||
StatusCode = 501
|
||||
};
|
||||
}
|
||||
|
||||
[HttpPatch("groups/{group_id}")]
|
||||
public async Task<IActionResult> GroupPatch(string group_id)
|
||||
{
|
||||
return new ObjectResult("Unimplemented")
|
||||
{
|
||||
StatusCode = 501
|
||||
};
|
||||
}
|
||||
|
||||
[HttpDelete("groups/{group_id}")]
|
||||
public async Task<IActionResult> GroupDelete(string group_id)
|
||||
{
|
||||
return new ObjectResult("Unimplemented")
|
||||
{
|
||||
StatusCode = 501
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
90
PluralKit.API/Controllers/v2/GroupMemberControllerV2.cs
Normal file
90
PluralKit.API/Controllers/v2/GroupMemberControllerV2.cs
Normal file
@ -0,0 +1,90 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace PluralKit.API
|
||||
{
|
||||
[ApiController]
|
||||
[ApiVersion("2.0")]
|
||||
[Route("v{version:apiVersion}")]
|
||||
public class GroupMemberControllerV2: PKControllerBase
|
||||
{
|
||||
public GroupMemberControllerV2(IServiceProvider svc) : base(svc) { }
|
||||
|
||||
[HttpGet("groups/{group_id}/members")]
|
||||
public async Task<IActionResult> GetGroupMembers(string group_id)
|
||||
{
|
||||
return new ObjectResult("Unimplemented")
|
||||
{
|
||||
StatusCode = 501
|
||||
};
|
||||
}
|
||||
|
||||
[HttpGet("members/{member_id}/groups")]
|
||||
public async Task<IActionResult> GetMemberGroups(string member_id)
|
||||
{
|
||||
return new ObjectResult("Unimplemented")
|
||||
{
|
||||
StatusCode = 501
|
||||
};
|
||||
}
|
||||
|
||||
[HttpPut("groups/{group_id}/members/{member_id}")]
|
||||
public async Task<IActionResult> GroupMemberPut(string group_id, string member_id)
|
||||
{
|
||||
return new ObjectResult("Unimplemented")
|
||||
{
|
||||
StatusCode = 501
|
||||
};
|
||||
}
|
||||
|
||||
[HttpPut("groups/{group_id}/members")]
|
||||
public async Task<IActionResult> GroupMembersPut(string group_id, [FromBody] JArray members)
|
||||
{
|
||||
return new ObjectResult("Unimplemented")
|
||||
{
|
||||
StatusCode = 501
|
||||
};
|
||||
}
|
||||
|
||||
[HttpDelete("groups/{group_id}/members/{member_id}")]
|
||||
public async Task<IActionResult> GroupMemberDelete(string group_id, string member_id)
|
||||
{
|
||||
return new ObjectResult("Unimplemented")
|
||||
{
|
||||
StatusCode = 501
|
||||
};
|
||||
}
|
||||
|
||||
[HttpDelete("groups/{group_id}/members")]
|
||||
public async Task<IActionResult> GroupMembersDelete(string group_id, [FromBody] JArray members)
|
||||
{
|
||||
return new ObjectResult("Unimplemented")
|
||||
{
|
||||
StatusCode = 501
|
||||
};
|
||||
}
|
||||
|
||||
[HttpPut("members/{member_id}/groups")]
|
||||
public async Task<IActionResult> MemberGroupsPut(string member_id, [FromBody] JArray groups)
|
||||
{
|
||||
return new ObjectResult("Unimplemented")
|
||||
{
|
||||
StatusCode = 501
|
||||
};
|
||||
}
|
||||
|
||||
[HttpDelete("members/{member_id}/groups")]
|
||||
public async Task<IActionResult> MemberGroupsDelete(string member_id, [FromBody] JArray groups)
|
||||
{
|
||||
return new ObjectResult("Unimplemented")
|
||||
{
|
||||
StatusCode = 501
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -16,13 +16,12 @@ namespace PluralKit.API
|
||||
{
|
||||
public SystemControllerV2(IServiceProvider svc) : base(svc) { }
|
||||
|
||||
[HttpGet("{system}")]
|
||||
public async Task<IActionResult> SystemGet(string system)
|
||||
[HttpGet("{systemRef}")]
|
||||
public async Task<IActionResult> SystemGet(string systemRef)
|
||||
{
|
||||
return new ObjectResult("Unimplemented")
|
||||
{
|
||||
StatusCode = 501
|
||||
};
|
||||
var system = await ResolveSystem(systemRef);
|
||||
if (system == null) return NotFound();
|
||||
else return Ok(system.ToJson(LookupContextFor(system)));
|
||||
}
|
||||
|
||||
[HttpPatch("{system}")]
|
||||
|
@ -0,0 +1,36 @@
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
using Dapper;
|
||||
|
||||
using PluralKit.Core;
|
||||
|
||||
namespace PluralKit.API
|
||||
{
|
||||
public class AuthorizationTokenHandlerMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
public AuthorizationTokenHandlerMiddleware(RequestDelegate next)
|
||||
{
|
||||
_next = next;
|
||||
}
|
||||
|
||||
public async Task Invoke(HttpContext ctx, IDatabase db)
|
||||
{
|
||||
ctx.Request.Headers.TryGetValue("authorization", out var authHeaders);
|
||||
if (authHeaders.Count > 0)
|
||||
{
|
||||
var systemId = await db.Execute(conn => conn.QuerySingleOrDefaultAsync<SystemId?>(
|
||||
"select id from systems where token = @token",
|
||||
new { token = authHeaders[0] }
|
||||
));
|
||||
|
||||
if (systemId != null)
|
||||
ctx.Items.Add("SystemId", systemId);
|
||||
}
|
||||
|
||||
await _next.Invoke(ctx);
|
||||
}
|
||||
}
|
||||
}
|
@ -124,6 +124,8 @@ namespace PluralKit.API
|
||||
return next();
|
||||
});
|
||||
|
||||
app.UseMiddleware<AuthorizationTokenHandlerMiddleware>();
|
||||
|
||||
//app.UseHttpsRedirection();
|
||||
app.UseCors(opts => opts.AllowAnyMethod().AllowAnyOrigin().WithHeaders("Content-Type", "Authorization"));
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user