Skip to content

Instantly share code, notes, and snippets.

@antonilol
Last active November 2, 2021 14:56
Show Gist options
  • Save antonilol/88a1edf32c5541baf9516a41bb0beb2d to your computer and use it in GitHub Desktop.
Save antonilol/88a1edf32c5541baf9516a41bb0beb2d to your computer and use it in GitHub Desktop.
getAuthCode.js: get the magister website authCode by sort of imitating the web client
/*
* Copyright (c) 2021 Antoni Spaanderman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
const https = require("https");
function get(path) {
return new Promise(r => {
const req = https.request({
hostname: 'accounts.magister.net',
port: 443,
path: path,
method: 'GET'
}, res => {
var data = "";
res.on('data', chunk => { data += chunk; });
res.on('end', () => { r({data, res}); });
})
req.on('error', error => {
console.error(error);
});
req.end();
});
}
async function getAuthCode() {
const sessionIdUrl = (await get('/Account/Login?ReturnUrl=%2Fconnect%2Fauthorize%2Fcallback%3Fclient_id%3DM6-marnix.magister.net%26redirect_uri%3Dhttps%253A%252F%252Fmarnix.magister.net%252Foidc%252Fredirect_callback.html%26response_type%3Did_token%2520token%26scope%3Dopenid%2520profile%2520opp.read%2520opp.manage%2520attendance.overview%2520calendar.ical.user%26state%3D00000000000000000000000000000000%26nonce%3D00000000000000000000000000000000%26acr_values%3Dtenant%253Amarnix.magister.net&X-Correlation-ID=000000000000&returnUrl=%2Fconnect%2Fauthorize%3Fclient_id%3DM6-marnix.magister.net%26redirect_uri%3Dhttps%253A%252F%252Fmarnix.magister.net%252Foidc%252Fredirect_callback.html%26response_type%3Did_token%2520token%26scope%3Dopenid%2520profile%2520opp.read%2520opp.manage%2520attendance.overview%2520calendar.ical.user%26state%3D00000000000000000000000000000000%26nonce%3D00000000000000000000000000000000%26acr_values%3Dtenant%253A')).res.headers.location;
const scriptUrl = "/" + (await get(sessionIdUrl)).data.split('script defer="defer" src="')[1].split('"')[0];
const z = JSON.parse("[[" + (await get(scriptUrl)).data.split(/=\(.=\[/)[1].split('.map')[0] + "]");
const authCode = z[1].map(x => z[0][parseInt(x)||0]).join("");
return authCode;
}
exports.getAuthCode = getAuthCode;
@antonilol
Copy link
Author

ok nu is het een regex

@antonilol
Copy link
Author

now you can import this script from another file with

const { getAuthCode } = require('./getAuthCode.js');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment