Refactor slightly the indieauth code

This commit is contained in:
Jonny Barnes 2017-03-01 20:59:09 +00:00
parent 4f57905bcc
commit fe8f871e3a
5 changed files with 70 additions and 59 deletions

View file

@ -2,7 +2,6 @@
namespace App\Http\Controllers;
use IndieAuth\Client;
use Illuminate\Http\Request;
use App\Services\TokenService;
use App\Services\IndieAuthService;
@ -14,11 +13,6 @@ class IndieAuthController extends Controller
*/
protected $indieAuthService;
/**
* The IndieAuth Client implementation.
*/
protected $client;
/**
* The Token handling service.
*/
@ -28,16 +22,14 @@ class IndieAuthController extends Controller
* Inject the dependencies.
*
* @param \App\Services\IndieAuthService $indieAuthService
* @param \IndieAuth\Client $client
* @param \App\Services\TokenService $tokenService
* @return void
*/
public function __construct(
IndieAuthService $indieAuthService = null,
Client $client = null,
TokenService $tokenService = null
) {
$this->indieAuthService = $indieAuthService ?? new IndieAuthService();
$this->client = $client ?? new Client();
$this->tokenService = $tokenService ?? new TokenService();
}
@ -53,21 +45,19 @@ class IndieAuthController extends Controller
public function start(Request $request)
{
$authorizationEndpoint = $this->indieAuthService->getAuthorizationEndpoint(
$request->input('me'),
$this->client
$request->input('me')
);
if ($authorizationEndpoint) {
if ($authorizationEndpoint !== null) {
$authorizationURL = $this->indieAuthService->buildAuthorizationURL(
$authorizationEndpoint,
$request->input('me'),
$this->client
$request->input('me')
);
if ($authorizationURL) {
return redirect($authorizationURL);
}
}
return redirect(route('micropub-client'))->withErrors('Unable to determine authorisation endpoint', 'indieauth');
return redirect(route('micropub-client'))->with('error', 'Unable to determine authorisation endpoint');
}
/**
@ -80,14 +70,17 @@ class IndieAuthController extends Controller
public function callback(Request $request)
{
if ($request->session()->get('state') != $request->input('state')) {
return redirect(route('micropub-client'))->withErrors(
'Invalid <code>state</code> value returned from indieauth server',
'indieauth'
return redirect(route('micropub-client'))->with(
'error',
'Invalid <code>state</code> value returned from indieauth server'
);
}
$tokenEndpoint = $this->indieAuthService->getTokenEndpoint($request->input('me'), $this->client);
$tokenEndpoint = $this->indieAuthService->getTokenEndpoint($request->input('me'));
if ($tokenEndpoint === false) {
return redirect(route('micropub-client'))->withErrors('Unable to determine token endpoint', 'indieauth');
return redirect(route('micropub-client'))->with(
'error',
'Unable to determine token endpoint'
);
}
$data = [
'endpoint' => $tokenEndpoint,
@ -97,7 +90,7 @@ class IndieAuthController extends Controller
'client_id' => route('micropub-client'),
'state' => $request->input('state'),
];
$token = $this->indieAuthService->getAccessToken($data, $this->client);
$token = $this->indieAuthService->getAccessToken($data);
if (array_key_exists('access_token', $token)) {
$request->session()->put('me', $token['me']);
@ -106,7 +99,10 @@ class IndieAuthController extends Controller
return redirect(route('micropub-client'));
}
return redirect(route('micropub-client'))->withErrors('Unable to get a token from the endpoint', 'indieauth');
return redirect(route('micropub-client'))->with(
'error',
'Unable to get a token from the endpoint'
);
}
/**
@ -136,7 +132,7 @@ class IndieAuthController extends Controller
'client_id' => $request->input('client_id'),
'state' => $request->input('state'),
];
$auth = $this->indieAuthService->verifyIndieAuthCode($authData, $this->client);
$auth = $this->indieAuthService->verifyIndieAuthCode($authData);
if (array_key_exists('me', $auth)) {
$scope = $auth['scope'] ?? '';
$tokenData = [