Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions app/Access/Oidc/OidcJwtSigningKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BookStack\Access\Oidc;

use Illuminate\Support\Facades\Log;
use phpseclib3\Crypt\Common\PublicKey;
use phpseclib3\Crypt\PublicKeyLoader;
use phpseclib3\Crypt\RSA;
Expand Down Expand Up @@ -63,8 +64,9 @@ protected function loadFromJwkArray(array $jwk)
// 'alg' is optional for a JWK, but we will still attempt to validate if
// it exists otherwise presume it will be compatible.
$alg = $jwk['alg'] ?? null;
if ($jwk['kty'] !== 'RSA' || !(is_null($alg) || $alg === 'RS256')) {
throw new OidcInvalidKeyException("Only RS256 keys are currently supported. Found key using {$alg}");
$algorithm = OidcJwtSigningKeyAlgorithm::tryFrom($alg ?? OidcJwtSigningKeyAlgorithm::RS256->value);
if ($jwk['kty'] !== 'RSA' || $algorithm === null) {
throw new OidcInvalidKeyException("Only " . OidcJwtSigningKeyAlgorithm::getSupportedAlgorithms() . " keys are currently supported. Found key using {$alg}");
}

// 'use' is optional for a JWK but we assume 'sig' where no value exists since that's what
Expand Down Expand Up @@ -97,7 +99,16 @@ protected function loadFromJwkArray(array $jwk)
throw new OidcInvalidKeyException('Key loaded from file path is not an RSA key as expected');
}

$this->key = $key->withPadding(RSA::SIGNATURE_PKCS1);
// apply key-algorithm depending hash
$key = match ($algorithm) {
OidcJwtSigningKeyAlgorithm::RS256 => $key->withHash('sha256'),
OidcJwtSigningKeyAlgorithm::RS512 => $key->withHash('sha512'),
};
// apply key-algorithm depending padding
$this->key = match ($algorithm) {
OidcJwtSigningKeyAlgorithm::RS256,
OidcJwtSigningKeyAlgorithm::RS512 => $key->withPadding(RSA::SIGNATURE_PKCS1),
};
}

/**
Expand Down
16 changes: 16 additions & 0 deletions app/Access/Oidc/OidcJwtSigningKeyAlgorithm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace BookStack\Access\Oidc;

use UnitEnum;

enum OidcJwtSigningKeyAlgorithm: string
{
case RS256 = 'RS256';
case RS512 = 'RS512';

public static function getSupportedAlgorithms(): string
{
return join(',', array_map(static fn (UnitEnum $enum) => $enum->value, self::cases()));
}
}
4 changes: 2 additions & 2 deletions app/Access/Oidc/OidcJwtWithClaims.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ protected function validateTokenStructure(): void
*/
protected function validateTokenSignature(): void
{
if ($this->header['alg'] !== 'RS256') {
throw new OidcInvalidTokenException("Only RS256 signature validation is supported. Token reports using {$this->header['alg']}");
if (OidcJwtSigningKeyAlgorithm::tryFrom($this->header['alg']) === null) {
throw new OidcInvalidTokenException("Only " . OidcJwtSigningKeyAlgorithm::getSupportedAlgorithms() . " signature validation is supported. Token reports using {$this->header['alg']}");
}

$parsedKeys = array_map(function ($key) {
Expand Down
4 changes: 2 additions & 2 deletions app/Access/Oidc/OidcProviderSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ protected function loadSettingsFromIssuerDiscovery(ClientInterface $httpClient):
protected function filterKeys(array $keys): array
{
return array_filter($keys, function (array $key) {
$alg = $key['alg'] ?? 'RS256';
$alg = $key['alg'] ?? OidcJwtSigningKeyAlgorithm::RS256->value;
$use = $key['use'] ?? 'sig';

return $key['kty'] === 'RSA' && $use === 'sig' && $alg === 'RS256';
return $key['kty'] === 'RSA' && $use === 'sig' && OidcJwtSigningKeyAlgorithm::tryFrom($alg) !== null;
});
}

Expand Down