Passed
Push — master ( 63d167...fb60c6 )
by Rutger
03:11
created

Oauth2JwksAction   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 2
eloc 13
c 2
b 0
f 0
dl 0
loc 31
ccs 13
cts 14
cp 0.9286
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 22 2
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\base\Oauth2BaseWebAction;
8
use rhertogh\Yii2Oauth2Server\controllers\web\Oauth2CertificatesController;
9
use rhertogh\Yii2Oauth2Server\interfaces\controllers\web\certificates\Oauth2JwksActionInterface;
10
use yii\base\InvalidConfigException;
11
use yii\helpers\StringHelper;
12
13
/**
14
 * @property Oauth2CertificatesController $controller
15
 */
16
class Oauth2JwksAction extends Oauth2BaseWebAction implements Oauth2JwksActionInterface
17
{
18
    /**
19
     * RFC7517: JSON Web Key (JWK)
20
     * https://datatracker.ietf.org/doc/html/rfc7517
21
     *
22
     * For algorithms see RFC7518: JSON Web Algorithms - Parameters for RSA Keys
23
     * https://datatracker.ietf.org/doc/html/rfc7518#section-6.3.
24
     */
25 4
    public function run()
26
    {
27 4
        if (!extension_loaded('openssl')) {
28
            throw new InvalidConfigException('JWKS functionality requires the openssl extension to be loaded in PHP.');
29
        }
30
31 4
        $module = $this->controller->module;
32
33 4
        $publicKey = $module->getPublicKey();
34
35 4
        $keyInfo = openssl_pkey_get_details(openssl_pkey_get_public($publicKey->getKeyContents()));
36
37 4
        $keys = [new JWK([
38
            // ToDo 'kid' => '', // https://datatracker.ietf.org/doc/html/rfc7517#section-4.5.
39 4
            'kty' => 'RSA',
40 4
            'alg' => 'RS256', // https://datatracker.ietf.org/doc/html/rfc7518#section-6.3.
41 4
            'use' => 'sig',
42 4
            'n' => rtrim(StringHelper::base64UrlEncode($keyInfo['rsa']['n']), '='),
43 4
            'e' => rtrim(StringHelper::base64UrlEncode($keyInfo['rsa']['e']), '='),
44 4
        ])];
45
46 4
        return new JWKSet($keys);
47
    }
48
}
49