about summary refs log tree commit diff
path: root/Tests/LibMatrix.HomeserverEmulator/Controllers/AuthController.cs
blob: da56ec41de0db09d5c6e0f182d92f77d168749bb (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
using System.Text.Json.Nodes;
using LibMatrix.HomeserverEmulator.Services;
using LibMatrix.Responses;
using LibMatrix.Services;
using Microsoft.AspNetCore.Mvc;

namespace LibMatrix.HomeserverEmulator.Controllers;

[ApiController]
[Route("/_matrix/client/{version}/")]
public class AuthController(ILogger<AuthController> logger, UserStore userStore, TokenService tokenService) : ControllerBase {
    [HttpPost("login")]
    public async Task<LoginResponse> Login(LoginRequest request) {
        if(!request.Identifier.User.StartsWith('@'))
            request.Identifier.User = $"@{request.Identifier.User}:{tokenService.GenerateServerName(HttpContext)}";
        if(request.Identifier.User.EndsWith("localhost"))
            request.Identifier.User = request.Identifier.User.Replace("localhost", tokenService.GenerateServerName(HttpContext));
        
        var user = await userStore.GetUserById(request.Identifier.User);
        if(user is null) {
            user = await userStore.CreateUser(request.Identifier.User);
        }

        return user.Login();
    }

    [HttpGet("login")]
    public async Task<LoginFlowsResponse> GetLoginFlows() {
        return new LoginFlowsResponse {
            Flows = ((string[]) [
                "m.login.password",
                "m.login.recaptcha",
                "m.login.sso",
                "m.login.email.identity",
                "m.login.msisdn",
                "m.login.dummy",
                "m.login.registration_token",
            ]).Select(x => new LoginFlowsResponse.LoginFlow { Type = x }).ToList()
        };
    }
}

public class LoginFlowsResponse {
    public required List<LoginFlow> Flows { get; set; }

    public class LoginFlow {
        public required string Type { get; set; }
    }
}