Minor improvements and explanations in the client model

This commit is contained in:
Artem Vasilev 2024-03-12 01:19:08 +03:00
parent 4b9ebe81e4
commit d87a9b1471

View File

@ -68,12 +68,14 @@ class ClientModel extends AdminModel
$data = $this->getItem(); $data = $this->getItem();
} }
$root = Uri::root(); if ($data)
$uri = new Uri($root); {
$uri = new Uri(Uri::root());
$data->def('authorize_url', (string) $uri->setPath('login/oauth/authorize')); $data->def('authorize_url', (string) $uri->setPath('login/oauth/authorize'));
$data->def('token_url', (string) $uri->setPath('login/oauth/token')); $data->def('token_url', (string) $uri->setPath('login/oauth/token'));
$data->def('profile_url', (string) $uri->setPath('login/oauth/profile')); $data->def('profile_url', (string) $uri->setPath('login/oauth/profile'));
}
$this->preprocessData('com_oauthserver.client', $data); $this->preprocessData('com_oauthserver.client', $data);
@ -82,17 +84,14 @@ class ClientModel extends AdminModel
public function validate($form, $data, $group = null): bool|array public function validate($form, $data, $group = null): bool|array
{ {
// Since the clients identifier and secret key are created on the server and completely
// exclude the users influence on their value, we remove them from the request to eliminate
// any possibility of substitution of this data.
unset($data['identifier'], $data['secret']); unset($data['identifier'], $data['secret']);
unset($data['authorize_url'], $data['token_url'], $data['profile_url']);
return parent::validate($form, $data, $group); return parent::validate($form, $data, $group);
} }
public function save($data)
{
return parent::save($data); // TODO: Change the autogenerated stub
}
/** /**
* @param ClientTable $table * @param ClientTable $table
* *
@ -102,38 +101,42 @@ class ClientModel extends AdminModel
*/ */
protected function prepareTable($table): void protected function prepareTable($table): void
{ {
$app = Factory::getApplication(); $app = Factory::getApplication();
$input = $app->getInput();
$task = strtolower($input->getCmd('task', ''));
if (empty($table->id)) if ($table->id > 0)
{ {
$table->identifier = $this->generateNewIdentifier(); $table->identifier = $this->generateNewIdentifier();
} }
if ($task === 'save2reset' || empty($table->id)) if (empty($table->secret)
&& !$table->public
&& ($table->id > 0 || $app->getInput()->get('task') == 'save2reset'))
{ {
$table->secret = ''; $table->secret = $this->generateNewSecret();
} }
if (!!$table->public) if ($table->public)
{ {
$table->secret = ''; $table->secret = '';
} }
else
{
if (empty($table->secret))
{
$table->secret = $this->generateNewSecret();
}
}
$table->name = htmlspecialchars_decode($table->name, ENT_QUOTES); $table->name = htmlspecialchars_decode($table->name, ENT_QUOTES);
parent::prepareTable($table); parent::prepareTable($table);
} }
protected function generateNewHash($field, $algo = 'sha256', $length = 16) /**
* Generate a hash value of string for table field and check it unique
*
* @param string $field
* @param string $algo
* @param int $length
*
* @return string
* @throws \Exception
* @since version
*/
protected function generateNewHash(string $field, string $algo = 'sha256', int $length = 16): string
{ {
$hash = hash($algo, Crypt::genRandomBytes($length)); $hash = hash($algo, Crypt::genRandomBytes($length));
$table = $this->getTable(); $table = $this->getTable();
@ -146,11 +149,25 @@ class ClientModel extends AdminModel
return $hash; return $hash;
} }
/**
* Generate unique hash value for client identifier
*
* @return string
* @throws \Exception
* @since version
*/
protected function generateNewIdentifier(): string protected function generateNewIdentifier(): string
{ {
return $this->generateNewHash('identifier', 'md5'); return $this->generateNewHash('identifier', 'md5');
} }
/**
* Generate unique hash value for client secret key
*
* @return string
* @throws \Exception
* @since version
*/
protected function generateNewSecret(): string protected function generateNewSecret(): string
{ {
return $this->generateNewHash('secret', 'sha512', 32); return $this->generateNewHash('secret', 'sha512', 32);