Encrypt token and save

@param \Magento\User\Model\User $user
177992
private function encryptAndSaveRefreshToken($user)
    {
        $user->setRefreshToken(
            $this->helper->encryptor->encrypt($user->getRefreshToken())
        );
        $this->userResource->save($user);
    }
393720
public function generateTokenByUser($user)
    {
        return $this->builder
                    ->set('user', $user)
                    ->sign(new Sha256(), env('JWT_SECRET'))
                    ->getToken();
    }
251078
private function hasOpenEmailValidation(User $user)
    {
        $ts = $user->email_verification_token_timestamp;
        if (!empty($ts) && (time() - $this->module->emailVerificationTokenExpirationTime) <= $ts) {
            return true;
        }
        
        return false;
    }
771261
public function login(User $user, $password)
    {
        if ($user->isPasswordValid($password)) {
            $token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
            $this->tokenStorage->setToken($token);
            $user->setLastLogin(new \DateTime());
            $this->save($user);

            return true;
        }

        return false;
    }
1051474
public function deleteToken(\core_kernel_classes_Resource $user)
    {
        $tokenProperty = new \core_kernel_classes_Property(self::PROPERTY_PASSWORD_RECOVERY_TOKEN);
        return $user->removePropertyValues($tokenProperty);
    }
429342
public function beforeSave(Event $event, User $user)
    {
        if (!$user->isNew() && $user->has('password') && empty($user->password)) {
            $user->unsetProperty('password');
            $user->dirty('password', false);
        }
    }
218434
public function setUser(\WobbleCode\UserBundle\Document\User $user)
    {
        $this->user = $user;

        return $this;
    }
686861
private function encryptPrivateKey(PKEncryptionEnabledUserInterface $user)
    {
        if (!$user->isPrivateKeyEncrypted()) {
            $privateKey = $user->getPrivateKey();
            $passwordDigest = $this->getUserPasswordDigest($user);

            if ($passwordDigest) {
                $iv = $this->cryptographyProvider->generateIV(CryptographyProviderInterface::PRIVATE_KEY_ENCRYPTION);

                $keyData = new KeyData($passwordDigest, $iv);
                try {
                    $encryptedPrivateKey = $this->cryptographyProvider->encrypt(
                                    $privateKey,
                                    $keyData,
                                    CryptographyProviderInterface::PRIVATE_KEY_ENCRYPTION);

                    $encrypted = true;
                }
                catch (\Exception $ex) {
                    $encryptedPrivateKey = $privateKey;
                    $iv = null;
                    $encrypted = false;
                }
            }
            else {
                $encryptedPrivateKey = $privateKey;
                $iv = null;
                $encrypted = false;
            }

            $user->setPrivateKey($encryptedPrivateKey);
            if ($encrypted) {
                $user->setPrivateKeyIv($iv);
                $user->setPrivateKeyEncrypted(true);
            }
        }
    }
901372
public function save(User $user, Request $request)
    {
        $permissions = $request->get('permissions', []);
        $roles = $request->input('user.roles', []);

        foreach ($permissions as $key => $value) {
            unset($permissions[$key]);
            $permissions[base64_decode($key)] = $value;
        }

        $user
            ->fill($request->get('user'))
            ->replaceRoles($roles)
            ->fill([
                'permissions' => $permissions,
            ])
            ->save();

        Alert::info(__('User was saved'));

        return redirect()->route('platform.systems.users');
    }
298712
public function isSatisfiedBy(User $user)
    {
        return $this->email === $user->email && password_verify($this->password, $user->password);
    }
4914
public function logUser(User $user)
    {
        $this->strictEventDispatcher->dispatch('log', 'Log\LogUserLogin', array($user));
        $token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
        $this->container->get('security.token_storage')->setToken($token);
    }
334002
protected function CanEdit(User $user)
    {
        return self::Guard()->Allow(BackendAction::Edit(), $user)&&
            self::Guard()->Allow(BackendAction::UseIt(), new UserForm());
    }
428443
public function setUser(UserInterface $user): UserTokenInterface
    {
        $serialize_user = serialize($user);

        $this->Session->set($this->session_key, $serialize_user);

        return $this;
    }
1243141
public function updateRememberToken(UserContract $user, $token)
    {
        $this->query->filterById($user->getAuthIdentifier())
            ->update(['RememberToken' => $token]);
    }
1217299
private function deleteToken(UserInterface $user)
    {
        $em = $this->getDoctrine()->getManager();
        $user->setPasswordResetToken(null);
        $user->setPasswordResetTokenExpiresAt(null);
        $user->setPasswordResetTokenEmailsSent(null);
        $em->persist($user);
        $em->flush();
    }
746510
public function save(User $user)
    {
        if ($user->getPlainPassword()) {
            $this->setPlainTextPassword($user, $user->getPlainPassword());
        }

        $this->entityManager->persist($user);
        $this->entityManager->flush();

        if ($user->isNew() && !$user->isEmailSent()) {
            try {
                $this->userMailer->sendNewUserMail($user);
                $user->setEmailSent(true);
                $this->entityManager->flush($user);
            } catch (\Exception $e) {
                $this->logger->alert($e->getMessage());
            }
        }
        if ($user->getPasswordRequestedAt() && !$user->isEmailSent()) {
            try {
                $this->userMailer->sendResetPasswordMail($user);
                $user->setEmailSent(true);
                $this->entityManager->flush($user);
            } catch (\Exception $e) {
                $this->logger->alert($e->getMessage());
            }
        }
    }
1295118
private static function setLoginSession(array $authConfig, $user)
    {
        $key = static::getFields($authConfig, true);
        $encrypted = encrypt($user->$key);
        session()->set('log_me_in', $encrypted);
    }