Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Allow secret to be also an array #138

Merged
merged 1 commit into from
Jan 26, 2019
Merged

Allow secret to be also an array #138

merged 1 commit into from
Jan 26, 2019

Conversation

TiMESPLiNTER
Copy link
Contributor

@TiMESPLiNTER TiMESPLiNTER commented Oct 17, 2018

Remove type hint for string in the secret() method as firebase/php-jwt allows an array of secrets for decoding the token:

https://github.com/firebase/php-jwt/blob/master/src/JWT.php#L99

I need it as I want to provide an array of secrets provided by a JKWS file. firebase/php-jwt will then automatically choose the right one out of the given ones.

@tuupola
Copy link
Owner

tuupola commented Oct 17, 2018

I need to ponder a bit if this is a BC break. I tend to think not, since public api does not change, so everything should be ok.

@TiMESPLiNTER
Copy link
Contributor Author

TiMESPLiNTER commented Oct 17, 2018

I think also it's not a BC as it opens up something and doesn't narrow it down and it's a private function in the class whos signature is affected.

@tuupola
Copy link
Owner

tuupola commented Oct 17, 2018

Also related: #128

@TiMESPLiNTER
Copy link
Contributor Author

TiMESPLiNTER commented Oct 17, 2018

Regarding #128 I also played around a bit with that callable idea but it just adds complexity which is not necessary to solve the problem imo. Then the algorithm option would also need to be a callable because the algorithm can also come from the JWK.

This is how I solve it now:

class MiddlewareServiceProvider implements ServiceProviderInterface
{

    /**
     * Registers services on the given container.
     *
     * This method should only be used to configure services and parameters.
     * It should not get services.
     *
     * @param Container $container A container instance
     * @return void
     */
    public function register(Container $container)
    {
        $container[JwtAuthentication::class] = function () use ($container) {
            return new JwtAuthentication([
                'secret' => $this->getSecret($container['authentication.jwks.url']),
                'algorithm' => ['RS256'],
                'path' => ['/private'],
            ]);
        };
    }

    private function getSecret(string $jwksUrl): array
    {
        if (null === $jwksData = json_decode(file_get_contents($jwksUrl), true)) {
            throw new \RuntimeException(sprintf('Could not read JWKS information from "%s"', $jwksUrl));
        }

        $secret = [];

        foreach ($jwksData['keys'] as $jwk) {
            $secret[$jwk['kid']] = $this->formatCert($jwk['x5c'][0]);
        }

        return $secret;
    }

    private function formatCert(string $certStr): string
    {
        $formattedCert = implode("\n", str_split($certStr, 64));
        return "-----BEGIN CERTIFICATE-----\n".$formattedCert."\n-----END CERTIFICATE-----";
    }
}

@TiMESPLiNTER
Copy link
Contributor Author

Any update on this? 🙂

@tuupola
Copy link
Owner

tuupola commented Dec 22, 2018

Not forgotten, been busy with other work. I think this change should be ok. The secret() method should probably still throw an InvalidArgumentException if something else than string or array is passed.

@tuupola tuupola self-assigned this Dec 22, 2018
tuupola added a commit that referenced this pull request Dec 28, 2018
@tuupola tuupola merged commit b1f5928 into tuupola:3.x Jan 26, 2019
tuupola added a commit that referenced this pull request Jan 26, 2019
* Add test cases for secrets as array (see #138)
* Make sure secret is either string or array
@tuupola
Copy link
Owner

tuupola commented Jan 26, 2019

Released as 3.2.0. Thanks!

@TiMESPLiNTER TiMESPLiNTER deleted the accept-array-of-secrets branch January 29, 2019 08:25
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants