Add regex routes requirement to providerId

Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
This commit is contained in:
John Molakvoæ 2021-12-10 08:16:06 +01:00 committed by blizzz (Rebase PR Action)
parent 66759ce3eb
commit 24a632588c
11 changed files with 61 additions and 56 deletions

View File

@ -80,6 +80,9 @@ return [
'verb' => 'GET',
'defaults' => [
'providerId' => 1
],
'requirements' => [
'providerId' => '\d+'
]
],
[
@ -88,6 +91,9 @@ return [
'verb' => 'POST',
'defaults' => [
'providerId' => 1
],
'requirements' => [
'providerId' => '\d+'
]
],
[
@ -100,7 +106,10 @@ return [
'url' => '/settings/providerSettings/{providerId}',
'verb' => 'DELETE',
'defaults' => [
'providerId' => '1'
'providerId' => 1
],
'requirements' => [
'providerId' => '\d+'
]
],
[

View File

@ -68,7 +68,7 @@ class ConfigSet extends Base {
$settings = $this->samlSettings->get($pId);
foreach ($input->getOptions() as $key => $value) {
if(!in_array($key, SAMLSettings::IDP_CONFIG_KEYS) || $value === null) {
if (!in_array($key, SAMLSettings::IDP_CONFIG_KEYS) || $value === null) {
continue;
}
if ($value === '') {

View File

@ -96,7 +96,7 @@ class SettingsController extends Controller {
$key = $category . '-' . $setting;
}
if (isset ($details['global']) && $details['global']) {
if (isset($details['global']) && $details['global']) {
$settings[$category][$setting] = $this->config->getAppValue('user_saml', $key, '');
} else {
$settings[$category][$setting] = $storedSettings[$key] ?? '';

View File

@ -45,7 +45,8 @@ class ConfigurationsMapper extends QBMapper {
public function deleteById(int $id): void {
$entity = new ConfigurationsEntity();
$entity->setId($id);;
$entity->setId($id);
;
$this->delete($entity);
}
@ -98,5 +99,4 @@ class ConfigurationsMapper extends QBMapper {
$newEntity->importConfiguration([]);
return $this->insert($newEntity)->getId();
}
}

View File

@ -16,7 +16,6 @@ use OCP\Migration\SimpleMigrationStep;
* Auto-generated migration step: Please modify to your needs!
*/
class Version5000Date20211025124248 extends SimpleMigrationStep {
private const IDP_CONFIG_KEYS = [
'general-idp0_display_name',
'general-uid_mapping',
@ -187,7 +186,7 @@ class Version5000Date20211025124248 extends SimpleMigrationStep {
$isPrefixed = \preg_match('/^[0-9]*-/', $prefixedKey, $matches);
if ($isPrefixed === 0) {
return $prefixedKey;
} else if ($isPrefixed === 1) {
} elseif ($isPrefixed === 1) {
return \substr($prefixedKey, strlen($matches[0]));
}
throw new \RuntimeException('Invalid regex pattern');

View File

@ -241,7 +241,7 @@ class SAMLSettings {
return;
}
if ($idp !== -1) {
if ($idp !== -1) {
$this->configurations[$idp] = $this->mapper->get($idp);
} else {
$configs = $this->mapper->getAll();

View File

@ -107,8 +107,8 @@ class UserBackend implements IApacheBackend, UserInterface, IUserBackend {
* @param string $uid
* @param array $attributes
*/
public function createUserIfNotExists($uid, array $attributes = array()) {
if(!$this->userExistsInDatabase($uid)) {
public function createUserIfNotExists($uid, array $attributes = []) {
if (!$this->userExistsInDatabase($uid)) {
$values = [
'uid' => $uid,
];
@ -123,12 +123,12 @@ class UserBackend implements IApacheBackend, UserInterface, IUserBackend {
if ($home !== '') {
//if attribute's value is an absolute path take this, otherwise append it to data dir
//check for / at the beginning or pattern c:\ resp. c:/
if( '/' !== $home[0]
if ('/' !== $home[0]
&& !(3 < strlen($home) && ctype_alpha($home[0])
&& $home[1] === ':' && ('\\' === $home[2] || '/' === $home[2]))
) {
$home = $this->config->getSystemValue('datadirectory',
\OC::$SERVERROOT.'/data' ) . '/' . $home;
\OC::$SERVERROOT.'/data') . '/' . $home;
}
$values['home'] = $home;
@ -137,13 +137,12 @@ class UserBackend implements IApacheBackend, UserInterface, IUserBackend {
/* @var $qb IQueryBuilder */
$qb = $this->db->getQueryBuilder();
$qb->insert('user_saml_users');
foreach($values as $column => $value) {
foreach ($values as $column => $value) {
$qb->setValue($column, $qb->createNamedParameter($value));
}
$qb->execute();
$this->initializeHomeDir($uid);
}
}
@ -203,8 +202,8 @@ class UserBackend implements IApacheBackend, UserInterface, IUserBackend {
$data = $result->fetchAll();
$result->closeCursor();
foreach($data as $passwords) {
if(password_verify($password, $passwords['token'])) {
foreach ($data as $passwords) {
if (password_verify($password, $passwords['token'])) {
return $uid;
}
}
@ -219,7 +218,7 @@ class UserBackend implements IApacheBackend, UserInterface, IUserBackend {
* @since 4.5.0
*/
public function deleteUser($uid) {
if($this->userExistsInDatabase($uid)) {
if ($this->userExistsInDatabase($uid)) {
/* @var $qb IQueryBuilder */
$qb = $this->db->getQueryBuilder();
$qb->delete('user_saml_users')
@ -237,7 +236,7 @@ class UserBackend implements IApacheBackend, UserInterface, IUserBackend {
* @return string
*/
public function getHome($uid) {
if($this->userExistsInDatabase($uid)) {
if ($this->userExistsInDatabase($uid)) {
$qb = $this->db->getQueryBuilder();
$qb->select('home')
->from('user_saml_users')
@ -277,7 +276,7 @@ class UserBackend implements IApacheBackend, UserInterface, IUserBackend {
* @since 4.5.0
*/
public function userExists($uid) {
if($backend = $this->getActualUserBackend($uid)) {
if ($backend = $this->getActualUserBackend($uid)) {
return $backend->userExists($uid);
} else {
return $this->userExistsInDatabase($uid);
@ -285,7 +284,7 @@ class UserBackend implements IApacheBackend, UserInterface, IUserBackend {
}
public function setDisplayName($uid, $displayName) {
if($backend = $this->getActualUserBackend($uid)) {
if ($backend = $this->getActualUserBackend($uid)) {
return $backend->setDisplayName($uid, $displayName);
}
@ -309,10 +308,10 @@ class UserBackend implements IApacheBackend, UserInterface, IUserBackend {
* @since 4.5.0
*/
public function getDisplayName($uid) {
if($backend = $this->getActualUserBackend($uid)) {
if ($backend = $this->getActualUserBackend($uid)) {
return $backend->getDisplayName($uid);
} else {
if($this->userExistsInDatabase($uid)) {
if ($this->userExistsInDatabase($uid)) {
$qb = $this->db->getQueryBuilder();
$qb->select('displayname')
->from('user_saml_users')
@ -374,7 +373,7 @@ class UserBackend implements IApacheBackend, UserInterface, IUserBackend {
* @since 4.5.0
*/
public function hasUserListings() {
if($this->autoprovisionAllowed()) {
if ($this->autoprovisionAllowed()) {
return true;
}
@ -487,14 +486,14 @@ class UserBackend implements IApacheBackend, UserInterface, IUserBackend {
public function getCurrentUserId() {
$user = \OC::$server->getUserSession()->getUser();
if($user instanceof IUser && $this->session->get('user_saml.samlUserData')) {
if ($user instanceof IUser && $this->session->get('user_saml.samlUserData')) {
$uid = $user->getUID();
} else {
$this->userData->setAttributes($this->session->get('user_saml.samlUserData') ?? []);
$uid = $this->userData->getEffectiveUid();
}
if($uid !== '' && $this->userExists($uid)) {
if ($uid !== '' && $this->userExists($uid)) {
$this->session->set('last-password-confirm', strtotime('+4 year', time()));
return $uid;
}
@ -527,8 +526,8 @@ class UserBackend implements IApacheBackend, UserInterface, IUserBackend {
* @return null|UserInterface
*/
public function getActualUserBackend($uid) {
foreach(self::$backends as $backend) {
if($backend->userExists($uid)) {
foreach (self::$backends as $backend) {
if ($backend->userExists($uid)) {
return $backend;
}
}
@ -549,8 +548,7 @@ class UserBackend implements IApacheBackend, UserInterface, IUserBackend {
/**
* @throws \OCP\DB\Exception
*/
private function getAttributeKeys($name)
{
private function getAttributeKeys($name) {
$settings = $this->settings->get($this->settings->getProviderId());
$keys = explode(' ', $settings[$name] ?? $this->config->getAppValue('user_saml', $name, ''));
@ -564,17 +562,17 @@ class UserBackend implements IApacheBackend, UserInterface, IUserBackend {
$keys = $this->getAttributeKeys($name);
$value = '';
foreach($keys as $key) {
foreach ($keys as $key) {
if (isset($attributes[$key])) {
if (is_array($attributes[$key])) {
foreach ($attributes[$key] as $attribute_part_value) {
if($value !== '') {
if ($value !== '') {
$value .= ' ';
}
$value .= $attribute_part_value;
}
} else {
if($value !== '') {
if ($value !== '') {
$value .= ' ';
}
$value .= $attributes[$key];
@ -588,8 +586,8 @@ class UserBackend implements IApacheBackend, UserInterface, IUserBackend {
private function getAttributeArrayValue($name, array $attributes) {
$keys = $this->getAttributeKeys($name);
$value = array();
foreach($keys as $key) {
$value = [];
foreach ($keys as $key) {
if (isset($attributes[$key])) {
if (is_array($attributes[$key])) {
$value = array_merge($value, array_values($attributes[$key]));

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2020 Arthur Schiwon <blizzz@arthur-schiwon.de>
@ -66,7 +67,7 @@ class UserData {
}
public function getEffectiveUid(): string {
if($this->uid !== null) {
if ($this->uid !== null) {
return $this->uid;
}
$this->assertIsInitialized();
@ -83,7 +84,7 @@ class UserData {
protected function extractSamlUserId(): string {
$uidMapping = $this->getUidMappingAttribute();
if($uidMapping !== null && isset($this->attributes[$uidMapping])) {
if ($uidMapping !== null && isset($this->attributes[$uidMapping])) {
if (is_array($this->attributes[$uidMapping])) {
return trim($this->attributes[$uidMapping][0]);
} else {
@ -105,13 +106,13 @@ class UserData {
}
$candidate = base64_decode($uid, true);
if($candidate === false) {
if ($candidate === false) {
return $uid;
}
$candidate = $this->convertObjectGUID2Str($candidate);
// the regex only matches the structure of the UUID, not its semantic
// (i.e. version or variant) simply to be future compatible
if(preg_match('/^[a-f0-9]{8}(-[a-f0-9]{4}){4}[a-f0-9]{8}$/i', $candidate) === 1) {
if (preg_match('/^[a-f0-9]{8}(-[a-f0-9]{4}){4}[a-f0-9]{8}$/i', $candidate) === 1) {
$uid = $candidate;
}
return $uid;
@ -123,15 +124,15 @@ class UserData {
protected function convertObjectGUID2Str($oguid): string {
$hex_guid = bin2hex($oguid);
$hex_guid_to_guid_str = '';
for($k = 1; $k <= 4; ++$k) {
for ($k = 1; $k <= 4; ++$k) {
$hex_guid_to_guid_str .= substr($hex_guid, 8 - 2 * $k, 2);
}
$hex_guid_to_guid_str .= '-';
for($k = 1; $k <= 2; ++$k) {
for ($k = 1; $k <= 2; ++$k) {
$hex_guid_to_guid_str .= substr($hex_guid, 12 - 2 * $k, 2);
}
$hex_guid_to_guid_str .= '-';
for($k = 1; $k <= 2; ++$k) {
for ($k = 1; $k <= 2; ++$k) {
$hex_guid_to_guid_str .= substr($hex_guid, 16 - 2 * $k, 2);
}
$hex_guid_to_guid_str .= '-' . substr($hex_guid, 16, 4);
@ -141,7 +142,7 @@ class UserData {
}
protected function assertIsInitialized() {
if($this->attributes === null) {
if ($this->attributes === null) {
throw new \LogicException('UserData have to be initialized with setAttributes first');
}
}

View File

@ -94,7 +94,6 @@ class FeatureContext implements Context {
*/
public function theSettingIsSetTo($settingName,
$value) {
if (in_array($settingName, [
'type',
'general-require_provisioned_account',

View File

@ -29,18 +29,18 @@ use Symfony\Component\Console\Output\OutputInterface;
class GetMetadataTest extends \Test\TestCase {
/** @var GetMetadata|MockObject*/
protected $GetMetadata;
/** @var SAMLSettings|MockObject*/
private $samlSettings;
/** @var GetMetadata|MockObject*/
protected $GetMetadata;
/** @var SAMLSettings|MockObject*/
private $samlSettings;
protected function setUp(): void {
$this->samlSettings = $this->createMock(SAMLSettings::class);
$this->GetMetadata = new GetMetadata($this->samlSettings);
protected function setUp(): void {
$this->samlSettings = $this->createMock(SAMLSettings::class);
$this->GetMetadata = new GetMetadata($this->samlSettings);
parent::setUp();
}
public function testGetMetadata(){
parent::setUp();
}
public function testGetMetadata() {
$inputInterface = $this->createMock(InputInterface::class);
$outputInterface = $this->createMock(OutputInterface::class);
@ -66,5 +66,4 @@ class GetMetadataTest extends \Test\TestCase {
$this->invokePrivate($this->GetMetadata, 'execute', [$inputInterface, $outputInterface]);
}
}

View File

@ -30,7 +30,7 @@ use OCP\IL10N;
use OneLogin\Saml2\Constants;
use PHPUnit\Framework\MockObject\MockObject;
class AdminTest extends \Test\TestCase {
class AdminTest extends \Test\TestCase {
/** @var SAMLSettings|MockObject */
private $settings;
/** @var Admin */
@ -62,7 +62,7 @@ class AdminTest extends \Test\TestCase {
$this->l10n
->expects($this->any())
->method('t')
->will($this->returnCallback(function($text, $parameters = array()) {
->will($this->returnCallback(function ($text, $parameters = []) {
return vsprintf($text, $parameters);
}));