<commit_before><?php

/*
 * eduVPN - End-user friendly VPN.
 *
 * Copyright: 2016-2018, The Commons Conservancy eduVPN Programme
 * SPDX-License-Identifier: AGPL-3.0+
 */

$baseDir = dirname(__DIR__);
/** @psalm-suppress UnresolvableInclude */
require_once sprintf('%s/vendor/autoload.php', $baseDir);

use fkooman\OAuth\Server\BearerValidator;
use fkooman\OAuth\Server\ClientInfo;
use fkooman\OAuth\Server\SodiumSigner;
use fkooman\OAuth\Server\Storage;
use ParagonIE\ConstantTime\Base64;
use SURFnet\VPN\Common\Config;
use SURFnet\VPN\Common\FileIO;
use SURFnet\VPN\Common\Http\JsonResponse;
use SURFnet\VPN\Common\Http\Request;
use SURFnet\VPN\Common\Http\Service;
use SURFnet\VPN\Common\HttpClient\CurlHttpClient;
use SURFnet\VPN\Common\HttpClient\ServerClient;
use SURFnet\VPN\Common\Logger;
use SURFnet\VPN\Portal\BearerAuthenticationHook;
use SURFnet\VPN\Portal\ForeignKeyListFetcher;
use SURFnet\VPN\Portal\OAuthClientInfo;
use SURFnet\VPN\Portal\VpnApiModule;

$logger = new Logger('vpn-user-api');

try {
    $request = new Request($_SERVER, $_GET, $_POST);

    if (false === $instanceId = getenv('VPN_INSTANCE_ID')) {
        $instanceId = $request->getServerName();
    }

    $dataDir = sprintf('%s/data/%s', $baseDir, $instanceId);
    if (!file_exists($dataDir)) {
        if (false === @mkdir($dataDir, 0700, true)) {
            throw new RuntimeException(sprintf('unable to create folder "%s"', $dataDir));
        }
    }
    $config = Config::fromFile(sprintf('%s/config/%s/config.php', $baseDir, $instanceId));

    $service = new Service();

    if ($config->hasSection('Api')) {
        $storage = new Storage(new PDO(sprintf('sqlite://%s/tokens.sqlite', $dataDir)));
        $storage->init();

        $getClientInfo = function ($clientId) use ($config) {
            if (false === $config->getSection('Api')->getSection('consumerList')->hasItem($clientId)) {
                // if not in configuration file, check if it is in the hardcoded list
                return OAuthClientInfo::getClient($clientId);
            }

            // XXX switch to only support 'redirect_uri_list' for 2.0
            $clientInfoData = $config->getSection('Api')->getSection('consumerList')->getItem($clientId);
            $redirectUriList = [];
            if (array_key_exists('redirect_uri_list', $clientInfoData)) {
                $redirectUriList = array_merge($redirectUriList, (array) $clientInfoData['redirect_uri_list']);
            }
            if (array_key_exists('redirect_uri', $clientInfoData)) {
                $redirectUriList = array_merge($redirectUriList, (array) $clientInfoData['redirect_uri']);
            }
            $clientInfoData['redirect_uri_list'] = $redirectUriList;

            return new ClientInfo($clientInfoData);
        };

        $foreignKeys = [];
        if ($config->getSection('Api')->hasItem('foreignKeys')) {
            foreach ($config->getSection('Api')->getItem('foreignKeys') as $tokenIssuer => $publicKey) {
                $foreignKeys[$tokenIssuer] = Base64::decode($publicKey);
            }
        }

        if ($config->getSection('Api')->hasItem('foreignKeyListSource')) {
            $foreignKeyListFetcher = new ForeignKeyListFetcher(sprintf('%s/data/%s/foreign_key_list.json', $baseDir, $instanceId));
            $foreignKeys = array_merge($foreignKeys, $foreignKeyListFetcher->extract());
        }<commit_msg>use keyID as var name<commit_after><?php

/*
 * eduVPN - End-user friendly VPN.
 *
 * Copyright: 2016-2018, The Commons Conservancy eduVPN Programme
 * SPDX-License-Identifier: AGPL-3.0+
 */

$baseDir = dirname(__DIR__);
/** @psalm-suppress UnresolvableInclude */
require_once sprintf('%s/vendor/autoload.php', $baseDir);

use fkooman\OAuth\Server\BearerValidator;
use fkooman\OAuth\Server\ClientInfo;
use fkooman\OAuth\Server\SodiumSigner;
use fkooman\OAuth\Server\Storage;
use ParagonIE\ConstantTime\Base64;
use SURFnet\VPN\Common\Config;
use SURFnet\VPN\Common\FileIO;
use SURFnet\VPN\Common\Http\JsonResponse;
use SURFnet\VPN\Common\Http\Request;
use SURFnet\VPN\Common\Http\Service;
use SURFnet\VPN\Common\HttpClient\CurlHttpClient;
use SURFnet\VPN\Common\HttpClient\ServerClient;
use SURFnet\VPN\Common\Logger;
use SURFnet\VPN\Portal\BearerAuthenticationHook;
use SURFnet\VPN\Portal\ForeignKeyListFetcher;
use SURFnet\VPN\Portal\OAuthClientInfo;
use SURFnet\VPN\Portal\VpnApiModule;

$logger = new Logger('vpn-user-api');

try {
    $request = new Request($_SERVER, $_GET, $_POST);

    if (false === $instanceId = getenv('VPN_INSTANCE_ID')) {
        $instanceId = $request->getServerName();
    }

    $dataDir = sprintf('%s/data/%s', $baseDir, $instanceId);
    if (!file_exists($dataDir)) {
        if (false === @mkdir($dataDir, 0700, true)) {
            throw new RuntimeException(sprintf('unable to create folder "%s"', $dataDir));
        }
    }
    $config = Config::fromFile(sprintf('%s/config/%s/config.php', $baseDir, $instanceId));

    $service = new Service();

    if ($config->hasSection('Api')) {
        $storage = new Storage(new PDO(sprintf('sqlite://%s/tokens.sqlite', $dataDir)));
        $storage->init();

        $getClientInfo = function ($clientId) use ($config) {
            if (false === $config->getSection('Api')->getSection('consumerList')->hasItem($clientId)) {
                // if not in configuration file, check if it is in the hardcoded list
                return OAuthClientInfo::getClient($clientId);
            }

            // XXX switch to only support 'redirect_uri_list' for 2.0
            $clientInfoData = $config->getSection('Api')->getSection('consumerList')->getItem($clientId);
            $redirectUriList = [];
            if (array_key_exists('redirect_uri_list', $clientInfoData)) {
                $redirectUriList = array_merge($redirectUriList, (array) $clientInfoData['redirect_uri_list']);
            }
            if (array_key_exists('redirect_uri', $clientInfoData)) {
                $redirectUriList = array_merge($redirectUriList, (array) $clientInfoData['redirect_uri']);
            }
            $clientInfoData['redirect_uri_list'] = $redirectUriList;

            return new ClientInfo($clientInfoData);
        };

        $foreignKeys = [];
        if ($config->getSection('Api')->hasItem('foreignKeys')) {
            foreach ($config->getSection('Api')->getItem('foreignKeys') as $keyId => $publicKey) {
                $foreignKeys[$keyId] = Base64::decode($publicKey);
            }
        }

        if ($config->getSection('Api')->hasItem('foreignKeyListSource')) {
            $foreignKeyListFetcher = new ForeignKeyListFetcher(sprintf('%s/data/%s/foreign_key_list.json', $baseDir, $instanceId));
            $foreignKeys = array_merge($foreignKeys, $foreignKeyListFetcher->extract());
        }
{'fraction_non_alphanumeric': 0.14967589864466707, 'fraction_numerical': 0.0058927519151443725, 'mean_word_length': 3.422801302931596, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, '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': '6044263', 'n_tokens_mistral': 2206, 'n_tokens_neox': 2116, 'n_words': 358}