Skip to content
This repository has been archived by the owner on Nov 18, 2018. It is now read-only.

Commit

Permalink
Extensible through inheritance and events!
Browse files Browse the repository at this point in the history
  • Loading branch information
panrafal authored and Florin Popa committed Mar 10, 2013
1 parent baafe99 commit fc21653
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 67 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"version": "dev",
"licence": "MIT",
"require": {
"opauth/opauth": "0.4.2"
"opauth/opauth": "*"
},
"require-dev": {
"silex/silex": "dev-master",
Expand Down
143 changes: 77 additions & 66 deletions src/SilexOpauth/OpauthExtension.php
Original file line number Diff line number Diff line change
@@ -1,80 +1,91 @@
<?php

namespace SilexOpauth;
namespace SilexOpauth; // Non psr-0 namespace usage. :(


use Opauth;
use Silex\Application;
use Silex\ServiceProviderInterface;
use Opauth; // Non psr-0 namespace usage. :(

class OpauthExtension implements ServiceProviderInterface
{
private $serviceConfig;

public function register(Application $app)
{
$this->serviceConfig = $app['opauth'];
$this->serviceConfig['config'] = array_merge(array(
'path' => $app['opauth']['login'] . '/',
'callback_url' => $app['opauth']['callback'],// Handy shortcut.
'callback_transport' => 'post' // Won't work with silex session
), $app['opauth']['config']);

$config = $this->serviceConfig['config'];

$init = function() use ($app, $config) {
new Opauth($config);
};
use Symfony\Component\EventDispatcher\GenericEvent;

class OpauthExtension implements ServiceProviderInterface {

/** @var Application */
protected $app;
protected $serviceConfig;

const EVENT_ERROR = 'opauth.error';
const EVENT_SUCCESS = 'opauth.success';

public function register(Application $app) {
$this->app = $app;
$this->serviceConfig = $app['opauth'];
$this->serviceConfig['config'] = array_merge(
array(
'path' => $app['opauth']['login'] . '/',
'callback_url' => $app['opauth']['callback'], // Handy shortcut.
'callback_transport' => 'post' // Won't work with silex session
), $app['opauth']['config']
);

$app->match($this->serviceConfig['callback'], function() { return $this->loginCallback(); });

$app->match($this->serviceConfig['login'] . '/{strategy}', function() { return $this->loginAction(); });
$app->match($this->serviceConfig['login'] . '/{strategy}/{return}', function() { return $this->loginAction(); });

$app->match($this->serviceConfig['login'] . '/{strategy}', $init);
$app->match($this->serviceConfig['login'] . '/{strategy}/{return}', $init);

$app->match($this->serviceConfig['callback'], function() use ($config){
$Opauth = new Opauth($config, false );

$response = unserialize(base64_decode( $_POST['opauth'] ));

$failureReason = null;
/**
* Check if it's an error callback
*/
if (array_key_exists('error', $response)){
echo '<strong style="color: red;">Authentication error: </strong> Opauth returns error auth response.'."<br>\n";
}
}

/**
* Auth response validation
*
* To validate that the auth response received is unaltered, especially auth response that
* is sent through GET or POST.
*/
else{
if (empty($response['auth']) || empty($response['timestamp']) || empty($response['signature']) || empty($response['auth']['provider']) || empty($response['auth']['uid'])){
echo '<strong style="color: red;">Invalid auth response: </strong>Missing key auth response components.'."<br>\n";
}
elseif (!$Opauth->validate(sha1(print_r($response['auth'], true)), $response['timestamp'], $response['signature'], $failureReason)){
echo '<strong style="color: red;">Invalid auth response: </strong>'.$failureReason.".<br>\n";
protected function loginAction() {
new Opauth($this->serviceConfig['config']);
return '';
}

protected function loginCallback() {
$Opauth = new Opauth($this->serviceConfig['config'], false);

This comment has been minimized.

Copy link
@vollossy

vollossy Mar 24, 2013

Can't understand, why are you passing false to second argument? It forces to override OpauthStrategy::callback method in custom strategies, isn't it?


$response = unserialize(base64_decode($_POST['opauth']));

This comment has been minimized.

Copy link
@vollossy

vollossy Mar 24, 2013

This piece of code also not so obvious for me? Is using only a $_POST array mistake?


$failureReason = null;
/**
* Check if it's an error callback
*/
if (array_key_exists('error', $response)) {
return $this->onAuthenticationError($response['error'], $response);
}
else{
echo '<strong style="color: green;">OK: </strong>Auth response is validated.'."<br>\n";

/**
* It's all good. Go ahead with your application-specific authentication logic
*/
/**
* Auth response validation
*
* To validate that the auth response received is unaltered, especially auth response that
* is sent through GET or POST.
*/ else {
if (empty($response['auth']) || empty($response['timestamp']) || empty($response['signature']) || empty($response['auth']['provider']) || empty($response['auth']['uid'])) {
return $this->onAuthenticationError('Missing key auth response components', $response);
} elseif (!$Opauth->validate(sha1(print_r($response['auth'], true)), $response['timestamp'], $response['signature'], $failureReason)) {
return $this->onAuthenticationError($failureReason, $response);
} else {
return $this->onAuthenticationSuccess($response);
}
}
}


/**
* Auth response dump
*/
echo "<pre>";
print_r($response);
echo "</pre>";

});

return '';
}

protected function onAuthenticationError($message, $response) {
$e = new GenericEvent($response, array('message' => $message));
$e->setArgument('result', '');
return $this->app['dispatcher']->dispatch(self::EVENT_ERROR, $e)->getArgument('result');
}

protected function onAuthenticationSuccess($response) {
$e = new GenericEvent($response);
$e->setArgument('result', '');
return $this->app['dispatcher']->dispatch(self::EVENT_SUCCESS, $e)->getArgument('result');
}
public function boot(Application $app)
{

public function boot(Application $app) {

}


}

0 comments on commit fc21653

Please sign in to comment.