Skip to content
Snippets Groups Projects
Select Git revision
  • 1108cd9bb8f0afd682b37e216421da59a55dee20
  • main default protected
  • Kelvinrr-patch-3
  • radius_update
  • revert-616-apollo_pan
  • vims
  • 0.10
  • Kelvinrr-patch-2
  • revert-563-minirf_fix
  • Kelvinrr-patch-1
  • 0.9
  • acpaquette-patch-3
  • acpaquette-patch-2
  • acpaquette-patch-1
  • spiceql
  • ci-coverage
  • 0.10.0
  • 0.9.1
  • 0.9.0
  • 0.8.7
  • 0.8.8
  • 0.8.6
  • 0.8.3
  • 0.8.4
  • 0.8.5
  • 0.8.2
  • 0.8.1
  • 0.8.0
  • 0.7.3
  • 0.7.2
  • 0.7.1
  • 0.7.0
  • 0.6.5
  • 0.6.4
  • 0.6.3
  • 0.6.2
36 results

setup.py

Blame
  • JWKSHandler.php 2.01 KiB
    <?php
    
    namespace RAP;
    
    use phpseclib\Crypt\RSA;
    
    /**
     * Manages the JWT Key Sets (currently only RSA).
     */
    class JWKSHandler {
    
        private $locator;
    
        public function __construct(Locator $locator) {
            $this->locator = $locator;
        }
    
        public function generateKeyPair() {
    
            $rsa = new RSA();
    
            $rsa->setPrivateKeyFormat(RSA::PRIVATE_FORMAT_PKCS1);
            $rsa->setPublicKeyFormat(RSA::PUBLIC_FORMAT_PKCS8);
            // Guacamole needs a key of at least 2048
            $result = $rsa->createKey(2048);
    
            $keyPair = new RSAKeyPair();
            $keyPair->alg = 'RS256';
            $keyPair->privateKey = $result['privatekey'];
            $keyPair->publicKey = $result['publickey'];
            $keyPair->keyId = bin2hex(random_bytes(8));
    
            $dao = $this->locator->getJWKSDAO();
            $dao->insertRSAKeyPair($keyPair);
    
            return $keyPair;
        }
    
        public function getJWKS() {
    
            $dao = $this->locator->getJWKSDAO();
    
            $keyPairs = $dao->getRSAKeyPairs();
    
            $keys = [];
            foreach ($keyPairs as $keyPair) {
    
                $rsa = new RSA();
                $rsa->loadKey($keyPair->publicKey);
                $rsa->setPublicKey();
                $publicKeyXML = $rsa->getPublicKey(RSA::PUBLIC_FORMAT_XML);
    
                $rsaModulus = $this->getTagContent($publicKeyXML, "Modulus");
                $rsaExponent = $this->getTagContent($publicKeyXML, "Exponent");
    
                $urisafeModulus = strtr($rsaModulus, '+/', '-_');
    
                $jwk = [];
                $jwk['kty'] = "RSA";
                $jwk['kid'] = $keyPair->keyId;
                $jwk['use'] = "sig";
                $jwk['n'] = $urisafeModulus;
                $jwk['e'] = $rsaExponent;
    
                array_push($keys, $jwk);
            }
    
            return [
                "keys" => $keys
            ];
        }
    
        private function getTagContent(string $publicKeyXML, string $tagname): string {
            $matches = [];
            $pattern = "#<\s*?$tagname\b[^>]*>(.*?)</$tagname\b[^>]*>#s";
            preg_match($pattern, $publicKeyXML, $matches);
            return $matches[1];
        }
    
    }