Change method to generate tokens and allow people to generate new tokens

This commit is contained in:
Frédéric Guillot 2013-08-14 21:08:01 -04:00
parent a79228195f
commit 68a8e8f954
4 changed files with 30 additions and 5 deletions

View File

@ -492,6 +492,14 @@ Router\post_action('add', function() {
}); });
// Re-generate tokens
Router\get_action('generate-tokens', function() {
Model\new_tokens();
Response\redirect('?action=config#api');
});
// Optimize the database manually // Optimize the database manually
Router\get_action('optimize-db', function() { Router\get_action('optimize-db', function() {

View File

@ -102,9 +102,25 @@ function write_debug()
} }
function generate_api_token() function generate_token()
{ {
return substr(base64_encode(file_get_contents('/dev/urandom', false, null, 0, 20)), 0, 15); if (ini_get('open_basedir') === '') {
return substr(base64_encode(file_get_contents('/dev/urandom', false, null, 0, 20)), 0, 15);
}
else {
return substr(base64_encode(uniqid(mt_rand(), true)), 0, 20);
}
}
function new_tokens()
{
$values = array(
'api_token' => generate_token(),
'feed_token' => generate_token(),
);
return \PicoTools\singleton('db')->table('config')->update($values);
} }

View File

@ -5,7 +5,7 @@ namespace Schema;
function version_14($pdo) function version_14($pdo)
{ {
$pdo->exec('ALTER TABLE config ADD COLUMN feed_token TEXT DEFAULT "'.\Model\generate_api_token().'"'); $pdo->exec('ALTER TABLE config ADD COLUMN feed_token TEXT DEFAULT "'.\Model\generate_token().'"');
} }
@ -17,7 +17,7 @@ function version_13($pdo)
function version_12($pdo) function version_12($pdo)
{ {
$pdo->exec('ALTER TABLE config ADD COLUMN api_token TEXT DEFAULT "'.\Model\generate_api_token().'"'); $pdo->exec('ALTER TABLE config ADD COLUMN api_token TEXT DEFAULT "'.\Model\generate_token().'"');
} }

View File

@ -38,7 +38,7 @@
</div> </div>
<section> <section>
<div class="alert alert-normal"> <div class="alert alert-normal">
<h3><?= t('API') ?></h3> <h3 id="api"><?= t('API') ?></h3>
<ul> <ul>
<li><?= t('Bookmarks RSS Feed:') ?> <li><?= t('Bookmarks RSS Feed:') ?>
<a href="<?= Helper\get_current_base_url().'feed.php?token='.urlencode($values['feed_token']) ?>" target="_blank"><?= Helper\get_current_base_url().'feed.php?token='.urlencode($values['feed_token']) ?></a> <a href="<?= Helper\get_current_base_url().'feed.php?token='.urlencode($values['feed_token']) ?>" target="_blank"><?= Helper\get_current_base_url().'feed.php?token='.urlencode($values['feed_token']) ?></a>
@ -46,6 +46,7 @@
<li><?= t('API endpoint:') ?> <strong><?= Helper\get_current_base_url().'jsonrpc.php' ?></strong></li> <li><?= t('API endpoint:') ?> <strong><?= Helper\get_current_base_url().'jsonrpc.php' ?></strong></li>
<li><?= t('API username:') ?> <strong><?= Helper\escape($values['username']) ?></strong></li> <li><?= t('API username:') ?> <strong><?= Helper\escape($values['username']) ?></strong></li>
<li><?= t('API token:') ?> <strong><?= Helper\escape($values['api_token']) ?></strong></li> <li><?= t('API token:') ?> <strong><?= Helper\escape($values['api_token']) ?></strong></li>
<li><a href="?action=generate-tokens"><?= t('Generate new tokens') ?></a></li>
</ul> </ul>
</div> </div>
<div class="alert alert-normal"> <div class="alert alert-normal">