<commit_before><?php

namespace Northstar\Auth;

use Illuminate\Auth\TokenGuard;
use Illuminate\Http\Request;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\UserProvider;
use Mockery\CountValidator\Exception;
use Northstar\Models\Token;
use Northstar\Models\User;

class NorthstarTokenGuard extends TokenGuard implements Guard
{
    /**
     * Create a new authentication guard.
     *
     * @param  \Illuminate\Contracts\Auth\UserProvider  $provider
     * @param  \Illuminate\Http\Request  $request
     */
    public function __construct(UserProvider $provider, Request $request)
    {
        parent::__construct($provider, $request);

        $this->inputKey = 'token';
        $this->storageKey = 'key';
    }

    /**
     * Get the currently authenticated user.
     *
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function user()
    {
        // If we've already retrieved the user for the current request we can just
        // return it back immediately. We do not want to fetch the user data on
        // every call to this method because that would be tremendously slow.
        if (! is_null($this->user)) {
            return $this->user;
        }

        $user = null;

        $token = $this->getTokenForRequest();
        $user = $this->getUserForToken($token);

        return $this->user = $user;
    }

    /**
     * Get the associated user for a given token string.
     *
     * @param string $tokenKey
     * @return \Northstar\Models\User|null
     */
    public function getUserForToken($tokenKey = '')
    {
        // If the provided token is 32 characters long, it's a legacy
        // database token. Otherwise, it must be a JWT access token.
        if (strlen($tokenKey) === 32) {
            return $this->getUserFromLegacyToken($tokenKey);
        }

        return $this->getUserFromJWTAccessToken();
    }

    /**
     * Fetch a legacy authentication token from the database to
     * get it's corresponding user.
     *
     * @param $tokenKey
     * @return User|null
     */
    public function getUserFromLegacyToken($tokenKey)
    {
        $token = Token::where($this->storageKey, $tokenKey)->first();

        if (empty($token)) {
            return null;
        }

        return User::find($token->user_id);
    }

    /**
     * Retrieve the user from a parsed JWT access token that
     * was provided with the current request.
     *
     * @return User|null
     */
    public function getUserFromJWTAccessToken()
    {
        $id = request()->attributes->get('oauth_user_id');

        if (empty($id)) {
            return null;
        }

        return User::find($id);
    }

    /**
     * Get the token key for the current request.
     *
     * @return string
     */
    public function getTokenForRequest()
    {
        // Support deprecated "Session" header authentication.
        $tokenKey = $this->request->header('Session');

        if (! empty($tokenKey)) {
            return $tokenKey;
        }

        return parent::getTokenForRequest();
    }

    /**
     * Validate a user's credentials.
     *
     * @param  array  $credentials
     * @return bool
     */
    public function validate(array $credentials = [])
    {
        if ($this->getUserForToken($credentials[$this->inputKey])) {
            return true;
        }

        return false;
    }

    /**
     * Get the legacy Token instance for the current request.
     *
     * @return Token|null
     */
    public function token()
    {
        $tokenKey = $this->getTokenForRequest();

        // If the provided token is 32 characters long, it's a legacy
        // database token. Otherwise, it must be a JWT access token,
        // which does not have a corresponding database record.<commit_msg>The token guard's validate method should check credentials like other guards.<commit_after><?php

namespace Northstar\Auth;

use Illuminate\Auth\TokenGuard;
use Illuminate\Http\Request;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\UserProvider;
use Mockery\CountValidator\Exception;
use Northstar\Models\Token;
use Northstar\Models\User;

class NorthstarTokenGuard extends TokenGuard implements Guard
{
    /**
     * Create a new authentication guard.
     *
     * @param  \Illuminate\Contracts\Auth\UserProvider  $provider
     * @param  \Illuminate\Http\Request  $request
     */
    public function __construct(UserProvider $provider, Request $request)
    {
        parent::__construct($provider, $request);

        $this->inputKey = 'token';
        $this->storageKey = 'key';
    }

    /**
     * Get the currently authenticated user.
     *
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function user()
    {
        // If we've already retrieved the user for the current request we can just
        // return it back immediately. We do not want to fetch the user data on
        // every call to this method because that would be tremendously slow.
        if (! is_null($this->user)) {
            return $this->user;
        }

        $user = null;

        $token = $this->getTokenForRequest();
        $user = $this->getUserForToken($token);

        return $this->user = $user;
    }

    /**
     * Get the associated user for a given token string.
     *
     * @param string $tokenKey
     * @return \Northstar\Models\User|null
     */
    public function getUserForToken($tokenKey = '')
    {
        // If the provided token is 32 characters long, it's a legacy
        // database token. Otherwise, it must be a JWT access token.
        if (strlen($tokenKey) === 32) {
            return $this->getUserFromLegacyToken($tokenKey);
        }

        return $this->getUserFromJWTAccessToken();
    }

    /**
     * Fetch a legacy authentication token from the database to
     * get it's corresponding user.
     *
     * @param $tokenKey
     * @return User|null
     */
    public function getUserFromLegacyToken($tokenKey)
    {
        $token = Token::where($this->storageKey, $tokenKey)->first();

        if (empty($token)) {
            return null;
        }

        return User::find($token->user_id);
    }

    /**
     * Retrieve the user from a parsed JWT access token that
     * was provided with the current request.
     *
     * @return User|null
     */
    public function getUserFromJWTAccessToken()
    {
        $id = request()->attributes->get('oauth_user_id');

        if (empty($id)) {
            return null;
        }

        return User::find($id);
    }

    /**
     * Get the token key for the current request.
     *
     * @return string
     */
    public function getTokenForRequest()
    {
        // Support deprecated "Session" header authentication.
        $tokenKey = $this->request->header('Session');

        if (! empty($tokenKey)) {
            return $tokenKey;
        }

        return parent::getTokenForRequest();
    }

    /**
     * Validate a user's credentials.
     *
     * @param  array  $credentials
     * @return bool
     */
    public function validate(array $credentials = [])
    {
        $user = $this->provider->retrieveByCredentials($credentials);

        return $this->provider->validateCredentials($user, $credentials);
    }

    /**
     * Get the legacy Token instance for the current request.
     *
     * @return Token|null
     */
    public function token()
    {
        $tokenKey = $this->getTokenForRequest();

        // If the provided token is 32 characters long, it's a legacy
        // database token. Otherwise, it must be a JWT access token,
        // which does not have a corresponding database record.
{'fraction_non_alphanumeric': 0.11046057767369243, 'fraction_numerical': 0.00156128024980484, 'mean_word_length': 2.1896265560165977, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 43, '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': '3495298', 'n_tokens_mistral': 2138, 'n_tokens_neox': 2102, 'n_words': 608}