<commit_before><?php namespace Franzose\UloginLaravel\Controllers;

use Auth;
use Config;
use Controller;
use Franzose\UloginLaravel\Models\UloginProfile;
use Franzose\UloginLaravel\Validators\UloginResponseValidator;
use Illuminate\Auth\UserInterface;
use Input;
use Redirect;
use Request;
use Session;

class AuthController extends Controller {

    /**
     * User instance.
     *
     * @var UserInterface
     */
    protected $user;

    /**
     * uLogin user profile.
     *
     * @var UloginProfile
     */
    protected $profile;

    /**
     * Constructor.
     *
     * @param UserInterface $user
     * @param UloginProfile $profile
     * @param UloginResponseValidator $validator
     */
    public function __construct(
        UserInterface $user,
        UloginProfile $profile,
        UloginResponseValidator $validator
    )
    {
        $this->user = $user;
        $this->profile = $profile;
        $this->validator = $validator;
    }

    /**
     * Export redirect URI to uLogin service.
     *
     * @return string
     */
    public function getLogin()
    {
        return Config::get('ulogin-laravel::config.redirect_uri');
    }

    /**
     * Attempt to login.
     *
     * @return \Illuminate\Http\RedirectResponse|mixed
     */
    public function postLogin()
    {
        $ulogin = $this->requestUloginData();

        if (isset($ulogin['error']))
        {
            return $this->handleUloginResponseError($ulogin['error']);
        }

        $profile = $this->profile->where('identity', '=', $ulogin['identity'])->first();

        if ($profile)
        {
            return $this->authUserById($profile->user_id);
        }

        if ($this->validator->with($ulogin)->passes())
        {
            return $this->authUserByUlogin($ulogin);
        }

        return $this->makeErrorResponse($this->validator->getErrors()->all(':message'));
    }

    /**
     * Request user data from a social network via uLogin.
     *
     * @return array
     */
    protected function requestUloginData()
    {
        $url = 'http://ulogin.ru/token.php?token=' . Input::get('tokenn') . '&host=' . Request::server('HTTP_HOST');
        $content = file_get_contents($url);

        return json_decode($content, true);
    }

    /**
     * Handle error response from uLogin.
     *
     * @param $error<commit_msg>fixed typo in get parameter<commit_after><?php namespace Franzose\UloginLaravel\Controllers;

use Auth;
use Config;
use Controller;
use Franzose\UloginLaravel\Models\UloginProfile;
use Franzose\UloginLaravel\Validators\UloginResponseValidator;
use Illuminate\Auth\UserInterface;
use Input;
use Redirect;
use Request;
use Session;

class AuthController extends Controller {

    /**
     * User instance.
     *
     * @var UserInterface
     */
    protected $user;

    /**
     * uLogin user profile.
     *
     * @var UloginProfile
     */
    protected $profile;

    /**
     * Constructor.
     *
     * @param UserInterface $user
     * @param UloginProfile $profile
     * @param UloginResponseValidator $validator
     */
    public function __construct(
        UserInterface $user,
        UloginProfile $profile,
        UloginResponseValidator $validator
    )
    {
        $this->user = $user;
        $this->profile = $profile;
        $this->validator = $validator;
    }

    /**
     * Export redirect URI to uLogin service.
     *
     * @return string
     */
    public function getLogin()
    {
        return Config::get('ulogin-laravel::config.redirect_uri');
    }

    /**
     * Attempt to login.
     *
     * @return \Illuminate\Http\RedirectResponse|mixed
     */
    public function postLogin()
    {
        $ulogin = $this->requestUloginData();

        if (isset($ulogin['error']))
        {
            return $this->handleUloginResponseError($ulogin['error']);
        }

        $profile = $this->profile->where('identity', '=', $ulogin['identity'])->first();

        if ($profile)
        {
            return $this->authUserById($profile->user_id);
        }

        if ($this->validator->with($ulogin)->passes())
        {
            return $this->authUserByUlogin($ulogin);
        }

        return $this->makeErrorResponse($this->validator->getErrors()->all(':message'));
    }

    /**
     * Request user data from a social network via uLogin.
     *
     * @return array
     */
    protected function requestUloginData()
    {
        $url = 'http://ulogin.ru/token.php?token=' . Input::get('token') . '&host=' . Request::server('HTTP_HOST');
        $content = file_get_contents($url);

        return json_decode($content, true);
    }

    /**
     * Handle error response from uLogin.
     *
     * @param $error
{'fraction_non_alphanumeric': 0.13423671395299597, 'fraction_numerical': 0.0, 'mean_word_length': 2.273735273735274, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, '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': '3379324', 'n_tokens_mistral': 1397, 'n_tokens_neox': 1382, 'n_words': 275}