summaryrefslogtreecommitdiff
path: root/alarm/node_modules/pronote-api/src/cas/toutatice.js
blob: 84c10b0a9f991d828cb0760b9f117fbf27ab85cb (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
const jsdom = require('jsdom');
const axioRequest = require('../axioRequest')

const { getDOM, submitForm, extractStart } = require('./api');
const educonnect = require('./generics/educonnect');
const querystring = require('querystring');
const http = require('../http');

async function login(url, account, username, password) {
    const jar = new jsdom.CookieJar();
    let dom = await getDOM({
        url: 'https://www.toutatice.fr/portail/auth/pagemarker/2/MonEspace',
        jar
    });

    dom = await submitForm({
        dom,
        jar,
        actionRoot: 'https://www.toutatice.fr/wayf/',
        extraParams: {
            // eslint-disable-next-line camelcase
            _saml_idp: 'educonnect'
        }
    });
    dom = await educonnect({ dom, jar, url, account, username, password });

    let redirectURL = dom.window.document.getElementsByTagName('a')[0].href

    let response = await axioRequest({
        url: redirectURL,
        jar
    })

    redirectURL = getOrigin(redirectURL) + response.headers.location

    const parsed = querystring.parse(redirectURL.split('?')[1])
    const conversation = parsed.conversation
    const sessionid = parsed.sessionid

    // eslint-disable-next-line max-len
    redirectURL = `${getOrigin(redirectURL)}/idp/Authn/RemoteUser?conversation=${conversation}&redirectToLoaderRemoteUser=0&sessionid=${sessionid}`

    response = await axioRequest({
        url: redirectURL,
        jar
    })
    // eslint-disable-next-line max-len
    const remoteUserParsed = response.data.match(/<conversation>(.+)<\/conversation><uidInSession>(.+)<\/uidInSession>/u)

    const remoteUserConversation = remoteUserParsed[1]
    const uidInSession = remoteUserParsed[2]

    // eslint-disable-next-line max-len
    redirectURL = `${getOrigin(redirectURL)}/idp/Authn/RemoteUser?conversation=${remoteUserConversation}&uidInSession=${uidInSession}&sessionid=${sessionid}`

    response = await http({
        url: redirectURL,
        jar,
        followRedirects: true
    })

    return extractStart(await getDOM({
        url: `${url}${account.value}.html`,
        jar,
        asIs: true
    }))
}

function getOrigin(url) {
    const noProtocol = url.substring(url.indexOf('/') + 2);
    return url.substring(0, url.indexOf('/')) + '//' + noProtocol.substring(0, noProtocol.indexOf('/'));
}

module.exports = login;