From e1a2a8a876d447f1394eae23c0b241aa82eb335a Mon Sep 17 00:00:00 2001 From: tcubansk Date: Wed, 9 May 2012 08:51:52 -0700 Subject: [PATCH 2/3] Update googledocs repository use secure authsub tokens --- .../googledocs/lang/en/repository_googledocs.php | 2 + repository/googledocs/lib.php | 51 +++++++++++++++++--- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/repository/googledocs/lang/en/repository_googledocs.php b/repository/googledocs/lang/en/repository_googledocs.php index 38f1436..71b8712 100644 --- a/repository/googledocs/lang/en/repository_googledocs.php +++ b/repository/googledocs/lang/en/repository_googledocs.php @@ -26,3 +26,5 @@ $string['googledocs:view'] = 'View google docs repository'; $string['pluginname'] = 'Google Docs'; $string['configplugin'] = 'Configurate Google Docs plugin'; +$string['usesecure'] = 'Use Secure Tokens'; +$string['googleapikey'] = 'Google API private key'; diff --git a/repository/googledocs/lib.php b/repository/googledocs/lib.php index acb991d..c56ae3d 100644 --- a/repository/googledocs/lib.php +++ b/repository/googledocs/lib.php @@ -29,20 +29,33 @@ require_once($CFG->libdir.'/googleapi.php'); class repository_googledocs extends repository { private $subauthtoken = ''; + private $usesecuretoken = false; + private $googleapikey = ''; public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) { global $USER; parent::__construct($repositoryid, $context, $options); + $this->googleapikey = get_config('googledocs', 'googleapikey'); + $this->usesecuretoken = get_config('googledocs', 'usesecure'); + // TODO: I wish there was somewhere we could explicitly put this outside of constructor.. $googletoken = optional_param('token', false, PARAM_RAW); if($googletoken){ - $gauth = new google_authsub(false, $googletoken); // will throw exception if fails + $gauth = $this->get_google_authsub(false, $googletoken); // will throw exception if fails google_docs::set_sesskey($gauth->get_sessiontoken(), $USER->id); } $this->check_login(); } + private function get_google_authsub($sessiontoken = '', $authtoken = '', $options = array()) { + if ($this->usesecuretoken) { + return new google_authsub($sessiontoken, $authtoken, $options, $this->googleapikey); + } else { + return new google_authsub($sessiontoken, $authtoken, $options, ''); + } + } + public function check_login() { global $USER; @@ -50,7 +63,7 @@ class repository_googledocs extends repository { if($sesskey){ try{ - $gauth = new google_authsub($sesskey); + $gauth = $this->get_google_authsub($sesskey); $this->subauthtoken = $sesskey; return true; }catch(Exception $e){ @@ -69,14 +82,14 @@ class repository_googledocs extends repository { $popup_btn = new stdClass(); $popup_btn->type = 'popup'; $returnurl = $CFG->wwwroot.'/repository/repository_callback.php?callback=yes&repo_id='.$this->id; - $popup_btn->url = google_authsub::login_url($returnurl, google_docs::REALM); + $popup_btn->url = google_authsub::login_url($returnurl, google_docs::REALM, get_config('googledocs', 'usesecure')); $ret['login'] = array($popup_btn); return $ret; } } public function get_listing($path='', $page = '') { - $gdocs = new google_docs(new google_authsub($this->subauthtoken)); + $gdocs = new google_docs($this->get_google_authsub($this->subauthtoken)); $ret = array(); $ret['dynload'] = true; @@ -85,7 +98,7 @@ class repository_googledocs extends repository { } public function search($search_text, $page = 0) { - $gdocs = new google_docs(new google_authsub($this->subauthtoken)); + $gdocs = new google_docs($this->get_google_authsub($this->subauthtoken)); $ret = array(); $ret['dynload'] = true; @@ -98,7 +111,7 @@ class repository_googledocs extends repository { $token = google_docs::get_sesskey($USER->id); - $gauth = new google_authsub($token); + $gauth = $this->get_google_authsub($this->subauthtoken); // revoke token from google $gauth->revoke_session_token(); @@ -113,7 +126,7 @@ class repository_googledocs extends repository { $path = $this->prepare_file($file); $fp = fopen($path, 'w'); - $gdocs = new google_docs(new google_authsub($this->subauthtoken)); + $gdocs = new google_docs($this->get_google_authsub($this->subauthtoken)); $gdocs->download_file($url, $fp); return array('path'=>$path, 'url'=>$url); @@ -125,5 +138,29 @@ class repository_googledocs extends repository { public function supported_returntypes() { return FILE_INTERNAL; } + + public static function type_config_form($mform, $classname='repository_googledocs') { + global $CFG; + parent::type_config_form($mform); + $use_secure_text = get_string('usesecure', 'repository_googledocs'); + $googleapikey_text = get_string('googleapikey', 'repository_googledocs'); + + $use_secure = get_config('googledocs', 'usesecure'); + $googleapikey = get_config('googledocs', 'googleapikey'); + + if (empty($use_secure)) { + $use_secure = ''; + } + if (empty($googleapikey)) { + $googleapikey = ''; + } + + $mform->addElement('checkbox', 'usesecure', $use_secure_text, '', array('value' => $use_secure)); + $mform->addElement('textarea', 'googleapikey', $googleapikey_text, '', array('value' => $googleapikey)); + } + + public static function get_type_option_names() { + return array('usesecure', 'googleapikey', 'pluginname'); + } } //Icon from: http://www.iconspedia.com/icon/google-2706.html -- 1.7.7.3