<commit_before><?php

namespace fkooman\OAuth;

use fkooman\Rest\Service;
use fkooman\Http\Request;
use fkooman\Rest\Plugin\Authentication\UserInfoInterface;
use fkooman\Rest\Plugin\Authentication\AuthenticationPluginInterface;

class OAuthService extends Service
{
    /** @var OAuthServer */
    protected $server;

    public function __construct(OAuthServer $server, AuthenticationPluginInterface $authenticationPlugin)
    {
        parent::__construct();

        $this->server = $server;
        $this->registerAuthenticationPlugin($authenticationPlugin);
        $this->registerRoutes();
    }

    private function registerAuthenticationPlugin(AuthenticationPluginInterface $authenticationPlugin)
    {
        // register 'user' authentication
        $authenticationPlugin->register($authenticationPlugin, 'user');

#        // register 'client' authentication
#        $clientAuthentication = new BasicAuthentication(
#            function ($clientId) {
#                $client = $server->getClientStorage()->getClient($clientId);
#                if (false === $client) {
#                    return false;
#                }

#                return $client->getSecret();
#            },
#            array(
#                'realm' => 'OAuth',
#            )
#        );
#        $authenticationPlugin->register($resourceServerAuthentication, 'client');

        // register 'resource server' authentication
        $resourceServerAuthentication = new BasicAuthentication(
            function ($resourceServerId) {
                $resourceServer = $server->getResourceServerStorage()->getResourceServer($resourceServerId);
                if (false === $resourceServer) {
                    return false;
                }

                return $resourceServer->getSecret();
            },
            array(
                'realm' => 'OAuth',
            )
        );
        $authenticationPlugin->register($resourceServerAuthentication, 'resource_server');

        $this->getPluginRegistry()->registerDefaultPlugin($authenticationPlugin);
    }

    private function registerRoutes()
    {
        $this->get(
            '/authorize',
            function (Request $request, UserInfoInterface $userInfo) {
                return $this->server->getAuthorize($request, $userInfo);
            },
            array(
                'fkooman\Rest\Plugin\Authentication\AuthenticationPlugin' => array(
                    'activate' => array('user'),
                ),
            )
        );

        $this->post(
            '/authorize',
            function (Request $request, UserInfoInterface $userInfo) {
                return $this->server->postAuthorize($request, $userInfo);
            },
            array(
                'fkooman\Rest\Plugin\Authentication\AuthenticationPlugin' => array(
                    'activate' => array('user'),<commit_msg>fix authentication plugin<commit_after><?php

namespace fkooman\OAuth;

use fkooman\Rest\Service;
use fkooman\Http\Request;
use fkooman\Rest\Plugin\Authentication\AuthenticationPlugin;
use fkooman\Rest\Plugin\Authentication\UserInfoInterface;
use fkooman\Rest\Plugin\Authentication\AuthenticationPluginInterface;

class OAuthService extends Service
{
    /** @var OAuthServer */
    protected $server;

    public function __construct(OAuthServer $server, AuthenticationPluginInterface $userAuthenticationPlugin)
    {
        parent::__construct();

        $this->server = $server;
        $this->registerAuthenticationPlugin($userAuthenticationPlugin);
        $this->registerRoutes();
    }

    private function registerAuthenticationPlugin(AuthenticationPluginInterface $userAuthenticationPlugin)
    {
        $authenticationPlugin = new AuthenticationPlugin();

        // register 'user' authentication
        $authenticationPlugin->register($userAuthenticationPlugin, 'user');

#        // register 'client' authentication
#        $clientAuthentication = new BasicAuthentication(
#            function ($clientId) {
#                $client = $server->getClientStorage()->getClient($clientId);
#                if (false === $client) {
#                    return false;
#                }

#                return $client->getSecret();
#            },
#            array(
#                'realm' => 'OAuth AS',
#            )
#        );
#        $authenticationPlugin->register($resourceServerAuthentication, 'client');

        // register 'resource server' authentication
        $resourceServerAuthentication = new BasicAuthentication(
            function ($resourceServerId) {
                $resourceServer = $server->getResourceServerStorage()->getResourceServer($resourceServerId);
                if (false === $resourceServer) {
                    return false;
                }

                return $resourceServer->getSecret();
            },
            array(
                'realm' => 'OAuth AS',
            )
        );
        $authenticationPlugin->register($resourceServerAuthentication, 'resource_server');

        $this->getPluginRegistry()->registerDefaultPlugin($authenticationPlugin);
    }

    private function registerRoutes()
    {
        $this->get(
            '/authorize',
            function (Request $request, UserInfoInterface $userInfo) {
                return $this->server->getAuthorize($request, $userInfo);
            },
            array(
                'fkooman\Rest\Plugin\Authentication\AuthenticationPlugin' => array(
                    'activate' => array('user'),
                ),
            )
        );

        $this->post(
            '/authorize',
            function (Request $request, UserInfoInterface $userInfo) {
                return $this->server->postAuthorize($request, $userInfo);
            },
            array(
                'fkooman\Rest\Plugin\Authentication\AuthenticationPlugin' => array(
                    'activate' => array('user'),
{'fraction_non_alphanumeric': 0.11328388401888065, 'fraction_numerical': 0.0, 'mean_word_length': 2.1424788135593222, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 55, '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': '109078', 'n_tokens_mistral': 1411, 'n_tokens_neox': 1353, 'n_words': 244}