send json encoded token response

This commit is contained in:
Jonny Barnes 2018-01-31 22:21:57 +00:00
parent c4374613f5
commit 03cb6a2645
3 changed files with 27 additions and 17 deletions

View file

@ -5,8 +5,8 @@ declare(strict_types=1);
namespace App\Http\Controllers; namespace App\Http\Controllers;
use IndieAuth\Client; use IndieAuth\Client;
use Illuminate\Http\Response;
use App\Services\TokenService; use App\Services\TokenService;
use Illuminate\Http\JsonResponse;
class TokenEndpointController extends Controller class TokenEndpointController extends Controller
{ {
@ -37,9 +37,9 @@ class TokenEndpointController extends Controller
/** /**
* If the user has authd via the IndieAuth protocol, issue a valid token. * If the user has authd via the IndieAuth protocol, issue a valid token.
* *
* @return \Illuminate\Http\Response * @return \Illuminate\Http\JsonResponse
*/ */
public function create(): Response public function create(): JsonResponse
{ {
$authorizationEndpoint = $this->client->discoverAuthorizationEndpoint(normalize_url(request()->input('me'))); $authorizationEndpoint = $this->client->discoverAuthorizationEndpoint(normalize_url(request()->input('me')));
if ($authorizationEndpoint) { if ($authorizationEndpoint) {
@ -58,21 +58,22 @@ class TokenEndpointController extends Controller
'scope' => $scope, 'scope' => $scope,
]; ];
$token = $this->tokenService->getNewToken($tokenData); $token = $this->tokenService->getNewToken($tokenData);
$content = http_build_query([ $content = [
'me' => request()->input('me'), 'me' => request()->input('me'),
'scope' => $scope, 'scope' => $scope,
'access_token' => $token, 'access_token' => $token,
]); ];
return response($content)->header( return response()->json($content);
'Content-Type',
'application/x-www-form-urlencoded'
);
} }
return response('There was an error verifying the authorisation code.', 400); return response()->json([
'error' => 'There was an error verifying the authorisation code.'
], 401);
} }
return response('Cant determine the authorisation endpoint.', 400); return response()->json([
'error' => 'Cant determine the authorisation endpoint.'
], 400);
} }
} }

View file

@ -1,5 +1,8 @@
# Changelog # Changelog
## Version {next}
- Send tokens as a json response
## Version 0.15.6 (2018-01-27) ## Version 0.15.6 (2018-01-27)
- Fix uploading files sent to the media endpoint to S3 - Fix uploading files sent to the media endpoint to S3

View file

@ -5,6 +5,7 @@ namespace Tests\Feature;
use Mockery; use Mockery;
use Tests\TestCase; use Tests\TestCase;
use IndieAuth\Client; use IndieAuth\Client;
use Illuminate\Http\JsonResponse;
class TokenEndpointTest extends TestCase class TokenEndpointTest extends TestCase
{ {
@ -28,9 +29,10 @@ class TokenEndpointTest extends TestCase
'client_id' => config('app.url') . '/micropub-client', 'client_id' => config('app.url') . '/micropub-client',
'state' => mt_rand(1000, 10000), 'state' => mt_rand(1000, 10000),
]); ]);
parse_str($response->content(), $output); $response->assertJson([
$this->assertEquals(config('app.url'), $output['me']); 'me' => config('app.url'),
$this->assertTrue(array_key_exists('access_token', $output)); 'scope' => 'create update',
]);
} }
public function test_token_endpoint_returns_error_when_auth_endpoint_lacks_me_data() public function test_token_endpoint_returns_error_when_auth_endpoint_lacks_me_data()
@ -52,8 +54,10 @@ class TokenEndpointTest extends TestCase
'client_id' => config('app.url') . '/micropub-client', 'client_id' => config('app.url') . '/micropub-client',
'state' => mt_rand(1000, 10000), 'state' => mt_rand(1000, 10000),
]); ]);
$response->assertStatus(400); $response->assertStatus(401);
$response->assertSeeText('There was an error verifying the authorisation code.'); $response->assertJson([
'error' => 'There was an error verifying the authorisation code.'
]);
} }
public function test_token_endpoint_returns_error_when_no_auth_endpoint_found() public function test_token_endpoint_returns_error_when_no_auth_endpoint_found()
@ -72,6 +76,8 @@ class TokenEndpointTest extends TestCase
'state' => mt_rand(1000, 10000), 'state' => mt_rand(1000, 10000),
]); ]);
$response->assertStatus(400); $response->assertStatus(400);
$response->assertSeeText('Cant determine the authorisation endpoint.'); $response->assertJson([
'error' => 'Cant determine the authorisation endpoint.']
);
} }
} }