summaryrefslogtreecommitdiff
path: root/school/node_modules/pronote-api/src/cas/generics/aten.js
blob: 44cfa83885a439ebfb6a136866908376373b6d78 (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
const fs = require('fs');
const path = require('path');

const jsdom = require('jsdom');

const errors = require('../../errors');
const { getDOM, submitForm, extractStart } = require('../api');

// eslint-disable-next-line no-sync
const jsEncrypt = fs.readFileSync(path.join(__dirname, 'jsencrypt.min.js'));

async function login({ url, account, username, password, startURL, atenURL, postSubmit })
{
    if (!startURL.startsWith('http')) {
        if (startURL.startsWith('/')) {
            startURL = startURL.substring(1);
        }

        startURL = `https://${atenURL}/${startURL}`;
    }

    const jar = new jsdom.CookieJar();
    const dom = await getDOM({
        url: startURL,
        jar,
        runScripts: true,
        hook
    });

    await submit({ dom, jar, username, password, atenURL });

    if (postSubmit) {
        await postSubmit({ dom, jar });
    }

    return extractStart(await getDOM({
        url: url + account.value + '.html',
        jar,
        asIs: true
    }));
}

async function submit({ dom, jar, username, password, atenURL })
{
    dom.window.document.getElementById('user').value = username;
    dom.window.document.getElementById('password').value = password;

    dom.window.eval('creerCookie(document.getElementById(\'user\'), document.getElementById(\'password\'));');

    const result = await submitForm({
        dom,
        jar,
        actionRoot: `https://${atenURL}/login/`
    });

    if (result.window.document.getElementById('aten-auth')) {
        throw errors.WRONG_CREDENTIALS.drop();
    }

    return submitForm({
        dom: result,
        jar,
        asIs: true
    });
}

function hook(window)
{
    window.eval(jsEncrypt + '; window.JSEncrypt = JSEncrypt;');
}

module.exports = {
    login,
    submit,
    hook
};