Skip to content

Instantly share code, notes, and snippets.

@TK009
Last active October 28, 2020 11:37
Show Gist options
  • Select an option

  • Save TK009/dbec8e76f32b7176e16c to your computer and use it in GitHub Desktop.

Select an option

Save TK009/dbec8e76f32b7176e16c to your computer and use it in GitHub Desktop.
login with (firefox) remembered aalto account data automatically, also a+ ultimate destination fix
// ==UserScript==
// @name aalto auto login
// @namespace tkscripts
// @description login with (firefox) remembered aalto account data automatically, also a+ ultimate destination fix
// @include https://idp.aalto.fi/idp/Authn/*
// @include https://noppa.aalto.fi/noppa/app
// @include https://oodi.aalto.fi/a/*
// @include https://plus.cs.hut.fi/
// @include https://plus.cs.hut.fi/accounts/login/?*
// @include https://plus.cs.aalto.fi/
// @include https://plus.cs.aalto.fi/accounts/login/?*
// @include https://haka.funet.fi/shibboleth/*
// @include https://rubyric.com/
// @include https://rubyric.com/session/new
// @include https://stack.tkk.fi/*
// @include https://optima.aalto.fi/
// @include https://optima.aalto.fi/login*
// @include https://piazza.com/
// @include https://mail.aalto.fi/owa/auth*
// @include https://mycourses.aalto.fi/
// @include https://mycourses.aalto.fi/login*
// @include https://version.aalto.fi/*
// @include https://idp.aalto.fi/idp/profile/SAML2/Redirect/SSO*
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js
// @version 2.2.2
// @grant none
// ==/UserScript==
// This is GreaseMonkey UserScript for firefox
// To use this:
// * log in to aalto and/or piazza and click "Remember password"
// * install GreaseMonkey
// * open this script in the browser and you should be prompted to
// install with GreaseMonkey
//THE SCRIPT 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 SCRIPT OR THE USE OR OTHER DEALINGS IN
//THE SCRIPT.
// The script may break and result in an infinite redirect loop if some
// associated web page updates and as a result some login pages may block you (hopefullu for
// a short time only)
this.$ = this.jQuery = jQuery.noConflict(true);
$(function() {
if (typeof String.prototype.startsWith != 'function') {
String.prototype.startsWith = function (str){
return this.slice(0, str.length) == str;
};
}
function test(target) {
return typeof target !== "undefined" && target !== null
}
function gotoLogin(loginLink){
if (test(loginLink)) {
console.log('aalto autologin: loggin in with link: ' + loginLink);
window.location.href = loginLink;
return true;
} else {
console.log('aalto autologin: invalid login link: ' + loginLink);
return false;
}
}
// For aalto and piazza logins
function tryLogin() {
console.log('aalto autologin: try login');
var form = $("#login"+ // aalto
",#login-form"); // piazza
if ($('#username'+ // aalto
',#email_field' // piazza
).val().length > 0
//&& $('#password'+ // aalto
// ',#password_field' // piazza
// ).val().length > 0
) {
console.log('aalto autologin: submitting');
form.submit();
return true;
} else {
$('#username'+
',#email_field').on('change', () => form.submit())
return false;
}
};
var pageurl = window.location.href;
// Page dependent action:
// ======================
// Courses
if (pageurl == "https://noppa.aalto.fi/noppa/app" || pageurl.startsWith("https://mycourses.aalto.fi/")) {
var loginLink = $(
'a:contains("Kirjaudu sisään")'+
',a:contains("Log in")'
).attr('href');
gotoLogin(loginLink);
//} else if (pageurl.startsWith("https://oodi.aalto.fi/a/")){
// function oodi() {
// var loginLink = $(
// 'a:contains("OPISKELIJA - Kirjaudu sisään")'+
// ',a:contains("STUDENT - Log in")',
// window.parent.frames[1].document)
// .attr('href');
// gotoLogin(loginLink);
// }
// oodi();
// // second frame seems to be loaded after this script is run
// window.setTimeout(oodi, 600);
} else if (pageurl.startsWith("https://plus.cs.hut.fi/accounts/login/")){
// save ultimate destination, it seems that a+ ?next= ultimate destination
// system works when logged in, so use current location
window.sessionStorage.udestination = window.location.href;
console.log('aalto autologin: saving destination: ' + window.location.href);
var loginLink = $('a:contains("Aalto WebLogin")').attr('href');
gotoLogin(loginLink);
} else if (pageurl.startsWith("https://haka.funet.fi/shibboleth/")){
// Select aalto university from organizations list XXX: url hardcoded
// jquery should be ok if not found
console.log("aalto autologin: Haka idp select")
$('#userIdPSelection').val("https://idp.aalto.fi/idp/shibboleth");
$('#IdPList').submit();
return;
} else if (pageurl.startsWith("https://idp.aalto.fi/idp/profile/SAML2/Redirect/SSO")){
$('input[value="Accept"]').click();
} else if (pageurl == "https://plus.cs.hut.fi/"){
if (test(window.sessionStorage.udestination)) {
var dest = window.sessionStorage.udestination;
console.log('aalto autologin: go to destination: ' + dest);
delete window.sessionStorage.udestination;
window.location.href = dest;
}
//} else if (pageurl.startsWith("https://stack.tkk.fi/")) {
// // the "old" stack system
// // "Täällä" button login redirect
// var loginLink = $('a:contains("Täällä")').attr('href');
// gotoLogin(loginLink);
// // Haka
// var loginLink = $('img[alt="HAKA-federation"]' +
// ',img[alt="HAKA-federaatio"]'
// ).parent().attr('href');
// gotoLogin(loginLink);
// // Click here...
// var gotoLink = $('a:contains("Click here to continue.")' +
// ',a:contains("Klikkaa tätä jatkaaksesi.")'
// ).attr('href');
// gotoLogin(gotoLink);
} else if (pageurl.startsWith("https://idp.aalto.fi/idp/Authn/")) {
// for the real aalto login page
//var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
if (tryLogin()) return;
// is the remembered password inserted instantly?
//window.setTimeout(tryLogin, 120);
} else if (pageurl.startsWith("https://piazza.com/")) {
if (tryLogin()) return;
//window.setTimeout(tryLogin, 120);
}
// Generalized login solver
// ========================
// searches for link containing "shibboleth"
// should work for example some haka logins and aalto shibboleth
console.log('aalto autologin: trying generic login link finder');
const links = $('a');
const loginLinks = links.filter(
'a[href*="shibboleth"],'+
'a[href*="Shibboleth.sso"]').toArray();
for (const loginLink of loginLinks)
if (gotoLogin($(loginLink).attr('href'))) return;
const loginLinks2 = links.filter((i,e) =>
e.textContent.match(/(kirjaudu sisään|log ?in|sign in|aalto login)/gi) != null
).toArray();
for (const loginLink of loginLinks2)
if (gotoLogin($(loginLink).attr('href'))) return;
console.log('aalto autologin: page not supported: ' + pageurl);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment