summaryrefslogtreecommitdiff
path: root/login/auth.php
blob: 304cb9357ce66f838bea7c4a7f0b66aee7333b69 (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

require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/key.php"; global $key;

if (!isset($_POST["username"]) || trim($_POST["username"]) === "" ||
    !isset($_POST["password"]) || trim($_POST["password"]) === ""
) {
    header("Location: /login/?error=Données de connexion incorrectes");
    die();
}

$data = [
    "url" => $key["valid"]["url"],
    "username" => $_POST["username"],
    "password" => $_POST["password"]
];

if ($key["valid"]["all"]) {
    if (!isset($_POST["server"]) || trim($_POST["server"]) === "" || !filter_var($_POST["server"], FILTER_VALIDATE_URL)) {
        header("Location: /login/?error=Données de connexion incorrectes");
        die();
    }

    $data["url"] = $_POST["server"];
}

$request = file_get_contents("http://127.0.0.1:21727/auth/login", false, stream_context_create([
    "http" => [
        "method" => "POST",
        "header" => "Content-Type: application/json",
        "content" => json_encode($data)
    ]
]));

if ($request === false) {
    header("Location: /login/?error=Identifiants incorrects, vous ne devez pas entrer vos identifiants d'ENT. %RM%");
} else {
    $token = json_decode($request, true)["token"];

    $space = json_decode(file_get_contents("http://127.0.0.1:21727/graphql", false, stream_context_create([
        "http" => [
            "method" => "POST",
            "header" => "Content-Type: application/json\r\n" .
                "Token: " . $token,
            "content" => json_encode([
                "query" => "{params{title}}"
            ])
        ]
    ])), true)["data"]["params"]["title"];

    if ($space !== "Espace Élèves") {
        header("Location: /login/?error=Seul l'espace élève est supporté pour le moment");
        die();
    }

    setcookie("nots_session", $token, 0, "/", "", false, true);
    header("Location: /");
}