src/JwtAuthController.php
/**
     * @param Request $request
     * @return ResponseInterface
     * @throws PermissionDeniedException
     */
@param Request $request
@return ResponseInterface
@throws PermissionDeniedException
@param Request $request @return ResponseInterface @throws PermissionDeniedException
public function handle(Request $request): ResponseInterface
    {
        $queryParams = $request->getQueryParams();
        $token = (new Parser())->parse((string) $queryParams['token']);
        if (!$token->verify(new Sha256(), new Key($this->signer_key))) {
            throw new PermissionDeniedException('Signature key does not correspond to the one on the token!');
        }
        $jwt_user = $token->getClaim('user');
        $avatar = Arr::get($jwt_user, 'attributes.avatarUrl');
        $param = '?sz=';
        if (strpos($avatar, $param)) {
            $avatar = substr($avatar, 0, strpos($avatar, $param));
        }
        $user = $this->users->findByIdentification(Arr::get($jwt_user, 'attributes.email') ?? Arr::get($jwt_user, 'attributes.username'));
        if ($user !== null) {
            $token = $this->login($user, $avatar, $request);
        } else {
            $jti = $token->getClaim('jti');
            $data = new ValidationData(); 
            $data->setIssuer($this->iss);
            $data->setAudience($this->site_url);
            $data->setId($jti);
            if (!$token->validate($data)) {
                if ($token->isExpired()) {
                    $message = 'Token expired.';
                }
                throw new PermissionDeniedException($message ?? 'Invalid token.');
            }
            $jwt_user['attributes']['isEmailConfirmed'] = true;
            $controller = CreateUserController::class;
            $actor = $this->users->findOrFail(1);
            $body = ['data' => $jwt_user];
            try {
                $response = $this->api->send($controller, $actor, [], $body);
            } catch (Exception $e) {
                throw new RuntimeException("Error during signup");
            }
            $body = json_decode($response->getBody(), false);
            if (isset($body->data)) {
                $id = $body->data->id;
                $user = $this->users->findOrFail($id);
                $user->activate();
                $user->save();
                $token = $this->login($user, $avatar, $request);
            }
        }
        return new JsonResponse($token);
    }
public function handle ( Request $ request ) : ResponseInterface { $ queryParams = $ request -> getQueryParams ( ) ; $ token = ( new Parser ( ) ) -> parse ( ( string ) $ queryParams [ 'token' ] ) ; if ( ! $ token -> verify ( new Sha256 ( ) , new Key ( $ this -> signer_key ) ) ) { throw new PermissionDeniedException ( 'Signature key does not correspond to the one on the token!' ) ; } $ jwt_user = $ token -> getClaim ( 'user' ) ; $ avatar = Arr :: get ( $ jwt_user , 'attributes.avatarUrl' ) ; $ param = '?sz=' ; if ( strpos ( $ avatar , $ param ) ) { $ avatar = substr ( $ avatar , 0 , strpos ( $ avatar , $ param ) ) ; } $ user = $ this -> users -> findByIdentification ( Arr :: get ( $ jwt_user , 'attributes.email' ) ?? Arr :: get ( $ jwt_user , 'attributes.username' ) ) ; if ( $ user !== null ) { $ token = $ this -> login ( $ user , $ avatar , $ request ) ; } else { $ jti = $ token -> getClaim ( 'jti' ) ; $ data = new ValidationData ( ) ; $ data -> setIssuer ( $ this -> iss ) ; $ data -> setAudience ( $ this -> site_url ) ; $ data -> setId ( $ jti ) ; if ( ! $ token -> validate ( $ data ) ) { if ( $ token -> isExpired ( ) ) { $ message = 'Token expired.' ; } throw new PermissionDeniedException ( $ message ?? 'Invalid token.' ) ; } $ jwt_user [ 'attributes' ] [ 'isEmailConfirmed' ] = true ; $ controller = CreateUserController :: class ; $ actor = $ this -> users -> findOrFail ( 1 ) ; $ body = [ 'data' => $ jwt_user ] ; try { $ response = $ this -> api -> send ( $ controller , $ actor , [ ] , $ body ) ; } catch ( Exception $ e ) { throw new RuntimeException ( " Error during signup " ) ; } $ body = json_decode ( $ response -> getBody ( ) , false ) ; if ( isset ( $ body -> data ) ) { $ id = $ body -> data -> id ; $ user = $ this -> users -> findOrFail ( $ id ) ; $ user -> activate ( ) ; $ user -> save ( ) ; $ token = $ this -> login ( $ user , $ avatar , $ request ) ; } } return new JsonResponse ( $ token ) ; }
@param Request $request
 @return ResponseInterface
 @throws PermissionDeniedException
@param Request $request @return ResponseInterface @throws PermissionDeniedException
// remove any sizing params // Signup // It will use the current time to validate (iat, nbf and exp)