about summary refs log tree commit diff
path: root/MatrixRoomUtils.Core/Authentication/MatrixAuth.cs
blob: b1b0362d6bdde69cc2f501f054a0259a8c482e61 (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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System.Net.Http.Json;
using System.Text.Json;
using MatrixRoomUtils.Core.Extensions;
using MatrixRoomUtils.Core.Responses;
using MatrixRoomUtils.Core.StateEventTypes;

namespace MatrixRoomUtils.Core.Authentication;

public class MatrixAuth {
    public static async Task<LoginResponse> Login(string homeserver, string username, string password) {
        Console.WriteLine($"Logging in to {homeserver} as {username}...");
        homeserver = (await new RemoteHomeServer(homeserver).Configure()).FullHomeServerDomain;
        var hc = new MatrixHttpClient();
        var payload = new {
            type = "m.login.password",
            identifier = new {
                type = "m.id.user",
                user = username
            },
            password,
            initial_device_display_name = "Rory&::MatrixRoomUtils"
        };
        Console.WriteLine($"Sending login request to {homeserver}...");
        var resp = await hc.PostAsJsonAsync($"{homeserver}/_matrix/client/v3/login", payload);
        Console.WriteLine($"Login: {resp.StatusCode}");
        var data = await resp.Content.ReadFromJsonAsync<JsonElement>();
        if (!resp.IsSuccessStatusCode) Console.WriteLine("Login: " + data);

        Console.WriteLine($"Login: {data.ToJson()}");
        return data.Deserialize<LoginResponse>();
        //var token = data.GetProperty("access_token").GetString();
        //return token;
    }

    [Obsolete("Migrate to IHomeServer instance")]
    public static async Task<ProfileResponse> GetProfile(string homeserver, string mxid) => await (await new RemoteHomeServer(homeserver).Configure()).GetProfile(mxid);

    private static async Task<bool> CheckSuccessStatus(string url) {
        //cors causes failure, try to catch
        try {
            using var hc = new HttpClient();
            var resp = await hc.GetAsync(url);
            return resp.IsSuccessStatusCode;
        }
        catch (Exception e) {
            Console.WriteLine($"Failed to check success status: {e.Message}");
            return false;
        }
    }
}