Extension:Typo3Auth

From MediaWiki.org

Jump to: navigation, search

             

Manual on MediaWiki Extensions
List of MediaWiki Extensions
Crystal Clear action run.png
Typo3Auth

Release status: beta

Implementation  User identity
Description Authenticate users using info from a Typo3 Extension
Author(s)  CmTalk
Last Version  1.0.0 (28 October 2009)
MediaWiki  1.15.1
PHP  5
License GPL
Download Extension:Typo3Auth#Code

check usage (experimental)

Contents

[edit] What can this extension do?

Authenticates users based on a Typo3 Installation which has the Typo3 extension 'authagainstTypo3' installed.

[edit] Comments & Feedback

Comments & Feedback discussion page.

[edit] Download instructions

Please cut and paste the code found below and place it in $IP/extensions/Typo3Auth/Typo3Auth.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

[edit] Installation

To install this extension:

require_once("extensions/Typo3Auth/Typo3Auth.php");
$wgAuth = new Typo3Auth();
$wgTypo3AuthUrl = 'http://yourTypo3WebserviceUrl'; // The URL to the Typo3 page where authagainstTypo3 is installed
$wgTypo3AuthServiceUser = 'ServiceUser'; // The username for the Typo3 webservice
$wgTypo3AuthServicePassword = 'Pass'; // The password for the Typo3 webservice

[edit] Code

<?php 
 
/**
 * Authentication plugin interface. Instantiate a subclass of AuthPlugin
 * and set $wgAuth to it to authenticate against an external tool.
 *
 * Our subclass requires that all accounts authenticate externally 
 * and transparently creates internal wiki accounts the first time
 * someone logs in who can be authenticated externally.
 *
 * @author	Cynthia Mattingly @ Marketing Factory Consulting <cm@marketing-factory.de>
 * Typo3 Extension: http://forge.typo3.org/projects/show/extension-authagainsttypo3
 * 
 * @license GNU GPL 2.0 or later
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
/**
    * Add into LocalSettings.php the following code: 
    *
 
	require_once("extensions/Typo3Auth/Typo3Auth.php");
	$wgAuth = new Typo3Auth();
	$wgTypo3AuthUrl = 'http://yourTypo3WebserviceUrl'; // The URL to the Typo3 page where authagainstTypo3 is installed
	$wgTypo3AuthServiceUser = 'ServiceUser'; // The username for the Typo3 webservice
	$wgTypo3AuthServicePassword = 'Pass'; // The password for the Typo3 webservice
 
*/
 
$version = '1.0.0';
 
$wgExtensionCredits['parserhook'][] = array (
	'name' => 'Typo3Auth',
	'author' => '[http://www.mediawiki.org/wiki/User:Cm]',
	'description' => 'Authenticate users using info from a Typo3 Extension',
	'url' => 'http://www.mediawiki.org/wiki/Extension:Typo3Auth',
	'version' => $version,
);
 
require_once ( 'AuthPlugin.php' );
class Typo3Auth extends AuthPlugin
{
	/**
     * Check whether there exists a user account with the given name.
     * The name will be normalized to MediaWiki's requirements, so
     * you might need to munge it (for instance, for lowercase initial
     * letters).
     *
     * @param $username String: username.
     * @return bool
     * @public
     */
    function userExists( $username ) {
        # Override this!
        return true;
    }
 
