Skip to content

Commit

Permalink
Bug #1, Re-factored auth, event and index PHP [iet:8269283]
Browse files Browse the repository at this point in the history
  • Loading branch information
nfreear committed Mar 10, 2017
1 parent ed1c69e commit b0b44ac
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
23 changes: 23 additions & 0 deletions auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

class auth_plugin_ouopenid extends auth_plugin_base {

const OPENID_URL_REGEX = '@^http:\/\/openid\.open\.ac\.uk\/oucu\/(?P<oucu>\w+)$@';

/**
* Class constructor
*
Expand All @@ -26,6 +28,27 @@ public function __construct() {
$this->config = get_config($this->pluginconfig);
}

public static function set_user(&$resp, &$user) {
$oucu = null;
$identity_url = $resp->identity_url;
if ($identity_url && preg_match(self::OPENID_URL_REGEX, $identity_url, $matches)) {
$oucu = $matches[ 'oucu' ];
}

if ($oucu && $user->auth == 'openid' && ( ! $user->firstname || $user->firstname === 'test' )) {
$user->firstname = $oucu;
}

if ($oucu && $user->auth == 'openid' && ! $user->email) {
$user->email = $oucu . '@openmail.open.ac.uk';
}

$user->profile[ 'ouopenid_custom' ] = [ 'a' => 1 ];

self::debug([
__FUNCTION__, $identity_url, $oucu, $user->email, $user->username, 'userid=', $user->id ]);
}

public static function debug($obj) {
static $count = 0;
header(sprintf('X-auth-ou-openid-%02d: %s', $count, json_encode($obj)));
Expand Down
22 changes: 4 additions & 18 deletions event.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,13 @@
function on_openid_login(&$resp, &$user, $mainid = true) {
auth_plugin_ouopenid::debug([ __FUNCTION__, $resp->identity_url, $resp->message->args->values, $user ]);

$oucu = null;
$identity_url = $resp->identity_url;
if ($identity_url &&
preg_match('@^http:\/\/openid\.open\.ac\.uk\/oucu\/(?P<oucu>\w+)$@', $identity_url, $matches)) {
$oucu = $matches[ 'oucu' ];
}

if ($oucu && $user->auth == 'openid' && ( ! $user->firstname || $user->firstname === 'test' )) {
$user->firstname = $oucu;
}

if ($oucu && $user->auth == 'openid' && ! $user->email) {
$user->email = $oucu . '@openmail.open.ac.uk';
}

auth_plugin_ouopenid::debug([
__FUNCTION__, $identity_url, $oucu, $user->email, $user->username, $user->auth, 'userid=', $user->id ]);
auth_plugin_ouopenid::set_user($resp, $user);
}

function on_openid_create_account(&$resp, &$user) {
auth_plugin_ouopenid::debug([ __FUNCTION__, $resp, $user ]);
auth_plugin_ouopenid::debug([ __FUNCTION__, $resp->identity_url, $resp->message->args->values, $user ]);

auth_plugin_ouopenid::set_user($resp, $user);
}

//End.
14 changes: 11 additions & 3 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@

// TODO: check if plugin is enabled or not !!

class Ou_Open_Id_Form {

const ACTION = '/login/index.php';
const OUCU_REGEX = '[a-z]\w{2,7}';
const OPEN_ID_URL = 'http://openid.open.ac.uk/oucu/';
const JQUERY_URL = 'https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js';
}


?>
<!doctype html><html lang="en"><meta charset="utf-8" />
Expand All @@ -30,7 +38,7 @@


<form
action="http://moodle.ouuk.tesla-project.eu/login/index.php"
action="<?php echo Ou_Open_Id_Form::ACTION ?>"
method="post"
id="openidlogin"
name="openidlogin"
Expand Down Expand Up @@ -59,7 +67,7 @@



<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="<?php echo Ou_Open_Id_Form::JQUERY_URL ?>"></script>
<script>
window.jQuery(function ($) {

Expand All @@ -68,7 +76,7 @@

window.console.debug('Submit, OUCU: ', oucu);

$('#openid_url').val('http://openid.open.ac.uk/oucu/' + oucu);
$('#openid_url').val('<?php echo Ou_Open_Id_Form::OPEN_ID_URL ?>' + oucu);
});

});
Expand Down

0 comments on commit b0b44ac

Please sign in to comment.