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;
|
2020-09-06 18:21:39 +00:00
|
|
|
use App\Models\User;
|
2018-08-18 13:19:40 +00:00
|
|
|
use App\Services\LastfmService;
|
2020-09-06 18:21:39 +00:00
|
|
|
use App\Services\TokenManager;
|
|
|
|
use Illuminate\Contracts\Auth\Authenticatable;
|
2017-06-04 01:30:45 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Illuminate\Http\RedirectResponse;
|
2015-12-20 12:17:35 +00:00
|
|
|
|
2018-12-09 21:24:43 +00:00
|
|
|
/**
|
|
|
|
* @group Last.fm integration
|
|
|
|
*/
|
2015-12-20 12:17:35 +00:00
|
|
|
class LastfmController extends Controller
|
|
|
|
{
|
2018-08-19 16:05:10 +00:00
|
|
|
private $lastfmService;
|
2020-09-06 18:21:39 +00:00
|
|
|
private $tokenManager;
|
2015-12-20 12:17:35 +00:00
|
|
|
|
2020-09-06 18:21:39 +00:00
|
|
|
/** @var User */
|
|
|
|
private $currentUser;
|
|
|
|
|
|
|
|
public function __construct(LastfmService $lastfmService, TokenManager $tokenManager, Authenticatable $currentUser)
|
2018-09-04 06:25:24 +00:00
|
|
|
{
|
2018-08-19 16:05:10 +00:00
|
|
|
$this->lastfmService = $lastfmService;
|
2020-09-06 18:21:39 +00:00
|
|
|
$this->tokenManager = $tokenManager;
|
|
|
|
$this->currentUser = $currentUser;
|
2015-12-20 12:17:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-12 08:18:17 +00:00
|
|
|
* Connect to Last.fm
|
2018-12-09 21:24:43 +00:00
|
|
|
*
|
|
|
|
* [Connect](https://www.last.fm/api/authentication) the current user to Last.fm.
|
|
|
|
* This is actually NOT an API request. The application should instead redirect the current user to this route,
|
|
|
|
* which will send them to Last.fm for authentication. After authentication is successful, the user will be
|
|
|
|
* redirected back to `api/lastfm/callback?token=<Last.fm token>`.
|
|
|
|
*
|
2020-09-06 18:21:39 +00:00
|
|
|
* @queryParam jwt-token required The JWT token of the user. (Deprecated. Use api_token instead).
|
|
|
|
* @queryParam api_token required Authentication token of the current user.
|
2020-04-07 21:38:16 +00:00
|
|
|
* @response []
|
2015-12-20 12:17:35 +00:00
|
|
|
*
|
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
|
|
|
|
2020-09-06 18:21:39 +00:00
|
|
|
$callbackUrl = urlencode(sprintf(
|
|
|
|
'%s?api_token=%s',
|
|
|
|
route('lastfm.callback'),
|
|
|
|
request('api_token')
|
|
|
|
));
|
2015-12-30 04:14:47 +00:00
|
|
|
|
2018-09-04 02:24:29 +00:00
|
|
|
$url = sprintf('https://www.last.fm/api/auth/?api_key=%s&cb=%s', $this->lastfmService->getKey(), $callbackUrl);
|
|
|
|
|
|
|
|
return redirect($url);
|
2015-12-20 12:17:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serve the callback request from Last.fm.
|
|
|
|
*/
|
2018-08-19 16:05:10 +00:00
|
|
|
public function callback(LastfmCallbackRequest $request)
|
2015-12-20 12:17:35 +00:00
|
|
|
{
|
2018-09-04 06:25:24 +00:00
|
|
|
$sessionKey = $this->lastfmService->getSessionKey($request->token);
|
2018-08-19 16:05:10 +00:00
|
|
|
|
|
|
|
abort_unless($sessionKey, 500, 'Invalid token key.');
|
2015-12-20 12:17:35 +00:00
|
|
|
|
2020-09-06 18:21:39 +00:00
|
|
|
$this->currentUser->savePreference('lastfm_session_key', $sessionKey);
|
2015-12-20 12:17:35 +00:00
|
|
|
|
|
|
|
return view('api.lastfm.callback');
|
|
|
|
}
|
|
|
|
|
2016-01-26 06:32:29 +00:00
|
|
|
/**
|
2020-04-12 08:18:17 +00:00
|
|
|
* Set Last.fm session key
|
2018-12-09 21:24:43 +00:00
|
|
|
*
|
|
|
|
* Set the Last.fm session key for the current user. This call should be made after the user is
|
|
|
|
* [connected to Last.fm](https://www.last.fm/api/authentication).
|
|
|
|
*
|
|
|
|
* @bodyParam key string required The Last.fm [session key](https://www.last.fm/api/show/auth.getSession).
|
|
|
|
* @response []
|
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
|
|
|
{
|
2020-09-06 18:21:39 +00:00
|
|
|
$this->currentUser->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()
|
|
|
|
{
|
2020-09-06 18:21:39 +00:00
|
|
|
$this->currentUser->deletePreference('lastfm_session_key');
|
2018-09-04 02:25:34 +00:00
|
|
|
|
|
|
|
return response()->json();
|
2015-12-20 12:17:35 +00:00
|
|
|
}
|
|
|
|
}
|