<commit_before><?php

namespace Controllers\Mship;

use \Config;
use \Auth;
use \URL;
use \Input;
use \Session;
use \Redirect;
use \VatsimSSO;
use \Models\Mship\Account;
use \Models\Mship\Qualification as QualificationType;

class Authentication extends \Controllers\BaseController {

    public function getRedirect() {
        // If there's NO basic auth, send to login.
        if (!Auth::user()->check()) {
            return Redirect::route("mship.auth.login");
        }

        // If there's NO secondary, but it's needed, send to secondary.
        if (!Auth::user()->get()->auth_extra && Auth::user()->get()->current_security && !Session::has("auth_override")) {
            return Redirect::route("mship.security.auth");
        }

        // What about if there's secondary, but it's expired?
        if (!Session::has("auth_override") && Auth::user()->get()->auth_extra && (Auth::user()->get()->auth_extra_at == NULL OR Auth::user()->get()->auth_extra_at->addHours(4)->isPast())) {
            $user = Auth::user()->get();
            $user->auth_extra = 0;
            $user->save();
            return Redirect::route("mship.auth.redirect");
        }

        // Send them home!
        return Redirect::to(Session::pull("auth_return", URL::route("mship.manage.dashboard")));
    }

    public function getLoginAlternative() {
        if (!Session::has("cert_offline")) {
            return Redirect::route("mship.auth.login");
        }

        // Display an alternative login form.
        $this->_pageTitle = "Alternative Login";
        return $this->viewMake("mship.authentication.login_alternative");
    }

    public function postLoginAlternative() {
        if (!Session::has("cert_offline")) {
            return Redirect::route("mship.auth.login");
        }

        if (!Input::get("cid", false) OR ! Input::get("password", false)) {
            return Redirect::route("mship.auth.loginAlternative")->withError("You must enter a cid and password.");
        }

        // Let's find the member.
        $account = Account::find(Input::get("cid"));

        if (!$account) {
            return Redirect::route("mship.auth.loginAlternative")->withError("You must enter a valid cid and password combination.");
        }

        // Let's get their current security and verify...
        if (!$account->current_security OR !$account->current_security->verifyPassword(Input::get("password"))) {
            return Redirect::route("mship.auth.loginAlternative")->withError("You must enter a valid cid and password combination.");
        }

        // We're in!
        // Let's do lots of logins....
        $account->last_login = \Carbon\Carbon::now();
        $account->last_login_ip = array_get($_SERVER, 'REMOTE_ADDR', '127.0.0.1');
        $account->auth_extra = 1;
        $account->auth_extra_at = \Carbon\Carbon::now();
        $account->save();

        Auth::user()->login($account, true);
        Session::forget("cert_offline");

        // Let's send them over to the authentication redirect now.
        return Redirect::route("mship.auth.redirect");
    }

    public function getLogin() {
        Session::set("auth_return", Input::get("returnURL", URL::route("mship.manage.dashboard")));

        // Do we already have some kind of CID? If so, we can skip this bit and go to the redirect!
        if (Auth::user()->check() OR Auth::user()->viaremember()) {
            return Redirect::route("mship.auth.redirect");
        }

        // Just, native VATSIM.net SSO login.
        return VatsimSSO::login(
                        [URL::route("mship.auth.verify"), "suspended" => true, "inactive" => true], function($key, $secret, $url) {
                    Session::put('vatsimauth', compact('key', 'secret'));
                    return Redirect::to($url);
                }, function($error) {
                    Session::set("cert_offline", true);
                    return Redirect::route("mship.auth.loginAlternative");
                }
        );
    }

