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
6declare(strict_types=1);
7
8namespace App\Http\Controllers;
9
10use App\Exceptions\ModelNotSavedException;
11use App\Transformers\LegacyApiKeyTransformer;
12use Auth;
13use Request;
14
15class LegacyApiKeyController extends Controller
16{
17 public function __construct()
18 {
19 $this->middleware('auth');
20 $this->middleware('verify-user');
21 }
22
23 public function destroy()
24 {
25 Auth::user()->apiKeys()->available()->update(['revoked' => true]);
26
27 return response(null, 204);
28 }
29
30 public function store()
31 {
32 priv_check('LegacyApiKeyStore')->ensureCan();
33
34 $params = get_params(Request::all(), 'legacy_api_key', [
35 'app_name',
36 'app_url',
37 ]);
38 $apiKey = Auth::user()->apiKeys()->make([
39 ...$params,
40 'api_key' => bin2hex(random_bytes(20)),
41 ]);
42
43 try {
44 $apiKey->saveOrExplode();
45 } catch (ModelNotSavedException $e) {
46 return ModelNotSavedException::makeResponse($e, ['legacy_api_key' => $apiKey]);
47 }
48
49 return json_item($apiKey, new LegacyApiKeyTransformer());
50 }
51}