    /**
     * Check if a username+password pair is a valid login.
     * The name will be normalized to MediaWiki's requirements, so
     * you might need to munge it (for instance, for lowercase initial
     * letters).
     *
     * @param $username String: username.
     * @param $password String: user password.
     * @return bool
     * @public
     */
    function authenticate( $username, $password )
    {
    	$url = $GLOBALS['wgTypo3AuthUrl'];
 
		$ch = curl_init();
 
		// set the target url
		curl_setopt($ch, CURLOPT_URL,$url);
 
		// howmany parameter to post
		curl_setopt($ch, CURLOPT_POST, 1);
 
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
		// the parameter 'username' with its value 'johndoe'
		curl_setopt($ch, 
			CURLOPT_POSTFIELDS,
			array(	'user'			=>	$username,
					'pass'			=>	$password,
					'logintype'		=>	'login',
					'serviceUser'	=>	$GLOBALS['wgTypo3AuthServiceUser'],
					'servicePass'	=>	$GLOBALS['wgTypo3AuthServicePassword'],
					'version'		=>	$GLOBALS['version'])
		);
 
		$result = curl_exec($ch);
		curl_close ($ch);
 
		try {
			$xml = new SimpleXMLElement($result);
		} catch (Exception $e) {
			return false;
		}
 
		if (trim($xml->fe_user->username) != '' 
			&& strtolower($xml->fe_user->username) == strtolower($username)) {
			$this->UserRealName = $xml->fe_user->name;
			$this->UserEmail = $xml->fe_user->email;
			$this->Password = $password;
			return true;
		}
 
        return false;
    }
 
    /**
     * When a user logs in, optionally fill in preferences and such.
     * For instance, you might pull the email address or real name from the
     * external user database.
     *
     * The User object is passed by reference so it can be modified; don't
     * forget the & on your function declaration.
     *
     * @param User $user
     * @public
     */
    function updateUser( &$user )
    {
        $user->setRealName($this->UserRealName);
        $user->setEmail($this->UserEmail);
        $user->mEmailAuthenticated = wfTimestampNow();
        $user->saveSettings();
        //exit;
        # Override this and do something
        return true;
    }
 
    /**
     * Return true if the wiki should create a new local account automatically
     * when asked to login a user who doesn't exist locally but does in the
     * external auth system.
     *
     * If you don't automatically create accounts, you must still create
     * accounts in some way. It's not possible to authenticate without
     * a local account.
     *
     * This is just a question, and shouldn't perform any actions.
     *
     * @return bool
     * @public
     */
    function autoCreate() {
        return true;
    }
 
    function updateExternalDB () {
    	return true;
    }
 
    /**
     * Can users change their passwords?
     *
     * @return bool
     */
    function allowPasswordChange() {
        return false;
    }
 
    /**
     * Set the given password in the authentication service.
     * As a special case, the password may be set to null to request
     * locking the password to an unusable value, with the expectation
     * that it will be set later through a mail reset or other method.
     *
     * Return true if successful.
     *
     * @param $user User object.
     * @param $password String: password.
     * @return bool
     * @public
     */
    function setPassword( $user, $password ) {
        return true;
    }
 
    /**
     * Check to see if external accounts can be created.
     * Return true if external accounts can be created.
     * @return bool
     * @public
     */
    function canCreateAccounts() {
        return false;
    }
 
    /**
     * Add a user to the external authentication service.
     * Return true if successful.
     *
     * @param User $user - only the name should be assumed valid at this point
     * @param string $password
     * @param string $email
     * @param string $realname
     * @return bool
     * @public
     */
    function addUser( $user, $password, $email='', $realname='' ) {
        return false;
    }
 
    /**
     * Return true to prevent logins that don't authenticate here from being
     * checked against the local database's password fields.
     *
     * This is just a question, and shouldn't perform any actions.
     *
     * @return bool
     * @public
     */
    function strict() {
        return true;
    }
 
    /**
     * When creating a user account, optionally fill in preferences and such.
     * For instance, you might pull the email address or real name from the
     * external service.
     *
     * The User object is passed by reference so it can be modified; don't
     * forget the & on your function declaration.
     *
     * @param $user User object.
     * @param $autocreate bool True if user is being autocreated on login
     * @public
     */
    function initUser( &$user, $autocreate=false ) {
        $user->setRealName($this->UserRealName);
        $user->setEmail($this->UserEmail);
        $user->mEmailAuthenticated = wfTimestampNow();
    }
 
    /**
     * If you want to munge the case of an account name before the final
     * check, now is your chance.
     */
    function getCanonicalName( $username ) {
        return $username;
    }
}