-
-
Notifications
You must be signed in to change notification settings - Fork 446
OAuth2.0 #4102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hirale
wants to merge
28
commits into
OpenMage:main
Choose a base branch
from
hirale:OAuth2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
OAuth2.0 #4102
Changes from 8 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
80e2cd6
feat: oauth2
hirale 074a1a8
Merge branch 'OpenMage:main' into OAuth2
hirale c78147a
fix: fix minor bugs
hirale ccb193b
Update app/design/frontend/base/default/template/oauth2/device/verify…
hirale b195f07
fix: add missing translation
hirale 65cad86
Merge branch 'main' into OAuth2
kiatng 9b5660e
Merge branch 'main' into OAuth2
sreichel 0a0e256
Merge branch 'main' into OAuth2
sreichel 1b3dd27
Update app/code/core/Mage/Oauth2/Block/Adminhtml/Client/Edit.php
sreichel aacd9a3
Merge branch 'main' into OAuth2
sreichel 040fae4
Merge branch 'OpenMage:main' into OAuth2
hirale 29942a8
fix @Hanmac's suggestions
hirale 4826847
fix: fix minor bugs
hirale d67306e
Merge branch 'main' into OAuth2
sreichel 649d1db
fix: fix rector validation errors
hirale f7e267e
Merge branch 'main' into OAuth2
sreichel cbb03e7
Merge branch 'OpenMage:main' into OAuth2
hirale c961611
fix: add copyright headers and update meta files
hirale 4324867
Apply suggestions from code review
sreichel 10d0b98
Delete adminer-4.8.1-mysql-en.php
sreichel d5b9db9
Delete adminer.sql
sreichel 89ebfc9
Delete errors/local.xml
sreichel 21c5319
fix: remove sample logos and apply suggestions from code review
hirale cada379
fix: apply sreichel's suggestions
hirale 6668603
fix: apply suggestions from code review
hirale bb42f37
Merge branch 'main' into OAuth2
sreichel 6a0a49f
Merge branch 'main' into OAuth2
sreichel 605e5a3
Merge branch 'main' into OAuth2
kiatng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
/** | ||
* OpenMage | ||
* | ||
* This source file is subject to the Open Software License (OSL 3.0) | ||
* that is bundled with this package in the file LICENSE.txt. | ||
* It is also available at https://opensource.org/license/osl-3-0-php | ||
* | ||
* @category Mage | ||
* @package Mage_Api2 | ||
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | ||
*/ | ||
|
||
/** | ||
* OAuth2 Authentication adapter | ||
* | ||
* @category Mage | ||
* @package Mage_Api2 | ||
*/ | ||
class Mage_Api2_Model_Auth_Adapter_Oauth2 extends Mage_Api2_Model_Auth_Adapter_Abstract | ||
{ | ||
/** | ||
* Process request and figure out an API user type and its identifier | ||
* | ||
* Returns stdClass object with two properties: type and id | ||
* | ||
* @param Mage_Api2_Model_Request $request | ||
* @return stdClass | ||
*/ | ||
public function getUserParams(Mage_Api2_Model_Request $request) | ||
{ | ||
$userParamsObj = (object) ['type' => null, 'id' => null]; | ||
|
||
try { | ||
$token = $this->_validateToken($request); | ||
$userType = $token->getUserType(); | ||
|
||
if ($userType === 'admin') { | ||
$userParamsObj->id = $token->getAdminId(); | ||
} else { | ||
$userParamsObj->id = $token->getCustomerId(); | ||
} | ||
$userParamsObj->type = $userType; | ||
} catch (Exception $e) { | ||
throw new Mage_Api2_Exception($e->getMessage(), Mage_Api2_Model_Server::HTTP_UNAUTHORIZED); | ||
} | ||
|
||
return $userParamsObj; | ||
} | ||
|
||
/** | ||
* Validate the OAuth2 token | ||
* | ||
* @param Mage_Api2_Model_Request $request | ||
* @return Mage_Oauth2_Model_AccessToken | ||
* @throws Exception | ||
*/ | ||
protected function _validateToken(Mage_Api2_Model_Request $request) | ||
{ | ||
$authorizationHeader = $request->getHeader('Authorization'); | ||
if (!$authorizationHeader || strpos($authorizationHeader, 'Bearer ') !== 0) { | ||
throw new Exception('Missing or invalid Authorization header'); | ||
} | ||
|
||
$accessToken = substr($authorizationHeader, 7); | ||
$token = Mage::getModel('oauth2/accessToken')->load($accessToken, 'access_token'); | ||
if (!$token->getId() || $token->getExpiresIn() < time() || $token->getRevoked()) { | ||
throw new Exception('Invalid or expired access token'); | ||
} | ||
|
||
return $token; | ||
} | ||
|
||
/** | ||
* Check if request contains authentication info for adapter | ||
* | ||
* @param Mage_Api2_Model_Request $request | ||
* @return bool | ||
*/ | ||
public function isApplicableToRequest(Mage_Api2_Model_Request $request) | ||
{ | ||
$headerValue = $request->getHeader('Authorization'); | ||
return $headerValue && strtolower(substr($headerValue, 0, 7)) === 'bearer '; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
/** | ||
* OAuth2 Client Admin Grid Container | ||
*/ | ||
class Mage_Oauth2_Block_Adminhtml_Client extends Mage_Adminhtml_Block_Widget_Grid_Container | ||
{ | ||
/** | ||
* Constructor | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->_blockGroup = 'oauth2'; | ||
$this->_controller = 'adminhtml_client'; | ||
|
||
$helper = Mage::helper('oauth2'); | ||
$this->_headerText = $helper->__('Manage OAuth2 Clients'); | ||
$this->_addButtonLabel = $helper->__('Add New Client'); | ||
|
||
parent::__construct(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
class Mage_Oauth2_Block_Adminhtml_Client_Edit extends Mage_Adminhtml_Block_Widget_Form_Container | ||
{ | ||
protected $_model; | ||
|
||
/** | ||
* Constructs the object and initializes the block group, controller, and mode. | ||
* Updates the save and delete buttons with localized labels. | ||
* Removes the delete button if the user is not allowed to perform the delete action. | ||
* Adds a save and continue button with a localized label and onclick event. | ||
* Adds a form script to submit the form with a specific action. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
$this->_blockGroup = 'oauth2'; | ||
$this->_controller = 'adminhtml_client'; | ||
$this->_mode = 'edit'; | ||
|
||
$this->_updateButton('save', 'label', Mage::helper('oauth')->__('Save')); | ||
hirale marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$this->_updateButton('save', 'id', 'save_button'); | ||
$this->_updateButton('delete', 'label', Mage::helper('oauth')->__('Delete')); | ||
$this->_updateButton('delete', 'onclick', 'if(confirm(\'' . Mage::helper('core')->jsQuoteEscape( | ||
Mage::helper('adminhtml')->__('Are you sure you want to do this?') | ||
) . '\')) editForm.submit(\'' . $this->getUrl('*/*/delete', ['id' => $this->getModel()->getId()]) . '\'); return false;'); | ||
|
||
if (!$this->_isAllowedAction('delete')) { | ||
$this->_removeButton('delete'); | ||
} | ||
|
||
$this->_addButton('save_and_continue', [ | ||
'label' => Mage::helper('oauth')->__('Save and Continue Edit'), | ||
'onclick' => 'saveAndContinueEdit()', | ||
'class' => 'save' | ||
], 100); | ||
|
||
$this->_formScripts[] = "function saveAndContinueEdit()" . | ||
sreichel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"{editForm.submit($('edit_form').action + 'back/edit/');}"; | ||
} | ||
|
||
/** | ||
* Prepares the layout for the block. | ||
* | ||
*/ | ||
public function getHeaderText() | ||
{ | ||
return $this->getModel()->getId() | ||
? $this->__("Edit Client '%s'", $this->escapeHtml($this->getModel()->getName())) | ||
: $this->__('New Client'); | ||
} | ||
|
||
/** | ||
* Check if the current user is allowed to perform the specified action. | ||
* | ||
* @param string $action The action to check. | ||
* @return bool Returns true if the user is allowed, false otherwise. | ||
*/ | ||
protected function _isAllowedAction($action) | ||
{ | ||
return Mage::getSingleton('admin/session')->isAllowed('system/oauth2/client/' . $action); | ||
} | ||
|
||
/** | ||
* Retrieves the model object from the registry if it is not already set. | ||
* | ||
* @return mixed The model object from the registry. | ||
*/ | ||
protected function getModel() | ||
{ | ||
if (null === $this->_model) { | ||
$this->_model = Mage::registry('current_oauth2_client'); | ||
} | ||
return $this->_model; | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
app/code/core/Mage/Oauth2/Block/Adminhtml/Client/Edit/Form.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
class Mage_Oauth2_Block_Adminhtml_Client_Edit_Form extends Mage_Adminhtml_Block_Widget_Form | ||
{ | ||
protected $_model; | ||
|
||
/** | ||
* Prepares the form for the admin edit client block. | ||
* | ||
* @return Mage_Core_Block_Abstract | ||
*/ | ||
protected function _prepareForm() | ||
{ | ||
$form = new Varien_Data_Form( | ||
[ | ||
'id' => 'edit_form', | ||
'action' => $this->getData('action'), | ||
'method' => 'post' | ||
] | ||
); | ||
|
||
$fieldset = $form->addFieldset('base_fieldset', [ | ||
'legend' => Mage::helper('oauth2')->__('Client Information'), | ||
'class' => 'fieldset-wide' | ||
]); | ||
|
||
$fieldset->addType('text', Mage::getConfig()->getBlockClassName('oauth2/adminhtml_text')); | ||
|
||
$fieldset->addField('name', 'text', [ | ||
'label' => Mage::helper('oauth2')->__('Client Name'), | ||
'name' => 'name', | ||
'required' => true, | ||
'value' => $this->getModel()->getName(), | ||
]); | ||
$fieldset->addField('secret', 'text', [ | ||
'label' => Mage::helper('oauth2')->__('Client Secret'), | ||
'name' => 'secret', | ||
'required' => true, | ||
'disabled' => true, | ||
'data-copy-text' => $this->getModel()->getSecret(), | ||
'value' => $this->getModel()->getSecret(), | ||
]); | ||
|
||
$fieldset->addField('redirect_uri', 'text', [ | ||
'label' => Mage::helper('oauth2')->__('Redirect URI'), | ||
'name' => 'redirect_uri', | ||
'required' => true, | ||
'value' => $this->getModel()->getRedirectUri(), | ||
]); | ||
$fieldset->addField('grant_types', 'multiselect', [ | ||
'label' => Mage::helper('oauth2')->__('Grant Types'), | ||
'class' => 'required-entry', | ||
'required' => true, | ||
'name' => 'grant_types[]', | ||
'values' => [ | ||
['value' => 'authorization_code', 'label' => Mage::helper('oauth2')->__('Authorization Code')], | ||
['value' => 'refresh_token', 'label' => Mage::helper('oauth2')->__('Refresh Token')], | ||
], | ||
'value' => $this->getModel()->getGrantTypes(), | ||
]); | ||
|
||
$fieldset->addField('current_password', 'obscure', [ | ||
'name' => 'current_password', | ||
'label' => Mage::helper('oauth')->__('Current Admin Password'), | ||
'required' => true | ||
]); | ||
|
||
$form->setAction($this->getUrl('*/*/save', ['id' => $this->getModel()->getId()])); | ||
$form->setUseContainer(true); | ||
$this->setForm($form); | ||
return parent::_prepareForm(); | ||
} | ||
|
||
/** | ||
* Retrieves the model object from the registry if it is not already set. | ||
* | ||
* @return mixed The model object from the registry. | ||
*/ | ||
protected function getModel() | ||
{ | ||
if (null === $this->_model) { | ||
$this->_model = Mage::registry('current_oauth2_client'); | ||
} | ||
return $this->_model; | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
app/code/core/Mage/Oauth2/Block/Adminhtml/Client/Grid.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
/** | ||
* OAuth2 Client Grid Block | ||
*/ | ||
class Mage_Oauth2_Block_Adminhtml_Client_Grid extends Mage_Adminhtml_Block_Widget_Grid | ||
{ | ||
/** | ||
* @var bool | ||
*/ | ||
protected $_editAllow = false; | ||
|
||
/** | ||
* Constructor | ||
*/ | ||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
$this->setId('oauth2_client_grid') | ||
->setDefaultSort('entity_id') | ||
->setDefaultDir('DESC') | ||
->setSaveParametersInSession(true); | ||
|
||
$this->_editAllow = Mage::getSingleton('admin/session')->isAllowed('system/oauth/consumer/edit'); | ||
} | ||
|
||
/** | ||
* Prepare collection | ||
* | ||
* @return Mage_Oauth2_Block_Adminhtml_Client_Grid | ||
*/ | ||
protected function _prepareCollection() | ||
{ | ||
$collection = Mage::getModel('oauth2/client')->getCollection(); | ||
$this->setCollection($collection); | ||
return parent::_prepareCollection(); | ||
} | ||
|
||
/** | ||
* Prepare columns | ||
* | ||
* @return Mage_Oauth2_Block_Adminhtml_Client_Grid | ||
*/ | ||
protected function _prepareColumns() | ||
{ | ||
$helper = Mage::helper('oauth2'); | ||
|
||
$this->addColumn('entity_id', [ | ||
'header' => $helper->__('Entity ID'), | ||
'index' => 'entity_id', | ||
'type' => 'number', | ||
]); | ||
|
||
$this->addColumn('secret', [ | ||
'header' => $helper->__('Secret'), | ||
'index' => 'secret', | ||
]); | ||
|
||
$this->addColumn('redirect_uri', [ | ||
'header' => $helper->__('Redirect URI'), | ||
'index' => 'redirect_uri', | ||
]); | ||
|
||
$this->addColumn('grant_types', [ | ||
'header' => $helper->__('Grant Types'), | ||
'index' => 'grant_types', | ||
]); | ||
|
||
$this->addColumn('created_at', [ | ||
'header' => $helper->__('Created At'), | ||
'index' => 'created_at', | ||
'type' => 'datetime', | ||
]); | ||
|
||
$this->addColumn('updated_at', [ | ||
'header' => $helper->__('Updated At'), | ||
'index' => 'updated_at', | ||
'type' => 'datetime', | ||
]); | ||
|
||
return parent::_prepareColumns(); | ||
} | ||
|
||
/** | ||
* Get grid URL | ||
* | ||
* @return string | ||
*/ | ||
public function getGridUrl() | ||
{ | ||
return $this->getUrl('*/*/grid', ['_current' => true]); | ||
} | ||
|
||
/** | ||
* Get row URL | ||
* | ||
* @param Mage_Core_Model_Abstract $row | ||
* @return string|null | ||
*/ | ||
public function getRowUrl($row) | ||
{ | ||
return $this->_editAllow ? $this->getUrl('*/*/edit', ['id' => $row->getId()]) : null; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.