summaryrefslogtreecommitdiff
path: root/pages/pair.inc
blob: 3adc14fcc1b9723b75e9c432662a9a415a33f9bc (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php

require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/init.inc"; global $title; global $isLoggedIn; global $isLowerLoggedIn; global $lang; global $pages;
require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/components/header.inc'; global $_PROFILE;

?>

<br>
<div class="container">
    <h1>Pair a new device</h1>

    <div class="alert alert-success alert-dismissible" id="pair-confirm" style="display: none;">
        <button type="button" class="btn-close" onclick="document.getElementById('pair-confirm').style.display='none';"></button>
        Successfully paired "<span id="paired-name-1">-</span>" with your Ponycule account. You may now close this page.
    </div>

    <div class="alert alert-danger alert-dismissible" id="pair-cancel" style="display: none;">
        <button type="button" class="btn-close" onclick="document.getElementById('pair-cancel').style.display='none';"></button>
        Cancelled pairing "<span id="paired-name-2">-</span>", this device does not have access to your account. You may now close this page.
    </div>

    <p>Pairing allows a connected device that cannot normally use Ponycule to gather data. This device will get full access to your account as if they were acting on your behalf, so make sure you trust the device and/or application you are using.</p>

    <?php $data = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/tokens/" . str_replace("/", "", $_COOKIE['PEH2_SESSION_TOKEN'])), true); if (isset($data["profile"])): ?>
    <p>Equestria.dev may not have verified the application you use, always check for a signature. Enter the pairing code displayed on your device below:</p>

    <input autofocus type="text" placeholder="Pairing code" class="form-control" style="margin-bottom:15px;color:white;background:#111;border-color:#222;" id="code">
    <?php else: ?>
    <div class="alert alert-danger">
        <b>Error:</b> You cannot use the pairing feature because your current session is using the old authentication system. Please log out and log in again to continue.
    </div>
    <?php endif; ?>
</div>

<div class="modal fade" id="confirm">
    <div class="modal-dialog">
        <div class="modal-content">

            <div class="modal-header">
                <h4 class="modal-title">Confirm pairing request</h4>
                <button type="button" class="btn-close" onclick="reject();"></button>
            </div>

            <div class="modal-body">
                <p>You are about to pair the following device with your Ponycule account:</p>
                <blockquote style="display: grid; grid-template-columns: max-content 1fr; grid-gap: 10px;">
                    <div style="display: flex; align-items: center; justify-content: center;">
                        <img src="/assets/logo/newlogo-ng.png" id="device-icon" style="width: 32px; height: 32px;">
                    </div>
                    <div>
                        <b>Name:</b> <span id="device-name">-</span><br>
                        <b>Address:</b> <span id="device-address">-</span>
                    </div>
                </blockquote>
                <p class="text-danger">Only enter pairing codes coming from your physical device, not a screenshot or a photo. Never use a code sent to you by another user, even if you trust them.</p>
                <span class="btn btn-success disabled" id="modal-button" style="margin-right: 5px;" onclick="confirm();">Confirm</span><span class="btn btn-outline-secondary" onclick="reject();">Cancel</span>
            </div>
        </div>
    </div>
</div>

<style>
    <?php global $use2023UI; if (!$use2023UI): ?>
    .modal-header {
        border-bottom: 1px solid #353738;
    }

    .modal-content {
        border: 1px solid rgba(255, 255, 255, .2);
        background-color: #111;
    }

    .btn-close {
        filter: invert(1);
    }

    .alert-dismissible .btn-close {
        filter: none !important;
    }
    <?php endif ?>

    blockquote {
        margin-left: 5px;
        padding-left: 10px;
        border-left: 3px solid var(--palette-5, rgba(255, 255, 255, .25));
    }
</style>

<script src="/assets/editor/ua-parser.js"></script>
<script>
    const modal = new bootstrap.Modal(document.getElementById("confirm"));

    function getIconForName(name) {
        if (name.startsWith("Luna Desktop ") || name.startsWith("Luna Mobile ")) {
            return "https://git.equestria.dev/equestria.dev/luna/raw/branch/mane/icons/logo.png";
        }

        if (name.startsWith("Cold Haze for Wear OS ") || name.startsWith("Ponycule for Wear OS ")) {
            return "https://cdn.equestria.dev/ch-wear.png";
        }

        return "/assets/logo/newlogo-ng.png";
    }

    (async () => {
        const token = await (await fetch("/api/token")).text();
        let ws = window.ws = new WebSocket("wss://ponycule.p.equestria.dev/_PairingServices-WebSocket-EntryPoint/socket");

        ws.onopen = (event) => {
            console.log(event);

            if (location.hash && location.hash.startsWith("#/")) {
                document.getElementById("code").value = location.hash.substring(2);
                document.getElementById("code").onkeydown();
                location.hash = "";
            }
        }

        ws.onclose = (event) => {
            console.log(event);
        }

        ws.onmessage = (event) => {
            let data = JSON.parse(event.data);
            console.log(event, data);

            if (data.type === "init") {
                ws.send(JSON.stringify({
                    type: "init",
                    token
                }));
            } else if (data.type === "invalid") {
                document.getElementById("code").value = "";
                document.getElementById("code").disabled = false;
                modal.hide();
                document.getElementById("code").focus();
            } else if (data.type === "device") {
                document.getElementById("device-name").innerText = document.getElementById("paired-name-1").innerText = document.getElementById("paired-name-2").innerText = window.currentName = data.identity.name;
                document.getElementById("device-address").innerText = data.identity.address;
                document.getElementById("device-icon").src = getIconForName(data.identity.name ?? "");
                document.getElementById("modal-button").classList.add("disabled");
                modal.show();

                setTimeout(() => {
                    document.getElementById("modal-button").classList.remove("disabled");
                }, 3000);
            }
        }
    })();

    document.getElementById("code").onkeyup = document.getElementById("code").onkeydown = () => {
        if (document.getElementById("code").value.length > 0) {
            document.getElementById("pair-confirm").style.display = "none";
            document.getElementById("pair-cancel").style.display = "none";
        }

        if (document.getElementById("code").value.length === 5) {
            document.getElementById("code").disabled = true;
            pair(document.getElementById("code").value);
        }
    }

    window.currentCode = null;
    window.currentName = "";

    function pair(code) {
        const ua = new UAParser(navigator.userAgent).getResult();
        const username = `<?= $_PROFILE["name"] ?>`;

        window.currentCode = code;

        ws.send(JSON.stringify({
            type: "fetch",
            identity: {
                name: username,
                platform: ua.browser.name + " " + ua.browser.major + " on " + ua.os.name
            },
            code
        }));
    }

    async function confirm() {
        document.getElementById("pair-confirm").style.display = "";

        document.getElementById("code").value = "";
        document.getElementById("code").disabled = false;
        modal.hide();
        document.getElementById("code").focus();

        ws.send(JSON.stringify({
            type: "confirm",
            code: window.currentCode,
            token: (await (await fetch("/api/reauthenticate/?name=" + encodeURIComponent(btoa(window.currentName)))).text()).trim()
        }));
    }

    async function reject() {
        document.getElementById("pair-cancel").style.display = "";

        document.getElementById("code").value = "";
        document.getElementById("code").disabled = false;
        modal.hide();
        document.getElementById("code").focus();

        ws.send(JSON.stringify({
            type: "reject",
            code: window.currentCode
        }));
    }

    window.onload = () => {
        document.getElementById("code").focus();
    }
</script>

<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/components/footer.inc'; ?>