Skip to content

Commit

Permalink
Merge pull request #53 from hmic/patch-1
Browse files Browse the repository at this point in the history
Fix #43
  • Loading branch information
ADmad authored May 31, 2017
2 parents c010113 + 29bc9aa commit b49fbc9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Auth/JwtAuthenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public function getToken($request = null)
}

$header = $request->header($config['header']);
if ($header) {
if ($header && stripos($header, $config['prefix']) === 0) {
return $this->_token = str_ireplace($config['prefix'] . ' ', '', $header);
}

Expand Down
46 changes: 34 additions & 12 deletions tests/TestCase/Auth/JwtAuthenticateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ public function testAuthenticateTokenParameter()
$request = new Request('posts/index?tokenname=' . $this->token);
$result = $this->auth->getUser($request, $this->response);
$this->assertEquals($expected, $result);

$request = new Request('posts/index?wrongtoken=' . $this->token);
$result = $this->auth->getUser($request, $this->response);
$this->assertFalse($result);
}

/**
Expand All @@ -95,6 +99,10 @@ public function testAuthenticateTokenHeader()
$result = $this->auth->getUser($request, $this->response);
$this->assertEquals($expected, $result);

$request->env('HTTP_AUTHORIZATION', 'WrongBearer ' . $this->token);
$result = $this->auth->getUser($request, $this->response);
$this->assertFalse($result);

$this->setExpectedException('UnexpectedValueException');
$request->env('HTTP_AUTHORIZATION', 'Bearer foobar');
$result = $this->auth->getUser($request, $this->response);
Expand All @@ -117,6 +125,10 @@ public function testAuthenticateNoHeaderWithParameterDisabled()

$result = $this->auth->getUser($request, $this->response);
$this->assertFalse($result);

$request = new Request('posts/index?token=' . $this->token);
$result = $this->auth->getUser($request, $this->response);
$this->assertFalse($result);
}

/**
Expand All @@ -126,18 +138,20 @@ public function testAuthenticateNoHeaderWithParameterDisabled()
*/
public function testQueryDatasourceFalse()
{
$request = new Request('posts/index');

$expected = [
'id' => 99,
'username' => 'ADmad',
'group' => ['name' => 'admin'],
'id' => 99,
'username' => 'ADmad',
'group' => ['name' => 'admin'],
];
$request->env(
'HTTP_AUTHORIZATION',
'Bearer ' . JWT::encode($expected, Security::salt())
);
$token = JWT::encode($expected, Security::salt());
$this->auth->config('queryDatasource', false);

$request = new Request('posts/index');
$request->env('HTTP_AUTHORIZATION', 'Bearer ' . $token);
$result = $this->auth->getUser($request, $this->response);
$this->assertEquals($expected, $result);

$request = new Request('posts/index?token=' . $token);
$result = $this->auth->getUser($request, $this->response);
$this->assertEquals($expected, $result);
}
Expand All @@ -149,12 +163,16 @@ public function testQueryDatasourceFalse()
*/
public function testWithValidTokenButNoUserInDb()
{
$request = new Request('posts/index');

$token = JWT::encode(['id' => 4], Security::salt());

$request = new Request('posts/index');
$request->env('HTTP_AUTHORIZATION', 'Bearer ' . $token);
$result = $this->auth->getUser($request, $this->response);
$this->assertFalse($result);

$request = new Request('posts/index?token=' . $token);
$result = $this->auth->getUser($request, $this->response);
$this->assertFalse($result);
}

/**
Expand Down Expand Up @@ -270,9 +288,13 @@ public function testCustomKey()

$payload = ['sub' => 100];
$token = Jwt::encode($payload, $key);
$request = new Request();

$request = new Request('posts/index');
$request->env('HTTP_AUTHORIZATION', 'Bearer ' . $token);
$result = $auth->getUser($request, $this->response);
$this->assertEquals($payload, $result);

$request = new Request('posts/index?token=' . $token);
$result = $auth->getUser($request, $this->response);
$this->assertEquals($payload, $result);
}
Expand Down

0 comments on commit b49fbc9

Please sign in to comment.