<commit_before><?php
/**
 * This file is part of the Passwords App
 * created by <NAME>
 * and licensed under the AGPL.
 */

namespace OCA\Passwords\Middleware;

use OCA\Passwords\Controller\PageController;
use OCA\Passwords\Exception\ApiException;
use OCA\Passwords\Services\ConfigurationService;
use OCA\Passwords\Services\LoggingService;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Middleware;
use OCP\IRequest;

/**
 * Class ApiSecurityMiddleware
 *
 * @package OCA\Passwords\Middleware
 */
class ApiSecurityMiddleware extends Middleware {

    /**
     * @var LoggingService
     */
    protected $logger;

    /**
     * @var IRequest
     */
    protected $request;

    /**
     * @var ConfigurationService
     */
    protected $config;

    /**
     * ApiSecurityMiddleware constructor.
     *
     * @param LoggingService       $logger
     * @param ConfigurationService $config
     * @param IRequest             $request
     */
    public function __construct(LoggingService $logger, ConfigurationService $config, IRequest $request) {
        $this->logger  = $logger;
        $this->config  = $config;
        $this->request = $request;
    }

    /**
     * @param \OCP\AppFramework\Controller $controller
     * @param string                       $methodName
     *
     * @throws ApiException
     */
    public function beforeController($controller, $methodName): void {

        if(get_class($controller) !== PageController::class && $this->request->getServerProtocol() !== 'https') {
            throw new ApiException('HTTPS required', 400);
        }

        parent::beforeController($controller, $methodName);
    }

    /**
     * @param \OCP\AppFramework\Controller $controller
     * @param string                       $methodName
     * @param \Exception                   $exception
     *
     * @return null|JSONResponse
     */
    public function afterException($controller, $methodName, \Exception $exception): ?JSONResponse {
        if(substr(get_class($controller), 0, 28) !== 'OCA\Passwords\Controller\Api') {
            return null;
        }

        $message    = 'Unable to complete request';
        $id         = 0;
        $statusCode = Http::STATUS_SERVICE_UNAVAILABLE;

        $this->logger->logException($exception);

        if(get_class($exception) === ApiException::class || is_subclass_of($exception, ApiException::class)) {
            /** @var ApiException $exception */
            $id         = $exception->getId();
            $message    = $exception->getMessage();
            $statusCode = $exception->getHttpCode();
        }

        if(get_class($exception) === DoesNotExistException::class) {
            $id         = 404;
            $message    = 'Resource not found';
            $statusCode = 404;
        }

        $response = new JSONResponse(
            [
                'status'  => 'error',
                'id'      => $id,
                'message' => $message
            ], $statusCode
        );

        return $response;
    }
}<commit_msg>Fixed admin settings on http<commit_after><?php
/**
 * This file is part of the Passwords App
 * created by <NAME>
 * and licensed under the AGPL.
 */

namespace OCA\Passwords\Middleware;

use OCA\Passwords\Exception\ApiException;
use OCA\Passwords\Services\ConfigurationService;
use OCA\Passwords\Services\LoggingService;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Middleware;
use OCP\IRequest;

/**
 * Class ApiSecurityMiddleware
 *
 * @package OCA\Passwords\Middleware
 */
class ApiSecurityMiddleware extends Middleware {

    /**
     * @var LoggingService
     */
    protected $logger;

    /**
     * @var IRequest
     */
    protected $request;

    /**
     * @var ConfigurationService
     */
    protected $config;

    /**
     * ApiSecurityMiddleware constructor.
     *
     * @param LoggingService       $logger
     * @param ConfigurationService $config
     * @param IRequest             $request
     */
    public function __construct(LoggingService $logger, ConfigurationService $config, IRequest $request) {
        $this->logger  = $logger;
        $this->config  = $config;
        $this->request = $request;
    }

    /**
     * @param \OCP\AppFramework\Controller $controller
     * @param string                       $methodName
     *
     * @throws ApiException
     */
    public function beforeController($controller, $methodName): void {

        if($this->isApiClass($controller) && $this->request->getServerProtocol() !== 'https') {
            throw new ApiException('HTTPS required', 400);
        }

        parent::beforeController($controller, $methodName);
    }

    /**
     * @param \OCP\AppFramework\Controller $controller
     * @param string                       $methodName
     * @param \Exception                   $exception
     *
     * @return JSONResponse
     * @throws \Exception
     */
    public function afterException($controller, $methodName, \Exception $exception): JSONResponse {
        if(!$this->isApiClass($controller)) throw $exception;

        $message    = 'Unable to complete request';
        $id         = 0;
        $statusCode = Http::STATUS_SERVICE_UNAVAILABLE;

        $this->logger->logException($exception);

        if(get_class($exception) === ApiException::class || is_subclass_of($exception, ApiException::class)) {
            /** @var ApiException $exception */
            $id         = $exception->getId();
            $message    = $exception->getMessage();
            $statusCode = $exception->getHttpCode();
        }

        if(get_class($exception) === DoesNotExistException::class) {
            $id         = 404;
            $message    = 'Resource not found';
            $statusCode = 404;
        }

        $response = new JSONResponse(
            [
                'status'  => 'error',
                'id'      => $id,
                'message' => $message
            ], $statusCode
        );

        return $response;
    }

    /**
     * @param $object
     *
     * @return bool
     */
    protected function isApiClass($object): bool {
        return substr(get_class($object), 0, 28) === 'OCA\Passwords\Controller\Api';
    }
}
{'fraction_non_alphanumeric': 0.12216981132075472, 'fraction_numerical': 0.004088050314465409, 'mean_word_length': 2.1757363954068896, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7548549', 'n_tokens_mistral': 1758, 'n_tokens_neox': 1729, 'n_words': 363}