about summary refs log tree commit diff
path: root/Tests/LibMatrix.HomeserverEmulator/Services/TokenService.cs
blob: 8115beed90d4562080ccf1341672b1e563bbd2fa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
namespace LibMatrix.HomeserverEmulator.Services;

public class TokenService(IHttpContextAccessor accessor) {
    public string? GetAccessToken() {
        var ctx = accessor.HttpContext;
        if (ctx is null) return null;
        //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.Length == 2 && parts[0] == "Bearer") {
                return parts[1];
            }
        }
        return null;
    }

    public string? GenerateServerName() {
        var ctx = accessor.HttpContext;
        if (ctx is null) return null;
        return ctx.Request.Host.ToString();
    }
}