diff options
Diffstat (limited to 'alarm/node_modules/pronote-api/src/cas/toutatice.js')
-rw-r--r-- | alarm/node_modules/pronote-api/src/cas/toutatice.js | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/alarm/node_modules/pronote-api/src/cas/toutatice.js b/alarm/node_modules/pronote-api/src/cas/toutatice.js new file mode 100644 index 0000000..84c10b0 --- /dev/null +++ b/alarm/node_modules/pronote-api/src/cas/toutatice.js @@ -0,0 +1,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; |