    public function getVerify() {
        if (!Session::has('vatsimauth')) {
            throw new \AuthException('Session does not exist');
        }

        $session = Session::get('vatsimauth');

        if (Input::get('oauth_token') !== $session['key']) {
            throw new \AuthException('Returned token does not match');
        }

        if (!Input::has('oauth_verifier')) {
            throw new \AuthException('No verification code provided');
        }

        return VatsimSSO::validate($session['key'], $session['secret'], Input::get('oauth_verifier'), function($user, $request) {
                    Session::forget('vatsimauth');

                    // At this point WE HAVE data in the form of $user;
                    $account = Account::find($user->id);
                    if (is_null($account)) {
                        $account = new Account();
                        $account->account_id = $user->id;
                    }<commit_msg>Redirect users to vatsim-uk.co.uk who cancel their login; 404 direct access to auth page<commit_after><?php

namespace Controllers\Mship;

use \Config;
use \Auth;
use \URL;
use \Input;
use \Session;
use \Redirect;
use \VatsimSSO;
use \Models\Mship\Account;
use \Models\Mship\Qualification as QualificationType;

class Authentication extends \Controllers\BaseController {

    public function getRedirect() {
        // If there's NO basic auth, send to login.
        if (!Auth::user()->check()) {
            return Redirect::route("mship.auth.login");
        }

        // If there's NO secondary, but it's needed, send to secondary.
        if (!Auth::user()->get()->auth_extra && Auth::user()->get()->current_security && !Session::has("auth_override")) {
            return Redirect::route("mship.security.auth");
        }

        // What about if there's secondary, but it's expired?
        if (!Session::has("auth_override") && Auth::user()->get()->auth_extra && (Auth::user()->get()->auth_extra_at == NULL OR Auth::user()->get()->auth_extra_at->addHours(4)->isPast())) {
            $user = Auth::user()->get();
            $user->auth_extra = 0;
            $user->save();
            return Redirect::route("mship.auth.redirect");
        }

        // Send them home!
        return Redirect::to(Session::pull("auth_return", URL::route("mship.manage.dashboard")));
    }

    public function getLoginAlternative() {
        if (!Session::has("cert_offline")) {
            return Redirect::route("mship.auth.login");
        }

        // Display an alternative login form.
        $this->_pageTitle = "Alternative Login";
        return $this->viewMake("mship.authentication.login_alternative");
    }

    public function postLoginAlternative() {
        if (!Session::has("cert_offline")) {
            return Redirect::route("mship.auth.login");
        }

        if (!Input::get("cid", false) OR ! Input::get("password", false)) {
            return Redirect::route("mship.auth.loginAlternative")->withError("You must enter a cid and password.");
        }

        // Let's find the member.
        $account = Account::find(Input::get("cid"));

        if (!$account) {
            return Redirect::route("mship.auth.loginAlternative")->withError("You must enter a valid cid and password combination.");
        }

        // Let's get their current security and verify...
        if (!$account->current_security OR !$account->current_security->verifyPassword(Input::get("password"))) {
            return Redirect::route("mship.auth.loginAlternative")->withError("You must enter a valid cid and password combination.");
        }

        // We're in!
        // Let's do lots of logins....
        $account->last_login = \Carbon\Carbon::now();
        $account->last_login_ip = array_get($_SERVER, 'REMOTE_ADDR', '127.0.0.1');
        $account->auth_extra = 1;
        $account->auth_extra_at = \Carbon\Carbon::now();
        $account->save();

        Auth::user()->login($account, true);
        Session::forget("cert_offline");

        // Let's send them over to the authentication redirect now.
        return Redirect::route("mship.auth.redirect");
    }

    public function getLogin() {
        Session::set("auth_return", Input::get("returnURL", URL::route("mship.manage.dashboard")));

        // Do we already have some kind of CID? If so, we can skip this bit and go to the redirect!
        if (Auth::user()->check() OR Auth::user()->viaremember()) {
            return Redirect::route("mship.auth.redirect");
        }

        // Just, native VATSIM.net SSO login.
        return VatsimSSO::login(
                        [URL::route("mship.auth.verify"), "suspended" => true, "inactive" => true], function($key, $secret, $url) {
                    Session::put('vatsimauth', compact('key', 'secret'));
                    return Redirect::to($url);
                }, function($error) {
                    Session::set("cert_offline", true);
                    return Redirect::route("mship.auth.loginAlternative");
                }
        );
    }

    public function getVerify() {

        if (Input::get('oauth_cancel') !== null) {
            return Redirect::away('http://vatsim-uk.co.uk');
        }

        if (!Session::has('vatsimauth')) {
            throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
        }

        $session = Session::get('vatsimauth');

        if (Input::get('oauth_token') !== $session['key']) {
            throw new \AuthException('Returned token does not match');
        }

        if (!Input::has('oauth_verifier')) {
            throw new \AuthException('No verification code provided');
        }

        return VatsimSSO::validate($session['key'], $session['secret'], Input::get('oauth_verifier'), function($user, $request) {
                    Session::forget('vatsimauth');

                    // At this point WE HAVE data in the form of $user;
                    $account = Account::find($user->id);
                    if (is_null($account)) {
                        $account = new Account();
                        $account->account_id = $user->id;
                    }
{'fraction_non_alphanumeric': 0.15948869223205506, 'fraction_numerical': 0.002064896755162242, 'mean_word_length': 2.391463821273758, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '7136741', 'n_tokens_mistral': 2888, 'n_tokens_neox': 2771, 'n_words': 635}