Skip to content

Commit

Permalink
OpenId Provider: save trust/deny forever data
Browse files Browse the repository at this point in the history
  • Loading branch information
bartv2 committed Oct 4, 2012
1 parent ec7c6b4 commit efd82f7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 41 deletions.
72 changes: 31 additions & 41 deletions user_openid_provider/lib/OpenIdProviderStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ public function addUser($id, $password)
throw new ErrorException('Not implemented.');
}

/**
* Returns the username from given $id
*
* @param string $id user identity URL
* @return string
*/
protected function getUsernameFromId($id)
{
return substr($id, strrpos($id, '/')+2);
}

/**
* Returns true if user with given $id exists and false otherwise
*
Expand All @@ -87,7 +98,7 @@ public function addUser($id, $password)
*/
public function hasUser($id)
{
$userName=substr($id, strrpos($id, '/')+2);
$userName=$this->getUsernameFromId($id);
return OCP\User::userExists($userName);
}

Expand All @@ -104,7 +115,7 @@ public function checkUser($id, $password)
}

/**
* Removes information abou specified user
* Removes information about specified user
*
* @param string $id user identity URL
* @return bool
Expand All @@ -123,7 +134,13 @@ public function delUser($id)
*/
public function getTrustedSites($id)
{
return array();
$username = $this->getUsernameFromId($id);
$data = OCP\Config::getUserValue($username, 'user_openid_provider', 'trusted_sites');
$sites = array();
if (!empty($data)) {
$sites = unserialize($data);
}
return $sites;
}

/**
Expand All @@ -132,48 +149,21 @@ public function getTrustedSites($id)
* @param string $id user identity URL
* @param string $site site URL
* @param mixed $trusted trust data from extension or just a boolean value
* @return bool
*/
public function addSite($id, $site, $trusted)
{
$name = $this->_dir . '/user_' . md5($id);
$lock = @fopen($this->_dir . '/user.lock', 'w+');
if ($lock === false) {
return false;
}
if (!flock($lock, LOCK_EX)) {
fclose($lock);
return false;
$username = $this->getUsernameFromId($id);
$data = OCP\Config::getUserValue($username, 'user_openid_provider', 'trusted_sites');
$sites = array();
if (!empty($data)) {
$sites = unserialize($data);
}
try {
$f = @fopen($name, 'r+');
if ($f === false) {
fclose($lock);
return false;
}
$ret = false;
$data = stream_get_contents($f);
if (!empty($data)) {
list($storedId, $storedPassword, $sites) = unserialize($data);
if ($id === $storedId) {
if ($trusted === null) {
unset($sites[$site]);
} else {
$sites[$site] = $trusted;
}
rewind($f);
ftruncate($f, 0);
$data = serialize(array($id, $storedPassword, $sites));
fwrite($f, $data);
$ret = true;
}
}
fclose($f);
fclose($lock);
return $ret;
} catch (Exception $e) {
fclose($lock);
throw $e;
if ($trusted === null) {
unset($sites[$site]);
} else {
$sites[$site] = $trusted;
}
$data = serialize($sites);
OCP\Config::setUserValue($username, 'user_openid_provider', 'trusted_sites', $data);
}
}
6 changes: 6 additions & 0 deletions user_openid_provider/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@
} else if (isset($_GET['openid_action']) and $_GET['openid_action'] == 'trust') {
OCP\User::checkLoggedIn();
if (isset($_POST['allow'])) {
if (isset($_POST['forever'])) {
$server->allowSite($server->getSiteRoot($_GET));
}
$server->respondToConsumer($_GET);
} else if (isset($_POST['deny'])) {
if (isset($_POST['forever'])) {
$server->denySite($server->getSiteRoot($_GET));
}
Zend_OpenId::redirect($_GET['openid_return_to'],
array('openid.mode'=>'cancel'));
} else {
Expand Down

0 comments on commit efd82f7

Please sign in to comment.