Skip to content

Commit

Permalink
Improve JWK parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Bui Sy Nguyen committed Sep 30, 2015
1 parent 521384d commit 7964d02
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/JWK.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,28 @@ public static function parseKeySet($source)
$source = [$source];
}

if(isset($source['keys']))
$source = $source['keys'];

foreach($source as $k=>$v)
if(is_array($source))
{
if(!is_string($k))
if(isset($source['keys']))
$source = $source['keys'];

foreach($source as $k=>$v)
{
if(is_array($v) && isset($v['kid']))
$k = $v['kid'];
elseif(is_object($v) && property_exists($v,'kid'))
$k = $v->{'kid'};
if(!is_string($k))
{
if(is_array($v) && isset($v['kid']))
$k = $v['kid'];
elseif(is_object($v) && property_exists($v,'kid'))
$k = $v->{'kid'};
}
$v = self::parseKey($v);
$keys[$k] = $v;
}
$v = self::parseKey($v);
$keys[$k] = $v;

return $keys;
}

return $keys;
throw new UnexpectedValueException('Failed to parse JWK');
}

/**
Expand All @@ -64,9 +69,10 @@ public static function parseKey($source)
{
if(!is_array($source))
$source = (array)$source;
if(isset($source['kty']) && isset($source['n']) && isset($source['e']))
if(!empty($source) && isset($source['kty']) && isset($source['n']) && isset($source['e']))
{
switch ($source['kty']) {
switch ($source['kty'])
{
case 'RSA':
if (array_key_exists('d', $source))
throw new UnexpectedValueException('Failed to parse JWK: RSA private key is not supported');
Expand Down

0 comments on commit 7964d02

Please sign in to comment.