Update of /cvsroot/php-blog/additional_plugins/serendipity_event_browserid
In directory sfp-cvs-1.v30.ch3.sourceforge.com:/tmp/cvs-serv18870/serendipity_event_browserid
Modified Files:
serendipity_event_browserid.php
Added Files:
LICENSE README.md S9yStore.php composer.json composer.lock
phpunit.xml
Removed Files:
browserid_signin.png
Log Message:
gitclone.sh autocommit
--- NEW FILE: phpunit.xml ---
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php">
<testsuites>
<testsuite>
<directory>tests/</directory>
</testsuite>
</testsuites>
</phpunit>
--- NEW FILE: LICENSE ---
Copyright (C) 2016 Angry Bytes
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
--- browserid_signin.png DELETED ---
Index: serendipity_event_browserid.php
===================================================================
RCS file: /cvsroot/php-blog/additional_plugins/serendipity_event_browserid/serendipity_event_browserid.php,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- serendipity_event_browserid.php 12 Aug 2013 07:58:38 -0000 1.3
+++ serendipity_event_browserid.php 22 Feb 2017 22:03:06 -0000 1.4
@@ -16,18 +16,16 @@
$propbag->add('name', PLUGIN_BROWSERID_NAME);
$propbag->add('description', PLUGIN_BROWSERID_DESC);
$propbag->add('stackable', false);
- $propbag->add('author', 'Grischa Brockhaus');
- $propbag->add('version', '1.1');
+ $propbag->add('author', 'Grischa Brockhaus, Malte Paskuda');
+ $propbag->add('version', '2.0');
$propbag->add('requirements', array(
- 'serendipity' => '1.6',
- 'smarty' => '2.6.7',
- 'php' => '5.1.3'
+ 'serendipity' => '2.0',
+ 'php' => '7.0'
));
$propbag->add('groups', array('BACKEND_USERMANAGEMENT'));
$propbag->add('event_hooks', array(
'backend_login' => true,
'backend_login_page' => true,
- 'backend_header' => true,
'external_plugin' => true,
));
@@ -55,27 +53,26 @@
function event_hook($event, &$bag, &$eventData, $addData = null) {
global $serendipity;
- static $login_url = null;
- if ($login_url === null) {
- $login_url = $serendipity['baseURL'] . $serendipity['indexFile'] . '?/plugin/loginbox';
- }
+ require __DIR__ . '/vendor/autoload.php';
+ require_once 'S9yStore.php';
+ $verify_url = $serendipity['baseURL'] . 'index.php?/plugin/serendipity_event_browserid_verify';
+
+ $this->portier = new \Portier\Client\Client(
+ new \Portier\Client\S9yStore($this),
+ $verify_url
+ );
$hooks = &$bag->get('event_hooks');
if (isset($hooks[$event])) {
switch($event) {
case 'external_plugin':
- if ($eventData=="serendipity_event_browserid.js") {
- header('Content-Type: text/javascript');
- echo file_get_contents(dirname(__FILE__). '/serendipity_event_browserid.js');
- }
- else if ($eventData=="browserid_signin.png") {
- header('Content-Type: image/png');
- echo file_get_contents(dirname(__FILE__). '/browserid_signin.png');
+ if ($eventData=="serendipity_event_browserid_auth") {
+ $this->auth($serendipity['POST']['persona_email']);
}
else if ($eventData=="serendipity_event_browserid_verify") {
- $this->verify();
+ $this->verify($_POST['id_token']);
}
break;
@@ -92,9 +89,6 @@
}
return;
- case 'backend_header':
- $this->print_backend_header();
- return true;
default:
return false;
@@ -104,131 +98,79 @@
}
}
- function verify() {
+ function verify($idToken) {
global $serendipity;
-
- $url = 'https://browserid.org/verify';
- $assert = $_POST['assert'];
- $params = 'assertion='.$assert.'&audience=' .
- urlencode($serendipity['baseURL']);
- $ch = curl_init();
- curl_setopt($ch,CURLOPT_URL,$url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch,CURLOPT_POST,2);
- curl_setopt($ch,CURLOPT_POSTFIELDS, $params);
- $result = curl_exec($ch);
- curl_close($ch);
- $response = json_decode($result);
- if (isset($response) && $response->status=='okay') {
- $email = $response->email;
- $audience = $response->audience;
- if ($audience!=$serendipity['baseURL']) { // The login has the wrong host!
- $response->status = 'errorhost';
- $response->message= "Internal error logging you in (wrong host: $audience)";
- $_SESSION['serendipityAuthedUser'] = false;
- @session_destroy();
- }
- else { // host ist correct, check what we have with this email
- $password = md5($email);
- $query = "SELECT DISTINCT a.email, a.authorid, a.userlevel, a.right_publish, a.realname
- FROM
- {$serendipity['dbPrefix']}authors AS a
- WHERE
- a.email = '{$email}'";
- $row = serendipity_db_query($query, true, 'assoc');
- if (is_array($row)) {
- serendipity_setCookie('old_session', session_id());
- serendipity_setAuthorToken();
- $_SESSION['serendipityUser'] = $serendipity['serendipityUser'] = $row['realname'];
- $_SESSION['serendipityPassword'] = $serendipity['serendipityPassword'] = $password;
- $_SESSION['serendipityEmail'] = $serendipity['serendipityEmail'] = $email;
- $_SESSION['serendipityAuthorid'] = $serendipity['authorid'] = $row['authorid'];
- $_SESSION['serendipityUserlevel'] = $serendipity['serendipityUserlevel'] = $row['userlevel'];
- $_SESSION['serendipityAuthedUser'] = $serendipity['serendipityAuthedUser'] = true;
- $_SESSION['serendipityRightPublish']= $serendipity['serendipityRightPublish'] = $row['right_publish'];
- // Prevent session manupulation:
- $_SESSION['serendipityBrowserID'] = $this->get_install_token();
- serendipity_load_configuration($serendipity['authorid']);
- }
- else { // No user found for that email!
- $response->status = 's9yunknown';
- $response->message= "Sorry, we don't have a user for $email";
- $_SESSION['serendipityAuthedUser'] = false;
- @session_destroy();
- }
-
-
- }
- $result = json_encode($response);
- }
- echo $result;
+ $email = $this->portier->verify($idToken);
+ $this->login_user($email);
+ header("Location: ${_SESSION['serendipity_event_browserid_loginurl']}", true, 303);
}
-
- function reauth() {
- global $serendipity;
- // Reauth only, if valid session
- if (isset($_SESSION['serendipityBrowserID']) && $_SESSION['serendipityBrowserID']===$this->get_install_token()) {
- $serendipity['serendipityUser'] = $_SESSION['serendipityUser'];
- $serendipity['serendipityPassword'] = $_SESSION['serendipityPassword'];
- $serendipity['serendipityEmail'] = $_SESSION['serendipityEmail'];
- $serendipity['authorid'] = $_SESSION['serendipityAuthorid'];
- $serendipity['serendipityUserlevel'] = $_SESSION['serendipityUserlevel'];
- $serendipity['serendipityAuthedUser'] = $_SESSION['serendipityAuthedUser'];
- $serendipity['serendipityRightPublish'] = $_SESSION['serendipityRightPublish'];
- serendipity_load_configuration($serendipity['authorid']);
- return true;
- }
- return false;
+
+ function auth($email) {
+ global $serendipity;
+ $authUrl = $this->portier->authenticate($email);
+ header("Location: $authUrl", true, 303);
}
-
- /**
- * Produces or loads a token unique to this installation.
- */
- function get_install_token() {
- $token = $this->get_config("installationtoken");
- if (empty($token)) {
- $token = md5(time());
- $this->set_config("installationtoken", $token);
- }
- return $token;
+
+ function login_user($email) {
+ global $serendipity;
+ $query = "SELECT DISTINCT a.email, a.authorid, a.userlevel, a.right_publish, a.realname
+ FROM
+ {$serendipity['dbPrefix']}authors AS a
+ WHERE
+ a.email = '{$email}'";
+ $row = serendipity_db_query($query, true, 'assoc');
+ if (is_array($row)) {
+ serendipity_setCookie('old_session', session_id());
+ serendipity_setAuthorToken();
+ $_SESSION['serendipityUser'] = $serendipity['serendipityUser'] = $row['realname'];
+ $_SESSION['serendipityPassword'] = $serendipity['serendipityPassword'] = serendipity_hash($email);
+ $_SESSION['serendipityEmail'] = $serendipity['serendipityEmail'] = $email;
+ $_SESSION['serendipityAuthorid'] = $serendipity['authorid'] = $row['authorid'];
+ $_SESSION['serendipityUserlevel'] = $serendipity['serendipityUserlevel'] = $row['userlevel'];
+ $_SESSION['serendipityAuthedUser'] = $serendipity['serendipityAuthedUser'] = true;
+ $_SESSION['serendipityRightPublish']= $serendipity['serendipityRightPublish'] = $row['right_publish'];
+ serendipity_load_configuration($serendipity['authorid']);
+ } else { // No user found for that email!
+ echo "found no such user";
+ $response->status = 's9yunknown';
+ $response->message= "Sorry, we don't have a user for $email";
+ $_SESSION['serendipityAuthedUser'] = false;
+ @session_destroy();
+ }
}
- function print_backend_header() {
- echo '
-<script src="https://browserid.org/include.js" type="text/javascript"></script>
-';
+ function reauth() {
+ global $serendipity;
+ // Reauth only, if valid session
+ if ($_SESSION['serendipityAuthedUser']) {
+ $serendipity['serendipityUser'] = $_SESSION['serendipityUser'];
+ $serendipity['serendipityPassword'] = $_SESSION['serendipityPassword'];
+ $serendipity['serendipityEmail'] = $_SESSION['serendipityEmail'];
+ $serendipity['authorid'] = $_SESSION['serendipityAuthorid'];
+ $serendipity['serendipityUserlevel'] = $_SESSION['serendipityUserlevel'];
+ $serendipity['serendipityAuthedUser'] = $_SESSION['serendipityAuthedUser'];
+ $serendipity['serendipityRightPublish'] = $_SESSION['serendipityRightPublish'];
+ serendipity_load_configuration($serendipity['authorid']);
+ return true;
+ }
+ return false;
}
function print_loginpage(&$eventData) {
global $serendipity;
- $hidden = array('action'=>'admin');
- $bid_title = "Sign-in with BrowserID";
- $local_signin_img = $serendipity['baseURL'] . 'index.php?/plugin/browserid_signin.png';
- $local_js = $serendipity['baseURL'] . 'index.php?/plugin/serendipity_event_browserid.js';
- $verify_url = $serendipity['baseURL'] . 'index.php?/plugin/serendipity_event_browserid_verify';
-
- $eventData['header'] .= '
-<!-- browserid start -->
-<script type="text/javascript">var browserid_verify="'. $verify_url . '";</script>
-<div align="center">
-<section><button><img src="' . $local_signin_img . '" alt="' . $bid_title . '" title="' . $bid_title . '"></button></section>
-</div>
-<script src="' . $local_js . '" type="text/javascript"></script>
-<!-- browserid end -->
-';
+ $_SESSION['serendipity_event_browserid_loginurl'] = $_SERVER['REDIRECT_SCRIPT_URI'] . '?' . $_SERVER['QUERY_STRING'];
+ $auth_url = $serendipity['baseURL'] . 'index.php?/plugin/serendipity_event_browserid_auth';
+ echo '<form method="post" action="' . $auth_url . '" style="margin: auto; max-width: 23em; border: 1px solid #aaa; margin-top: 4em; padding: 1em;">
+ <fieldset>
+ <span class="wrap_legend"><legend>Please enter your email</legend></span>
+ <input name="serendipity[persona_email]" type="email">
+ <button type="submit">Login</button>
+ </fieldset>
+ </form>';
}
-
- function print_backend_footer() {
- global $serendipity;
- $local_js = $serendipity['baseURL'] . 'index.php?/plugin/serendipity_event_browserid.js';
- echo '
-<!-- browserid start -->
-<script src="' . $local_js . '" type="text/javascript"></script>
-<!-- browserid end -->
-';
- }
+
}
/* vim: set sts=4 ts=4 expandtab : */
--- NEW FILE: composer.json ---
{
"name": "portier/client",
"description": "Portier client for PHP",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Stéphan Kochen",
"email": "st...@ko..."
}
],
"autoload": {
"psr-4": {
"Portier\\Client\\": "src/"
}
},
"require": {
"spomky-labs/base64url": "^1.0",
"fgrosse/phpasn1": "^1.5",
"lcobucci/jwt": "^3.2",
"guzzlehttp/guzzle": "^6.2"
},
"require-dev": {
"phpunit/phpunit": "^5.6",
"squizlabs/php_codesniffer": "^2.7"
}
}
--- NEW FILE: S9yStore.php ---
<?php
namespace Portier\Client;
/**
* A store implementation that uses Redis as the backend.
*/
class S9yStore extends AbstractStore
{
public $plugin;
/**
* Constructor
*/
public function __construct($plugin)
{
parent::__construct();
$this->plugin = $plugin;
}
/**
* {@inheritDoc}
*/
public function fetchCached($cacheId, $url)
{
$key = 'cache:' . $cacheId;
$data = $this->get_config_ttl($key);
if ($data) {
return json_decode($data);
}
$res = $this->fetch($url);
$this->set_config_ttl($key, json_encode($res->data), $res->ttl);
return $res->data;
}
/**
* {@inheritDoc}
*/
public function createNonce($email)
{
$nonce = $this->generateNonce($email);
$this->set_config_ttl("nonce_$email", $nonce, $this->nonceTtl);
return $nonce;
}
/**
* {@inheritDoc}
*/
public function consumeNonce($nonce, $email)
{
$storedNonce = $this->get_config_ttl("nonce_$email");
$this->plugin->set_config("nonce_$email", null);
if ($storedNonce === null || $storedNonce !== $nonce) {
throw new \Exception('Invalid or expired nonce');
}
}
private function set_config_ttl($key, $value, $ttl) {
$this->plugin->set_config($key, $value);
$this->plugin->set_config("$key_valid_till", time() + $ttl);
}
private function get_config_ttl($key) {
$value = $this->plugin->get_config($key);
if (time() < $this->plugin->get_config("$key_valid_till")) {
return $value;
}
}
}
--- NEW FILE: composer.lock ---
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"content-hash": "f02db33d9ec35736160940a72df25ed0",
"packages": [
{
"name": "fgrosse/phpasn1",
"version": "1.5.2",
"source": {
"type": "git",
"url": "https://github.com/fgrosse/PHPASN1.git",
"reference": "a18b162eca6aa70f8f15615f4ac8c852dffbc7dd"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/fgrosse/PHPASN1/zipball/a18b162eca6aa70f8f15615f4ac8c852dffbc7dd",
[...1766 lines suppressed...]
"email": "bsc...@gm..."
}
],
"description": "Assertions to validate method input/output with nice error messages.",
"keywords": [
"assert",
"check",
"validate"
],
"time": "2016-11-23T20:04:58+00:00"
}
],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": []
}
--- NEW FILE: README.md ---
# portier-php
A [Portier] client library for PHP
[Portier]: https://portier.github.io/
### Example
```php
<?php
require 'vendor/autoload.php';
$app = new \Slim\App();
$redis = new Redis();
$redis->pconnect('127.0.0.1', 6379);
$portier = new \Portier\Client\Client(
new \Portier\Client\RedisStore($redis),
'http://localhost:8000/verify'
);
$app->get('/', function($req, $res) {
$res = $res
->withStatus(200)
->withHeader('Content-Type', 'text/html; charset=utf-8');
$res->getBody()->write(
<<<EOF
<p>Enter your email address:</p>
<form method="post" action="/auth">
<input name="email" type="email">
<button type="submit">Login</button>
</form>
EOF
);
return $res;
});
$app->post('/auth', function($req, $res) use ($portier) {
$authUrl = $portier->authenticate($req->getParsedBodyParam('email'));
return $res
->withStatus(303)
->withHeader('Location', $authUrl);
});
$app->post('/verify', function($req, $res) use ($portier) {
$email = $portier->verify($req->getParsedBodyParam('id_token'));
$res = $res
->withStatus(200)
->withHeader('Content-Type', 'text/html; charset=utf-8');
$res->getBody()->write(
<<<EOF
<p>Verified email address ${email}!</p>
EOF
);
return $res;
});
$app->run();
```
|