-
-
Notifications
You must be signed in to change notification settings - Fork 614
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from tnucera/master
Add cookie token extractor
- Loading branch information
Showing
5 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Lexik\Bundle\JWTAuthenticationBundle\Tests\TokenExtractor; | ||
|
||
use Lexik\Bundle\JWTAuthenticationBundle\TokenExtractor\CookieTokenExtractor; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* CookieTokenExtractorTest | ||
* | ||
* @author Nicolas Cabot <[email protected]> | ||
*/ | ||
class CookieTokenExtractorTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* test getRequestToken | ||
*/ | ||
public function testGetTokenRequest() | ||
{ | ||
$extractor = new CookieTokenExtractor('BEARER'); | ||
|
||
$request = new Request(); | ||
$this->assertFalse($extractor->extract($request)); | ||
|
||
$request = new Request(); | ||
$request->cookies->add(array('BEAR' => 'testtoken')); | ||
$this->assertFalse($extractor->extract($request)); | ||
|
||
$request = new Request(); | ||
$request->cookies->add(array('BEARER' => 'testtoken')); | ||
$this->assertEquals('testtoken', $extractor->extract($request)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Lexik\Bundle\JWTAuthenticationBundle\TokenExtractor; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* CookieTokenExtractor | ||
* | ||
* @author Nicolas Cabot <[email protected]> | ||
*/ | ||
class CookieTokenExtractor implements TokenExtractorInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $name; | ||
|
||
/** | ||
* @param string $name | ||
*/ | ||
public function __construct($name) | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
/** | ||
* @param Request $request | ||
* @return string | ||
*/ | ||
public function extract(Request $request) | ||
{ | ||
if (!$request->cookies->has($this->name)) { | ||
return false; | ||
} | ||
|
||
return $request->cookies->get($this->name); | ||
} | ||
} |