Passed
Push — master ( e9591a...c0a685 )
by Rutger
13:20
created

Oauth2JwksAction::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2.0014

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 12
c 2
b 0
f 0
dl 0
loc 22
ccs 13
cts 14
cp 0.9286
rs 9.8666
cc 2
nc 2
nop 0
crap 2.0014
1
<?php
2
3
namespace rhertogh\Yii2Oauth2Server\controllers\web\certificates;
4
5
use Jose\Component\Core\JWK;
6
use Jose\Component\Core\JWKSet;
7
use rhertogh\Yii2Oauth2Server\controllers\web\Oauth2CertificatesController;
8
use yii\base\Action;
9
use yii\base\InvalidConfigException;
10
use yii\helpers\StringHelper;
11
12
/**
13
 * @property Oauth2CertificatesController $controller
14
 */
15
class Oauth2JwksAction extends Action
16
{
17
    /**
18
     * RFC7517: JSON Web Key (JWK)
19
     * https://datatracker.ietf.org/doc/html/rfc7517
20
     *
21
     * For algorithms see RFC7518: JSON Web Algorithms - Parameters for RSA Keys
22
     * https://datatracker.ietf.org/doc/html/rfc7518#section-6.3.
23
     */
24 4
    public function run()
25
    {
26 4
        if (!extension_loaded('openssl')) {
27
            throw new InvalidConfigException('JWKS functionality requires the openssl extension to be loaded in PHP.');
28
        }
29
30 4
        $module = $this->controller->module;
31
32 4
        $publicKey = $module->getPublicKey();
33
34 4
        $keyInfo = openssl_pkey_get_details(openssl_pkey_get_public($publicKey->getKeyContents()));
35
36 4
        $keys = [new JWK([
37
            // ToDo 'kid' => '', // https://datatracker.ietf.org/doc/html/rfc7517#section-4.5.
38 4
            'kty' => 'RSA',
39 4
            'alg' => 'RS256', // https://datatracker.ietf.org/doc/html/rfc7518#section-6.3.
40 4
            'use' => 'sig',
41 4
            'n' => rtrim(StringHelper::base64UrlEncode($keyInfo['rsa']['n']), '='),
42 4
            'e' => rtrim(StringHelper::base64UrlEncode($keyInfo['rsa']['e']), '='),
43 4
        ])];
44
45 4
        return new JWKSet($keys);
46
    }
47
}
48