Diglin_Github - Version 1.0.0

Version Notes

- 1.0.0: version stable

Download this release

Release Info

Developer diglin
Extension Diglin_Github
Version 1.0.0
Comparing to
See all releases


Version 1.0.0

Files changed (34) hide show
  1. app/code/community/Diglin/Github/Block/Account/Existing.php +25 -0
  2. app/code/community/Diglin/Github/Block/Account/Register.php +30 -0
  3. app/code/community/Diglin/Github/Block/Adminhtml/Grid.php +25 -0
  4. app/code/community/Diglin/Github/Helper/Data.php +133 -0
  5. app/code/community/Diglin/Github/Model/Adapter/Oauth2.php +47 -0
  6. app/code/community/Diglin/Github/Model/Confirmation.php +32 -0
  7. app/code/community/Diglin/Github/Model/Log.php +24 -0
  8. app/code/community/Diglin/Github/Model/Observer.php +28 -0
  9. app/code/community/Diglin/Github/Model/Resource/Confirmation.php +28 -0
  10. app/code/community/Diglin/Github/Model/Resource/Confirmation/Collection.php +25 -0
  11. app/code/community/Diglin/Github/Model/Resource/Setup.php +19 -0
  12. app/code/community/Diglin/Github/Model/System/Config/Backend/Oauth.php +30 -0
  13. app/code/community/Diglin/Github/controllers/AccountController.php +392 -0
  14. app/code/community/Diglin/Github/etc/adminhtml.xml +25 -0
  15. app/code/community/Diglin/Github/etc/config.xml +126 -0
  16. app/code/community/Diglin/Github/etc/system.xml +71 -0
  17. app/code/community/Diglin/Github/sql/github_setup/install-1.0.0.0.php +88 -0
  18. app/design/frontend/base/default/layout/github.xml +112 -0
  19. app/design/frontend/base/default/template/github/customer/button.phtml +21 -0
  20. app/design/frontend/base/default/template/github/customer/create.phtml +84 -0
  21. app/design/frontend/base/default/template/github/customer/existing.phtml +53 -0
  22. app/design/frontend/base/default/template/github/customer/fields.phtml +27 -0
  23. app/design/frontend/base/default/template/github/customer/info.phtml +37 -0
  24. app/design/frontend/base/default/template/github/customer/login.phtml +30 -0
  25. app/etc/modules/Diglin_Github.xml +8 -0
  26. app/locale/en_US/template/email/github/existing_customer_link_confirmation.html +35 -0
  27. lib/Oauth2/Client.php +513 -0
  28. lib/Oauth2/GrantType/AuthorizationCode.php +41 -0
  29. lib/Oauth2/GrantType/ClientCredentials.php +25 -0
  30. lib/Oauth2/GrantType/IGrantType.php +15 -0
  31. lib/Oauth2/GrantType/Password.php +41 -0
  32. lib/Oauth2/GrantType/RefreshToken.php +34 -0
  33. lib/Oauth2/Test.php +38 -0
  34. package.xml +46 -0
app/code/community/Diglin/Github/Block/Account/Existing.php ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Diglin
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ *
12
+ *
13
+ * @category Diglin
14
+ * @package Diglin_Github
15
+ * @copyright Copyright (c) 2011-2013 Diglin (http://www.diglin.com)
16
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
+ */
18
+ class Diglin_Github_Block_Account_Existing extends Mage_Customer_Block_Form_Register
19
+ {
20
+
21
+ public function getPostActionUrl()
22
+ {
23
+ return $this->getUrl('github/account/existing');
24
+ }
25
+ }
app/code/community/Diglin/Github/Block/Account/Register.php ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Diglin
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ *
12
+ *
13
+ * @category Diglin
14
+ * @package Diglin_Github
15
+ * @copyright Copyright (c) 2011-2013 Diglin (http://www.diglin.com)
16
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
+ */
18
+ class Diglin_Github_Block_Account_Register extends Mage_Customer_Block_Form_Register
19
+ {
20
+
21
+ public function getPostActionUrl()
22
+ {
23
+ return $this->getUrl('github/account/createpost');
24
+ }
25
+
26
+ public function getBackUrl()
27
+ {
28
+ return $this->getUrl('customer/account/index');
29
+ }
30
+ }
app/code/community/Diglin/Github/Block/Adminhtml/Grid.php ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Diglin
4
+ *
5
+ * @category Diglin
6
+ * @package Diglin_Github
7
+ * @copyright Copyright (c) 2011-2013 Diglin (http://www.diglin.com)
8
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
9
+ */
10
+ class Diglin_Github_Block_Adminhtml_Grid extends Mage_Adminhtml_Block_Customer_Grid
11
+ {
12
+
13
+ protected function _prepareColumns()
14
+ {
15
+ if (Mage::getStoreConfigFlag('github/config/grid')) {
16
+ // Set a new column username after the column name
17
+ $this->addColumnAfter('github_login', array(
18
+ 'header' => Mage::helper('github')->__('Github'),
19
+ 'index' => 'github_login'
20
+ ),
21
+ 'name');
22
+ }
23
+ return parent::_prepareColumns();
24
+ }
25
+ }
app/code/community/Diglin/Github/Helper/Data.php ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Diglin
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ *
12
+ *
13
+ * @category Diglin
14
+ * @package Diglin_Github
15
+ * @copyright Copyright (c) 2011-2013 Diglin (http://www.diglin.com)
16
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
+ */
18
+ class Diglin_Github_Helper_Data extends Mage_Core_Helper_Abstract
19
+ {
20
+ public function getSession()
21
+ {
22
+ return Mage::getSingleton('customer/session');
23
+ }
24
+
25
+ /**
26
+ *
27
+ * @param string $token
28
+ * @return string
29
+ */
30
+ public function setAccessToken ($token)
31
+ {
32
+ return $this->getSession()->setData('github_access_token', $token);
33
+ }
34
+
35
+ /**
36
+ *
37
+ * @return string
38
+ */
39
+ public function getAccessToken ()
40
+ {
41
+ return $this->getSession()->getData('github_access_token');
42
+ }
43
+
44
+ /**
45
+ * The parameter data should be similar to
46
+ * <pre>
47
+ * array(29) {
48
+ * ["blog"] => string(21) "http://www.diglin.com"
49
+ * ["location"] => string(11) "Switzerland"
50
+ * ["type"] => string(4) "User"
51
+ * ["subscriptions_url"] => string(49) "https://api.github.com/users/diglin/subscriptions"
52
+ * ["url"] => string(35) "https://api.github.com/users/diglin"
53
+ * ["repos_url"] => string(41) "https://api.github.com/users/diglin/repos"
54
+ * ["gravatar_id"] => string(32) "5cfc92dca620b0aa0bbc2f12d9b5bfc7"
55
+ * ["avatar_url"] => string(158) "https://secure.gravatar.com/avatar/5cfc92dca620b0aa0bbc2f12d9b5bfc7?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png"
56
+ * ["email"] => NULL
57
+ * ["received_events_url"] => string(51) "https://api.github.com/users/diglin/received_events"
58
+ * ["html_url"] => string(25) "https://github.com/diglin"
59
+ * ["company"] => NULL
60
+ * ["events_url"] => string(52) "https://api.github.com/users/diglin/events{/privacy}"
61
+ * ["organizations_url"] => string(40) "https://api.github.com/users/diglin/orgs"
62
+ * ["login"] => string(6) "diglin"
63
+ * ["public_gists"] => int(0)
64
+ * ["updated_at"] => string(20) "2013-01-22T20:03:38Z"
65
+ * ["gists_url"] => string(51) "https://api.github.com/users/diglin/gists{/gist_id}"
66
+ * ["hireable"] => bool(false)
67
+ * ["public_repos"] => int(7)
68
+ * ["followers"] => int(2)
69
+ * ["following"] => int(6)
70
+ * ["name"] => string(13) "Sylvain Rayé"
71
+ * ["starred_url"] => string(58) "https://api.github.com/users/diglin/starred{/owner}{/repo}"
72
+ * ["bio"] => NULL
73
+ * ["followers_url"] => string(45) "https://api.github.com/users/diglin/followers"
74
+ * ["id"] => int(1337461)
75
+ * ["created_at"] => string(20) "2012-01-17T16:48:53Z"
76
+ * ["following_url"] => string(45) "https://api.github.com/users/diglin/following"
77
+ * }
78
+ * </pre>
79
+ *
80
+ * @param array $data
81
+ * @return Varien_Object
82
+ */
83
+ public function setUserData ($data)
84
+ {
85
+ return $this->getSession()->setData('github_data', $data);
86
+ }
87
+
88
+ /**
89
+ * @see $this->setUserData() to see the data format returned
90
+ *
91
+ * @return array
92
+ */
93
+ public function getUserData ()
94
+ {
95
+ return new Varien_Object($this->getSession()->getData('github_data'));
96
+ }
97
+
98
+ public function getCustomer($githubId)
99
+ {
100
+ /* @var $resource Mage_Customer_Model_Resource_Customer_Collection */
101
+ $resource = Mage::getResourceModel('customer/customer_collection');
102
+
103
+ return $resource->addAttributeToFilter('github_id', $githubId)
104
+ ->load()
105
+ ->getFirstItem();
106
+ }
107
+
108
+ /**
109
+ * Send corresponding email template
110
+ *
111
+ * @param string $emailTemplate configuration path of email template
112
+ * @param string $emailSender configuration path of email identity
113
+ * @param array $templateParams
114
+ * @param int|null $storeId
115
+ * @return Mage_Customer_Model_Customer
116
+ */
117
+ public function sendEmailTemplate($template, $sender, $templateParams = array(), $customer, $storeId = null)
118
+ {
119
+ /** @var $mailer Mage_Core_Model_Email_Template_Mailer */
120
+ $mailer = Mage::getModel('core/email_template_mailer');
121
+ $emailInfo = Mage::getModel('core/email_info');
122
+ $emailInfo->addTo($customer->getEmail(), $customer->getName());
123
+ $mailer->addEmailInfo($emailInfo);
124
+
125
+ // Set all required params and send emails
126
+ $mailer->setSender(Mage::getStoreConfig($sender, $storeId));
127
+ $mailer->setStoreId($storeId);
128
+ $mailer->setTemplateId(Mage::getStoreConfig($template, $storeId));
129
+ $mailer->setTemplateParams($templateParams);
130
+ $mailer->send();
131
+ return $this;
132
+ }
133
+ }
app/code/community/Diglin/Github/Model/Adapter/Oauth2.php ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Diglin
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ *
12
+ *
13
+ * @category Diglin
14
+ * @package Diglin_Github
15
+ * @copyright Copyright (c) 2011-2013 Diglin (http://www.diglin.com)
16
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
+ */
18
+
19
+ // Light Oauth2 Library located to /lib/Oauth2 of Magento installation
20
+ require_once 'Oauth2/Client.php';
21
+ require_once 'Oauth2/GrantType/IGrantType.php';
22
+ require_once 'Oauth2/GrantType/AuthorizationCode.php';
23
+
24
+ class Diglin_Github_Model_Adapter_Oauth2 extends OAuth2\Client
25
+ {
26
+ const AUTHORIZATION_ENDPOINT = 'https://github.com/login/oauth/authorize';
27
+ const TOKEN_ENDPOINT = 'https://github.com/login/oauth/access_token';
28
+ const USER_PROFILE = 'https://api.github.com/user';
29
+
30
+ public function __construct ($clientId = null, $clientSecret = null, $clientAuth = self::AUTH_TYPE_URI, $certificateFile = null)
31
+ {
32
+ if (empty($clientId)) {
33
+ $clientId = Mage::getStoreConfig('github/config/client_id');
34
+ }
35
+
36
+ if (empty($clientSecret)) {
37
+ $clientSecret = Mage::helper('core')->decrypt(Mage::getStoreConfig('github/config/client_secret'));
38
+ }
39
+
40
+ return parent::__construct($clientId, $clientSecret, $clientAuth, $certificateFile);
41
+ }
42
+
43
+ public function getRedirectUri ()
44
+ {
45
+ return Mage::getUrl('github/account/login');
46
+ }
47
+ }
app/code/community/Diglin/Github/Model/Confirmation.php ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Diglin
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ *
12
+ *
13
+ * @category Diglin
14
+ * @package Diglin_Github
15
+ * @copyright Copyright (c) 2011-2013 Diglin (http://www.diglin.com)
16
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
+ */
18
+ class Diglin_Github_Model_Confirmation extends Mage_Core_Model_Abstract
19
+ {
20
+ protected function _construct()
21
+ {
22
+ $this->_init('github/confirmation');
23
+ }
24
+
25
+ protected function _beforeSave()
26
+ {
27
+ if (!$this->getId() || $this->isObjectNew()) {
28
+ $this->setCreatedAt(now());
29
+ }
30
+ return parent::_beforeSave();
31
+ }
32
+ }
app/code/community/Diglin/Github/Model/Log.php ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Diglin
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ *
12
+ *
13
+ * @category Diglin
14
+ * @package Diglin_Github
15
+ * @copyright Copyright (c) 2011-2013 Diglin (http://www.diglin.com)
16
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
+ */
18
+ class Diglin_Github_Model_Log
19
+ {
20
+ public static function log($message, $level = null, $forceLog = false)
21
+ {
22
+ Mage::log($message, $level, 'github.log', $forceLog);
23
+ }
24
+ }
app/code/community/Diglin/Github/Model/Observer.php ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Diglin
4
+ *
5
+ * @category Diglin
6
+ * @package Diglin_Github
7
+ * @copyright Copyright (c) 2011-2013 Diglin (http://www.diglin.com)
8
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
9
+ */
10
+ class Diglin_Github_Model_Observer extends Mage_Customer_Model_Observer
11
+ {
12
+ /**
13
+ * Add on the fly the username attribute to the customer collection
14
+ *
15
+ * Event: eav_collection_abstract_load_before
16
+ *
17
+ * @param Varien_Event_Observer $observer
18
+ */
19
+ public function addAttributeToCollection ($observer)
20
+ {
21
+ /* @var $collection Mage_Eav_Model_Entity_Collection_Abstract */
22
+ $collection = $observer->getEvent()->getCollection();
23
+ if ($collection->getEntity()->getType() == 'customer') {
24
+ $collection->addAttributeToSelect('github_login');
25
+ }
26
+
27
+ }
28
+ }
app/code/community/Diglin/Github/Model/Resource/Confirmation.php ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Diglin
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ *
12
+ *
13
+ * @category Diglin
14
+ * @package Diglin_Github
15
+ * @copyright Copyright (c) 2011-2013 Diglin (http://www.diglin.com)
16
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
+ */
18
+ class Diglin_Github_Model_Resource_Confirmation extends Mage_Core_Model_Resource_Db_Abstract
19
+ {
20
+ /**
21
+ * Initialize resource model
22
+ *
23
+ */
24
+ protected function _construct()
25
+ {
26
+ $this->_init('github/confirmation', 'confirm_id');
27
+ }
28
+ }
app/code/community/Diglin/Github/Model/Resource/Confirmation/Collection.php ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Diglin
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ *
12
+ *
13
+ * @category Diglin
14
+ * @package Diglin_Github
15
+ * @copyright Copyright (c) 2011-2013 Diglin (http://www.diglin.com)
16
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
+ */
18
+ class Diglin_Github_Model_Resource_Confirmation_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
19
+ {
20
+ protected function _construct()
21
+ {
22
+ $this->_init('github/confirmation');
23
+ $this->_map['fields']['confirm_id'] = 'main_table.confirm_id';
24
+ }
25
+ }
app/code/community/Diglin/Github/Model/Resource/Setup.php ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Diglin
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ *
12
+ *
13
+ * @category Diglin
14
+ * @package Diglin_Github
15
+ * @copyright Copyright (c) 2011-2013 Diglin (http://www.diglin.com)
16
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
+ */
18
+
19
+ class Diglin_Github_Model_Resource_Setup extends Mage_Eav_Model_Entity_Setup {}
app/code/community/Diglin/Github/Model/System/Config/Backend/Oauth.php ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Diglin
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ *
12
+ *
13
+ * @category Diglin
14
+ * @package Diglin_Github
15
+ * @copyright Copyright (c) 2011-2013 Diglin (http://www.diglin.com)
16
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
+ */
18
+ class Diglin_Github_Model_System_Config_Backend_Oauth extends Mage_Core_Model_Config_Data
19
+ {
20
+ /**
21
+ * Cache tags to clean
22
+ *
23
+ * @var array
24
+ */
25
+ protected $_cacheTags = array(
26
+ Mage_Core_Model_Store::CACHE_TAG,
27
+ Mage_Cms_Model_Block::CACHE_TAG,
28
+ Mage_Core_Model_Config::CACHE_TAG
29
+ );
30
+ }
app/code/community/Diglin/Github/controllers/AccountController.php ADDED
@@ -0,0 +1,392 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Diglin
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ *
12
+ *
13
+ * @category Diglin
14
+ * @package Diglin_Github
15
+ * @copyright Copyright (c) 2011-2013 Diglin (http://www.diglin.com)
16
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
+ */
18
+
19
+ require_once 'Mage/Customer/controllers/AccountController.php';
20
+
21
+ class Diglin_Github_AccountController extends Mage_Customer_AccountController
22
+ {
23
+ const XML_PATH_GITHUB_EXISTING_CUSTOMER_LINK_EMAIL_TEMPLATE = 'github/link_account/email_template';
24
+ const XML_PATH_GITHUB_EXISTING_CUSTOMER_LINK_EMAIL_ENTITY = 'github/link_account/email_identity';
25
+
26
+ /**
27
+ * Action list where need check enabled cookie
28
+ *
29
+ * @var array
30
+ */
31
+ protected $_cookieCheckActions = array('createpost');
32
+
33
+ /**
34
+ * Action predispatch
35
+ *
36
+ * Check customer authentication for some actions
37
+ */
38
+ public function preDispatch()
39
+ {
40
+ parent::preDispatch();
41
+
42
+ // The parent may block the dispatch
43
+ $this->setFlag('', 'no-dispatch', false);
44
+
45
+ if (!$this->getRequest()->isDispatched()) {
46
+ return;
47
+ }
48
+
49
+ $action = $this->getRequest()->getActionName();
50
+ $openActions = array(
51
+ 'create',
52
+ 'login',
53
+ 'existing',
54
+ 'confirmation'
55
+ );
56
+ $pattern = '/^(' . implode('|', $openActions) . ')/i';
57
+
58
+ if (!preg_match($pattern, $action)) {
59
+ if (!$this->_getSession()->authenticate($this)) {
60
+ $this->setFlag('', 'no-dispatch', true);
61
+ }
62
+ } else {
63
+ $this->_getSession()->setNoReferer(true);
64
+ }
65
+ }
66
+
67
+ /**
68
+ * Retrieve customer session model object
69
+ *
70
+ * @return Mage_Customer_Model_Session
71
+ */
72
+ protected function _getSession ()
73
+ {
74
+ return Mage::getSingleton('customer/session');
75
+ }
76
+
77
+ public function indexAction()
78
+ {
79
+ $this->_forward('login');
80
+ }
81
+
82
+ public function loginAction ()
83
+ {
84
+ /* @var $helper Diglin_Github_Helper_Data */
85
+ $helper = Mage::helper('github');
86
+ $code = $this->getRequest()->getParam('code');
87
+ $returnedState = $this->getRequest()->getParam('state');
88
+
89
+ // Optional by the Github API, we use it to prevent man-in-the-middle attack
90
+ $state = $this->_getSession()->getData('state');
91
+ if (empty($state)) {
92
+ $state = Mage::helper('core')->getRandomString(20);
93
+ $this->_getSession()->setData('state', $state);
94
+ }
95
+
96
+ /* @var $oauth Diglin_Github_Model_Adapter_Oauth2 */
97
+ $oauth = Mage::getModel('github/adapter_oauth2');
98
+
99
+ // - Get code access from Github after user login and acceptation on Github
100
+ // - Scope value can be changed if we want to add features in future
101
+ // (possible values with separated comma: user, user:email, user:follow, public_repo, repo, repo:status, delete_repo, notifications, gist)
102
+
103
+ // 1) First, login the user via Github
104
+ if (empty($code)) {
105
+ $authentificationUrl = $oauth->getAuthenticationUrl($oauth::AUTHORIZATION_ENDPOINT, $oauth->getRedirectUri(), array(
106
+ 'state' => $state,
107
+ 'scope' => 'user:email'
108
+ ));
109
+ $this->_redirectUrl($authentificationUrl);
110
+ return;
111
+ }
112
+
113
+ // Check Man-In-The-Middle attack
114
+ if ($returnedState != $state) {
115
+ $this->_getSession()->addError($this->__('Sorry, we cannot accept this authentification. A problem occur during the communication with Github. Please, contact us.'));
116
+ Diglin_Github_Model_Log::log($this->__('Possible Hacker Attack during Github Autorization Process. Code: %s - State Returned: %s - State generated: %s', $code, $returnedState, $state), Zend_Log::ERR);
117
+ $this->_redirect('customer/account/login', array('_secure' => true));
118
+ return;
119
+ }
120
+
121
+ $params = array(
122
+ 'code' => $code,
123
+ 'redirect_uri' => $oauth->getRedirectUri(),// mandatory even if not used
124
+ 'state' => $state
125
+ );
126
+
127
+ // 2) Get Github Access Token to allow further API call
128
+ $response = $oauth->getAccessToken($oauth::TOKEN_ENDPOINT, 'authorization_code', $params);
129
+ parse_str($response['result'], $info);
130
+
131
+ if ($response['code'] != 200 || isset($info['error'])) {
132
+ $this->_getSession()->addError($this->__('A Problem occured while trying to get Github Authorization.'));
133
+ Diglin_Github_Model_Log::log('Github Authorization problem ' . print_r($response, true) . print_r($params, true));
134
+ $this->_redirect('customer/account/login', array('_secure' => true));
135
+ return;
136
+ }
137
+
138
+ $token = '';
139
+ if (!empty($info['access_token'])) {
140
+ $helper->setAccessToken($info['access_token']);
141
+ $token = $info['access_token'];
142
+ } else if ($helper->getAccessToken()) {
143
+ $token = $helper->getAccessToken();
144
+ }
145
+
146
+ // 3) Get user data, save it in session to use it later
147
+ $oauth->setAccessToken($token);
148
+ $response = $oauth->fetch($oauth::USER_PROFILE, array(), $oauth::HTTP_METHOD_GET, array('User-Agent' => $_SERVER["HTTP_USER_AGENT"]));// USER AGENT Required by Github
149
+
150
+ if (empty($response['result']['id'])) {
151
+ $this->_getSession()->addError($this->__("You don't seem to be logged in Github. Please, login again."));
152
+ Diglin_Github_Model_Log::log('Github Login problem ' . print_r($response, true));
153
+ $this->_redirect('customer/account/login', array('_secure' => true));
154
+ return;
155
+ } else {
156
+ $githubId = $response['result']['id'];
157
+ }
158
+
159
+ //Diglin_Github_Model_Log::log('Github Login Debug ' . print_r($response, true));
160
+
161
+ // Save github user data in session
162
+ $helper->setUserData($response['result']);
163
+
164
+ $customer = $helper->getCustomer($githubId);
165
+ if ($customer->getId()) {
166
+ $this->_getSession()->setCustomerAsLoggedIn($customer);
167
+ $this->_loginPostRedirect();
168
+ } else {
169
+ $this->_redirect('github/account/create', array('_secure' => true));
170
+ }
171
+ }
172
+
173
+ public function createAction ()
174
+ {
175
+ $session = $this->_getSession();
176
+ if ($session->isLoggedIn()) {
177
+ $this->_redirect('customer/account/index');
178
+ return;
179
+ }
180
+
181
+ /* @var $helper Diglin_Github_Helper_Data */
182
+ $helper = Mage::helper('github');
183
+
184
+ $token = $helper->getAccessToken();
185
+ $githubData = $helper->getUserData('github_data');
186
+
187
+ if (empty($token) || empty($githubData)) {
188
+ $session->addNotice($this->__('Please, login in Github before to try to register.'));
189
+ $this->_redirect('customer/account/login', array('_secure' => true));
190
+ return;
191
+ }
192
+
193
+ $email = '';
194
+ $firstname= '';
195
+ $lastname = '';
196
+
197
+ if ($githubData->getName()) {
198
+ $name = explode(' ', $githubData->getName(), 2);
199
+ // We have no clue of the order of the lastname or firstname, so hazard...
200
+ $firstname = $name[0];
201
+ $lastname = $name[1];
202
+ }
203
+
204
+ if ($githubData->getEmail()) {
205
+ $email = $githubData->getEmail();
206
+ }
207
+
208
+ $post = $this->getRequest()->getPost();
209
+ $post = array_merge($post, array(
210
+ 'github_id' => $githubData['id'],
211
+ 'github_login' => $githubData['login'],
212
+ 'lastname' => $lastname,
213
+ 'firstname' => $firstname,
214
+ 'email' => $email
215
+ ));
216
+
217
+ $session->setCustomerFormData($post);
218
+
219
+ $this->loadLayout();
220
+ $this->_initLayoutMessages('customer/session');
221
+ $this->renderLayout();
222
+ }
223
+
224
+ public function createPostAction ()
225
+ {
226
+ $session = $this->_getSession();
227
+ if ($session->isLoggedIn()) {
228
+ $this->_redirect('customer/account/index');
229
+ return;
230
+ }
231
+ $session->setEscapeMessages(true); // prevent XSS injection in user input
232
+
233
+ /* @var $helper Diglin_Github_Helper_Data */
234
+ $helper = Mage::helper('github');
235
+
236
+ $data = $this->getRequest()->getParams();
237
+
238
+ if ($data) {
239
+ $githubData = $helper->getUserData();
240
+
241
+ if ($data['github_id'] != $githubData->getId()) {
242
+ $session->addError($this->__('Your Github information has not been found.'));
243
+ $this->_redirect('customer/account/login', array('_secure' => true));
244
+ return;
245
+ }
246
+
247
+ // We use information provided by Github API instead of the form in case of form data modification
248
+ $data ['github_id'] = $githubData->getId();
249
+ $data ['github_login'] = $githubData->getLogin();
250
+
251
+ $customer = Mage::getModel('customer/customer');
252
+
253
+ // We generate a password to prevent customer validation error
254
+ $password = $customer->generatePassword();
255
+ $customer->setPassword($password)
256
+ ->setConfirmation($password);
257
+
258
+ $customer->addData($data);
259
+
260
+ try {
261
+ $customer->save();
262
+
263
+ Mage::dispatchEvent('customer_register_success',
264
+ array('account_controller' => $this, 'customer' => $customer)
265
+ );
266
+
267
+ if ($customer->getId()) {
268
+ $session->setCustomerAsLoggedIn($customer);
269
+ $this->_getSession()->addSuccess(
270
+ $this->__('Thank you for registering with %s.', Mage::app()->getStore()->getFrontendName())
271
+ );
272
+
273
+ $successUrl = Mage::getUrl('customer/account/index', array('_secure'=>true));
274
+ $this->_loginPostRedirect();
275
+ return;
276
+ }
277
+
278
+ } catch (Mage_Core_Exception $e) {
279
+ $session->setCustomerFormData($this->getRequest()->getPost());
280
+ if ($e->getCode() === Mage_Customer_Model_Customer::EXCEPTION_EMAIL_EXISTS) {
281
+ $url = Mage::getUrl('customer/account/forgotpassword');
282
+ $message = $this->__('There is already an account with this email address. If you are sure that it is your email address, <a href="%s">click here</a> to get your password and access your account.', $url);
283
+ $session->setEscapeMessages(false);
284
+ } else {
285
+ $message = $e->getMessage();
286
+ }
287
+ $session->addError($message);
288
+ } catch (Exception $e) {
289
+ $session->setCustomerFormData($this->getRequest()->getPost())
290
+ ->addException($e, $this->__('Cannot save the customer.'));
291
+ }
292
+ }
293
+
294
+ $this->_redirectError(Mage::getUrl('*/*/create', array('_secure' => true)));
295
+ }
296
+
297
+ public function existingAction()
298
+ {
299
+ $customerEmail = $this->getRequest()->getParam('email');
300
+ $githubId = (int) $this->getRequest()->getParam('github_id');
301
+ $githubLogin = $this->getRequest()->getParam('github_login');
302
+ $helper = Mage::helper('github');
303
+
304
+ if (!Zend_Validate::is($customerEmail, 'EmailAddress')) {
305
+ $this->_getSession()->addError($this->__('Email Address is not valid!'));
306
+ $this->_redirect('*/*/create');
307
+ return;
308
+ }
309
+
310
+ $customer = Mage::getModel('customer/customer')
311
+ ->setWebsiteId(Mage::app()->getWebsite()->getId())
312
+ ->loadByEmail($customerEmail);
313
+
314
+ if ($customer->getId() && !$customer->getGithubLogin()) {
315
+
316
+ /* @var $confirmation Diglin_Github_Model_Confirmation */
317
+ $confirmation = Mage::getModel('github/confirmation');
318
+
319
+ $confirmation->load($customer->getId(), 'customer_id');
320
+
321
+ if ($confirmation->getId()) {
322
+ $confirmation->delete();
323
+ $confirmation->unsetData();
324
+ }
325
+
326
+ try {
327
+ $confirmation
328
+ ->setKey(md5(uniqid()))
329
+ ->setGithubLogin($githubLogin)
330
+ ->setGithubId($githubId)
331
+ ->setCustomerId($customer->getId())
332
+ ->save();
333
+
334
+ $params = array(
335
+ 'github_confirmation' => $confirmation,
336
+ 'customer' => $customer,
337
+ 'back_url' => $this->_getSession()->getBeforeAuthUrl(),
338
+ );
339
+
340
+ $helper->sendEmailTemplate(self::XML_PATH_GITHUB_EXISTING_CUSTOMER_LINK_EMAIL_TEMPLATE, self::XML_PATH_GITHUB_EXISTING_CUSTOMER_LINK_EMAIL_ENTITY, $params, $customer);
341
+ $this->_getSession()->addSuccess($this->__('An email to confirm that this account belongs to you has been sent to your email address.'));
342
+ $this->_getSession()->addSuccess($this->__('Please, click on the link into the email to confirm the link between your Githug account and your shop\'s account'));
343
+ } catch (Exception $e) {
344
+ $this->_getSession()->addError($this->__('A problem occured while trying to send you the confirmation email. Please, contact us.'));
345
+ Mage::logException($e);
346
+ $this->_redirect('*/*/create');
347
+ return;
348
+ }
349
+ } else {
350
+ $this->_getSession()->addNotice($this->__('We are sorry, we didn\'t find your account in our database or your account is already linked to a Github account.'));
351
+ $this->_redirect('*/*/create');
352
+ return;
353
+ }
354
+
355
+ $this->_redirect('customer/account/login');
356
+ }
357
+
358
+ public function confirmationAction()
359
+ {
360
+ $confirmationKey = substr($this->getRequest()->getParam('key'), 0, 255);
361
+ //$backUrl = $this->getRequest()->getParam('back_url');
362
+ $customerId = (int)$this->getRequest()->getParam('id');
363
+
364
+ $customer = Mage::getModel('customer/customer')->load($customerId);
365
+ $confirmation = Mage::getModel('github/confirmation')->load($confirmationKey, 'key');
366
+
367
+ if ($customer->getId() && !$customer->getGithubLogin() && $confirmation->getId()) {
368
+ try {
369
+ $customer->setGithubId($confirmation->getGithubId())
370
+ ->setGithubLogin($confirmation->getGithubLogin())
371
+ ->save();
372
+
373
+ $confirmation->delete();
374
+
375
+ $this->_getSession()->setCustomerAsLoggedIn($customer);
376
+
377
+ $this->_getSession()->addSuccess($this->__('Congratulations! You are member of our private club :-)'));
378
+ } catch (Exception $e) {
379
+ $this->_getSession()->addError($this->__('A problem occured while trying to save your information.'));
380
+ Mage::logException($e);
381
+ $this->_redirect('customer/account/login');
382
+ return;
383
+ }
384
+ } else {
385
+ $this->_getSession()->addError($this->__('Sorry, information sent are invalid! Try again to login with your Github account and to recreate the linkage.'));
386
+ $this->_redirect('customer/account/login');
387
+ return;
388
+ }
389
+
390
+ $this->_redirect('customer/account/index');
391
+ }
392
+ }
app/code/community/Diglin/Github/etc/adminhtml.xml ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <config>
3
+ <acl>
4
+ <resources>
5
+ <all>
6
+ <title>Allow Everything</title>
7
+ </all>
8
+ <admin>
9
+ <children>
10
+ <system>
11
+ <children>
12
+ <config>
13
+ <children>
14
+ <github translate="title">
15
+ <title>Diglin - Github Configuration</title>
16
+ </github>
17
+ </children>
18
+ </config>
19
+ </children>
20
+ </system>
21
+ </children>
22
+ </admin>
23
+ </resources>
24
+ </acl>
25
+ </config>
app/code/community/Diglin/Github/etc/config.xml ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <config>
3
+ <modules>
4
+ <Diglin_Github>
5
+ <version>1.1.0</version>
6
+ </Diglin_Github>
7
+ </modules>
8
+
9
+ <default>
10
+ <github>
11
+ <config>
12
+ <enabled>0</enabled>
13
+ </config>
14
+ <link_account>
15
+ <email_identity>general</email_identity>
16
+ <email_template>github_existing_customer_link_email_template</email_template>
17
+ </link_account>
18
+ </github>
19
+ </default>
20
+
21
+
22
+ <global>
23
+ <fieldsets>
24
+ <customer_account>
25
+ <github_login>
26
+ <create>1</create>
27
+ <update>1</update>
28
+ </github_login>
29
+ </customer_account>
30
+ </fieldsets>
31
+
32
+ <blocks>
33
+ <github>
34
+ <class>Diglin_Github_Block</class>
35
+ </github>
36
+ <adminhtml>
37
+ <rewrite>
38
+ <customer_grid>Diglin_Github_Block_Adminhtml_Grid</customer_grid>
39
+ </rewrite>
40
+ </adminhtml>
41
+ </blocks>
42
+
43
+ <helpers>
44
+ <github>
45
+ <class>Diglin_Github_Helper</class>
46
+ </github>
47
+ </helpers>
48
+
49
+ <models>
50
+ <github>
51
+ <class>Diglin_Github_Model</class>
52
+ <resourceModel>github_resource</resourceModel>
53
+ </github>
54
+ <github_resource>
55
+ <class>Diglin_Github_Model_Resource</class>
56
+ <entities>
57
+ <confirmation>
58
+ <table>github_confirmation</table>
59
+ </confirmation>
60
+ </entities>
61
+ </github_resource>
62
+ </models>
63
+
64
+ <resources>
65
+ <github_setup>
66
+ <setup>
67
+ <module>Diglin_Github</module>
68
+ <class>Diglin_Github_Model_Resource_Setup</class>
69
+ </setup>
70
+ </github_setup>
71
+ </resources>
72
+
73
+ <template>
74
+ <email>
75
+ <github_existing_customer_link_email_template translate="label" module="github">
76
+ <label>Github Link Account Confirmation</label>
77
+ <file>github/existing_customer_link_confirmation.html</file>
78
+ <type>html</type>
79
+ </github_existing_customer_link_email_template>
80
+ </email>
81
+ </template>
82
+ </global>
83
+
84
+ <frontend>
85
+ <layout>
86
+ <updates>
87
+ <github>
88
+ <file>github.xml</file>
89
+ </github>
90
+ </updates>
91
+ </layout>
92
+
93
+ <routers>
94
+ <github>
95
+ <use>standard</use>
96
+ <args>
97
+ <module>Diglin_Github</module>
98
+ <frontName>github</frontName>
99
+ </args>
100
+ </github>
101
+ </routers>
102
+
103
+ <translate>
104
+ <modules>
105
+ <github>
106
+ <files>
107
+ <default>Diglin_Github.csv</default>
108
+ </files>
109
+ </github>
110
+ </modules>
111
+ </translate>
112
+ </frontend>
113
+
114
+ <adminhtml>
115
+ <events>
116
+ <eav_collection_abstract_load_before>
117
+ <observers>
118
+ <github_load_collection>
119
+ <class>github/observer</class>
120
+ <method>addAttributeToCollection</method>
121
+ </github_load_collection>
122
+ </observers>
123
+ </eav_collection_abstract_load_before>
124
+ </events>
125
+ </adminhtml>
126
+ </config>
app/code/community/Diglin/Github/etc/system.xml ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <config>
3
+ <tabs>
4
+ <diglin>
5
+ <label>Diglin</label>
6
+ <sort_order>400</sort_order>
7
+ </diglin>
8
+ </tabs>
9
+
10
+ <sections>
11
+ <github module="github">
12
+ <label>Github</label>
13
+ <tab>diglin</tab>
14
+ <frontend_type>text</frontend_type>
15
+ <sort_order>110</sort_order>
16
+ <show_in_default>1</show_in_default>
17
+ <show_in_website>1</show_in_website>
18
+ <show_in_store>1</show_in_store>
19
+ <groups>
20
+ <config>
21
+ <label>Global Configuration</label>
22
+ <frontend_type>text</frontend_type>
23
+ <sort_order>10</sort_order>
24
+ <show_in_default>1</show_in_default>
25
+ <show_in_website>1</show_in_website>
26
+ <show_in_store>1</show_in_store>
27
+ <comment><![CDATA[First, register your application on <a href="https://github.com/settings/applications/new" target="_blank" title="Got to Github Website">Github</a>. Provde during the application creation, as callback url, the url of your Magento Shop.]]></comment>
28
+ <fields>
29
+ <enabled>
30
+ <label>Oauth Authentification</label>
31
+ <frontend_type>select</frontend_type>
32
+ <source_model>adminhtml/system_config_source_enabledisable</source_model>
33
+ <backend_model>github/system_config_backend_oauth</backend_model>
34
+ <sort_order>10</sort_order>
35
+ <show_in_default>1</show_in_default>
36
+ <show_in_website>1</show_in_website>
37
+ <show_in_store>1</show_in_store>
38
+ </enabled>
39
+ <client_id>
40
+ <label>Client ID</label>
41
+ <frontend_type>text</frontend_type>
42
+ <sort_order>20</sort_order>
43
+ <show_in_default>1</show_in_default>
44
+ <show_in_website>1</show_in_website>
45
+ <show_in_store>1</show_in_store>
46
+ <comment><![CDATA[Client ID that you can get from your Github account after to have registered your application]]></comment>
47
+ </client_id>
48
+ <client_secret>
49
+ <label>Client Secret</label>
50
+ <frontend_type>obscure</frontend_type>
51
+ <backend_model>adminhtml/system_config_backend_encrypted</backend_model>
52
+ <sort_order>30</sort_order>
53
+ <show_in_default>1</show_in_default>
54
+ <show_in_website>1</show_in_website>
55
+ <show_in_store>1</show_in_store>
56
+ <comment><![CDATA[Client Secret that you can get from your Github account after to have registered your application]]></comment>
57
+ </client_secret>
58
+ <grid>
59
+ <label><![CDATA[Display Github Username in "Manage Customers" Grid]]></label>
60
+ <frontend_type>select</frontend_type>
61
+ <source_model>adminhtml/system_config_source_yesno</source_model>
62
+ <sort_order>40</sort_order>
63
+ <show_in_default>1</show_in_default>
64
+ <comment><![CDATA[Do you want to display the field 'Github Username' in the Manage Customer Grid?]]></comment>
65
+ </grid>
66
+ </fields>
67
+ </config>
68
+ </groups>
69
+ </github>
70
+ </sections>
71
+ </config>
app/code/community/Diglin/Github/sql/github_setup/install-1.0.0.0.php ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Diglin
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ *
12
+ *
13
+ * @category Diglin
14
+ * @package Diglin_Github
15
+ * @copyright Copyright (c) 2011-2013 Diglin (http://www.diglin.com)
16
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
+ */
18
+
19
+ /* @var $installer Diglin_Github_Model_Resource_Setup */
20
+ $installer = $this;
21
+ $installer->startSetup();
22
+
23
+ //$entityTypeId = $installer->getEntityType('customer')->getEntityTypeId();
24
+ $installer->addAttribute('customer', 'github_id', array(
25
+ 'type' => 'int',
26
+ 'input' => 'text',
27
+ 'label' => 'Github ID',
28
+ 'required' => false,
29
+ 'user_defined' => false,
30
+ 'default' => '',
31
+ 'unique' => true,
32
+ 'note' => 'Github ID provided by Github API after first login. Please, don\'t change it or delete it if you want to delete the login',
33
+ 'visible' => false,
34
+ ));
35
+
36
+ $installer->addAttribute('customer', 'github_login', array(
37
+ 'type' => 'varchar',
38
+ 'input' => 'text',
39
+ 'label' => 'Github Login',
40
+ 'required' => false,
41
+ 'user_defined' => false,
42
+ 'default' => '',
43
+ 'unique' => true,
44
+ 'note' => 'Github Login provided by Github API after first login',
45
+ 'visible' => true
46
+ ));
47
+
48
+ Mage::getSingleton('eav/config')
49
+ ->getAttribute('customer', 'github_login')
50
+ ->setData('used_in_forms', array('adminhtml_customer','customer_account_edit', 'customer_account_create'))
51
+ ->save();
52
+
53
+ Mage::getSingleton('eav/config')
54
+ ->getAttribute('customer', 'github_id')
55
+ ->setData('used_in_forms', array('adminhtml_customer'))
56
+ ->save();
57
+
58
+ $table = $installer->getConnection()
59
+ ->newTable($installer->getTable('github/confirmation'))
60
+ ->addColumn('confirm_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
61
+ 'identity' => true,
62
+ 'nullable' => false,
63
+ 'primary' => true,
64
+ ), 'Confirmation ID')
65
+ ->addColumn('customer_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
66
+ 'nullable' => false,
67
+ 'unsigned' => true,
68
+ ), 'Customer Id')
69
+ ->addColumn('github_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
70
+ 'nullable' => false,
71
+ 'unsigned' => true,
72
+ ), 'Github Id')
73
+ ->addColumn('github_login', Varien_Db_Ddl_Table::TYPE_VARCHAR, 255, array(
74
+ 'nullable' => false,
75
+ ), 'Github Username')
76
+ ->addColumn('key', Varien_Db_Ddl_Table::TYPE_VARCHAR, 255, array(
77
+ 'nullable' => false,
78
+ ), 'Github Confirmation Key')
79
+ ->addColumn('created_at', Varien_Db_Ddl_Table::TYPE_TIMESTAMP, null, array(
80
+ ), 'Confirmation Creation Time')
81
+ ->addForeignKey($installer->getFkName('github/confirmation', 'customer_id', 'customer/entity', 'entity_id'),
82
+ 'customer_id', $installer->getTable('customer/entity'), 'entity_id',
83
+ Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
84
+ ->setComment('Github Confirmation Table');
85
+
86
+ $installer->getConnection()->createTable($table);
87
+
88
+ $installer->endSetup();
app/design/frontend/base/default/layout/github.xml ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!--
3
+ /**
4
+ * Diglin
5
+ *
6
+ * NOTICE OF LICENSE
7
+ *
8
+ * This source file is subject to the Open Software License (OSL 3.0)
9
+ * that is bundled with this package in the file LICENSE.txt.
10
+ * It is also available through the world-wide-web at this URL:
11
+ * http://opensource.org/licenses/osl-3.0.php
12
+ *
13
+ *
14
+ * @category Diglin
15
+ * @package Diglin_Github
16
+ * @copyright Copyright (c) 2011-2013 Diglin (http://www.diglin.com)
17
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
18
+ */
19
+ -->
20
+ <layout>
21
+ <github_account_create>
22
+ <label>Github Customer Account Registration Form</label>
23
+ <remove name="right" />
24
+ <remove name="left" />
25
+
26
+ <reference name="root">
27
+ <action method="setTemplate">
28
+ <template>page/1column.phtml</template>
29
+ </action>
30
+ </reference>
31
+
32
+ <reference name="content">
33
+ <block type="github/account_register" name="github_account_register" as="github.account.register" template="github/customer/create.phtml">
34
+ <block type="page/html_wrapper" name="customer.form.register.fields.before" as="form_fields_before" translate="label">
35
+ <label>Form Fields Before</label>
36
+ </block>
37
+ <block type="page/html_wrapper" name="customer.form.register.fields.after" as="form_fields_after" translate="label">
38
+ <label>Form Fields After</label>
39
+ <block type="github/account_existing" name="github_existing_user" as="github.existing.user" template="github/customer/existing.phtml" />
40
+ </block>
41
+ </block>
42
+ </reference>
43
+ </github_account_create>
44
+
45
+ <customer_account_login>
46
+ <reference name="form.additional.info"><!-- Solution works for login with or w/o persistent module @see the login.phtml -->
47
+ <block type="core/template" name="login_github_button" as="login.github.button">
48
+ <block type="core/template" name="github_button" as="github.button" template="github/customer/button.phtml">
49
+ <action method="setClassAlign"><class>a-left</class></action>
50
+ </block>
51
+ <action method="setTemplate" ifconfig="github/config/enabled">
52
+ <template>github/customer/login.phtml</template>
53
+ </action>
54
+ </block>
55
+ </reference>
56
+ </customer_account_login>
57
+
58
+ <customer_account_create>
59
+ <reference name="form.additional.info">
60
+ <block type="core/template" name="register_github_information" as="register.github.information">
61
+ <action method="setTemplate" ifconfig="github/config/enabled">
62
+ <template>github/customer/fields.phtml</template>
63
+ </action>
64
+ </block>
65
+ </reference>
66
+ </customer_account_create>
67
+
68
+ <customer_account_index>
69
+ <reference name="customer_account_dashboard">
70
+ <block type="core/template" name="github_information" as="info1">
71
+ <action method="setTemplate" ifconfig="github/config/enabled">
72
+ <template>github/customer/info.phtml</template>
73
+ </action>
74
+ </block>
75
+ </reference>
76
+ </customer_account_index>
77
+
78
+ <checkout_onepage_index>
79
+ <reference name="checkout.onepage.login.before">
80
+ <block type="core/template" name="login_github_button" as="login.github.button">
81
+ <block type="core/template" name="github_button" as="github.button" template="github/customer/button.phtml">
82
+ <action method="setClassAlign"><class>a-left</class></action>
83
+ </block>
84
+ <action method="setTemplate" ifconfig="github/config/enabled">
85
+ <template>github/customer/login.phtml</template>
86
+ </action>
87
+ <action method="setHeader"><header>3</header></action>
88
+ </block>
89
+ </reference>
90
+
91
+ <!-- FIX ME The solution below works but you have to care about the captcha - dependency!!! -->
92
+ <!-- I tried to put the block login_github_button into the reference checkout.onepage.login > reference form.additional.info -->
93
+ <!-- but it put it into checkout.onepage.billing cause of how the layout generates blocks. The block form.additional.info is generated twice in the handle checkout_onepage_index -->
94
+ <!-- <reference name="checkout.onepage.login"> -->
95
+ <!-- <block type="core/text_list" name="form.additional.info"> -->
96
+ <!-- <block type="captcha/captcha" name="captcha"> -->
97
+ <!-- <reference name="head"> -->
98
+ <!-- <action method="addJs"><file>mage/captcha.js</file></action> -->
99
+ <!-- </reference> -->
100
+ <!-- <action method="setFormId"><formId>user_login</formId></action> -->
101
+ <!-- <action method="setImgWidth"><width>230</width></action> -->
102
+ <!-- <action method="setImgHeight"><width>50</width></action> -->
103
+ <!-- </block> -->
104
+ <!-- <block type="core/template" name="login_github_button" as="login.github.button"> -->
105
+ <!-- <action method="setTemplate" ifconfig="github/config/enabled"> -->
106
+ <!-- <template>github/customer/login.phtml</template> -->
107
+ <!-- </action> -->
108
+ <!-- </block> -->
109
+ <!-- </block> -->
110
+ <!-- </reference> -->
111
+ </checkout_onepage_index>
112
+ </layout>
app/design/frontend/base/default/template/github/customer/button.phtml ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Diglin
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ *
12
+ *
13
+ * @category Diglin
14
+ * @package Diglin_Github
15
+ * @copyright Copyright (c) 2011-2013 Diglin (http://www.diglin.com)
16
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
+ */
18
+ ?>
19
+ <div <?php echo ($this->getClassAlign())? 'class="' . $this->getClassAlign() . '"' : ''; ?>>
20
+ <a href="<?php echo $this->getUrl('github/account/login'); ?>"><img src="<?php echo $this->getSkinUrl('images/github/LoginWithGithub.png'); ?>" alt="<?php echo $this->__('Login via Github');?>" title="<?php echo $this->__('Login via Github');?>" /></a>
21
+ </div>
app/design/frontend/base/default/template/github/customer/create.phtml ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Diglin
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ *
12
+ *
13
+ * @category Diglin
14
+ * @package Diglin_Github
15
+ * @copyright Copyright (c) 2011-2013 Diglin (http://www.diglin.com)
16
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
+ */
18
+
19
+ /* @var $this Diglin_Github_Block_Account_Register */
20
+ ?>
21
+ <div class="account-create">
22
+ <div class="page-title"><h1><?php echo $this->__('Create an account thanks to your Github account') ?></h1></div>
23
+ <?php echo $this->getChildHtml('form_fields_before')?>
24
+ <?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
25
+ <?php /* Extensions placeholder */ ?>
26
+ <?php echo $this->getChildHtml('customer.form.register.extra')?>
27
+ <form action="<?php echo $this->getPostActionUrl() ?>" method="post" id="form-validate" name="github_customer_create">
28
+ <div class="fieldset">
29
+ <input type="hidden" name="github_id" id="github_id" value="<?php echo $this->escapeHtml($this->getFormData()->getGithubId()); ?>" />
30
+ <h2 class="legend"><?php echo $this->__('Personal Information') ?></h2>
31
+ <div class="content">
32
+ <p><?php echo $this->__('We need some information to help us to communicate with you. Please, fill in the required fields to create your account on our website.');?><br/>
33
+ <?php echo $this->__('We filled for you some information based on your public Github profile. No password necessary.');?></p>
34
+ </div>
35
+
36
+ <ul class="right">
37
+ <li>
38
+ <img src="<?php echo $this->getSkinUrl('images/github/octocat.png')?>" alt="<?php echo $this->__('Github Octocat');?>" title="<?php echo $this->__('Github Octocat');?>" style="width:250px;"/>
39
+ </li>
40
+ </ul>
41
+
42
+ <ul class="form-list left">
43
+ <li class="fields">
44
+ <label for="github_login"><?php echo $this->__('Github Username') ?></label>
45
+ <div class="input-box">
46
+ <input type="text" name="github_login" id="github_login" value="<?php echo $this->escapeHtml($this->getFormData()->getGithubLogin()) ?>" title="<?php echo $this->__('Github Username') ?>" disabled />
47
+ </div>
48
+ </li>
49
+ <li class="fields">
50
+ <?php echo $this->getLayout()->createBlock('customer/widget_name')->setObject($this->getFormData())->setForceUseCustomerAttributes(true)->toHtml() ?>
51
+ </li>
52
+ <li class="fields">
53
+ <label for="email_address" class="required"><em>*</em><?php echo $this->__('Email Address') ?></label>
54
+ <div class="input-box">
55
+ <input type="text" name="email" id="email_address" value="<?php echo $this->escapeHtml($this->getFormData()->getEmail()) ?>" title="<?php echo $this->__('Email Address') ?>" class="input-text validate-email required-entry" />
56
+ </div>
57
+ </li>
58
+ <?php if ($this->isNewsletterEnabled()): ?>
59
+ <li class="control">
60
+ <div class="input-box">
61
+ <input type="checkbox" name="is_subscribed" title="<?php echo $this->__('Sign Up for Newsletter') ?>" value="1" id="is_subscribed"<?php if($this->getFormData()->getIsSubscribed()): ?> checked="checked"<?php endif; ?> class="checkbox" />
62
+ </div>
63
+ <label for="is_subscribed"><?php echo $this->__('Sign Up for Newsletter') ?></label>
64
+ <?php /* Extensions placeholder */ ?>
65
+ <?php echo $this->getChildHtml('customer.form.register.newsletter')?>
66
+ </li>
67
+ <?php endif ?>
68
+ </ul>
69
+ <div class="clearer"></div>
70
+ </div>
71
+
72
+ <div class="buttons-set">
73
+ <p class="required"><?php echo $this->__('* Required Fields') ?></p>
74
+ <?php if (!$this->getChild('form_fields_after')): ?><p class="back-link"><a href="<?php echo $this->escapeUrl($this->getBackUrl()) ?>" class="back-link"><small>&laquo; </small><?php echo $this->__('Back') ?></a></p><?php endif; ?>
75
+ <button type="submit" title="<?php echo $this->__('Submit') ?>" class="button"><span><span><?php echo $this->__('Submit') ?></span></span></button>
76
+ </div>
77
+ </form>
78
+ <script type="text/javascript">
79
+ //<![CDATA[
80
+ var dataForm = new VarienForm('form-validate', true);
81
+ //]]>
82
+ </script>
83
+ <?php echo $this->getChildHtml('form_fields_after')?>
84
+ </div>
app/design/frontend/base/default/template/github/customer/existing.phtml ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Diglin
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ *
12
+ *
13
+ * @category Diglin
14
+ * @package Diglin_
15
+ * @copyright Copyright (c) 2011-2012 Diglin (http://www.diglin.com)
16
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
+ */
18
+
19
+ /* @var $this Mage_Core_Block_Template */
20
+ $grandParent = $this->getParentBlock()->getParentBlock();
21
+ ?>
22
+ <form action="<?php echo $this->getUrl('*/*/existing') ?>" method="post" id="form-existing-customer" name="github_existing_customer">
23
+ <div class="fieldset">
24
+ <input type="hidden" name="github_id" id="github_id" value="<?php echo $this->escapeHtml($grandParent->getFormData()->getGithubId()); ?>" />
25
+ <input type="hidden" name="github_login" id="github_login" value="<?php echo $this->escapeHtml($grandParent->getFormData()->getGithubLogin()) ?>" />
26
+ <h2 class="legend"><?php echo $this->__('Existing Customer') ?></h2>
27
+ <div class="content">
28
+ <p><?php echo $this->__('You already have an account but want to link your github account to your shop\'s account?');?><br/>
29
+ <?php echo $this->__('Just fill in the email address field, you will receive a confirmation email with a link to proove your ownership.');?></p>
30
+ </div>
31
+
32
+ <ul class="form-list left">
33
+ <li class="fields">
34
+ <label for="email_address" class="required"><em>*</em><?php echo $this->__('Email Address') ?></label>
35
+ <div class="input-box">
36
+ <input type="text" name="email" id="email_address" value="<?php echo $this->escapeHtml($grandParent->getFormData()->getEmail()) ?>" title="<?php echo $this->__('Email Address') ?>" class="input-text validate-email required-entry" />
37
+ </div>
38
+ </li>
39
+ </ul>
40
+ <div class="clearer"></div>
41
+ </div>
42
+
43
+ <div class="buttons-set">
44
+ <p class="required"><?php echo $this->__('* Required Fields') ?></p>
45
+ <p class="back-link"><a href="<?php echo $this->escapeUrl($this->getBackUrl()) ?>" class="back-link"><small>&laquo; </small><?php echo $this->__('Back') ?></a></p>
46
+ <button type="submit" title="<?php echo $this->__('Submit') ?>" class="button"><span><span><?php echo $this->__('Submit') ?></span></span></button>
47
+ </div>
48
+ <script type="text/javascript">
49
+ //<![CDATA[
50
+ var dataForm = new VarienForm('form-existing-customer', true);
51
+ //]]>
52
+ </script>
53
+ </form>
app/design/frontend/base/default/template/github/customer/fields.phtml ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Diglin
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ *
12
+ *
13
+ * @category Diglin
14
+ * @package Diglin_Github
15
+ * @copyright Copyright (c) 2011-2013 Diglin (http://www.diglin.com)
16
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
+ */
18
+ ?>
19
+ <li class="fields">
20
+ <div class="field">
21
+ <label for="github_login" class="required"><em>*</em><?php echo $this->__('Github Username') ?></label>
22
+ <div class="input-box">
23
+ <input type="text" name="github_login" id="github_login" title="<?php echo $this->__('Github Username') ?>" class="input-text required-entry" />
24
+ <p><?php echo $this->__("You can also <a href='%s'>login</a> with your Github account. You won't need to provide any password for our website and some information will be pre-filled for you.", $this->getUrl('github/account/login'));?></p>
25
+ </div>
26
+ </div>
27
+ </li>
app/design/frontend/base/default/template/github/customer/info.phtml ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Diglin
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ *
12
+ *
13
+ * @category Diglin
14
+ * @package Diglin_Github
15
+ * @copyright Copyright (c) 2011-2013 Diglin (http://www.diglin.com)
16
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
+ */
18
+ ?>
19
+ <div class="box-account box-info">
20
+ <div class="box-head">
21
+ <h2><?php echo $this->__('Additional Information') ?></h2>
22
+ </div>
23
+ <div class="col2-set">
24
+ <div class="col-1">
25
+ <div class="box">
26
+ <div class="box-title">
27
+ <h3><?php echo $this->__('Github Information') ?></h3>
28
+ </div>
29
+ <div class="box-content">
30
+ <p>
31
+ <?php echo $this->__('Username: ') . $this->htmlEscape(Mage::getSingleton('customer/session')->getCustomer()->getGithubLogin()) ?>
32
+ </p>
33
+ </div>
34
+ </div>
35
+ </div>
36
+ </div>
37
+ </div>
app/design/frontend/base/default/template/github/customer/login.phtml ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Diglin
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ *
12
+ *
13
+ * @category Diglin
14
+ * @package Diglin_Github
15
+ * @copyright Copyright (c) 2011-2013 Diglin (http://www.diglin.com)
16
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
+ */
18
+
19
+ /* @var $this Mage_Core_Block_Template */
20
+
21
+ $header = $this->getHeader();
22
+
23
+ if (empty($header)) {
24
+ $header = '2';
25
+ }
26
+ ?>
27
+ <div class="github-login">
28
+ <?php echo sprintf('<h%s>', $header) . $this->__('Login with your Github account.') . sprintf('</h%s>', $header) ; ?>
29
+ <?php echo $this->getChildHtml('github.button'); ?>
30
+ </div>
app/etc/modules/Diglin_Github.xml ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
1
+ <config>
2
+ <modules>
3
+ <Diglin_Github>
4
+ <active>true</active>
5
+ <codePool>community</codePool>
6
+ </Diglin_Github>
7
+ </modules>
8
+ </config>
app/locale/en_US/template/email/github/existing_customer_link_confirmation.html ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!--@subject Existing Customer - Account Github link confirmation for {{var customer.name}} @-->
2
+ <!--@styles
3
+ body,td { color:#2f2f2f; font:11px/1.35em Verdana, Arial, Helvetica, sans-serif; }
4
+ @-->
5
+
6
+ <body style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;">
7
+ <div style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;">
8
+ <table cellspacing="0" cellpadding="0" border="0" height="100%" width="100%">
9
+ <tr>
10
+ <td align="center" valign="top" style="padding:20px 0 20px 0">
11
+ <!-- [ header starts here] -->
12
+ <table bgcolor="FFFFFF" cellspacing="0" cellpadding="10" border="0" width="650" style="border:1px solid #E0E0E0;">
13
+ <tr>
14
+ <td valign="top">
15
+ <a href="{{store url=""}}"><img src="{{var logo_url}}" alt="{{var logo_alt}}" style="margin-bottom:10px;" border="0"/></a>
16
+ </td>
17
+ </tr>
18
+ <!-- [ middle starts here] -->
19
+ <tr>
20
+ <td valign="top">
21
+ <h1 style="font-size:22px; font-weight:normal; line-height:22px; margin:0 0 11px 0;">Dear {{htmlescape var=$customer.name}},</h1>
22
+ <p style="font-size:12px; line-height:16px; margin:0 0 8px 0;">To confirm that's this account belongs to you and to link it to your Github Account {{htmlescape var=$github_confirmation.github_login}}, please, use <a href="{{store url="github/account/confirmation/" _query_id=$customer.id _query_key=$github_confirmation.key _query_back_url=$back_url}}" style="color:#1E7EC8;">this confirmation link</a>. This link is valid only once.</p>
23
+ <p style="border:1px solid #E0E0E0; font-size:12px; line-height:16px; margin:0 0 16px 0; padding:13px 18px; background:#f9f9f9;">
24
+ <p style="font-size:12px; line-height:16px; margin:0;">If you have any questions about your account or any other matter, please feel free to contact us at <a href="mailto:{{config path='trans_email/ident_support/email'}}" style="color:#1E7EC8;">{{config path='trans_email/ident_support/email'}}</a> or by phone at {{config path='general/store_information/phone'}}.</p>
25
+ </td>
26
+ </tr>
27
+ <tr>
28
+ <td bgcolor="#EAEAEA" align="center" style="background:#EAEAEA; text-align:center;"><center><p style="font-size:12px; margin:0;">Thank you again, <strong>{{var store.getFrontendName()}}</strong></p></center></td>
29
+ </tr>
30
+ </table>
31
+ </td>
32
+ </tr>
33
+ </table>
34
+ </div>
35
+ </body>
lib/Oauth2/Client.php ADDED
@@ -0,0 +1,513 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Note : Code is released under the GNU LGPL
4
+ *
5
+ * Please do not change the header of this file
6
+ *
7
+ * This library is free software; you can redistribute it and/or modify it under the terms of the GNU
8
+ * Lesser General Public License as published by the Free Software Foundation; either version 2 of
9
+ * the License, or (at your option) any later version.
10
+ *
11
+ * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
+ *
14
+ * See the GNU Lesser General Public License for more details.
15
+ */
16
+
17
+ /**
18
+ * Light PHP wrapper for the OAuth 2.0 protocol.
19
+ *
20
+ * This client is based on the OAuth2 specification draft v2.15
21
+ * http://tools.ietf.org/html/draft-ietf-oauth-v2-15
22
+ *
23
+ * @author Pierrick Charron <pierrick@webstart.fr>
24
+ * @author Anis Berejeb <anis.berejeb@gmail.com>
25
+ * @version 1.2-dev
26
+ */
27
+ namespace OAuth2;
28
+
29
+ class Client
30
+ {
31
+ /**
32
+ * Different AUTH method
33
+ */
34
+ const AUTH_TYPE_URI = 0;
35
+ const AUTH_TYPE_AUTHORIZATION_BASIC = 1;
36
+ const AUTH_TYPE_FORM = 2;
37
+
38
+ /**
39
+ * Different Access token type
40
+ */
41
+ const ACCESS_TOKEN_URI = 0;
42
+ const ACCESS_TOKEN_BEARER = 1;
43
+ const ACCESS_TOKEN_OAUTH = 2;
44
+ const ACCESS_TOKEN_MAC = 3;
45
+
46
+ /**
47
+ * Different Grant types
48
+ */
49
+ const GRANT_TYPE_AUTH_CODE = 'authorization_code';
50
+ const GRANT_TYPE_PASSWORD = 'password';
51
+ const GRANT_TYPE_CLIENT_CREDENTIALS = 'client_credentials';
52
+ const GRANT_TYPE_REFRESH_TOKEN = 'refresh_token';
53
+
54
+ /**
55
+ * HTTP Methods
56
+ */
57
+ const HTTP_METHOD_GET = 'GET';
58
+ const HTTP_METHOD_POST = 'POST';
59
+ const HTTP_METHOD_PUT = 'PUT';
60
+ const HTTP_METHOD_DELETE = 'DELETE';
61
+ const HTTP_METHOD_HEAD = 'HEAD';
62
+
63
+ /**
64
+ * HTTP Form content types
65
+ */
66
+ const HTTP_FORM_CONTENT_TYPE_APPLICATION = 0;
67
+ const HTTP_FORM_CONTENT_TYPE_MULTIPART = 1;
68
+
69
+ /**
70
+ * Client ID
71
+ *
72
+ * @var string
73
+ */
74
+ protected $client_id = null;
75
+
76
+ /**
77
+ * Client Secret
78
+ *
79
+ * @var string
80
+ */
81
+ protected $client_secret = null;
82
+
83
+ /**
84
+ * Client Authentication method
85
+ *
86
+ * @var int
87
+ */
88
+ protected $client_auth = self::AUTH_TYPE_URI;
89
+
90
+ /**
91
+ * Access Token
92
+ *
93
+ * @var string
94
+ */
95
+ protected $access_token = null;
96
+
97
+ /**
98
+ * Access Token Type
99
+ *
100
+ * @var int
101
+ */
102
+ protected $access_token_type = self::ACCESS_TOKEN_URI;
103
+
104
+ /**
105
+ * Access Token Secret
106
+ *
107
+ * @var string
108
+ */
109
+ protected $access_token_secret = null;
110
+
111
+ /**
112
+ * Access Token crypt algorithm
113
+ *
114
+ * @var string
115
+ */
116
+ protected $access_token_algorithm = null;
117
+
118
+ /**
119
+ * Access Token Parameter name
120
+ *
121
+ * @var string
122
+ */
123
+ protected $access_token_param_name = 'access_token';
124
+
125
+ /**
126
+ * The path to the certificate file to use for https connections
127
+ *
128
+ * @var string Defaults to .
129
+ */
130
+ protected $certificate_file = null;
131
+
132
+ /**
133
+ * cURL options
134
+ *
135
+ * @var array
136
+ */
137
+ protected $curl_options = array();
138
+
139
+ /**
140
+ * Construct
141
+ *
142
+ * @param string $client_id Client ID
143
+ * @param string $client_secret Client Secret
144
+ * @param int $client_auth (AUTH_TYPE_URI, AUTH_TYPE_AUTHORIZATION_BASIC, AUTH_TYPE_FORM)
145
+ * @param string $certificate_file Indicates if we want to use a certificate file to trust the server. Optional, defaults to null.
146
+ * @return void
147
+ */
148
+ public function __construct($client_id, $client_secret, $client_auth = self::AUTH_TYPE_URI, $certificate_file = null)
149
+ {
150
+ if (!extension_loaded('curl')) {
151
+ throw new Exception('The PHP exention curl must be installed to use this library.', Exception::CURL_NOT_FOUND);
152
+ }
153
+
154
+ $this->client_id = $client_id;
155
+ $this->client_secret = $client_secret;
156
+ $this->client_auth = $client_auth;
157
+ $this->certificate_file = $certificate_file;
158
+ if (!empty($this->certificate_file) && !is_file($this->certificate_file)) {
159
+ throw new InvalidArgumentException('The certificate file was not found', InvalidArgumentException::CERTIFICATE_NOT_FOUND);
160
+ }
161
+ }
162
+
163
+ /**
164
+ * Get the client Id
165
+ *
166
+ * @return string Client ID
167
+ */
168
+ public function getClientId()
169
+ {
170
+ return $this->client_id;
171
+ }
172
+
173
+ /**
174
+ * Get the client Secret
175
+ *
176
+ * @return string Client Secret
177
+ */
178
+ public function getClientSecret()
179
+ {
180
+ return $this->client_secret;
181
+ }
182
+
183
+ /**
184
+ * getAuthenticationUrl
185
+ *
186
+ * @param string $auth_endpoint Url of the authentication endpoint
187
+ * @param string $redirect_uri Redirection URI
188
+ * @param array $extra_parameters Array of extra parameters like scope or state (Ex: array('scope' => null, 'state' => ''))
189
+ * @return string URL used for authentication
190
+ */
191
+ public function getAuthenticationUrl($auth_endpoint, $redirect_uri, array $extra_parameters = array())
192
+ {
193
+ $parameters = array_merge(array(
194
+ 'response_type' => 'code',
195
+ 'client_id' => $this->client_id,
196
+ 'redirect_uri' => $redirect_uri
197
+ ), $extra_parameters);
198
+ return $auth_endpoint . '?' . http_build_query($parameters, null, '&');
199
+ }
200
+
201
+ /**
202
+ * getAccessToken
203
+ *
204
+ * @param string $token_endpoint Url of the token endpoint
205
+ * @param int $grant_type Grant Type ('authorization_code', 'password', 'client_credentials', 'refresh_token', or a custom code (@see GrantType Classes)
206
+ * @param array $parameters Array sent to the server (depend on which grant type you're using)
207
+ * @return array Array of parameters required by the grant_type (CF SPEC)
208
+ */
209
+ public function getAccessToken($token_endpoint, $grant_type, array $parameters)
210
+ {
211
+ if (!$grant_type) {
212
+ throw new InvalidArgumentException('The grant_type is mandatory.', InvalidArgumentException::INVALID_GRANT_TYPE);
213
+ }
214
+ $grantTypeClassName = $this->convertToCamelCase($grant_type);
215
+ $grantTypeClass = __NAMESPACE__ . '\\GrantType\\' . $grantTypeClassName;
216
+ if (!class_exists($grantTypeClass)) {
217
+ throw new InvalidArgumentException('Unknown grant type \'' . $grant_type . '\'', InvalidArgumentException::INVALID_GRANT_TYPE);
218
+ }
219
+ $grantTypeObject = new $grantTypeClass();
220
+ $grantTypeObject->validateParameters($parameters);
221
+ if (!defined($grantTypeClass . '::GRANT_TYPE')) {
222
+ throw new Exception('Unknown constant GRANT_TYPE for class ' . $grantTypeClassName, Exception::GRANT_TYPE_ERROR);
223
+ }
224
+ $parameters['grant_type'] = $grantTypeClass::GRANT_TYPE;
225
+ $http_headers = array();
226
+ switch ($this->client_auth) {
227
+ case self::AUTH_TYPE_URI:
228
+ case self::AUTH_TYPE_FORM:
229
+ $parameters['client_id'] = $this->client_id;
230
+ $parameters['client_secret'] = $this->client_secret;
231
+ break;
232
+ case self::AUTH_TYPE_AUTHORIZATION_BASIC:
233
+ $parameters['client_id'] = $this->client_id;
234
+ $http_headers['Authorization'] = 'Basic ' . base64_encode($this->client_id . ':' . $this->client_secret);
235
+ break;
236
+ default:
237
+ throw new Exception('Unknown client auth type.', Exception::INVALID_CLIENT_AUTHENTICATION_TYPE);
238
+ break;
239
+ }
240
+
241
+ return $this->executeRequest($token_endpoint, $parameters, self::HTTP_METHOD_POST, $http_headers, self::HTTP_FORM_CONTENT_TYPE_APPLICATION);
242
+ }
243
+
244
+ /**
245
+ * setToken
246
+ *
247
+ * @param string $token Set the access token
248
+ * @return void
249
+ */
250
+ public function setAccessToken($token)
251
+ {
252
+ $this->access_token = $token;
253
+ }
254
+
255
+ /**
256
+ * Set the client authentication type
257
+ *
258
+ * @param string $client_auth (AUTH_TYPE_URI, AUTH_TYPE_AUTHORIZATION_BASIC, AUTH_TYPE_FORM)
259
+ * @return void
260
+ */
261
+ public function setClientAuthType($client_auth)
262
+ {
263
+ $this->client_auth = $client_auth;
264
+ }
265
+
266
+ /**
267
+ * Set an option for the curl transfer
268
+ *
269
+ * @param int $option The CURLOPT_XXX option to set
270
+ * @param mixed $value The value to be set on option
271
+ * @return void
272
+ */
273
+ public function setCurlOption($option, $value)
274
+ {
275
+ $this->curl_options[$option] = $value;
276
+ }
277
+
278
+ /**
279
+ * Set multiple options for a cURL transfer
280
+ *
281
+ * @param array $options An array specifying which options to set and their values
282
+ * @return void
283
+ */
284
+ public function setCurlOptions($options)
285
+ {
286
+ $this->curl_options = array_merge($this->curl_options, $options);
287
+ }
288
+
289
+ /**
290
+ * Set the access token type
291
+ *
292
+ * @param int $type Access token type (ACCESS_TOKEN_BEARER, ACCESS_TOKEN_MAC, ACCESS_TOKEN_URI)
293
+ * @param string $secret The secret key used to encrypt the MAC header
294
+ * @param string $algorithm Algorithm used to encrypt the signature
295
+ * @return void
296
+ */
297
+ public function setAccessTokenType($type, $secret = null, $algorithm = null)
298
+ {
299
+ $this->access_token_type = $type;
300
+ $this->access_token_secret = $secret;
301
+ $this->access_token_algorithm = $algorithm;
302
+ }
303
+
304
+ /**
305
+ * Fetch a protected ressource
306
+ *
307
+ * @param string $protected_ressource_url Protected resource URL
308
+ * @param array $parameters Array of parameters
309
+ * @param string $http_method HTTP Method to use (POST, PUT, GET, HEAD, DELETE)
310
+ * @param array $http_headers HTTP headers
311
+ * @param int $form_content_type HTTP form content type to use
312
+ * @return array
313
+ */
314
+ public function fetch($protected_resource_url, $parameters = array(), $http_method = self::HTTP_METHOD_GET, array $http_headers = array(), $form_content_type = self::HTTP_FORM_CONTENT_TYPE_MULTIPART)
315
+ {
316
+ if ($this->access_token) {
317
+ switch ($this->access_token_type) {
318
+ case self::ACCESS_TOKEN_URI:
319
+ if (is_array($parameters)) {
320
+ $parameters[$this->access_token_param_name] = $this->access_token;
321
+ } else {
322
+ throw new InvalidArgumentException(
323
+ 'You need to give parameters as array if you want to give the token within the URI.',
324
+ InvalidArgumentException::REQUIRE_PARAMS_AS_ARRAY
325
+ );
326
+ }
327
+ break;
328
+ case self::ACCESS_TOKEN_BEARER:
329
+ $http_headers['Authorization'] = 'Bearer ' . $this->access_token;
330
+ break;
331
+ case self::ACCESS_TOKEN_OAUTH:
332
+ $http_headers['Authorization'] = 'OAuth ' . $this->access_token;
333
+ break;
334
+ case self::ACCESS_TOKEN_MAC:
335
+ $http_headers['Authorization'] = 'MAC ' . $this->generateMACSignature($protected_resource_url, $parameters, $http_method);
336
+ break;
337
+ default:
338
+ throw new Exception('Unknown access token type.', Exception::INVALID_ACCESS_TOKEN_TYPE);
339
+ break;
340
+ }
341
+ }
342
+ return $this->executeRequest($protected_resource_url, $parameters, $http_method, $http_headers, $form_content_type);
343
+ }
344
+
345
+ /**
346
+ * Generate the MAC signature
347
+ *
348
+ * @param string $url Called URL
349
+ * @param array $parameters Parameters
350
+ * @param string $http_method Http Method
351
+ * @return string
352
+ */
353
+ private function generateMACSignature($url, $parameters, $http_method)
354
+ {
355
+ $timestamp = time();
356
+ $nonce = uniqid();
357
+ $parsed_url = parse_url($url);
358
+ if (!isset($parsed_url['port']))
359
+ {
360
+ $parsed_url['port'] = ($parsed_url['scheme'] == 'https') ? 443 : 80;
361
+ }
362
+ if ($http_method == self::HTTP_METHOD_GET) {
363
+ if (is_array($parameters)) {
364
+ $parsed_url['path'] .= '?' . http_build_query($parameters, null, '&');
365
+ } elseif ($parameters) {
366
+ $parsed_url['path'] .= '?' . $parameters;
367
+ }
368
+ }
369
+
370
+ $signature = base64_encode(hash_hmac($this->access_token_algorithm,
371
+ $timestamp . "\n"
372
+ . $nonce . "\n"
373
+ . $http_method . "\n"
374
+ . $parsed_url['path'] . "\n"
375
+ . $parsed_url['host'] . "\n"
376
+ . $parsed_url['port'] . "\n\n"
377
+ , $this->access_token_secret, true));
378
+
379
+ return 'id="' . $this->access_token . '", ts="' . $timestamp . '", nonce="' . $nonce . '", mac="' . $signature . '"';
380
+ }
381
+
382
+ /**
383
+ * Execute a request (with curl)
384
+ *
385
+ * @param string $url URL
386
+ * @param mixed $parameters Array of parameters
387
+ * @param string $http_method HTTP Method
388
+ * @param array $http_headers HTTP Headers
389
+ * @param int $form_content_type HTTP form content type to use
390
+ * @return array
391
+ */
392
+ private function executeRequest($url, $parameters = array(), $http_method = self::HTTP_METHOD_GET, array $http_headers = null, $form_content_type = self::HTTP_FORM_CONTENT_TYPE_MULTIPART)
393
+ {
394
+ $curl_options = array(
395
+ CURLOPT_RETURNTRANSFER => true,
396
+ CURLOPT_SSL_VERIFYPEER => true,
397
+ CURLOPT_CUSTOMREQUEST => $http_method
398
+ );
399
+
400
+ switch($http_method) {
401
+ case self::HTTP_METHOD_POST:
402
+ $curl_options[CURLOPT_POST] = true;
403
+ /* No break */
404
+ case self::HTTP_METHOD_PUT:
405
+
406
+ /**
407
+ * Passing an array to CURLOPT_POSTFIELDS will encode the data as multipart/form-data,
408
+ * while passing a URL-encoded string will encode the data as application/x-www-form-urlencoded.
409
+ * http://php.net/manual/en/function.curl-setopt.php
410
+ */
411
+ if(is_array($parameters) && self::HTTP_FORM_CONTENT_TYPE_APPLICATION === $form_content_type) {
412
+ $parameters = http_build_query($parameters, null, '&');
413
+ }
414
+ $curl_options[CURLOPT_POSTFIELDS] = $parameters;
415
+ break;
416
+ case self::HTTP_METHOD_HEAD:
417
+ $curl_options[CURLOPT_NOBODY] = true;
418
+ /* No break */
419
+ case self::HTTP_METHOD_DELETE:
420
+ case self::HTTP_METHOD_GET:
421
+ if (is_array($parameters)) {
422
+ $url .= '?' . http_build_query($parameters, null, '&');
423
+ } elseif ($parameters) {
424
+ $url .= '?' . $parameters;
425
+ }
426
+ break;
427
+ default:
428
+ break;
429
+ }
430
+
431
+ $curl_options[CURLOPT_URL] = $url;
432
+
433
+ if (is_array($http_headers)) {
434
+ $header = array();
435
+ foreach($http_headers as $key => $parsed_urlvalue) {
436
+ $header[] = "$key: $parsed_urlvalue";
437
+ }
438
+ $curl_options[CURLOPT_HTTPHEADER] = $header;
439
+ }
440
+
441
+ $ch = curl_init();
442
+ curl_setopt_array($ch, $curl_options);
443
+ // https handling
444
+ if (!empty($this->certificate_file)) {
445
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
446
+ curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
447
+ curl_setopt($ch, CURLOPT_CAINFO, $this->certificate_file);
448
+ } else {
449
+ // bypass ssl verification
450
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
451
+ curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
452
+ }
453
+ if (!empty($this->curl_options)) {
454
+ curl_setopt_array($ch, $this->curl_options);
455
+ }
456
+ $result = curl_exec($ch);
457
+ $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
458
+ $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
459
+ if ($curl_error = curl_error($ch)) {
460
+ throw new Exception($curl_error, Exception::CURL_ERROR);
461
+ } else {
462
+ $json_decode = json_decode($result, true);
463
+ }
464
+ curl_close($ch);
465
+
466
+ return array(
467
+ 'result' => (null === $json_decode) ? $result : $json_decode,
468
+ 'code' => $http_code,
469
+ 'content_type' => $content_type
470
+ );
471
+ }
472
+
473
+ /**
474
+ * Set the name of the parameter that carry the access token
475
+ *
476
+ * @param string $name Token parameter name
477
+ * @return void
478
+ */
479
+ public function setAccessTokenParamName($name)
480
+ {
481
+ $this->access_token_param_name = $name;
482
+ }
483
+
484
+ /**
485
+ * Converts the class name to camel case
486
+ *
487
+ * @param mixed $grant_type the grant type
488
+ * @return string
489
+ */
490
+ private function convertToCamelCase($grant_type)
491
+ {
492
+ $parts = explode('_', $grant_type);
493
+ array_walk($parts, function(&$item) { $item = ucfirst($item);});
494
+ return implode('', $parts);
495
+ }
496
+ }
497
+
498
+ class Exception extends \Exception
499
+ {
500
+ const CURL_NOT_FOUND = 0x01;
501
+ const CURL_ERROR = 0x02;
502
+ const GRANT_TYPE_ERROR = 0x03;
503
+ const INVALID_CLIENT_AUTHENTICATION_TYPE = 0x04;
504
+ const INVALID_ACCESS_TOKEN_TYPE = 0x05;
505
+ }
506
+
507
+ class InvalidArgumentException extends \InvalidArgumentException
508
+ {
509
+ const INVALID_GRANT_TYPE = 0x01;
510
+ const CERTIFICATE_NOT_FOUND = 0x02;
511
+ const REQUIRE_PARAMS_AS_ARRAY = 0x03;
512
+ const MISSING_PARAMETER = 0x04;
513
+ }
lib/Oauth2/GrantType/AuthorizationCode.php ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ namespace OAuth2\GrantType;
3
+
4
+ use OAuth2\InvalidArgumentException;
5
+
6
+ /**
7
+ * Authorization code Grant Type Validator
8
+ */
9
+ class AuthorizationCode implements IGrantType
10
+ {
11
+ /**
12
+ * Defines the Grant Type
13
+ *
14
+ * @var string Defaults to 'authorization_code'.
15
+ */
16
+ const GRANT_TYPE = 'authorization_code';
17
+
18
+ /**
19
+ * Adds a specific Handling of the parameters
20
+ *
21
+ * @return array of Specific parameters to be sent.
22
+ * @param mixed $parameters the parameters array (passed by reference)
23
+ */
24
+ public function validateParameters(&$parameters)
25
+ {
26
+ if (!isset($parameters['code']))
27
+ {
28
+ throw new InvalidArgumentException(
29
+ 'The \'code\' parameter must be defined for the Authorization Code grant type',
30
+ InvalidArgumentException::MISSING_PARAMETER
31
+ );
32
+ }
33
+ elseif (!isset($parameters['redirect_uri']))
34
+ {
35
+ throw new InvalidArgumentException(
36
+ 'The \'redirect_uri\' parameter must be defined for the Authorization Code grant type',
37
+ InvalidArgumentException::MISSING_PARAMETER
38
+ );
39
+ }
40
+ }
41
+ }
lib/Oauth2/GrantType/ClientCredentials.php ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ namespace OAuth2\GrantType;
3
+
4
+ /**
5
+ * Client Credentials Parameters
6
+ */
7
+ class ClientCredentials implements IGrantType
8
+ {
9
+ /**
10
+ * Defines the Grant Type
11
+ *
12
+ * @var string Defaults to 'client_credentials'.
13
+ */
14
+ const GRANT_TYPE = 'client_credentials';
15
+
16
+ /**
17
+ * Adds a specific Handling of the parameters
18
+ *
19
+ * @return array of Specific parameters to be sent.
20
+ * @param mixed $parameters the parameters array (passed by reference)
21
+ */
22
+ public function validateParameters(&$parameters)
23
+ {
24
+ }
25
+ }
lib/Oauth2/GrantType/IGrantType.php ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ namespace OAuth2\GrantType;
3
+ /**
4
+ * Specific GrantType Interface
5
+ */
6
+ interface IGrantType
7
+ {
8
+ /**
9
+ * Adds a specific Handling of the parameters
10
+ *
11
+ * @return array of Specific parameters to be sent.
12
+ * @param mixed $parameters the parameters array (passed by reference)
13
+ */
14
+ public function validateParameters(&$parameters);
15
+ }
lib/Oauth2/GrantType/Password.php ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ namespace OAuth2\GrantType;
3
+
4
+ use OAuth2\InvalidArgumentException;
5
+
6
+ /**
7
+ * Password Parameters
8
+ */
9
+ class Password implements IGrantType
10
+ {
11
+ /**
12
+ * Defines the Grant Type
13
+ *
14
+ * @var string Defaults to 'password'.
15
+ */
16
+ const GRANT_TYPE = 'password';
17
+
18
+ /**
19
+ * Adds a specific Handling of the parameters
20
+ *
21
+ * @return array of Specific parameters to be sent.
22
+ * @param mixed $parameters the parameters array (passed by reference)
23
+ */
24
+ public function validateParameters(&$parameters)
25
+ {
26
+ if (!isset($parameters['username']))
27
+ {
28
+ throw new InvalidArgumentException(
29
+ 'The \'username\' parameter must be defined for the Password grant type',
30
+ InvalidArgumentException::MISSING_PARAMETER
31
+ );
32
+ }
33
+ elseif (!isset($parameters['password']))
34
+ {
35
+ throw new InvalidArgumentException(
36
+ 'The \'password\' parameter must be defined for the Password grant type',
37
+ InvalidArgumentException::MISSING_PARAMETER
38
+ );
39
+ }
40
+ }
41
+ }
lib/Oauth2/GrantType/RefreshToken.php ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ namespace OAuth2\GrantType;
3
+
4
+ use OAuth2\InvalidArgumentException;
5
+
6
+ /**
7
+ * Refresh Token Parameters
8
+ */
9
+ class RefreshToken implements IGrantType
10
+ {
11
+ /**
12
+ * Defines the Grant Type
13
+ *
14
+ * @var string Defaults to 'refresh_token'.
15
+ */
16
+ const GRANT_TYPE = 'refresh_token';
17
+
18
+ /**
19
+ * Adds a specific Handling of the parameters
20
+ *
21
+ * @return array of Specific parameters to be sent.
22
+ * @param mixed $parameters the parameters array (passed by reference)
23
+ */
24
+ public function validateParameters(&$parameters)
25
+ {
26
+ if (!isset($parameters['refresh_token']))
27
+ {
28
+ throw new InvalidArgumentException(
29
+ 'The \'refresh_token\' parameter must be defined for the refresh token grant type',
30
+ InvalidArgumentException::MISSING_PARAMETER
31
+ );
32
+ }
33
+ }
34
+ }
lib/Oauth2/Test.php ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ require_once '../../app/Mage.php';
4
+ require('client.php');
5
+ require('GrantType/IGrantType.php');
6
+ require('GrantType/AuthorizationCode.php');
7
+
8
+ const CLIENT_ID = '';
9
+ const CLIENT_SECRET = '';
10
+
11
+ const REDIRECT_URI = 'http://dev.magento.local/lib/Oauth2/Test.php';
12
+ const AUTHORIZATION_ENDPOINT = 'https://github.com/login/oauth/authorize';
13
+ const TOKEN_ENDPOINT = 'https://github.com/login/oauth/access_token';
14
+
15
+ $state = Mage::helper('core')->getRandomString(20);
16
+
17
+ $client = new OAuth2\Client(CLIENT_ID, CLIENT_SECRET);
18
+ if (!isset($_GET['code']))
19
+ {
20
+ $auth_url = $client->getAuthenticationUrl(AUTHORIZATION_ENDPOINT, REDIRECT_URI, array('state' => $state, 'scope' => 'user'));
21
+ header('Location: ' . $auth_url);
22
+ die('Redirect');
23
+ } else if (isset($_GET['access_token'])) {
24
+ $client->setAccessToken($_GET['access_token']);
25
+ $response = $client->fetch('https://api.github.com/user/emails');
26
+ Zend_Debug::dump($response);
27
+ Zend_Debug::dump($response['result']);
28
+ } else
29
+ {
30
+ $params = array('code' => $_GET['code'], 'redirect_uri' => REDIRECT_URI, 'state' => $state, 'scope' => 'user');
31
+ $response = $client->getAccessToken(TOKEN_ENDPOINT, 'authorization_code', $params);
32
+ parse_str($response['result'], $info);
33
+ echo $info['access_token'];
34
+ $client->setAccessToken($info['access_token']);
35
+ $response = $client->fetch('https://api.github.com/user');
36
+ Zend_Debug::dump($response);
37
+ Zend_Debug::dump($response['result']);
38
+ }
package.xml ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>Diglin_Github</name>
4
+ <version>1.0.0</version>
5
+ <stability>stable</stability>
6
+ <license uri="http://www.opensource.org/licenses/osl-3.0.php">OSL v3.0</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>Autorize your customers/users to login &amp; register into Magento via their Github account</summary>
10
+ <description>&lt;p&gt;Do not hesitate to fork this project, share your changes or if you have an issue, visit the &lt;a href="https://github.com/diglin/Diglin_Github"&gt;GitHub&lt;/a&gt; repository&lt;/p&gt;&#xD;
11
+ &lt;p&gt;I don't receive any notification if you post a review. So contact me directly instead if you have any issue. Thanks.&lt;/p&gt;&#xD;
12
+ &lt;h2&gt;Diglin Github&lt;/h2&gt;&#xD;
13
+ &lt;p&gt;Autorize your customers/users to login and register into your Magento installatio via their Github account&lt;/p&gt;&#xD;
14
+ &lt;ul&gt;&#xD;
15
+ &lt;li&gt;Users will be redirected to Github to login into their account and redirected again after success on your shop&lt;br /&gt;&#xD;
16
+ &lt;/li&gt;&#xD;
17
+ &lt;li&gt;Allow non existing customer to create the account on your shop thanks to the Github information&lt;br /&gt;&#xD;
18
+ &lt;/li&gt;&#xD;
19
+ &lt;li&gt;Existing customers/users can also link their Github account to your shop account, they just need to try to login via Github and they will be redirected to the create/link account page&lt;br /&gt;&#xD;
20
+ &lt;/li&gt;&#xD;
21
+ &lt;li&gt;Notification for existing customer to confirm that they want to link their Github account with the one of the shop&lt;/li&gt;&#xD;
22
+ &lt;li&gt;Configure to display the github login information into Customer Management Grid&lt;/li&gt;&#xD;
23
+ &lt;/ul&gt;&#xD;
24
+ &lt;h2&gt;Documentation&lt;/h2&gt;&#xD;
25
+ &lt;ul&gt;&#xD;
26
+ &lt;li&gt;login into your backend&lt;/li&gt;&#xD;
27
+ &lt;li&gt;Go to System &amp;gt; Configuration &amp;gt; Diglin &amp;gt; Github&lt;/li&gt;&#xD;
28
+ &lt;li&gt;Just follow the instruction provided on the configuration page&lt;/li&gt;&#xD;
29
+ &lt;/ul&gt;&#xD;
30
+ &lt;p&gt;If you have a 404 Error page while accessing the configuration page, do logout/login from your backend and try again to get access. Otherwise, go to System &amp;gt; Permissions &amp;gt; Role, check the role of your user account and see if the configuration option for Github is marked then save again your role.&lt;/p&gt;&#xD;
31
+ &lt;p&gt;A live version is used on the &lt;a href="http://www.mage-hackathon.de" target="_blank"&gt;Mage Hackathon Website&lt;/a&gt;&lt;/p&gt;&#xD;
32
+ &lt;h2&gt;Donation&lt;/h2&gt;&#xD;
33
+ &lt;p&gt;&lt;a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&amp;hosted_button_id=Y66QHLU5VX5BC"&gt;Invite me for a drink&lt;/a&gt; (thank you for your donation):&lt;/p&gt;&#xD;
34
+ &lt;h2&gt;Author&lt;/h2&gt;&#xD;
35
+ &lt;p&gt;* Sylvain Ray&#xE9;&lt;br /&gt;&#xD;
36
+ * &lt;a href="http://www.sylvainraye.com/"&gt;http://www.sylvainraye.com/&lt;/a&gt;&lt;br /&gt;&#xD;
37
+ * &lt;a href="https://twitter.com/sylvainraye"&gt;@sylvainraye&lt;/a&gt;&lt;br /&gt;&#xD;
38
+ * Follow me on &lt;a href="https://github.com/diglin"&gt;github&lt;/a&gt;!&lt;/p&gt;</description>
39
+ <notes>- 1.0.0: version stable</notes>
40
+ <authors><author><name>diglin</name><user>diglin</user><email>sly@diglin.com</email></author></authors>
41
+ <date>2013-05-24</date>
42
+ <time>22:45:03</time>
43
+ <contents><target name="mageetc"><dir name="modules"><file name="Diglin_Github.xml" hash="03213e38a45f3b82df138a63cbe05144"/></dir></target><target name="magecommunity"><dir name="Diglin"><dir name="Github"><dir name="Block"><dir name="Account"><file name="Existing.php" hash="59922b44f9a013a8f70040dd4fd31920"/><file name="Register.php" hash="450651a6a8983836f0764f5edf7a7e41"/></dir><dir name="Adminhtml"><file name="Grid.php" hash="5af33bd265c4eb3c6ccbad9e25961b23"/></dir></dir><dir name="Helper"><file name="Data.php" hash="b7b1e0c40e25892b3b18dcbacf7e1103"/></dir><dir name="Model"><dir name="Adapter"><file name="Oauth2.php" hash="7f48d51779f931364c3aed644a648849"/></dir><file name="Confirmation.php" hash="17917247ea2fca7d9e04d63f5e541733"/><file name="Log.php" hash="292b654e87611c1a9492855134ce8664"/><file name="Observer.php" hash="6ee8ba378a6802afafba9404e75e5005"/><dir name="Resource"><dir name="Confirmation"><file name="Collection.php" hash="e550bab950edf15661f4bced1c1c0d10"/></dir><file name="Confirmation.php" hash="dc87839e7f775a39ff3dacf5d59cdeca"/><file name="Setup.php" hash="56ebd5ed91084919ae4aef07530bd417"/></dir><dir name="System"><dir name="Config"><dir name="Backend"><file name="Oauth.php" hash="dd910383b477401259c0a4e631928459"/></dir></dir></dir></dir><dir name="controllers"><file name="AccountController.php" hash="2060a745afb564be86e3b9a18a62cef9"/></dir><dir name="etc"><file name="adminhtml.xml" hash="92d705b3a147401074da3be4ce30f345"/><file name="config.xml" hash="a34bb160358906d4b8747d35f1d490ed"/><file name="system.xml" hash="e2072b6be59c741a56769ef3b436ddca"/></dir><dir name="sql"><dir name="github_setup"><file name="install-1.0.0.0.php" hash="a566a0b317b87348e8e38dad93868c4c"/></dir></dir></dir></dir></target><target name="magedesign"><dir name="frontend"><dir name="base"><dir name="default"><dir name="layout"><file name="github.xml" hash="578c5d0bc1fbf68e18369affed8b5dcb"/></dir><dir name="template"><dir name="github"><dir><dir name="customer"><file name="button.phtml" hash="e4aae5f9ecd5aafa3ef3cd2c698b9777"/><file name="create.phtml" hash="9836940d5111842227b4e5bf6951f9bf"/><file name="existing.phtml" hash="1a12ea0b135deeb08e40884bda7ff7cb"/><file name="fields.phtml" hash="e4520d7bb0505bbe29e9b5e13047aca9"/><file name="info.phtml" hash="bed607e849a2bff3a260b469792a95a2"/><file name="login.phtml" hash="316d52fe1546fd73f7a39e7e1d47f761"/></dir></dir></dir></dir></dir></dir></dir></target><target name="magelib"><dir name="Oauth2"><file name="Client.php" hash="eaccb047e274c9c3a43ccaf20517803a"/><dir name="GrantType"><file name="AuthorizationCode.php" hash="a1ff324a733fc33dbf938153c4c1667c"/><file name="ClientCredentials.php" hash="21b30fbb06d4b67263499d687316ae8b"/><file name="IGrantType.php" hash="ee03d4c5bc20f518716fc2c616b4d38d"/><file name="Password.php" hash="2e938e60c4af8bad71b5c4f0f1a40969"/><file name="RefreshToken.php" hash="a773f48348ad7aa510955099ba18d3ad"/></dir><file name="Test.php" hash="d3185c037658594e6f79efd08af76a81"/></dir></target><target name="magelocale"><dir name="en_US"><dir name="template"><dir name="email"><dir name="github"><file name="existing_customer_link_confirmation.html" hash="49f9ecee20ae25aac2dec7ecf8b03522"/></dir></dir></dir></dir></target></contents>
44
+ <compatible/>
45
+ <dependencies><required><php><min>5.2.9</min><max>6.0.0</max></php></required></dependencies>
46
+ </package>