Passed
Push — master ( 3e245e...ef140d )
by Phan
07:34
created

app/Http/Controllers/API/LastfmController.php (3 issues)

Labels
1
<?php
2
3
namespace App\Http\Controllers\API;
4
5
use App\Http\Requests\API\LastfmCallbackRequest;
6
use App\Http\Requests\API\LastfmSetSessionKeyRequest;
7
use App\Services\LastfmService;
8
use Illuminate\Contracts\Auth\Guard;
9
use Illuminate\Http\JsonResponse;
10
use Illuminate\Http\RedirectResponse;
11
use Tymon\JWTAuth\Exceptions\JWTException;
12
use Tymon\JWTAuth\JWTAuth;
13
14
/**
15
 * @group Last.fm integration
16
 */
17
class LastfmController extends Controller
18
{
19
    protected $auth;
20
    private $lastfmService;
21
    private $jwtAuth;
22
23 4
    public function __construct(Guard $auth, LastfmService $lastfmService, JWTAuth $jwtAuth)
24
    {
25 4
        $this->auth = $auth;
26 4
        $this->lastfmService = $lastfmService;
27 4
        $this->jwtAuth = $jwtAuth;
28 4
    }
29
30
    /**
31
     * Connect to Last.fm.
32
     *
33
     * [Connect](https://www.last.fm/api/authentication) the current user to Last.fm.
34
     * This is actually NOT an API request. The application should instead redirect the current user to this route,
35
     * which will send them to Last.fm for authentication. After authentication is successful, the user will be
36
     * redirected back to `api/lastfm/callback?token=<Last.fm token>`.
37
     *
38
     * @queryParam jwt-token required The JWT token of the user.
39
     *
40
     * @throws JWTException
41
     *
42
     * @return RedirectResponse
43
     */
44 1
    public function connect()
45
    {
46 1
        abort_unless($this->lastfmService->enabled(), 401, 'Koel is not configured to use with Last.fm yet.');
47
48
        // A workaround to make sure Tymon's JWTAuth get the correct token via our custom
49
        // "jwt-token" query string instead of the default "token".
50
        // This is due to the problem that Last.fm returns the token via "token" as well.
51 1
        $this->jwtAuth->parseToken('', '', 'jwt-token');
52
53 1
        $callbackUrl = urlencode(sprintf('%s?jwt-token=%s', route('lastfm.callback'), $this->jwtAuth->getToken()));
54 1
        $url = sprintf('https://www.last.fm/api/auth/?api_key=%s&cb=%s', $this->lastfmService->getKey(), $callbackUrl);
55
56 1
        return redirect($url);
57
    }
58
59
    /**
60
     * Serve the callback request from Last.fm.
61
     */
62 1
    public function callback(LastfmCallbackRequest $request)
63
    {
64 1
        $sessionKey = $this->lastfmService->getSessionKey($request->token);
65
66 1
        abort_unless($sessionKey, 500, 'Invalid token key.');
0 ignored issues
show
It seems like $sessionKey can also be of type string; however, parameter $boolean of abort_unless() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
        abort_unless(/** @scrutinizer ignore-type */ $sessionKey, 500, 'Invalid token key.');
Loading history...
67
68 1
        $this->auth->user()->savePreference('lastfm_session_key', $sessionKey);
0 ignored issues
show
The method savePreference() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
        $this->auth->user()->/** @scrutinizer ignore-call */ savePreference('lastfm_session_key', $sessionKey);
Loading history...
69
70 1
        return view('api.lastfm.callback');
71
    }
72
73
    /**
74
     * Set Last.fm session key.
75
     *
76
     * Set the Last.fm session key for the current user. This call should be made after the user is
77
     * [connected to Last.fm](https://www.last.fm/api/authentication).
78
     *
79
     * @bodyParam key string required The Last.fm [session key](https://www.last.fm/api/show/auth.getSession).
80
     * @response []
81
     *
82
     * @param LastfmSetSessionKeyRequest $request
83
     *
84
     * @return JsonResponse
85
     */
86 1
    public function setSessionKey(LastfmSetSessionKeyRequest $request)
87
    {
88 1
        $this->auth->user()->savePreference('lastfm_session_key', trim($request->key));
89
90 1
        return response()->json();
91
    }
92
93
    /**
94
     * Disconnect the current user from Last.fm.
95
     *
96
     * @return JsonResponse
97
     */
98 1
    public function disconnect()
99
    {
100 1
        $this->auth->user()->deletePreference('lastfm_session_key');
0 ignored issues
show
The method deletePreference() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

100
        $this->auth->user()->/** @scrutinizer ignore-call */ deletePreference('lastfm_session_key');
Loading history...
101
102 1
        return response()->json();
103
    }
104
}
105