Commit ac78d056 authored by Robert Lyon's avatar Robert Lyon Committed by Gerrit Code Review

Adjusting webservices to use ctime/mtime syntax (Bug #1394359)

To be more in keeping with existing codebase.

Change-Id: Iaa850c7cfc609afee0dd014bef6d2a2673c7a25f
Signed-off-by: Robert Lyon's avatarRobert Lyon <robertl@catalyst.net.nz>
parent 669645bc
This diff is collapsed.
......@@ -37,8 +37,8 @@ function xmldb_auth_webservice_upgrade($oldversion=0) {
$table->addFieldInfo('restrictedusers', XMLDB_TYPE_INTEGER, 1, null, XMLDB_NOTNULL, null, null, null, 0);
$table->addFieldInfo('tokenusers', XMLDB_TYPE_INTEGER, 1, null, XMLDB_NOTNULL, null, null, null, 0);
$table->addFieldInfo('component', XMLDB_TYPE_CHAR, 100, null, null);
$table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, null, null, null);
$table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, null, null, null);
$table->addFieldInfo('ctime', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL);
$table->addFieldInfo('mtime', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL);
$table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->addIndexInfo('nameuk', XMLDB_INDEX_UNIQUE, array('name'));
create_table($table);
......@@ -73,8 +73,8 @@ function xmldb_auth_webservice_upgrade($oldversion=0) {
$table->addFieldInfo('creatorid', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, null, null, 1);
$table->addFieldInfo('iprestriction', XMLDB_TYPE_CHAR, 255, null, null);
$table->addFieldInfo('validuntil', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, null, null, null);
$table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, null, null, null);
$table->addFieldInfo('lastaccess', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, null, null, null);
$table->addFieldInfo('ctime', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL);
$table->addFieldInfo('mtime', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL);
$table->addFieldInfo('publickey', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL);
$table->addFieldInfo('publickeyexpires', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, null, null, null);
$table->addFieldInfo('wssigenc', XMLDB_TYPE_INTEGER, 1, null, XMLDB_NOTNULL, null, null, null, 0);
......@@ -93,8 +93,8 @@ function xmldb_auth_webservice_upgrade($oldversion=0) {
$table->addFieldInfo('institution', XMLDB_TYPE_CHAR, 255, null, null);
$table->addFieldInfo('iprestriction', XMLDB_TYPE_CHAR, 255, null, null);
$table->addFieldInfo('validuntil', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, null, null, null);
$table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, null, null, null);
$table->addFieldInfo('lastaccess', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, null, null, null);
$table->addFieldInfo('ctime', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL);
$table->addFieldInfo('mtime', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL);
$table->addFieldInfo('publickey', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL);
$table->addFieldInfo('publickeyexpires', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, null, null, null);
$table->addFieldInfo('wssigenc', XMLDB_TYPE_INTEGER, 1, null, XMLDB_NOTNULL, null, null, null, 0);
......@@ -143,8 +143,8 @@ function xmldb_auth_webservice_upgrade($oldversion=0) {
$table->addFieldInfo('application_descr', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL);
$table->addFieldInfo('application_notes', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL);
$table->addFieldInfo('application_type', XMLDB_TYPE_CHAR, 255, null, XMLDB_NOTNULL);
$table->addFieldInfo('issue_date', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL);
$table->addFieldInfo('timestamp', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL);
$table->addFieldInfo('ctime', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL);
$table->addFieldInfo('mtime', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL);
$table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->addKeyInfo('useridfk', XMLDB_KEY_FOREIGN, array('userid'), 'usr', array('id'));
$table->addKeyInfo('externalserviceidfk', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
......@@ -157,9 +157,9 @@ function xmldb_auth_webservice_upgrade($oldversion=0) {
$table->addFieldInfo('consumer_key', XMLDB_TYPE_CHAR, 128, null, XMLDB_NOTNULL);
$table->addFieldInfo('token', XMLDB_TYPE_CHAR, 64, null, XMLDB_NOTNULL);
$table->addFieldInfo('nonce', XMLDB_TYPE_CHAR, 80, null, XMLDB_NOTNULL);
$table->addFieldInfo('timestamp', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL);
$table->addFieldInfo('ctime', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL);
$table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->addIndexInfo('keysuk', XMLDB_INDEX_UNIQUE, array('consumer_key', 'token', 'timestamp', 'nonce'));
$table->addIndexInfo('keysuk', XMLDB_INDEX_UNIQUE, array('consumer_key', 'token', 'ctime', 'nonce'));
create_table($table);
$table = new XMLDBTable('oauth_server_token');
......@@ -174,7 +174,7 @@ function xmldb_auth_webservice_upgrade($oldversion=0) {
$table->addFieldInfo('callback_uri', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL);
$table->addFieldInfo('verifier', XMLDB_TYPE_CHAR, 10, null, XMLDB_NOTNULL);
$table->addFieldInfo('token_ttl', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL, null, null, null, "'9999-12-31'");
$table->addFieldInfo('timestamp', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL);
$table->addFieldInfo('ctime', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL);
$table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->addKeyInfo('osrrefidfk', XMLDB_KEY_FOREIGN, array('osr_id_ref'), 'oauth_server_registry', array('id'));
$table->addKeyInfo('useridfk', XMLDB_KEY_FOREIGN, array('userid'), 'usr', array('id'));
......
......@@ -77,7 +77,7 @@ function webservices_function_groups_submit(Pieform $form, $values) {
$SESSION->add_error_msg(get_string('invalidinput', 'auth.webservice'));
}
else {
$service = array('name' => $service, 'restrictedusers' => 0, 'enabled' => 0, 'tokenusers' => 0, 'component' => 'webservice', 'timecreated' => time());
$service = array('name' => $service, 'restrictedusers' => 0, 'enabled' => 0, 'tokenusers' => 0, 'component' => 'webservice', 'ctime' => db_format_timestamp(time()));
insert_record('external_services', $service);
$SESSION->add_ok_msg(get_string('configsaved', 'auth.webservice'));
}
......@@ -171,7 +171,7 @@ function webservices_user_submit(Pieform $form, $values) {
$dbserviceuser = (object) array('externalserviceid' => $service->id,
'userid' => $dbuser->id,
'institution' => $auth_instance->institution,
'timecreated' => time(),
'ctime' => db_format_timestamp(time()),
'publickeyexpires' => time(),
'wssigenc' => 0,
'publickey' => '');
......
......@@ -284,7 +284,7 @@ function webservice_server_list_form($sopts, $iopts) {
consumer_secret as consumer_secret,
enabled as enabled,
status as status,
issue_date as issue_date,
osr.ctime as issue_date,
application_uri as application_uri,
application_title as application_title,
application_descr as application_descr,
......
......@@ -228,7 +228,7 @@ function allocate_webservice_functions_submit(Pieform $form, $values) {
if (!$service_function) {
$service_function = array('externalserviceid' => $service, 'functionname' => $dbfunction->name);
insert_record('external_services_functions', $service_function);
$dbservice->timemodified = time();
$dbservice->mtime = db_format_timestamp(time());
update_record('external_services', $dbservice);
}
}
......@@ -236,7 +236,7 @@ function allocate_webservice_functions_submit(Pieform $form, $values) {
// disabled - record should not exist
if ($service_function) {
delete_records('external_services_functions', 'externalserviceid', $service, 'functionname',$dbfunction->name);
$dbservice->timemodified = time();
$dbservice->mtime = db_format_timestamp(time());
update_record('external_services', $dbservice);
}
}
......
......@@ -238,6 +238,7 @@ function allocate_webservice_tokens_submit(Pieform $form, $values) {
if ($dbtoken->institution != $values['institution']) {
$dbtoken->institution = trim($values['institution']);
}
$dbtoken->mtime = db_format_timestamp(time());
update_record('external_tokens', $dbtoken);
$SESSION->add_ok_msg(get_string('configsaved', 'auth.webservice'));
......
......@@ -235,6 +235,7 @@ function allocate_webservice_users_submit(Pieform $form, $values) {
$dbserviceuser->externalserviceid = $values['service'];
}
}
$dbserviceuser->mtime = db_format_timestamp(time());
update_record('external_services_users', $dbserviceuser);
$SESSION->add_ok_msg(get_string('configsaved', 'auth.webservice'));
......
......@@ -38,8 +38,8 @@ foreach ($dbservices as $dbservice) {
$dbtoken = get_record('external_tokens', 'externalserviceid', $dbservice->id, 'userid', $USER->get('id'), 'tokentype', EXTERNAL_TOKEN_USER);
if ($dbtoken) {
$dbservice->token = $dbtoken->token;
$dbservice->timecreated = $dbtoken->timecreated;
$dbservice->lastaccess = $dbtoken->lastaccess;
$dbservice->ctime = $dbtoken->ctime;
$dbservice->mtime = $dbtoken->mtime;
$dbservice->institution = $dbtoken->institution;
$dbservice->validuntil = $dbtoken->validuntil;
}
......@@ -133,13 +133,13 @@ if (!empty($dbservices)) {
);
// last time the token was accessed if there is a token
$userform['elements']['id'. $service->id . '_last_access'] = array(
'value' => (empty($service->lastaccess) ? ' ' : date("F j, Y H:i", $service->lastaccess)),
'value' => (empty($service->mtime) ? ' ' : format_date(strtotime($service->mtime))),
'type' => 'html',
'key' => $service->id,
);
// expiry date for the token if it exists
$userform['elements']['id' . $service->id . '_expires'] = array(
'value' => (empty($service->validuntil) && empty($service->lastaccess) ? '' : date("F j, Y H:i", (empty($service->validuntil) ? $service->lastaccess + EXTERNAL_TOKEN_USER_EXPIRES : $service->validuntil))),
'value' => (empty($service->validuntil) && empty($service->mtime) ? '' : format_date((empty($service->validuntil) ? strtotime($service->mtime) + EXTERNAL_TOKEN_USER_EXPIRES : $service->validuntil))),
'type' => 'html',
'key' => $service->id,
);
......@@ -199,7 +199,7 @@ if (!empty($dbservices)) {
$dbtokens = get_records_sql_assoc('
SELECT ost.id as id,
ost.token as token,
ost.timestamp as timestamp,
ost.ctime as ctime,
osr.institution as institution,
osr.externalserviceid as externalserviceid,
es.name as service_name,
......@@ -207,7 +207,7 @@ $dbtokens = get_records_sql_assoc('
osr.consumer_secret as consumer_secret,
osr.enabled as enabled,
osr.status as status,
osr.issue_date as issue_date,
osr.ctime as issue_date,
osr.application_uri as application_uri,
osr.application_title as application_title,
osr.application_descr as application_descr,
......@@ -221,7 +221,7 @@ $dbtokens = get_records_sql_assoc('
ON es.id = osr.externalserviceid
WHERE ost.userid = ? AND
ost.token_type = ?
ORDER BY application_title, timestamp desc
ORDER BY application_title, ctime desc
', array($USER->get('id'), 'access'));
$oauthform = get_string('notokens', 'auth.webservice');
......@@ -299,7 +299,7 @@ if (!empty($dbtokens)) {
);
// token last access time
$oauthform['elements']['id' . $token->id . '_last_access'] = array(
'value' => date("F j, Y H:i", strtotime($token->timestamp)),
'value' => format_date(strtotime($token->ctime)),
'type' => 'html',
'key' => $token->id,
);
......
......@@ -232,7 +232,7 @@ function webservice_generate_token($tokentype, $serviceorid, $userid, $instituti
$newtoken->institution = $institution;
$newtoken->creatorid = $USER->get('id');
$newtoken->timecreated = time();
$newtoken->ctime = db_format_timestamp(time());
$newtoken->publickeyexpires = time();
$newtoken->wssigenc = 0;
$newtoken->publickey = '';
......@@ -829,7 +829,7 @@ abstract class webservice_server implements webservice_server_interface {
if ($tokentype == EXTERNAL_TOKEN_PERMANENT || $tokentype == EXTERNAL_TOKEN_USER) {
$token = get_record('external_tokens', 'token', $this->token);
// trap personal tokens with no valid until time set
if ($token && $token->tokentype == EXTERNAL_TOKEN_USER && $token->validuntil == 0 && (($token->timecreated - time()) > EXTERNAL_TOKEN_USER_EXPIRES)) {
if ($token && $token->tokentype == EXTERNAL_TOKEN_USER && $token->validuntil == 0 && ((strtotime($token->ctime) - time()) > EXTERNAL_TOKEN_USER_EXPIRES)) {
delete_records('external_tokens', 'token', $this->token);
throw new WebserviceAccessException(get_string('invalidtimedtoken', 'auth.webservice'));
}
......@@ -872,7 +872,7 @@ abstract class webservice_server implements webservice_server_interface {
$user = get_record('usr', 'id', $token->userid, 'deleted', 0);
// log token access
set_field('external_tokens', 'lastaccess', time(), 'id', $token->id);
set_field('external_tokens', 'mtime', db_format_timestamp(time()), 'id', $token->id);
// set the global for the web service users defined institution
$WEBSERVICE_INSTITUTION = $token->institution;
......@@ -1937,7 +1937,7 @@ function external_reload_component($component, $dir=true) {
$dbservice->restrictedusers = ((isset($service['restrictedusers']) && $service['restrictedusers'] == 1) ? 1 : 0);
$dbservice->tokenusers = ((isset($service['tokenusers']) && $service['tokenusers'] == 1) ? 1 : 0);
$dbservice->component = $component;
$dbservice->timecreated = time();
$dbservice->ctime = db_format_timestamp(time());
$dbservice->id = insert_record('external_services', $dbservice, 'id', true);
foreach ($service['functions'] as $fname) {
$newf = new stdClass();
......
......@@ -212,14 +212,14 @@ class OAuthStoreMahara extends OAuthStoreAbstract {
if (is_null($consumer['userid'])) {
execute_sql('
UPDATE {oauth_server_registry}
SET userid = NULL
SET userid = NULL, mtime = NOW(),
WHERE id = ?
', array($consumer['id']));
}
else {
execute_sql('
UPDATE {oauth_server_registry}
SET userid = ?
SET userid = ?, mtime = NOW(),
WHERE id = ?
', array($consumer['userid'], $consumer['id']));
}
......@@ -236,7 +236,7 @@ class OAuthStoreMahara extends OAuthStoreAbstract {
application_descr = ?,
application_notes = ?,
application_type = ?,
timestamp = NOW(),
mtime = NOW(),
institution = ?,
externalserviceid = ?
WHERE id = ?
......@@ -295,8 +295,8 @@ class OAuthStoreMahara extends OAuthStoreAbstract {
application_descr,
application_notes,
application_type,
timestamp,
issue_date)
mtime,
ctime)
VALUES(?,
?,
?,
......@@ -410,8 +410,8 @@ class OAuthStoreMahara extends OAuthStoreAbstract {
}
}
$ttl = date("Y-m-d H:i:s", (time() + $ttl));
$ts = date("Y-m-d H:i:s", time());
$ttl = db_format_timestamp(time() + $ttl);
$ts = db_format_timestamp(time());
execute_sql('
INSERT INTO {oauth_server_token}
(osr_id_ref,
......@@ -420,7 +420,7 @@ class OAuthStoreMahara extends OAuthStoreAbstract {
token_secret,
token_type,
token_ttl,
timestamp,
ctime,
referrer_host,
verifier,
callback_uri)
......@@ -498,7 +498,7 @@ class OAuthStoreMahara extends OAuthStoreAbstract {
UPDATE {oauth_server_token}
SET authorized = ?,
userid = ?,
timestamp = NOW(),
ctime = NOW(),
referrer_host = ?,
verifier = ?
WHERE token = ?
......@@ -521,7 +521,7 @@ class OAuthStoreMahara extends OAuthStoreAbstract {
// Maximum time to live for this token
if (isset($options['token_ttl']) && is_numeric($options['token_ttl'])) {
$ttl_sql = date("Y-m-d H:i:s", (time() + intval($options['token_ttl'])));
$ttl_sql = db_format_timestamp(time() + intval($options['token_ttl']));
}
else {
$ttl_sql = '9999-12-31';
......@@ -555,7 +555,7 @@ class OAuthStoreMahara extends OAuthStoreAbstract {
$db_token->token_secret = $new_secret;
$db_token->token_type = 'access';
$db_token->token_ttl = $ttl_sql;
$db_token->timestamp = date("Y-m-d H:i:s", time());
$db_token->ctime = db_format_timestamp(time());
$result = update_record('oauth_server_token', $db_token);
if (!$result) {
......@@ -618,7 +618,7 @@ class OAuthStoreMahara extends OAuthStoreAbstract {
consumer_secret as consumer_secret,
enabled as enabled,
status as status,
issue_date as issue_date,
osr.ctime as issue_date,
application_uri as application_uri,
application_title as application_title,
application_descr as application_descr,
......@@ -645,9 +645,9 @@ class OAuthStoreMahara extends OAuthStoreAbstract {
* @exception OAuthException2 thrown when the timestamp is not in sequence or nonce is not unique
*/
public function checkServerNonce($consumer_key, $token, $timestamp, $nonce) {
$high_water = date("Y-m-d H:i:s", ($timestamp + $this->max_timestamp_skew));
$high_water = db_format_timestamp($timestamp + $this->max_timestamp_skew);
$r = get_records_sql_assoc('
SELECT MAX(timestamp) AS max_stamp, MAX(timestamp) > ? AS max_highwater
SELECT MAX(ctime) AS max_stamp, MAX(ctime) > ? AS max_highwater
FROM {oauth_server_nonce}
WHERE consumer_key = ?
AND token = ?
......@@ -659,12 +659,12 @@ class OAuthStoreMahara extends OAuthStoreAbstract {
}
// Insert the new combination
$timestamp_fmt = date("Y-m-d H:i:s", $timestamp);
$timestamp_fmt = db_format_timestamp($timestamp);
$result = execute_sql('
INSERT INTO {oauth_server_nonce}
( consumer_key,
token,
timestamp,
ctime,
nonce )
VALUES (?, ?, ?, ?)
', array($consumer_key, $token, $timestamp_fmt, $nonce));
......@@ -674,12 +674,12 @@ class OAuthStoreMahara extends OAuthStoreAbstract {
}
// Clean up all timestamps older than the one we just received
$low_water = date("Y-m-d H:i:s", ($timestamp - $this->max_timestamp_skew));
$low_water = db_format_timestamp($timestamp - $this->max_timestamp_skew);
delete_records_sql('
DELETE FROM {oauth_server_nonce}
WHERE consumer_key = ?
AND token = ?
AND timestamp < ?
AND ctime < ?
', array($consumer_key, $token, $low_water));
}
}
......@@ -134,7 +134,7 @@ class WebServiceTestBase extends MaharaUnitTest {
// construct a test service from all available functions
$dbservice = get_record('external_services', 'name', $this->servicename);
if (empty($dbservice)) {
$service = array('name' => $this->servicename, 'tokenusers' => 0, 'restrictedusers' => 0, 'enabled' => 1, 'component' => 'webservice', 'timecreated' => time());
$service = array('name' => $this->servicename, 'tokenusers' => 0, 'restrictedusers' => 0, 'enabled' => 1, 'component' => 'webservice', 'ctime' => db_format_timestamp(time()));
insert_record('external_services', $service);
$dbservice = get_record('external_services', 'name', $this->servicename);
}
......@@ -144,7 +144,7 @@ class WebServiceTestBase extends MaharaUnitTest {
if (!$sfexists) {
$service_function = array('externalserviceid' => $dbservice->id, 'functionname' => $function->name);
insert_record('external_services_functions', $service_function);
$dbservice->timemodified = time();
$dbservice->mtime = db_format_timestamp(time());
update_record('external_services', $dbservice);
}
}
......@@ -186,7 +186,7 @@ class WebServiceTestBase extends MaharaUnitTest {
$dbserviceuser = (object) array('externalserviceid' => $dbservice->id,
'userid' => $dbuser->id,
'institution' => 'mahara',
'timecreated' => time(),
'ctime' => db_format_timestamp(time()),
'wssigenc' => 0,
'publickeyexpires' => 0);
$dbserviceuser->id = insert_record('external_services_users', $dbserviceuser, 'id', true);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment