<commit_before><?php

namespace Bolt\Extension\Bolt\Members\Oauth2\Client;

use Bolt\Extension\Bolt\Members\Config\Config;
use Bolt\Extension\Bolt\Members\Exception;
use Bolt\Extension\Bolt\Members\Oauth2\Handler\HandlerInterface;
use GuzzleHttp\Client;
use League\OAuth2\Client\Provider\AbstractProvider;
use Psr\Log\LoggerInterface;
use Silex\Application;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
 * Provider object management class.
 *
 * Copyright (C) 2014-2016 <NAME>
 *
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2014-2016, <NAME>
 * @license   https://opensource.org/licenses/MIT MIT
 */
class ProviderManager
{
    /** @var Config */
    protected $config;
    /** @var \GuzzleHttp\Client */
    protected $guzzleClient;
    /** @var \Psr\Log\LoggerInterface */
    protected $logger;
    /** @var string */
    protected $urlGenerator;
    /** @var AbstractProvider */
    protected $provider;
    /** @var string */
    protected $providerName;
    /** @var HandlerInterface */
    protected $providerHandler;

    /**
     * Constructor.
     *
     * @param Config                $config
     * @param Client                $guzzleClient
     * @param LoggerInterface       $logger
     * @param UrlGeneratorInterface $urlGenerator
     */
    public function __construct(Config $config, Client $guzzleClient, LoggerInterface $logger, UrlGeneratorInterface $urlGenerator)
    {
        $this->config = $config;
        $this->guzzleClient = $guzzleClient;
        $this->logger = $logger;
        $this->urlGenerator = $urlGenerator;
    }

    /**
     * Set the provider for this request.
     *
     * @internal
     *
     * @param Application $app
     * @param string      $providerName
     *
     * @throws Exception\InvalidProviderException
     */
    public function setProvider(Application $app, $providerName)
    {
        // Set the provider name that we're using for this request
        $providerName = $providerName === 'submit' ? 'local' : $providerName;
        $this->providerName = $providerName;

        $providerKey = 'members.oauth.provider.' . strtolower($providerName);

        $app['members.oauth.provider'] = $app->share(
            function ($app) use ($providerKey) {
                return $app[$providerKey]([]);
            }
        );

        $app['members.oauth.provider.name'] = $app->share(
            function () use ($providerName) {
                return $providerName;
            }
        );

        // Get the provider class name
        if (!isset($app['members.oauth.provider.map'][$providerName])) {
            throw new Exception\InvalidProviderException(Exception\InvalidProviderException::UNMAPPED_PROVIDER);
        }
        $providerClass = $app['members.oauth.provider.map'][$providerName];
        if (!class_exists($providerClass)) {
            throw new Exception\InvalidProviderException(Exception\InvalidProviderException::INVALID_PROVIDER);
        }
        $options = $this->getProviderOptions($providerName);
        $collaborators = ['httpClient' => $this->guzzleClient];

        $this->provider = new $providerClass($options, $collaborators);

        $app['logger.system']->debug('[Members][Provider]: Created provider name: ' . $providerName, ['event' => 'extensions']);

        $this->setProviderHandler($app);
    }

    /**
     * Get a provider class object.
     *
     * @param string $providerName
     *
     * @throws Exception\InvalidProviderException
     *
     * @return AbstractProvider
     */
    public function getProvider($providerName)
    {
        $this->logger->debug('[Members][Provider]: Fetching provider object: ' . $providerName);

        if ($this->provider === null) {
            throw new Exception\InvalidProviderException(Exception\InvalidProviderException::UNSET_PROVIDER);
        }

        return $this->provider;
    }

    /**
     * Get a provider name for the request.
     *
     * @return string
     */
    public function getProviderName()
    {
        if ($this->providerName !== null) {
            return $this->providerName;
        }

        // If we have no provider name set, and no valid request, we're out of
        // cycle… and that's like bad… 'n stuff
        throw new \RuntimeException('Attempting to get provider name outside of the request cycle.');
    }

    /**
     * Get a provider config for passing to the library.
     *
     * @param string $providerName
     *
     * @throws Exception\ConfigurationException
     *
     * @return array
     */
    public function getProviderOptions($providerName)
    {
        $providerConfig = $this->config->getProvider($providerName);

        if (empty($providerConfig->getClientId())) {
            throw new Exception\ConfigurationException('Provider client ID required: ' . $providerName);
        }
        if (empty($providerConfig->getClientSecret())) {
            throw new Exception\ConfigurationException('Provider secret key required: ' . $providerName);
        }
        if (empty($providerConfig->getScopes())) {
            throw new Exception\ConfigurationException('Provider scope(s) required: ' . $providerName);
        }

        $options = [
            'clientId'     => $providerConfig->getClientId(),
            'clientSecret' => $providerConfig->getClientSecret(),
            'scope'        => $providerConfig->getScopes(),
            'redirectUri'  => $this->getCallbackUrl($providerName),
        ] + $providerConfig->getOptions();

        if ($providerName === 'facebook') {
            $options['graphApiVersion'] = 'v2.5';
        }

        //if ($providerName === 'local') {
        //    $base = $this->config->getUrlRoot() . $this->config->getUriBase() . '/';
        //    $options['urlAuthorize'] = $base . $this->config->getUriAuthorise();
        //    $options['urlAccessToken'] = $base . $this->config->getUriAccessToken();
        //    $options['urlResourceOwnerDetails'] = $base . $this->config->getUriResourceOwnerDetails();
        //}

        return $options;
    }<commit_msg>Handle scope for some WP-OAuth  implementations<commit_after><?php

namespace Bolt\Extension\Bolt\Members\Oauth2\Client;

use Bolt\Extension\Bolt\Members\Config\Config;
use Bolt\Extension\Bolt\Members\Exception;
use Bolt\Extension\Bolt\Members\Oauth2\Handler\HandlerInterface;
use GuzzleHttp\Client;
use League\OAuth2\Client\Provider\AbstractProvider;
use Psr\Log\LoggerInterface;
use Silex\Application;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
 * Provider object management class.
 *
 * Copyright (C) 2014-2016 <NAME>
 *
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2014-2016, <NAME>
 * @license   https://opensource.org/licenses/MIT MIT
 */
class ProviderManager
{
    /** @var Config */
    protected $config;
    /** @var \GuzzleHttp\Client */
    protected $guzzleClient;
    /** @var \Psr\Log\LoggerInterface */
    protected $logger;
    /** @var string */
    protected $urlGenerator;
    /** @var AbstractProvider */
    protected $provider;
    /** @var string */
    protected $providerName;
    /** @var HandlerInterface */
    protected $providerHandler;

    /**
     * Constructor.
     *
     * @param Config                $config
     * @param Client                $guzzleClient
     * @param LoggerInterface       $logger
     * @param UrlGeneratorInterface $urlGenerator
     */
    public function __construct(Config $config, Client $guzzleClient, LoggerInterface $logger, UrlGeneratorInterface $urlGenerator)
    {
        $this->config = $config;
        $this->guzzleClient = $guzzleClient;
        $this->logger = $logger;
        $this->urlGenerator = $urlGenerator;
    }

    /**
     * Set the provider for this request.
     *
     * @internal
     *
     * @param Application $app
     * @param string      $providerName
     *
     * @throws Exception\InvalidProviderException
     */
    public function setProvider(Application $app, $providerName)
    {
        // Set the provider name that we're using for this request
        $providerName = $providerName === 'submit' ? 'local' : $providerName;
        $this->providerName = $providerName;

        $providerKey = 'members.oauth.provider.' . strtolower($providerName);

        $app['members.oauth.provider'] = $app->share(
            function ($app) use ($providerKey) {
                return $app[$providerKey]([]);
            }
        );

        $app['members.oauth.provider.name'] = $app->share(
            function () use ($providerName) {
                return $providerName;
            }
        );

        // Get the provider class name
        if (!isset($app['members.oauth.provider.map'][$providerName])) {
            throw new Exception\InvalidProviderException(Exception\InvalidProviderException::UNMAPPED_PROVIDER);
        }
        $providerClass = $app['members.oauth.provider.map'][$providerName];
        if (!class_exists($providerClass)) {
            throw new Exception\InvalidProviderException(Exception\InvalidProviderException::INVALID_PROVIDER);
        }
        $options = $this->getProviderOptions($providerName);
        $collaborators = ['httpClient' => $this->guzzleClient];

        $this->provider = new $providerClass($options, $collaborators);

        $app['logger.system']->debug('[Members][Provider]: Created provider name: ' . $providerName, ['event' => 'extensions']);

        $this->setProviderHandler($app);
    }

    /**
     * Get a provider class object.
     *
     * @param string $providerName
     *
     * @throws Exception\InvalidProviderException
     *
     * @return AbstractProvider
     */
    public function getProvider($providerName)
    {
        $this->logger->debug('[Members][Provider]: Fetching provider object: ' . $providerName);

        if ($this->provider === null) {
            throw new Exception\InvalidProviderException(Exception\InvalidProviderException::UNSET_PROVIDER);
        }

        return $this->provider;
    }

    /**
     * Get a provider name for the request.
     *
     * @return string
     */
    public function getProviderName()
    {
        if ($this->providerName !== null) {
            return $this->providerName;
        }

        // If we have no provider name set, and no valid request, we're out of
        // cycle… and that's like bad… 'n stuff
        throw new \RuntimeException('Attempting to get provider name outside of the request cycle.');
    }

    /**
     * Get a provider config for passing to the library.
     *
     * @param string $providerName
     *
     * @throws Exception\ConfigurationException
     *
     * @return array
     */
    public function getProviderOptions($providerName)
    {
        $providerConfig = $this->config->getProvider($providerName);

        if (empty($providerConfig->getClientId())) {
            throw new Exception\ConfigurationException('Provider client ID required: ' . $providerName);
        }
        if (empty($providerConfig->getClientSecret())) {
            throw new Exception\ConfigurationException('Provider secret key required: ' . $providerName);
        }
        if (empty($providerConfig->getScopes()) && $providerName !== 'wpoauth') {
            throw new Exception\ConfigurationException('Provider scope(s) required: ' . $providerName);
        }

        $options = [
            'clientId'     => $providerConfig->getClientId(),
            'clientSecret' => $providerConfig->getClientSecret(),
            'scope'        => $providerConfig->getScopes(),
            'redirectUri'  => $this->getCallbackUrl($providerName),
        ] + $providerConfig->getOptions();

        if ($providerName === 'facebook') {
            $options['graphApiVersion'] = 'v2.5';
        }

        //if ($providerName === 'local') {
        //    $base = $this->config->getUrlRoot() . $this->config->getUriBase() . '/';
        //    $options['urlAuthorize'] = $base . $this->config->getUriAuthorise();
        //    $options['urlAccessToken'] = $base . $this->config->getUriAccessToken();
        //    $options['urlResourceOwnerDetails'] = $base . $this->config->getUriResourceOwnerDetails();
        //}

        return $options;
    }
{'fraction_non_alphanumeric': 0.13406952965235175, 'fraction_numerical': 0.0034355828220858898, 'mean_word_length': 2.537615740740741, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 101, 'https://': 2, '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': '233931', 'n_tokens_mistral': 3345, 'n_tokens_neox': 3244, 'n_words': 697}