Skip to content

Commit

Permalink
Disabling none by default
Browse files Browse the repository at this point in the history
  • Loading branch information
odino committed Feb 17, 2015
1 parent ce5cef8 commit 35ea443
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Namshi/JOSE/JWS.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function getTokenString()
* @return JWS
* @throws \InvalidArgumentException
*/
public static function load($jwsTokenString)
public static function load($jwsTokenString, $allowUnsecure = false)
{
$encoder = strpbrk($jwsTokenString, '+/=') ? new Base64Encoder() : new Base64UrlSafeEncoder();
$parts = explode('.', $jwsTokenString);
Expand All @@ -94,6 +94,10 @@ public static function load($jwsTokenString)
$payload = json_decode($encoder->decode($parts[1]), true);

if (is_array($header) && is_array($payload)) {
if ($header['alg'] === 'None' && !$allowUnsecure) {
throw new InvalidArgumentException(sprintf('The token "%s" cannot be validated in a secure context, as it uses the unallowed "none" algorithm', $jwsTokenString));
}

$jws = new self($header['alg'], isset($header['typ']) ? $header['typ'] : null);
$jws->setEncoder($encoder);
$jws->setPayload($payload);
Expand Down
34 changes: 34 additions & 0 deletions tests/Namshi/JOSE/Test/JWSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,40 @@ public function setup()
$this->jws->setPayload($data);
}

/**
* @expectedException InvalidArgumentException
*/
public function testLoadingUnsecureJws()
{
$date = new DateTime('tomorrow');
$data = array(
'a' => 'b',
'exp' => $date->format('U')
);
$this->jws = new JWS('None');
$this->jws->setPayload($data);
$this->jws->sign('111');
$jws = JWS::load($this->jws->getTokenString());
$this->assertFalse($jws->verify('111'));
$payload = $jws->getPayload();
$this->assertEquals('b', $payload['a']);
}
public function testAllowingUnsecureJws()
{
$date = new DateTime('tomorrow');
$data = array(
'a' => 'b',
'exp' => $date->format('U')
);
$this->jws = new JWS('None');
$this->jws->setPayload($data);
$this->jws->sign('111');
$jws = JWS::load($this->jws->getTokenString(), true);
$this->assertTrue($jws->verify('111'));
$payload = $jws->getPayload();
$this->assertEquals('b', $payload['a']);
}

public function testVerificationRS256()
{
$privateKey = openssl_pkey_get_private(SSL_KEYS_PATH . "private.key", self::SSL_KEY_PASSPHRASE);
Expand Down

0 comments on commit 35ea443

Please sign in to comment.