Skip to content

Commit

Permalink
test(codestyle): satisfy linter
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Schiwon <[email protected]>
  • Loading branch information
blizzz committed Jul 24, 2023
1 parent 51f578c commit 5e4a71b
Show file tree
Hide file tree
Showing 25 changed files with 207 additions and 232 deletions.
21 changes: 10 additions & 11 deletions appinfo/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
// If we run in CLI mode do not setup the app as it can fail the OCC execution
// since the URLGenerator isn't accessible.
$cli = false;
if(OC::$CLI) {
if (OC::$CLI) {
$cli = true;
}
try {
Expand Down Expand Up @@ -70,7 +70,7 @@
// Setting up the one login config may fail, if so, do not catch the requests later.
$returnScript = false;
$type = '';
switch($config->getAppValue('user_saml', 'type')) {
switch ($config->getAppValue('user_saml', 'type')) {
case 'saml':
try {
$oneLoginSettings = new \OneLogin\Saml2\Settings($samlSettings->getOneLoginSettingsArray(1));
Expand All @@ -96,7 +96,7 @@
OC_User::handleApacheAuth();
}

if($returnScript === true) {
if ($returnScript === true) {
return;
}

Expand All @@ -122,7 +122,7 @@

// All requests that are not authenticated and match against the "/login" route are
// redirected to the SAML login endpoint
if(!$cli &&
if (!$cli &&
!$userSession->isLoggedIn() &&
\OC::$server->getRequest()->getPathInfo() === '/login' &&
$type !== '') {
Expand All @@ -145,10 +145,10 @@
// UX (users don't have to reauthenticate) we default to disallow the access via
// SAML at the moment.
$useSamlForDesktopClients = $config->getAppValue('user_saml', 'general-use_saml_auth_for_desktop', '0');
if($useSamlForDesktopClients === '1') {
if ($useSamlForDesktopClients === '1') {
$currentUrl = substr(explode('?',$request->getRequestUri(), 2)[0], strlen(\OC::$WEBROOT));
if(substr($currentUrl, 0, 12) === '/remote.php/' || substr($currentUrl, 0, 5) === '/ocs/') {
if(!$userSession->isLoggedIn() && $request->isUserAgent([\OCP\IRequest::USER_AGENT_CLIENT_DESKTOP])) {
if (substr($currentUrl, 0, 12) === '/remote.php/' || substr($currentUrl, 0, 5) === '/ocs/') {
if (!$userSession->isLoggedIn() && $request->isUserAgent([\OCP\IRequest::USER_AGENT_CLIENT_DESKTOP])) {
$redirectSituation = true;

if (preg_match('/^.*\/(\d+\.\d+\.\d+).*$/', $request->getHeader('USER_AGENT'), $matches) === 1) {
Expand All @@ -173,7 +173,7 @@
// ignore exception when PUT is called since getParams cannot parse parameters in that case
}
$redirectUrl = '';
if(isset($params['redirect_url'])) {
if (isset($params['redirect_url'])) {
$redirectUrl = $params['redirect_url'];
}

Expand All @@ -185,17 +185,16 @@
);
header('Location: '.$targetUrl);
exit();

}

if($redirectSituation === true) {
if ($redirectSituation === true) {
try {
$params = $request->getParams();
} catch (\LogicException $e) {
// ignore exception when PUT is called since getParams cannot parse parameters in that case
}
$originalUrl = '';
if(isset($params['redirect_url'])) {
if (isset($params['redirect_url'])) {
$originalUrl = $urlGenerator->getAbsoluteURL($params['redirect_url']);
}

Expand Down
5 changes: 2 additions & 3 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
use OCP\SabrePluginEvent;

class Application extends App {
public function __construct(array $urlParams = array()) {
public function __construct(array $urlParams = []) {
parent::__construct('user_saml', $urlParams);
$container = $this->getContainer();

Expand Down Expand Up @@ -57,7 +57,6 @@ public function __construct(array $urlParams = array()) {
}

public function registerDavAuth() {

$container = $this->getContainer();

$dispatcher = $container->getServer()->getEventDispatcher();
Expand All @@ -74,7 +73,7 @@ private function timezoneHandling() {
$config = $container->getServer()->getConfig();

$dispatcher = $container->getServer()->getEventDispatcher();
$dispatcher->addListener('OCA\Files::loadAdditionalScripts', function() use ($session, $config, $userSession) {
$dispatcher->addListener('OCA\Files::loadAdditionalScripts', function () use ($session, $config, $userSession) {
if (!$userSession->isLoggedIn()) {
return;
}
Expand Down
51 changes: 22 additions & 29 deletions lib/Controller/SAMLController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
use OCP\IRequest;
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Security\ICrypto;
use OneLogin\Saml2\Auth;
Expand Down Expand Up @@ -123,7 +121,7 @@ public function __construct(
private function autoprovisionIfPossible() {
$auth = $this->userData->getAttributes();

if(!$this->userData->hasUidMappingAttribute()) {
if (!$this->userData->hasUidMappingAttribute()) {
throw new NoUserFoundException('IDP parameter for the UID not found. Possible parameters are: ' . json_encode(array_keys($auth)));
}

Expand All @@ -145,17 +143,17 @@ private function autoprovisionIfPossible() {
return;
}
$autoProvisioningAllowed = $this->userBackend->autoprovisionAllowed();
if($userExists) {
if($autoProvisioningAllowed) {
if ($userExists) {
if ($autoProvisioningAllowed) {
$this->userBackend->updateAttributes($uid, $auth);
}
return;
}

$uid = $this->userData->getOriginalUid();
if(!$userExists && !$autoProvisioningAllowed) {
if (!$userExists && !$autoProvisioningAllowed) {
throw new NoUserFoundException('Auto provisioning not allowed and user ' . $uid . ' does not exist');
} elseif(!$userExists && $autoProvisioningAllowed) {
} elseif (!$userExists && $autoProvisioningAllowed) {
$this->userBackend->createUserIfNotExists($uid, $auth);
$this->userBackend->updateAttributes($uid, $auth);
return;
Expand All @@ -174,7 +172,7 @@ private function autoprovisionIfPossible() {
*/
public function login($idp) {
$type = $this->config->getAppValue($this->appName, 'type');
switch($type) {
switch ($type) {
case 'saml':
$auth = new Auth($this->SAMLSettings->getOneLoginSettingsArray($idp));
$ssoUrl = $auth->login(null, [], false, false, true);
Expand All @@ -185,7 +183,7 @@ public function login($idp) {

if ($this->session->get(ClientFlowLoginController::STATE_NAME) !== null) {
$flowData['cf1'] = $this->session->get(ClientFlowLoginController::STATE_NAME);
} else if ($this->session->get(ClientFlowLoginV2Controller::TOKEN_NAME) !== null) {
} elseif ($this->session->get(ClientFlowLoginV2Controller::TOKEN_NAME) !== null) {
$flowData['cf2'] = [
'token' => $this->session->get(ClientFlowLoginV2Controller::TOKEN_NAME),
'state' => $this->session->get(ClientFlowLoginV2Controller::STATE_NAME),
Expand Down Expand Up @@ -296,18 +294,17 @@ public function assertionConsumerService(): Http\RedirectResponse {
if (isset($data['flow'])) {
if (isset($data['flow']['cf1'])) {
$this->session->set(ClientFlowLoginController::STATE_NAME, $data['flow']['cf1']);
} else if (isset($data['flow']['cf2'])) {
} elseif (isset($data['flow']['cf2'])) {
$this->session->set(ClientFlowLoginV2Controller::TOKEN_NAME, $data['flow']['cf2']['token']);
$this->session->set(ClientFlowLoginV2Controller::STATE_NAME, $data['flow']['cf2']['state']);
}

}

$AuthNRequestID = $data['AuthNRequestID'];
$idp = $data['Idp'];
// need to keep the IdP config ID during session lifetime (SAMLSettings::getPrefix)
$this->session->set('user_saml.Idp', $idp);
if(is_null($AuthNRequestID) || $AuthNRequestID === '' || is_null($idp)) {
if (is_null($AuthNRequestID) || $AuthNRequestID === '' || is_null($idp)) {
$this->logger->debug('Invalid auth payload', ['app' => 'user_saml']);
return new Http\RedirectResponse($this->urlGenerator->getAbsoluteURL('/'));
}
Expand All @@ -323,7 +320,7 @@ public function assertionConsumerService(): Http\RedirectResponse {
$errors = $auth->getErrors();

if (!empty($errors)) {
foreach($errors as $error) {
foreach ($errors as $error) {
$this->logger->error($error, ['app' => $this->appName]);
}
$this->logger->error($auth->getLastErrorReason(), ['app' => $this->appName]);
Expand Down Expand Up @@ -371,14 +368,14 @@ public function assertionConsumerService(): Http\RedirectResponse {
}

$originalUrl = $data['OriginalUrl'];
if($originalUrl !== null && $originalUrl !== '') {
if ($originalUrl !== null && $originalUrl !== '') {
$response = new Http\RedirectResponse($originalUrl);
} else {
$response = new Http\RedirectResponse(\OC::$server->getURLGenerator()->getAbsoluteURL('/'));
}
// The Nextcloud desktop client expects a cookie with the key of "_shibsession"
// to be there.
if($this->request->isUserAgent(['/^.*(mirall|csyncoC)\/.*$/'])) {
if ($this->request->isUserAgent(['/^.*(mirall|csyncoC)\/.*$/'])) {
$response->addCookie('_shibsession_', 'authenticated');
}

Expand All @@ -400,17 +397,17 @@ public function singleLogoutService() {

// Some IDPs send the SLO request via POST, but OneLogin php-saml only handles GET.
// To hack around this issue we copy the request from _POST to _GET.
if(!empty($_POST['SAMLRequest'])) {
if (!empty($_POST['SAMLRequest'])) {
$_GET['SAMLRequest'] = $_POST['SAMLRequest'];
}

$isFromIDP = !$isFromGS && !empty($_GET['SAMLRequest']);

if($isFromIDP) {
if ($isFromIDP) {
// requests comes from the IDP so let it manage the logout
// (or raise Error if request is invalid)
$pass = True ;
} elseif($isFromGS) {
$pass = true ;
} elseif ($isFromGS) {
// Request is from master GlobalScale
// Request validity is check via a JSON Web Token
$jwt = $this->request->getParam('jwt', '');
Expand All @@ -420,11 +417,11 @@ public function singleLogoutService() {
$pass = $this->request->passesCSRFCheck();
}

if($pass) {
if ($pass) {
$idp = $this->session->get('user_saml.Idp');
$auth = new Auth($this->SAMLSettings->getOneLoginSettingsArray($idp));
$stay = true ; // $auth will return the redirect URL but won't perform the redirect himself
if($isFromIDP){
if ($isFromIDP) {
// validator (called with processSLO()) needs an XML entity loader
$targetUrl = $this->callWithXmlEntityLoader(function () use ($auth, $idp): string {
return $auth->processSLO(
Expand All @@ -437,7 +434,7 @@ public function singleLogoutService() {
});
} else {
// If request is not from IDP, we must send him the logout request
$parameters = array();
$parameters = [];
$nameId = $this->session->get('user_saml.samlNameId');
$nameIdFormat = $this->session->get('user_saml.samlNameIdFormat');
$nameIdNameQualifier = $this->session->get('user_saml.samlNameIdNameQualifier');
Expand All @@ -450,11 +447,11 @@ public function singleLogoutService() {
$this->userSession->logout();
}
}
if(!empty($targetUrl) && !$auth->getLastErrorReason()){
if (!empty($targetUrl) && !$auth->getLastErrorReason()) {
$this->userSession->logout();
}
}
if(empty($targetUrl)){
if (empty($targetUrl)) {
$targetUrl = $this->urlGenerator->getAbsoluteURL('/');
}

Expand Down Expand Up @@ -493,7 +490,6 @@ public function genericError($message) {
* @return Http\TemplateResponse
*/
public function selectUserBackEnd($redirectUrl) {

$attributes = ['loginUrls' => []];

if ($this->SAMLSettings->allowMultipleUserBackEnds()) {
Expand Down Expand Up @@ -545,9 +541,8 @@ private function getIdps($redirectUrl) {
* @return string
*/
private function getSSOUrl($redirectUrl, $idp) {

$originalUrl = '';
if(!empty($redirectUrl)) {
if (!empty($redirectUrl)) {
$originalUrl = $this->urlGenerator->getAbsoluteURL($redirectUrl);
}

Expand All @@ -563,7 +558,6 @@ private function getSSOUrl($redirectUrl, $idp) {
);

return $ssoUrl;

}

/**
Expand Down Expand Up @@ -614,5 +608,4 @@ public function base() {
$message = $this->l->t('This page should not be visited directly.');
return new Http\TemplateResponse($this->appName, 'error', ['message' => $message], 'guest');
}

}
1 change: 0 additions & 1 deletion lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,4 @@ public function deleteSamlProviderSettings($providerId) {
}
return new Response();
}

}
1 change: 1 addition & 0 deletions lib/Controller/TimezoneController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2019, Roeland Jago Douma <[email protected]>
Expand Down
3 changes: 0 additions & 3 deletions lib/DavPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@
use OCA\DAV\Connector\Sabre\Auth;
use OCP\IConfig;
use OCP\ISession;
use Sabre\DAV\CorePlugin;
use Sabre\DAV\FS\Directory;
use Sabre\DAV\Server;
use Sabre\DAV\ServerPlugin;
use Sabre\DAV\Tree;
use Sabre\HTTP\RequestInterface;
use Sabre\HTTP\ResponseInterface;

Expand Down
7 changes: 3 additions & 4 deletions lib/Middleware/OnlyLoggedInMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

namespace OCA\User_SAML\Middleware;

use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Middleware;
use OCP\AppFramework\Utility\IControllerMethodReflector;
Expand Down Expand Up @@ -61,8 +60,8 @@ public function __construct(
* @param string $methodName
* @throws \Exception
*/
public function beforeController($controller, $methodName){
if($this->reflector->hasAnnotation('OnlyUnauthenticatedUsers') && $this->userSession->isLoggedIn()) {
public function beforeController($controller, $methodName) {
if ($this->reflector->hasAnnotation('OnlyUnauthenticatedUsers') && $this->userSession->isLoggedIn()) {
throw new \Exception('User is already logged-in');
}
}
Expand All @@ -75,7 +74,7 @@ public function beforeController($controller, $methodName){
* @throws \Exception
*/
public function afterException($controller, $methodName, \Exception $exception) {
if($exception->getMessage() === 'User is already logged-in') {
if ($exception->getMessage() === 'User is already logged-in') {
return new RedirectResponse($this->urlGenerator->getAbsoluteURL('/'));
}

Expand Down
Loading

0 comments on commit 5e4a71b

Please sign in to comment.