Permalink
Browse files

Modification of adapter to use latest ZF2 release. Also migrated to its

own namespace.
  • Loading branch information...
1 parent 0e4b800 commit 4dbc4da2add9c5be3c81780581c52ec82b2a8cc1 @jfaustin committed Nov 25, 2010
View
1 zf2/example/oauth/.gitignore
@@ -0,0 +1 @@
+/config.php
View
34 zf2/example/oauth/auth.php
@@ -0,0 +1,34 @@
+<?php
+set_include_path(realpath(dirname(__FILE__) . '/../../library'));
+
+spl_autoload_register(function ($class) { require_once ltrim(str_replace('\\', '/', $class), '/') . '.php'; });
+
+use Zend\Authentication\AuthenticationService as AuthenticationService;
+$auth = new AuthenticationService();
+
+use Zend\Config\Config as Config;
+$config = new Config(require_once 'config.php');
+$options = $config->toArray();
+
+use Zend\Oauth\Consumer as OauthConsumer;
+$consumer = new OauthConsumer($options);
+
+use Ja\Authentication\Adapter\Oauth as OauthAdapter;
+$adapter = new OauthAdapter();
+$adapter->setConsumer($consumer);
+
+if (isset($_GET['oauth_token'])) {
+ $adapter->setQueryData($_GET);
+}
+
+if ($auth->hasIdentity()) {
+ header("Location: index.php");
+}
+
+$result = $auth->authenticate($adapter);
+
+if ($result->isValid()) {
+ header('Location: index.php');
+} else {
+ echo "Error authenticating: " . implode('<br />', $result->getMessages());
+}
View
7 zf2/example/oauth/config.example.php
@@ -0,0 +1,7 @@
+<?php
+return array(
+ 'consumerKey' => '',
+ 'consumerSecret' => '',
+ 'callbackUrl' => '',
+ 'siteUrl' => ''
+);
View
16 zf2/example/oauth/index.php
@@ -0,0 +1,16 @@
+<?php
+set_include_path(realpath(dirname(__FILE__) . '/../../library'));
+
+spl_autoload_register(function ($class) { require_once ltrim(str_replace('\\', '/', $class), '/') . '.php'; });
+
+use Zend\Authentication\AuthenticationService;
+
+$auth = new AuthenticationService();
+
+if ($auth->hasIdentity()) {
+ echo "Logged into OAuth source. Identity is: <Br /><pre>" . print_r($auth->getIdentity(), true) . "</pre>";
+ echo "<br /><br />";
+ echo '<a href="logout.php">Logout</a>';
+} else {
+ echo '<a href="auth.php">Authenticate with OAuth Source</a>';
+}
View
12 zf2/example/oauth/logout.php
@@ -0,0 +1,12 @@
+<?php
+set_include_path(realpath(dirname(__FILE__) . '/../../library'));
+
+spl_autoload_register(function ($class) { require_once ltrim(str_replace('\\', '/', $class), '/') . '.php'; });
+
+use Zend\Authentication\AuthenticationService;
+
+$auth = new AuthenticationService();
+
+$auth->clearIdentity();
+
+header('Location: index.php');
View
1 zf2/example/twitter/.gitignore
@@ -0,0 +1 @@
+/config.php
View
31 zf2/example/twitter/auth.php
@@ -0,0 +1,31 @@
+<?php
+set_include_path(realpath(dirname(__FILE__) . '/../../library'));
+
+require_once 'Zend/Config.php';
+$config = new Zend_Config(require_once 'config.php');
+
+require_once 'Zend/Auth.php';
+require_once 'Ja/Auth/Adapter/Oauth/Twitter.php';
+
+$adapter = new Ja_Auth_Adapter_Oauth_Twitter();
+$adapter->setConsumerKey($config->consumerKey)
+ ->setConsumerSecret($config->consumerSecret)
+ ->setCallbackUrl($config->callbackUrl);
+
+if (isset($_GET)) {
+ $adapter->setQueryData($_GET);
+}
+
+$auth = Zend_Auth::getInstance();
+
+if ($auth->hasIdentity()) {
+ header("Location: index.php");
+}
+
+$result = $auth->authenticate($adapter);
+
+if ($result->isValid()) {
+ header('Location: index.php');
+} else {
+ echo "Error authenticating: " . implode('<br />', $result->getMessages());
+}
View
6 zf2/example/twitter/config.example.php
@@ -0,0 +1,6 @@
+<?php
+return array(
+ 'consumerKey' => '',
+ 'consumerSecret' => '',
+ 'callbackUrl' => ''
+);
View
16 zf2/example/twitter/index.php
@@ -0,0 +1,16 @@
+<?php
+set_include_path(realpath(dirname(__FILE__) . '/../../library'));
+
+require_once 'Zend/Auth.php';
+
+$auth = Zend_Auth::getInstance();
+
+if ($auth->hasIdentity()) {
+ $identity = $auth->getIdentity();
+
+ echo "Logged into Twitter as user " . $identity['screen_name'];
+ echo "<br /><br />";
+ echo '<a href="logout.php">Logout</a>';
+} else {
+ echo '<a href="auth.php">Authenticate with Twitter</a>';
+}
View
10 zf2/example/twitter/logout.php
@@ -0,0 +1,10 @@
+<?php
+set_include_path(realpath(dirname(__FILE__) . '/../../library'));
+
+require_once 'Zend/Auth.php';
+
+$auth = Zend_Auth::getInstance();
+
+$auth->clearIdentity();
+
+header('Location: index.php');
View
1 zf2/library/.gitignore
@@ -0,0 +1 @@
+/Zend
View
248 zf2/library/Ja/Authentication/Adapter/Oauth.php
@@ -0,0 +1,248 @@
+<?php
+/**
+ * Ja Zend Framework Auth Adapters
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category Ja/Zend
+ * @package Zend_Auth
+ * @subpackage Zend_Auth_Adapter
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @version $Id: $
+ */
+
+/**
+ * @namespace
+ */
+namespace Ja\Authentication\Adapter;
+
+use Zend\Authentication\Adapter as AuthenticationAdapter,
+ Zend\Authentication\Result as AuthenticationResult,
+ Zend\Session\Container as SessionContainer;
+
+/**
+ * @category Ja/Zend
+ * @package Zend_Auth
+ * @subpackage Zend_Auth_Adapter
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+class Oauth implements AuthenticationAdapter
+{
+ /**
+ * OAuth consumer object
+ *
+ * @var null|Zend\Oauth\Consumer
+ */
+ protected $_consumer = null;
+
+ /**
+ * OAuth query data
+ *
+ * @var null|array
+ */
+ protected $_queryData = null;
+
+ /**
+ * OAuth access token after successful authentication
+ *
+ * @var null|Zend\Oauth\Token\Access
+ */
+ protected $_accessToken = null;
+
+ /**
+ * Array of options for this adapter. Options include:
+ * - sessionNamespace: session namespace override
+ *
+ * @var null|array
+ */
+ protected $_options = null;
+
+ /**
+ * Default session container to store session credentials
+ *
+ * @var string
+ */
+ const DEFAULT_SESSION_CONTAINER = 'Ja_Authentication_Adapter_Oauth';
+
+ /**
+ * Constructor
+ *
+ * @param array $options An array of options for this adapter
+ * @param Zend_Oauth_Consumer $consumer Consumer object
+ */
+ public function __construct(array $options = array(), \Zend\Oauth\Consumer $consumer = null)
+ {
+ $this->setOptions($options);
+
+ if ($consumer !== null) {
+ $this->setConsumer($consumer);
+ }
+ }
+
+ /**
+ * Sets the consumer object for authentication
+ *
+ * @param Zend\Oauth\Consumer $consumer
+ * @return Ja\Authentication\Adapter\Oauth Fluent interface
+ */
+ public function setConsumer(\Zend\Oauth\Consumer $consumer)
+ {
+ $this->_consumer = $consumer;
+ return $this;
+ }
+
+ /**
+ * Gets the consumer
+ *
+ * @return Zend\Oauth\Consumer|null
+ */
+ public function getConsumer()
+ {
+ return $this->_consumer;
+ }
+
+ /**
+ * Sets the query data for generation of the access token. Data
+ * is typically passed back to the application from the remote
+ * OAuth authentication source.
+ *
+ * @param array $queryData array of query data
+ * @return Ja\Authentication\Adapter\Oauth Fluent interface
+ */
+ public function setQueryData(array $queryData)
+ {
+ $this->_queryData = $queryData;
+ return $this;
+ }
+
+ /**
+ * Gets the query data
+ *
+ * @return array|null
+ */
+ public function getQueryData()
+ {
+ return $this->_queryData;
+ }
+
+ /**
+ * Sets the access token after a successful authentication attempt
+ *
+ * @param Zend\Oauth\Token\Access $token access token
+ * @return Ja\Authentication\Adapter\Oauth Fluent interface
+ */
+ public function setAccessToken(\Zend\Oauth\Token\Access $token)
+ {
+ $this->_accessToken = $token;
+ return $this;
+ }
+
+ /**
+ * Gets the access token result
+ *
+ * @return Zend\Oauth\Token\Access|null
+ */
+ public function getAccessToken()
+ {
+ return $this->_accessToken;
+ }
+
+ /**
+ * Returns the array of arrays of options of this adapter.
+ *
+ * @return array|null
+ */
+ public function getOptions()
+ {
+ return $this->_options;
+ }
+
+ /**
+ * Sets the array of arrays of options to be used by
+ * this adapter.
+ *
+ * @param array $options The array of arrays of options
+ * @return Provides a fluent interface
+ */
+ public function setOptions($options)
+ {
+ $this->_options = is_array($options) ? $options : array();
+ return $this;
+ }
+
+ /**
+ * Authenticate the user
+ *
+ * @return Zend_Auth_Result
+ */
+ public function authenticate()
+ {
+ if (!$this->_consumer) {
+ $code = AuthenticationResult::FAILURE;
+ $message = array('A valid Zend\Oauth\Consumer key is required');
+ return new AuthenticationResult($code, '', $message);
+ }
+
+ $sessionContainer = self::DEFAULT_SESSION_CONTAINER;
+
+ if (isset($this->_options['sessionContainer']) && $this->_options['sessionContainer'] != '') {
+ $sessionContainer = $this->_options['sessionContainer'];
+ }
+
+ $session = new SessionContainer($sessionContainer);
+
+ try {
+ if (!$session->requestToken) {
+
+ $token = $this->_consumer->getRequestToken();
+
+ $session->requestToken = serialize($token);
+
+ $this->_consumer->redirect();
+
+ } else {
+
+ $accessToken = $this->_consumer->getAccessToken($this->_queryData, unserialize($session->requestToken));
+
+ $this->setAccessToken($accessToken);
+
+ unset($session->requestToken);
+
+ $body = $accessToken->getResponse()->getBody();
+
+ $returnParams = array();
+
+ $parts = explode('&', $body);
+ foreach ($parts as $kvpair) {
+ $pair = explode('=', $kvpair);
+ $returnParams[rawurldecode($pair[0])] = rawurldecode($pair[1]);
+ }
+ }
+ } catch (Zend\Oauth\Exception $e) {
+ $session->unsetAll();
+
+ $code = AuthenticationResult::FAILURE;
+ $message = array('Access denied by OAuth source');
+ return new AuthenticationResult($code, '', $message);
+ } catch (Exception $e) {
+ $session->unsetAll();
+
+ $code = AuthenticationResult::FAILURE;
+ $message = array($e->getMessage());
+ return new AuthenticationResult($code, '', $message);
+ }
+
+ return new AuthenticationResult(AuthenticationResult::SUCCESS, $returnParams, array());
+ }
+}

0 comments on commit 4dbc4da

Please sign in to comment.