2015-12-20 12:17:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
|
2017-12-09 18:34:27 +00:00
|
|
|
use App\Http\Requests\API\LastfmCallbackRequest;
|
|
|
|
use App\Http\Requests\API\LastfmSetSessionKeyRequest;
|
2018-08-18 13:19:40 +00:00
|
|
|
use App\Services\LastfmService;
|
2015-12-20 12:17:35 +00:00
|
|
|
use Illuminate\Contracts\Auth\Guard;
|
2017-06-04 01:30:45 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
use Illuminate\Http\Response;
|
2018-08-19 16:05:10 +00:00
|
|
|
use Tymon\JWTAuth\Exceptions\JWTException;
|
2015-12-30 04:14:47 +00:00
|
|
|
use Tymon\JWTAuth\JWTAuth;
|
2015-12-20 12:17:35 +00:00
|
|
|
|
|
|
|
class LastfmController extends Controller
|
|
|
|
{
|
|
|
|
protected $auth;
|
2018-08-19 16:05:10 +00:00
|
|
|
private $lastfmService;
|
|
|
|
private $jwtAuth;
|
2015-12-20 12:17:35 +00:00
|
|
|
|
2018-08-19 16:05:10 +00:00
|
|
|
public function __construct(Guard $auth, LastfmService $lastfmService, JWTAuth $jwtAuth)
|
2015-12-20 12:17:35 +00:00
|
|
|
{
|
|
|
|
$this->auth = $auth;
|
2018-08-19 16:05:10 +00:00
|
|
|
$this->lastfmService = $lastfmService;
|
|
|
|
$this->jwtAuth = $jwtAuth;
|
2015-12-20 12:17:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect the current user to Last.fm.
|
|
|
|
*
|
2018-08-19 16:05:10 +00:00
|
|
|
* @throws JWTException
|
2018-08-19 16:05:54 +00:00
|
|
|
*
|
|
|
|
* @return RedirectResponse
|
2015-12-20 12:17:35 +00:00
|
|
|
*/
|
2018-08-19 16:05:10 +00:00
|
|
|
public function connect()
|
2015-12-20 12:17:35 +00:00
|
|
|
{
|
2018-08-19 16:05:10 +00:00
|
|
|
abort_unless($this->lastfmService->enabled(), 401, 'Koel is not configured to use with Last.fm yet.');
|
2015-12-30 04:14:47 +00:00
|
|
|
|
|
|
|
// A workaround to make sure Tymon's JWTAuth get the correct token via our custom
|
|
|
|
// "jwt-token" query string instead of the default "token".
|
|
|
|
// This is due to the problem that Last.fm returns the token via "token" as well.
|
2018-08-19 16:05:10 +00:00
|
|
|
$this->jwtAuth->parseToken('', '', 'jwt-token');
|
2015-12-30 04:14:47 +00:00
|
|
|
|
2018-08-19 16:05:10 +00:00
|
|
|
return redirect(
|
2015-12-20 12:17:35 +00:00
|
|
|
'https://www.last.fm/api/auth/?api_key='
|
2018-08-19 16:05:10 +00:00
|
|
|
.$this->lastfmService->getKey()
|
|
|
|
.'&cb='.urlencode(route('lastfm.callback').'?jwt-token='.$this->jwtAuth->getToken())
|
2015-12-20 12:17:35 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serve the callback request from Last.fm.
|
2016-03-06 04:11:28 +00:00
|
|
|
*
|
2017-12-09 18:34:27 +00:00
|
|
|
* @param LastfmCallbackRequest $request
|
2015-12-20 12:17:35 +00:00
|
|
|
*
|
2017-06-04 01:30:45 +00:00
|
|
|
* @return Response
|
2015-12-20 12:17:35 +00:00
|
|
|
*/
|
2018-08-19 16:05:10 +00:00
|
|
|
public function callback(LastfmCallbackRequest $request)
|
2015-12-20 12:17:35 +00:00
|
|
|
{
|
2018-08-19 16:05:10 +00:00
|
|
|
$sessionKey = $this->lastfmService->getSessionKey($request->token);
|
|
|
|
|
|
|
|
abort_unless($sessionKey, 500, 'Invalid token key.');
|
2015-12-20 12:17:35 +00:00
|
|
|
|
|
|
|
$this->auth->user()->savePreference('lastfm_session_key', $sessionKey);
|
|
|
|
|
|
|
|
return view('api.lastfm.callback');
|
|
|
|
}
|
|
|
|
|
2016-01-26 06:32:29 +00:00
|
|
|
/**
|
|
|
|
* Set the Last.fm session key of the current user.
|
2016-01-26 15:23:55 +00:00
|
|
|
*
|
2017-12-09 18:34:27 +00:00
|
|
|
* @param LastfmSetSessionKeyRequest $request
|
2016-01-26 15:23:55 +00:00
|
|
|
*
|
2017-06-04 01:30:45 +00:00
|
|
|
* @return JsonResponse
|
2016-01-26 06:32:29 +00:00
|
|
|
*/
|
2017-12-09 18:34:27 +00:00
|
|
|
public function setSessionKey(LastfmSetSessionKeyRequest $request)
|
2016-01-26 06:32:29 +00:00
|
|
|
{
|
2017-04-29 02:55:41 +00:00
|
|
|
$this->auth->user()->savePreference('lastfm_session_key', trim($request->key));
|
2016-01-26 06:32:29 +00:00
|
|
|
|
|
|
|
return response()->json();
|
|
|
|
}
|
|
|
|
|
2015-12-20 12:17:35 +00:00
|
|
|
/**
|
|
|
|
* Disconnect the current user from Last.fm.
|
2016-03-06 04:11:28 +00:00
|
|
|
*
|
2017-06-04 01:30:45 +00:00
|
|
|
* @return JsonResponse
|
2015-12-20 12:17:35 +00:00
|
|
|
*/
|
|
|
|
public function disconnect()
|
|
|
|
{
|
2015-12-21 02:17:12 +00:00
|
|
|
return response()->json($this->auth->user()->deletePreference('lastfm_session_key'));
|
2015-12-20 12:17:35 +00:00
|
|
|
}
|
|
|
|
}
|