1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace App\Http\Controllers\OAuth;
7
8use App\Http\Controllers\Controller;
9use App\Models\OAuth\Client;
10
11class ClientsController extends Controller
12{
13 public function __construct()
14 {
15 parent::__construct();
16
17 $this->middleware('auth');
18 $this->middleware('verify-user');
19 }
20
21 public function destroy($clientId)
22 {
23 $client = auth()->user()->oauthClients()->findOrFail($clientId);
24 $client->revoke();
25
26 return response(null, 204);
27 }
28
29 public function index()
30 {
31 return json_collection(auth()->user()->oauthClients()->where('revoked', false)->get(), 'OAuth\Client', ['redirect', 'secret']);
32 }
33
34 public function resetSecret($clientId)
35 {
36 $client = auth()->user()->oauthClients()->findOrFail($clientId);
37
38 if (!$client->resetSecret()) {
39 return error_popup(osu_trans('oauth.client.reset_failed'));
40 }
41
42 return json_item($client, 'OAuth\Client', ['redirect', 'secret']);
43 }
44
45 public function store()
46 {
47 $params = get_params(request()->all(), null, [
48 'name',
49 'redirect',
50 ]);
51
52 // from ClientRepository::create but with custom Client.
53 $client = (new Client())->forceFill([
54 'user_id' => auth()->user()->getKey(),
55 'name' => $params['name'] ?? null,
56 'secret' => str_random(40),
57 'redirect' => $params['redirect'] ?? '',
58 'personal_access_client' => false,
59 'password_client' => false,
60 'revoked' => false,
61 ]);
62
63 if (!$client->save()) {
64 return response([
65 'form_error' => $client->validationErrors()->all(),
66 ], 422);
67 }
68
69 return json_item($client, 'OAuth\Client', ['redirect', 'secret']);
70 }
71
72 public function update($clientId)
73 {
74 $client = auth()->user()->oauthClients()->findOrFail($clientId);
75
76 $params = request(['redirect']);
77
78 // client doesn't inherit from our base model.
79 if (!$client->fill($params)->save()) {
80 return response([
81 'form_error' => $client->validationErrors()->all(),
82 ], 422);
83 }
84
85 return json_item($client, 'OAuth\Client', ['redirect', 'secret']);
86 }
87}