2015-12-20 12:17:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
|
2015-12-20 12:30:28 +00:00
|
|
|
use App\Services\Lastfm;
|
2015-12-20 12:17:35 +00:00
|
|
|
use Illuminate\Contracts\Auth\Guard;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Routing\Redirector;
|
2015-12-30 04:14:47 +00:00
|
|
|
use Tymon\JWTAuth\JWTAuth;
|
2015-12-20 12:17:35 +00:00
|
|
|
|
|
|
|
class LastfmController extends Controller
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The Guard implementation.
|
2016-03-06 04:11:28 +00:00
|
|
|
*
|
2015-12-20 12:17:35 +00:00
|
|
|
* @var Guard
|
|
|
|
*/
|
|
|
|
protected $auth;
|
|
|
|
|
|
|
|
/**
|
2015-12-20 16:07:20 +00:00
|
|
|
* Construct the controller and inject the current auth.
|
2016-03-06 04:11:28 +00:00
|
|
|
*
|
2015-12-21 13:50:26 +00:00
|
|
|
* @param Guard $auth
|
2015-12-20 12:17:35 +00:00
|
|
|
*/
|
|
|
|
public function __construct(Guard $auth)
|
|
|
|
{
|
|
|
|
$this->auth = $auth;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect the current user to Last.fm.
|
|
|
|
*
|
|
|
|
* @param Redirector $redirector
|
|
|
|
* @param Lastfm $lastfm
|
2015-12-30 04:14:47 +00:00
|
|
|
* @param JWTAuth $auth
|
2015-12-20 12:17:35 +00:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2015-12-30 04:14:47 +00:00
|
|
|
public function connect(Redirector $redirector, Lastfm $lastfm, JWTAuth $auth = null)
|
2015-12-20 12:17:35 +00:00
|
|
|
{
|
2016-03-11 10:17:54 +00:00
|
|
|
abort_unless($lastfm->enabled(), 401, 'Koel is not configured to use with Last.fm yet.');
|
2015-12-20 12:17:35 +00:00
|
|
|
|
2015-12-30 04:14:47 +00:00
|
|
|
$auth = $auth ?: $this->app['tymon.jwt.auth'];
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
$auth->parseToken('', '', 'jwt-token');
|
|
|
|
|
2015-12-20 12:17:35 +00:00
|
|
|
return $redirector->to(
|
|
|
|
'https://www.last.fm/api/auth/?api_key='
|
|
|
|
.$lastfm->getKey()
|
2015-12-30 04:14:47 +00:00
|
|
|
.'&cb='.urlencode(route('lastfm.callback').'?jwt-token='.$auth->getToken())
|
2015-12-20 12:17:35 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serve the callback request from Last.fm.
|
2016-03-06 04:11:28 +00:00
|
|
|
*
|
2015-12-20 12:17:35 +00:00
|
|
|
* @param Request $request
|
|
|
|
* @param Lastfm $lastfm
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function callback(Request $request, Lastfm $lastfm)
|
|
|
|
{
|
2016-03-11 10:17:54 +00:00
|
|
|
abort_unless($token = $request->input('token'), 500, 'Something wrong happened.');
|
2015-12-20 12:17:35 +00:00
|
|
|
|
|
|
|
// Get the session key using the obtained token.
|
2016-03-11 10:17:54 +00:00
|
|
|
abort_unless($sessionKey = $lastfm->getSessionKey($token), 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
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2016-01-26 06:32:29 +00:00
|
|
|
*/
|
|
|
|
public function setSessionKey(Request $request)
|
|
|
|
{
|
2016-01-26 15:23:55 +00:00
|
|
|
$this->auth->user()->savePreference('lastfm_session_key', trim($request->input('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
|
|
|
*
|
2015-12-20 12:17:35 +00:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|