|
| 1 | +<?php |
| 2 | +/** |
| 3 | +* Plugin Name: latch |
| 4 | +* Plugin URI: http://www.elevenpaths.com |
| 5 | +* Description: Latch MediaWiki integration |
| 6 | +* Author: Paula - paula.rm@gmail.com |
| 7 | +* Version: 1.0 |
| 8 | +* Compatibility: MediaWiki 1.23.8 |
| 9 | +*/ |
| 10 | + |
| 11 | + |
| 12 | +$dir = __DIR__ . '/'; //set the internationalization files directory for the extension |
| 13 | +$wgMessagesDirs['latch'] = __DIR__ . '/i18n'; |
| 14 | + |
| 15 | +$wgHooks['UserLoadAfterLoadFromSession'][] = 'onUserLoadAfterLoadFromSession'; |
| 16 | +$wgHooks['GetPreferences'][] = 'onPreferencesForm';//to add buttons, text box, to the 2FA tab in user's preferences |
| 17 | +$wgHooks['PreferencesFormPreSave'][] = 'onPreferencesFormPreSave'; |
| 18 | +$wgHooks['UserLogout'][] = 'onLogout'; |
| 19 | + |
| 20 | +/** |
| 21 | + * This hook is called when the user logs off Mediawiki |
| 22 | + * to unset the session variable latchStatus. |
| 23 | + * This variable is used in order to check only once |
| 24 | + * during the logging if the user has an active latch that do not permit the login |
| 25 | + */ |
| 26 | +function onLogout( &$user ) |
| 27 | +{ |
| 28 | + global $wgRequest; |
| 29 | + $wgRequest->setSessionData("latchStatus", null); |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +/** |
| 34 | + * Called to authenticate users on external/environmental means; |
| 35 | + * occurs after session is loaded |
| 36 | + */ |
| 37 | +function onUserLoadAfterLoadFromSession( $user ) |
| 38 | +{ |
| 39 | + |
| 40 | + global $wgRequest; //used to set a session variable, when the user logs into MW, check once if there is a latch and save it in the variable |
| 41 | + |
| 42 | + if( $user->getId() > 0 ) // user logged in MediaWiki |
| 43 | + { |
| 44 | + $latchStatus = $wgRequest->getSessionData("latchStatus");// have we checked the 2FA status for this session yet? |
| 45 | + |
| 46 | + if( $latchStatus==NULL ) |
| 47 | + { |
| 48 | + // no, the 2FA should be checked |
| 49 | + //if( (string)LatchController::checkLatchStatus( ) == "on" ) |
| 50 | + if( LatchController::checkLatchStatus( ) == "on" ) |
| 51 | + { |
| 52 | + // 2FA says ok. Save this in session in order to avoid |
| 53 | + // further checks in this session |
| 54 | + $wgRequest->setSessionData("latchStatus", true); |
| 55 | + } |
| 56 | + else // 2FA says no. Logout the user inmediatly. |
| 57 | + { $user->logout(); } |
| 58 | + } |
| 59 | + else { |
| 60 | + // the 2FA is in session. No need to check again. |
| 61 | + } |
| 62 | + } |
| 63 | + return true; // Required return value of a hook function. |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * This hook is invoked through getPreferences(...) function from /includes/Preferences.php |
| 68 | + * the onPreferencesForm function displays a tab in the user's preferences options |
| 69 | + * with 2FAuthentication settings (pair/unpair account) |
| 70 | + */ |
| 71 | +function onPreferencesForm( $user, &$preferences ) |
| 72 | +{ |
| 73 | + |
| 74 | + if( dbHelper::isPaired( ) ) //if the user is paired render the view with unpair options in the form |
| 75 | + { |
| 76 | + $preferences['formPairedButton'] = array( |
| 77 | + 'type' => 'submit', |
| 78 | + 'section' => '2FA/Latch',//'2nd factor authentication |
| 79 | + 'id'=>'unpairButton', |
| 80 | + 'default' => wfMessage("prefs-2FA-button-unpair"), |
| 81 | + ); |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + else //if the user is not paired render the view with pair options in the form |
| 86 | + { |
| 87 | + $preferences['formUnpairedTextbox'] = array( |
| 88 | + 'type' => 'text', |
| 89 | + 'section' => '2FA/Latch', |
| 90 | + 'label-message' => 'prefs-2FA-label', |
| 91 | + 'maxlength' => '6', //OTP is maximum 6 characters. |
| 92 | + 'default' => '',//clear the last user input |
| 93 | + 'id'=>'pairingToken', |
| 94 | + ); |
| 95 | + |
| 96 | + $preferences['formUnpairedButton'] = array( |
| 97 | + 'type' => 'submit', |
| 98 | + 'section' => '2FA/Latch', |
| 99 | + 'id'=>'pairButton', |
| 100 | + 'default' => wfMessage("prefs-2FA-button-pair"), |
| 101 | + ); |
| 102 | + |
| 103 | + $preferences['formUnpairedMessage'] = array ( |
| 104 | + 'type' => 'info', |
| 105 | + 'section' => '2FA/Latch', |
| 106 | + 'help-message' => 'prefs-2FA-help', |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + return true; // Required return value of a hook function. |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * Allows extension to modify what preferences will be saved |
| 115 | + */ |
| 116 | +function onPreferencesFormPreSave( $formData, $form, $user, &$result ) |
| 117 | +{ |
| 118 | + $errorMsg = ''; |
| 119 | + //the user has not paired Mediawiki account with Latch |
| 120 | + if( !dbHelper::isPaired() ) |
| 121 | + { |
| 122 | + $oneTimePassword = $formData["formUnpairedTextbox"]; //get the OTP writen by the user in the textbox form |
| 123 | + LatchController::doPair($oneTimePassword); |
| 124 | + |
| 125 | + } |
| 126 | + //the user has paired Mediawiki account with Latch |
| 127 | + else if( dbHelper::isPaired() && isset( $_POST["wpformPairedButton"] ) ) |
| 128 | + { |
| 129 | + $responseUnpair = LatchController::doUnpair(); |
| 130 | + if( $responseUnpair == -1 ) |
| 131 | + { |
| 132 | + $errorMsg = "Error durante proceso de despareado, por favor vuelve a intentarlo."; |
| 133 | + if ( $errorMsg <> '' ) |
| 134 | + { |
| 135 | + return $errorMsg; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + } |
| 140 | + |
| 141 | + return true; // Required return value of a hook function. |
| 142 | +} |
| 143 | + |
0 commit comments