<commit_before><?php

namespace Synapse\Controller;

use Mustache_Engine;

use Synapse\User\UserService;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;

use OAuth2\HttpFoundationBridge\Response as BridgeResponse;
use OAuth2\HttpFoundationBridge\Request as OAuthRequest;
use OAuth2\Server as OAuth2Server;

use Synapse\Application\SecurityAwareInterface;
use Synapse\Application\SecurityAwareTrait;
use Synapse\Controller\AbstractController;
use Synapse\OAuth2\Mapper\AccessToken as AccessTokenMapper;
use Synapse\OAuth2\Mapper\RefreshToken as RefreshTokenMapper;
use Synapse\Stdlib\Arr;

use OutOfBoundsException;

class OAuthController extends AbstractController implements SecurityAwareInterface
{
    use SecurityAwareTrait;

    protected $server;
    protected $userService;
    protected $accessTokenMapper;
    protected $refreshTokenMapper;
    protected $mustache;
    protected $session;

    public function __construct(
        OAuth2Server $server,
        UserService $userService,
        AccessTokenMapper $accessTokenMapper,
        RefreshTokenMapper $refreshTokenMapper,
        Mustache_Engine $mustache,
        Session $session
    ) {
        $this->server             = $server;
        $this->userService        = $userService;
        $this->accessTokenMapper  = $accessTokenMapper;
        $this->refreshTokenMapper = $refreshTokenMapper;
        $this->mustache           = $mustache;
        $this->session            = $session;
    }

    /**
     * The user is directed here to log in
     *
     * @param Request $request
     */
    public function authorize(Request $request)
    {
        $submitUrl = $this->url('oauth-authorize-form-submit');

        $vars = array();
        foreach ($request->query->all() as $param => $value) {
            $vars[] = array(
                'name'  => $param,
                'value' => $value
            );
        }

        return $this->mustache->render('OAuth/Authorize', array(
            'submitUrl' => $submitUrl,
            'vars'      => $vars,
        ));
    }

    public function authorizeFormSubmit(Request $request)
    {
        $response     = new BridgeResponse;
        $oauthRequest = OAuthRequest::createFromRequest($request);

        $user = $this->userService->findByEmail($request->query->get('username'));

        if ($user && password_verify($request->query->get('password'), $user->getPassword())) {
            $authorized = true;
        } else {
            $authorized = false;
        }

        if (! $user) {
            return $this->createNotFoundResponse();
        }

        $res = $this->server->handleAuthorizeRequest($oauthRequest, $response, $authorized, $user->getId());
        return $res;
    }

    public function token(Request $request)
    {
        $bridgeResponse = new BridgeResponse;
        $oauthRequest   = OAuthRequest::createFromRequest($request);

        $response = $this->server->handleTokenRequest($oauthRequest, $bridgeResponse);

        if ($response->isOk()) {
            $userId = $response->getParameter('user_id');

            $this->setLastLogin($userId);

            $this->session->set('user', $userId);
        }

        return $response;
    }

    protected function setLastLogin($userId)<commit_msg>Refs #23 - Only attempt to authorize the request if the user input correct credentials.<commit_after><?php

namespace Synapse\Controller;

use Mustache_Engine;

use Synapse\User\UserService;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;

use OAuth2\HttpFoundationBridge\Response as BridgeResponse;
use OAuth2\HttpFoundationBridge\Request as OAuthRequest;
use OAuth2\Server as OAuth2Server;

use Synapse\Application\SecurityAwareInterface;
use Synapse\Application\SecurityAwareTrait;
use Synapse\Controller\AbstractController;
use Synapse\OAuth2\Mapper\AccessToken as AccessTokenMapper;
use Synapse\OAuth2\Mapper\RefreshToken as RefreshTokenMapper;
use Synapse\Stdlib\Arr;

use OutOfBoundsException;

class OAuthController extends AbstractController implements SecurityAwareInterface
{
    use SecurityAwareTrait;

    protected $server;
    protected $userService;
    protected $accessTokenMapper;
    protected $refreshTokenMapper;
    protected $mustache;
    protected $session;

    public function __construct(
        OAuth2Server $server,
        UserService $userService,
        AccessTokenMapper $accessTokenMapper,
        RefreshTokenMapper $refreshTokenMapper,
        Mustache_Engine $mustache,
        Session $session
    ) {
        $this->server             = $server;
        $this->userService        = $userService;
        $this->accessTokenMapper  = $accessTokenMapper;
        $this->refreshTokenMapper = $refreshTokenMapper;
        $this->mustache           = $mustache;
        $this->session            = $session;
    }

    /**
     * The user is directed here to log in
     *
     * @param Request $request
     */
    public function authorize(Request $request)
    {
        $submitUrl = $this->url('oauth-authorize-form-submit');

        $vars = array();
        foreach ($request->query->all() as $param => $value) {
            $vars[] = array(
                'name'  => $param,
                'value' => $value
            );
        }

        return $this->mustache->render('OAuth/Authorize', array(
            'submitUrl' => $submitUrl,
            'vars'      => $vars,
        ));
    }

    public function authorizeFormSubmit(Request $request)
    {
        $response     = new BridgeResponse;
        $oauthRequest = OAuthRequest::createFromRequest($request);

        $user = $this->userService->findByEmail($request->query->get('username'));

        if ($user && password_verify($request->query->get('password'), $user->getPassword())) {
            $correctPassword = true;
        } else {
            $correctPassword = false;
        }

        if (! $user) {
            return $this->createNotFoundResponse();
        }

        if (! $correctPassword) {
            return $this->getSimpleResponse(422, 'Invalid credentials');
        }

        // Automatically authorize the user
        $authorized = true;

        $res = $this->server->handleAuthorizeRequest($oauthRequest, $response, $authorized, $user->getId());

        return $res;
    }

    public function token(Request $request)
    {
        $bridgeResponse = new BridgeResponse;
        $oauthRequest   = OAuthRequest::createFromRequest($request);

        $response = $this->server->handleTokenRequest($oauthRequest, $bridgeResponse);

        if ($response->isOk()) {
            $userId = $response->getParameter('user_id');

            $this->setLastLogin($userId);

            $this->session->set('user', $userId);
        }

        return $response;
    }

    protected function setLastLogin($userId)
{'fraction_non_alphanumeric': 0.11296772350756927, 'fraction_numerical': 0.002713510425592688, 'mean_word_length': 2.6896733403582718, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 72, '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': '6334009', 'n_tokens_mistral': 1904, 'n_tokens_neox': 1860, 'n_words': 388}