8000 Add token creator · Lucky-Loek/Symfony-API-Example@1541720 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Dec 17, 2019. It is now read-only.

Commit 1541720

Browse files
author
Loek van der Linde
committed
Add token creator
1 parent bedb1bc commit 1541720

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace AppBundle\Controller;
4+
5+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
6+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
7+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8+
use Symfony\Component\HttpFoundation\JsonResponse;
9+
use Symfony\Component\HttpFoundation\Request;
10+
use Symfony\Component\HttpFoundation\Response;
11+
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
12+
13+
class TokenController extends Controller
14+
{
15+
/**
16+
* @Route("api/token")
17+
* @Method("POST")
18+
*/
19+
public function newTokenAction(Request $request)
20+
{
21+
$repository = $this->getDoctrine()->getRepository('AppBundle:User');
22+
$user = $repository->findOneBy(['username' => $request->getUser()]);
23+
24+
if (is_null($user)) {
25+
throw new BadCredentialsException();
26+
}
27+
28+
$encoder = $this->get('security.password_encoder');
29+
$passwordValid = $encoder->isPasswordValid($user, $request->getPassword());
30+
31+
if (!$passwordValid) {
32+
throw new BadCredentialsException();
33+
}
34+
35+
$tokenEncoder = $this->get('lexik_jwt_authentication.encoder');
36+
$token = $tokenEncoder->encode([
37+
'username' => $user->getUsername(),
38+
'exp' => time() + 600
39+
]);
40+
41+
return new JsonResponse(['token' => $token]);
42+
}
43+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace tests\AppBundle\Controller;
4+
use GuzzleHttp\Client;
5+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
6+
7+
class TokenControllerTest extends \PHPUnit_Framework_TestCase
8+
{
9+
/**
10+
* @var Client
11+
*/
12+
protected $client;
13+
14+
public function setUp() {
15+
$this->client = new Client([
16+
'base_uri' => 'http://symfony.app'
17+
]);
18+
}
19+
20+
/**
21+
* @test
22+
*/
23+
public function shouldCreateTokenOnValidCredentials()
24+
{
25+
$response = $this->client->post('/api/token', [
26+
'auth' => ['admin', 'unsafepassword']
27+
]);
28+
29+
$body = $response->getBody()->getContents();
30+
$body = json_decode($body);
31+
32+
$this->assertEquals(200, $response->getStatusCode());
33+
$this->assertObjectHasAttribute('token', $body);
34+
}
35+
36+
/**
37+
* @test
38+
*/
39+
public function shouldThrowExceptionOnInvalidCredentials()
40+
{
41+
$response = $this->client->post('/api/token', [
42+
'auth' => ['admin', 'reallysafepassword']
43+
]);
44+
45+
$this->assertEquals(401, $response->getStatusCode());
46+
}
47+
}

0 commit comments

Comments
 (0)
0