diff --git a/auth.php b/auth.php index 06f53ec..8157c6c 100644 --- a/auth.php +++ b/auth.php @@ -13,6 +13,8 @@ class auth_plugin_ouopenid extends auth_plugin_base { + const OPENID_URL_REGEX = '@^http:\/\/openid\.open\.ac\.uk\/oucu\/(?P\w+)$@'; + /** * Class constructor * @@ -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))); diff --git a/event.php b/event.php index 60d117d..785e676 100644 --- a/event.php +++ b/event.php @@ -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\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. diff --git a/index.php b/index.php index 1228dd7..78a558b 100644 --- a/index.php +++ b/index.php @@ -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'; +} + ?> @@ -30,7 +38,7 @@
+