<commit_before><?php namespace App\Extensions\JWTAuth;

use Exception;
use RuntimeException;
use Tymon\JWTAuth\Token;
use Tymon\JWTAuth\JWTAuth as JWTAuthBase;
use App\Exceptions\BadRequestException;
use App\Exceptions\UnauthorizedException;
use App\Exceptions\TokenInvalidException;
use App\Exceptions\UnprocessableEntityException;
use Tymon\JWTAuth\Exceptions\TokenExpiredException;

class JWTAuth extends JWTAuthBase
{
    /**
     * Get the token contained within the current request.
     *
     * @throws BadRequestException
     * @return Token
     */
    public function getTokenFromRequest()
    {
        if (!$token = $this->setRequest($this->request)->getToken()) {
            throw new BadRequestException('Token not provided.');
        }

        return $token;
    }

    /**
     * Get the user from the request token.
     *
     * @throws UnauthorizedException
     * @throws UnprocessableEntityException
     * @throws RuntimeException
     * @return \App\Model\User
     */
    public function getUser()
    {
        $token = $this->getTokenFromRequest();

        try {
            $user = $this->authenticate((string) $token);
        } catch (TokenExpiredException $e) {
            throw new UnauthorizedException('Token expired.', 401, $e);
        } catch (TokenInvalidException $e) {
            throw new UnprocessableEntityException($e->getMessage(), 422, $e);
        }

        if (!$user) {
            throw new RuntimeException('The user does not exist.');
        }

        return $user;
    }

    /**
     * Helper to get current user with null if none instead of exceptions.
     *
     * @return \App\Models\User|null
     */
    public function user()
    {
        try {
            $user = $this->getUser();
        } catch (Exception $e) {
            $user = null;
        }

        return $user;
    }
}
<commit_msg>Adds user object to token payload<commit_after><?php namespace App\Extensions\JWTAuth;

use App;
use Exception;
use RuntimeException;
use Tymon\JWTAuth\Token;
use App\Exceptions\BadRequestException;
use Tymon\JWTAuth\JWTAuth as JWTAuthBase;
use App\Exceptions\UnauthorizedException;
use App\Exceptions\TokenInvalidException;
use App\Exceptions\UnprocessableEntityException;
use Tymon\JWTAuth\Exceptions\TokenExpiredException;

class JWTAuth extends JWTAuthBase
{
    /**
     * Get the token contained within the current request.
     *
     * @throws BadRequestException
     * @return Token
     */
    public function getTokenFromRequest()
    {
        if (!$token = $this->setRequest($this->request)->getToken()) {
            throw new BadRequestException('Token not provided.');
        }

        return $token;
    }

    /**
     * Get the user from the request token.
     *
     * @throws UnauthorizedException
     * @throws UnprocessableEntityException
     * @throws RuntimeException
     * @return \App\Model\User
     */
    public function getUser()
    {
        $token = $this->getTokenFromRequest();

        try {
            $user = $this->authenticate((string) $token);
        } catch (TokenExpiredException $e) {
            throw new UnauthorizedException('Token expired.', 401, $e);
        } catch (TokenInvalidException $e) {
            throw new UnprocessableEntityException($e->getMessage(), 422, $e);
        }

        if (!$user) {
            throw new RuntimeException('The user does not exist.');
        }

        return $user;
    }

    /**
     * Helper to get current user with null if none instead of exceptions.
     *
     * @return \App\Models\User|null
     */
    public function user()
    {
        try {
            $user = $this->getUser();
        } catch (Exception $e) {
            $user = null;
        }

        return $user;
    }

    /**
     * Create a Payload instance.
     *
     * @param mixed $subject
     * @param array $customClaims
     * @return \Tymon\JWTAuth\Payload
     */
    protected function makePayload($subject, array $customClaims = [])
    {
        $users = App::make('App\Repositories\UserRepository');
        $user = $users->find($subject);

        return $this->manager->getPayloadFactory()->make(
            array_merge($customClaims, [
                'sub' => $subject,
                '_user' => $user->toArray()
            ])
        );
    }
}

{'fraction_non_alphanumeric': 0.11337815905402272, 'fraction_numerical': 0.0027822861117551586, 'mean_word_length': 2.2607709750566896, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 24, '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': '2350033', 'n_tokens_mistral': 1250, 'n_tokens_neox': 1227, 'n_words': 297}