Coder Social home page Coder Social logo

Changed version phpseclib 3 about jose-php HOT 9 OPEN

nov avatar nov commented on August 30, 2024
Changed version phpseclib 3

from jose-php.

Comments (9)

benjy avatar benjy commented on August 30, 2024 3

As a temporary workaround you can do that in your own project with

composer require phpseclib/phpseclib:^2.0

from jose-php.

darkdim avatar darkdim commented on August 30, 2024

"require": {
"php": ">=5.6",
"phpseclib/phpseclib": ">=2.0.0"
},
in new ver phpseclib used NS as phpseclib3
need to limit ver phpseclib

from jose-php.

terrafrost avatar terrafrost commented on August 30, 2024

composer require phpseclib/phpseclib2_compat:~1.0 should also work. That'd let you use the phpseclib v2 API with phpseclib v3

from jose-php.

nextofblake avatar nextofblake commented on August 30, 2024

Limiting the version to v2

composer require phpseclib/phpseclib:~2.0

Also phpseclib2_compat did not work for me

$rsa = new RSA();
$rsa->_convertPublicKey($n, $e) // method not found

from jose-php.

terrafrost avatar terrafrost commented on August 30, 2024

@nextofblake - the presence of the underscore means that that method is not an officially supported method. This can be traced back to the old PEAR coding standards. Quoting them, "Private class members are preceded by a single underscore".

phpseclib 3 doesn't follow this convention but phpseclib 1/2 did.

Anyway, I'd rewrite your code thusly:

$rsa = new RSA;
$rsa->load([
    'e' => new BigInteger('...'),
    'n' => new BigInteger('...')
]);

phpseclib 2 has supported this since the initial release of 2.0 (2.0.0).

from jose-php.

nextofblake avatar nextofblake commented on August 30, 2024

@terrafrost thanks for the tip however the issue is specific to this package.
inspect jose-php/src/JOSE/JWK.php

function toKey() {
... stuff
$pem_string = $rsa->_convertPublicKey($n, $e);
}

from jose-php.

kmuenkel avatar kmuenkel commented on August 30, 2024

I wrote an override class that addresses most of not all the compatibility issues. Note that it depends on Firebase\JWT to facilitate what RSA::_convertPublicKey() previously offered. And that some Reflection hackery was needed to regain access to now-protected properties.

<?php

namespace App\Overrides\JOSE;

use Error;
use JOSE_JWK;
use ReflectionClass;
use JOSE_URLSafeBase64;
use ReflectionException;
use phpseclib3\Crypt\RSA;
use UnexpectedValueException;
use phpseclib3\Crypt\RSA\PublicKey;
use Firebase\JWT\JWK as FirebaseJWK;
use phpseclib3\Crypt\PublicKeyLoader;
use JOSE_Exception_UnexpectedAlgorithm;

/**
 * Override to cope with changes from phpseclib/phpseclib v2 to 3
 */
class JWK extends JOSE_JWK
{
    /**
     * @inheritDoc
     * @throws ReflectionException
     */
    public static function encode($key, $extra_components = [])
    {
        try {
            return parent::encode($key, $extra_components);
        } catch (JOSE_Exception_UnexpectedAlgorithm $error) {
            //
        }

        if ($key instanceof RSA\PublicKey) {
            $modulus = static::getProtectedProperty($key, 'modulus');
            $exponent = static::getProtectedProperty($key, 'exponent');

            $components = [
                'kty' => 'RSA',
                'e' => JOSE_URLSafeBase64::encode($key->publicExponent->toBytes()),
                'n' => JOSE_URLSafeBase64::encode($modulus->toBytes())
            ];

            if ($exponent != $key->publicExponent) {
                $components = array_merge($components, ['d' => JOSE_URLSafeBase64::encode($exponent->toBytes())]);
            }

            return new static(array_merge($components, $extra_components));
        }

        throw new JOSE_Exception_UnexpectedAlgorithm('Unknown key type: '.get_class($key));
    }

    /**
     * @param object $object
     * @param string $propertyName
     * @return mixed
     * @throws ReflectionException
     */
    protected static function getProtectedProperty(object $object, string $propertyName)
    {
        $reflectionClass = app(ReflectionClass::class, ['argument' => $object]);
        $reflectionProperty = $reflectionClass->getProperty($propertyName);

        if ($reflectionProperty->isPublic()) {
            throw new UnexpectedValueException($reflectionClass->name."::$propertyName is public");
        }

        $reflectionProperty->setAccessible(true);

        return $reflectionProperty->getValue($object);
    }

    /**
     * @inheritDoc
     */
    public static function decode($components)
    {
        $jwk = new static($components);

        return $jwk->toKey();
    }

    /**
     * @inheritDoc
     */
    public function toKey()
    {
        try {
            return parent::toKey();
        } catch (Error $e) {
            //
        }

        switch ($this->components['kty']) {
            case 'RSA':
                $pemResource = FirebaseJWK::parseKey($this->components);
                $keyData = openssl_pkey_get_details($pemResource);
                $keyString = $keyData['key'];

                /** @var PublicKey $rsa */
                $rsa = PublicKeyLoader::loadPublicKey($keyString);

                return $rsa;
            default:
                throw new JOSE_Exception_UnexpectedAlgorithm('Unknown key type');
        }
    }
}

from jose-php.

Hailong avatar Hailong commented on August 30, 2024

Here is my PR to change to phpseclib 3, #45

from jose-php.

ilazaridis avatar ilazaridis commented on August 30, 2024

@nov any chance to merge the PR above and release a new version ?

from jose-php.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.