summaryrefslogtreecommitdiff
path: root/src/Controllers/LoginController.php
blob: d48628b6697b7cdab7f2ced021aafc6eb174af1d (plain)
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
51
52
53
54
55
56
57
58
<?php

namespace App\Controllers;

use App\Errors\UnknownError;
use App\Support\Parser;
use App\Types\LoginFlow;
use App\Types\LoginType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;

class LoginController
{
  /**
   * GET /_matrix/client/r0/login
   * GET /_matrix/client/v3/login
   */
  public function supportedLoginTypes(): Response
  {
    return new JsonResponse([
      "flows" => [
        (new LoginFlow(LoginType::PASSWORD))->toArray(),
      ],
    ]);
  }

  /**
   * POST /_matrix/client/v3/login
   */
  public function login(): Response
  {
    $request = Request::createFromGlobals();
    $content = json_decode($request->getContent(), true);

    // validate login type
    $loginType = null;
    try {
      $loginType = LoginType::from($content["type"]);
    } catch (\ValueError $error) {
      throw new UnknownError("Bad login type.", Response::HTTP_BAD_REQUEST);
    }

    // get user name
    $user = Parser::parseUser($content["identifier"]["user"]);

    #if ($loginType == LoginType::PASSWORD) {}

    return new JsonResponse([
      "access_token" => "abc123",
      "device_id" => "ABC",
      "expires_in_ms" => 60000,
      "refresh_token" => "def456",
      "user_id" => "@{$user["username"]}:{$_ENV["DOMAIN"]}",
      #"well_known" => [],
    ]);
  }
}