koel/app/Http/Controllers/LastfmController.php

55 lines
1.6 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
use App\Http\Requests\API\LastfmCallbackRequest;
use App\Models\User;
use App\Services\LastfmService;
use App\Services\TokenManager;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Http\Response;
class LastfmController extends Controller
{
2022-07-29 06:47:10 +00:00
/** @param User $currentUser */
public function __construct(
2024-04-18 14:36:28 +00:00
private readonly LastfmService $lastfm,
private readonly TokenManager $tokenManager,
private readonly ?Authenticatable $currentUser
2022-07-29 06:47:10 +00:00
) {
}
public function connect()
{
abort_unless(
2022-08-08 16:00:59 +00:00
LastfmService::enabled(),
Response::HTTP_NOT_IMPLEMENTED,
'Koel is not configured to use with Last.fm yet.'
);
$callbackUrl = urlencode(sprintf(
'%s?api_token=%s',
route('lastfm.callback'),
2020-10-26 15:29:29 +00:00
// create a temporary token that can be deleted later
$this->tokenManager->createToken($this->currentUser)->plainTextToken
));
2022-08-08 16:00:59 +00:00
$url = sprintf('https://www.last.fm/api/auth/?api_key=%s&cb=%s', config('koel.lastfm.key'), $callbackUrl);
return redirect($url);
}
public function callback(LastfmCallbackRequest $request)
{
$sessionKey = $this->lastfm->getSessionKey($request->token);
2021-06-05 10:47:56 +00:00
abort_unless((bool) $sessionKey, Response::HTTP_INTERNAL_SERVER_ERROR, 'Invalid token key.');
$this->lastfm->setUserSessionKey($this->currentUser, $sessionKey);
// delete the tmp. token we created earlier
$this->tokenManager->deleteTokenByPlainTextToken($request->api_token);
return view('lastfm.callback');
}
}