diff --git a/composer.json b/composer.json index d92ea0a..69a9c77 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ "version": "dev", "licence": "MIT", "require": { - "opauth/opauth": "0.4.2" + "opauth/opauth": "*" }, "require-dev": { "silex/silex": "dev-master", diff --git a/src/SilexOpauth/OpauthExtension.php b/src/SilexOpauth/OpauthExtension.php index b8c3612..32891a5 100644 --- a/src/SilexOpauth/OpauthExtension.php +++ b/src/SilexOpauth/OpauthExtension.php @@ -1,80 +1,91 @@ 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 'Authentication error: Opauth returns error auth response.'."
\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 'Invalid auth response: Missing key auth response components.'."
\n"; - } - elseif (!$Opauth->validate(sha1(print_r($response['auth'], true)), $response['timestamp'], $response['signature'], $failureReason)){ - echo 'Invalid auth response: '.$failureReason.".
\n"; + protected function loginAction() { + new Opauth($this->serviceConfig['config']); + return ''; + } + + protected function loginCallback() { + $Opauth = new Opauth($this->serviceConfig['config'], false); + + $response = unserialize(base64_decode($_POST['opauth'])); + + $failureReason = null; + /** + * Check if it's an error callback + */ + if (array_key_exists('error', $response)) { + return $this->onAuthenticationError($response['error'], $response); } - else{ - echo 'OK: Auth response is validated.'."
\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 "
";
-      print_r($response);
-      echo "
"; - - }); + + 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) { + } + + } \ No newline at end of file