about summary refs log tree commit diff
path: root/Utilities/LibMatrix.HomeserverEmulator/Services/TokenService.cs
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2024-09-17 04:26:25 +0200
committerRory& <root@rory.gay>2024-09-17 04:26:25 +0200
commit37a8ac420278fd53ab8218956f1ba13692feb48e (patch)
tree83afec51130a254f6ed0ba11c45975f3f2ba8b9f /Utilities/LibMatrix.HomeserverEmulator/Services/TokenService.cs
parentDrop example bots (diff)
parentMove around some projects, further cleanup pending (diff)
downloadLibMatrix-bak-37a8ac420278fd53ab8218956f1ba13692feb48e.tar.xz
Merge remote-tracking branch 'origin/dev/project-cleanup'
Diffstat (limited to 'Utilities/LibMatrix.HomeserverEmulator/Services/TokenService.cs')
-rw-r--r--Utilities/LibMatrix.HomeserverEmulator/Services/TokenService.cs29
1 files changed, 29 insertions, 0 deletions
diff --git a/Utilities/LibMatrix.HomeserverEmulator/Services/TokenService.cs b/Utilities/LibMatrix.HomeserverEmulator/Services/TokenService.cs
new file mode 100644

index 0000000..cf79aae --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/Services/TokenService.cs
@@ -0,0 +1,29 @@ +namespace LibMatrix.HomeserverEmulator.Services; + +public class TokenService{ + public string? GetAccessTokenOrNull(HttpContext ctx) { + //qry + if (ctx.Request.Query.TryGetValue("access_token", out var token)) { + return token; + } + //header + if (ctx.Request.Headers.TryGetValue("Authorization", out var auth)) { + var parts = auth.ToString().Split(' '); + if (parts is ["Bearer", _]) { + return parts[1]; + } + } + return null; + } + + public string GetAccessToken(HttpContext ctx) { + return GetAccessTokenOrNull(ctx) ?? throw new MatrixException() { + ErrorCode = MatrixException.ErrorCodes.M_UNKNOWN_TOKEN, + Error = "Missing token" + }; + } + + public string? GenerateServerName(HttpContext ctx) { + return ctx.Request.Host.ToString(); + } +} \ No newline at end of file