From 7b9e50bde28c7de9a6cccb6ac4584b7786ee56e7 Mon Sep 17 00:00:00 2001 From: notfire Date: Fri, 11 Apr 2025 18:22:56 -0400 Subject: [PATCH] oauth stuff --- .../Controllers/Mastodon/AuthController.cs | 42 +++++++++++++++++++ .../Mastodon/Renderers/UserRenderer.cs | 2 +- .../Entities/PleromaOauthTokenEntity.cs | 11 +++++ .../Schemas/Entities/PleromaUserExtensions.cs | 6 +-- Iceshrimp.Backend/configuration.ini | 4 +- 5 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 Iceshrimp.Backend/Controllers/Pleroma/Schemas/Entities/PleromaOauthTokenEntity.cs diff --git a/Iceshrimp.Backend/Controllers/Mastodon/AuthController.cs b/Iceshrimp.Backend/Controllers/Mastodon/AuthController.cs index 29900b75..9cda530a 100644 --- a/Iceshrimp.Backend/Controllers/Mastodon/AuthController.cs +++ b/Iceshrimp.Backend/Controllers/Mastodon/AuthController.cs @@ -1,6 +1,7 @@ using System.Net; using System.Net.Mime; using Iceshrimp.Backend.Controllers.Mastodon.Attributes; +using Iceshrimp.Backend.Controllers.Pleroma.Schemas.Entities; using Iceshrimp.Backend.Controllers.Shared.Attributes; using Iceshrimp.Backend.Core.Database; using Iceshrimp.Backend.Core.Database.Tables; @@ -8,6 +9,7 @@ using Iceshrimp.Backend.Core.Extensions; using Iceshrimp.Backend.Core.Helpers; using Iceshrimp.Backend.Core.Middleware; using Iceshrimp.Backend.Core.Services; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.RateLimiting; @@ -157,4 +159,44 @@ public class AuthController(DatabaseContext db, MetaService meta) : ControllerBa return new object(); } + + [Authenticate] + [HttpGet("/api/oauth_tokens.json")] + [ProducesResults(HttpStatusCode.OK)] + public async Task> GetOauthTokens() + { + var user = HttpContext.GetUserOrFail(); + var oauthTokens = await db.OauthTokens + .Where(p => p.User == user) + .Include(oauthToken => oauthToken.App) + .ToListAsync(); + + List result = []; + foreach (var token in oauthTokens) + { + result.Add(new PleromaOauthTokenEntity() + { + Id = token.Id, + AppName = token.App.Name, + ValidUntil = token.CreatedAt + TimeSpan.FromDays(365 * 100) + }); + } + + return result; + } + + [Authenticate] + [HttpDelete("/api/oauth_tokens/{id}")] + [ProducesResults(HttpStatusCode.Created)] + [ProducesErrors(HttpStatusCode.BadRequest, HttpStatusCode.Forbidden)] + public async Task RevokeOauthTokenPleroma(string id) + { + var token = await db.OauthTokens.FirstOrDefaultAsync(p => p.Id == id) ?? + throw GracefulException.Forbidden("You are not authorized to revoke this token"); + + db.Remove(token); + await db.SaveChangesAsync(); + + Response.StatusCode = 201; + } } \ No newline at end of file diff --git a/Iceshrimp.Backend/Controllers/Mastodon/Renderers/UserRenderer.cs b/Iceshrimp.Backend/Controllers/Mastodon/Renderers/UserRenderer.cs index d7e4a661..ca0b163d 100644 --- a/Iceshrimp.Backend/Controllers/Mastodon/Renderers/UserRenderer.cs +++ b/Iceshrimp.Backend/Controllers/Mastodon/Renderers/UserRenderer.cs @@ -65,7 +65,7 @@ public class UserRenderer( } else { - favicon = "http://localhost:3000/_content/Iceshrimp.Assets.Branding/favicon.wy3b9djz5j.png"; + favicon = config.Value.WebDomain + "/_content/Iceshrimp.Assets.Branding/favicon.png"; softwareName = "iceshrimp"; softwareVersion = config.Value.Version; } diff --git a/Iceshrimp.Backend/Controllers/Pleroma/Schemas/Entities/PleromaOauthTokenEntity.cs b/Iceshrimp.Backend/Controllers/Pleroma/Schemas/Entities/PleromaOauthTokenEntity.cs new file mode 100644 index 00000000..d965b7c1 --- /dev/null +++ b/Iceshrimp.Backend/Controllers/Pleroma/Schemas/Entities/PleromaOauthTokenEntity.cs @@ -0,0 +1,11 @@ +using System.Runtime.InteropServices.JavaScript; +using J = System.Text.Json.Serialization.JsonPropertyNameAttribute; + +namespace Iceshrimp.Backend.Controllers.Pleroma.Schemas.Entities; + +public class PleromaOauthTokenEntity +{ + [J("id")] public required string Id { get; set; } + [J("valid_until")] public required DateTime ValidUntil { get; set; } + [J("app_name")] public required string? AppName { get; set; } +} \ No newline at end of file diff --git a/Iceshrimp.Backend/Controllers/Pleroma/Schemas/Entities/PleromaUserExtensions.cs b/Iceshrimp.Backend/Controllers/Pleroma/Schemas/Entities/PleromaUserExtensions.cs index 5f0b7807..3f817d51 100644 --- a/Iceshrimp.Backend/Controllers/Pleroma/Schemas/Entities/PleromaUserExtensions.cs +++ b/Iceshrimp.Backend/Controllers/Pleroma/Schemas/Entities/PleromaUserExtensions.cs @@ -6,7 +6,7 @@ namespace Iceshrimp.Backend.Controllers.Pleroma.Schemas.Entities; [Keyless] public class PleromaUserExtensions { - [J("is_admin")] public required bool IsAdmin { get; set; } - [J("is_moderator")] public required bool IsModerator { get; set; } - [J("favicon")] public required string Favicon { get; set; } + [J("is_admin")] public required bool IsAdmin { get; set; } + [J("is_moderator")] public required bool IsModerator { get; set; } + [J("favicon")] public required string Favicon { get; set; } } \ No newline at end of file diff --git a/Iceshrimp.Backend/configuration.ini b/Iceshrimp.Backend/configuration.ini index 21dea1a4..b5b2c20a 100644 --- a/Iceshrimp.Backend/configuration.ini +++ b/Iceshrimp.Backend/configuration.ini @@ -7,8 +7,8 @@ ListenHost = localhost ;;ListenSocketPerms = 660 ;; Caution: changing these settings after initial setup *will* break federation -WebDomain = shrimp.example.org -AccountDomain = example.org +WebDomain = localhost:3000 +AccountDomain = localhost:3000 ;; End of problematic settings block ;; Additional domains this instance allows API access from, separated by commas.