]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/UserApiTokenController.php
Removed token 'client' text, avoid confusion w/ oAuth
[bookstack] / app / Http / Controllers / UserApiTokenController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use BookStack\Api\ApiToken;
4 use BookStack\Auth\User;
5 use Illuminate\Http\Request;
6 use Illuminate\Support\Carbon;
7 use Illuminate\Support\Facades\Hash;
8 use Illuminate\Support\Str;
9
10 class UserApiTokenController extends Controller
11 {
12
13     /**
14      * Show the form to create a new API token.
15      */
16     public function create(int $userId)
17     {
18         // Ensure user is has access-api permission and is the current user or has permission to manage the current user.
19         $this->checkPermission('access-api');
20         $this->checkPermissionOrCurrentUser('users-manage', $userId);
21
22         $user = User::query()->findOrFail($userId);
23         return view('users.api-tokens.create', [
24             'user' => $user,
25         ]);
26     }
27
28     /**
29      * Store a new API token in the system.
30      */
31     public function store(Request $request, int $userId)
32     {
33         $this->checkPermission('access-api');
34         $this->checkPermissionOrCurrentUser('users-manage', $userId);
35
36         $this->validate($request, [
37             'name' => 'required|max:250',
38             'expires_at' => 'date_format:Y-m-d',
39         ]);
40
41         $user = User::query()->findOrFail($userId);
42         $secret = Str::random(32);
43         $expiry = $request->get('expires_at', (Carbon::now()->addYears(100))->format('Y-m-d'));
44
45         $token = (new ApiToken())->forceFill([
46             'name' => $request->get('name'),
47             'token_id' => Str::random(32),
48             'secret' => Hash::make($secret),
49             'user_id' => $user->id,
50             'expires_at' => $expiry
51         ]);
52
53         while (ApiToken::query()->where('token_id', '=', $token->token_id)->exists()) {
54             $token->token_id = Str::random(32);
55         }
56
57         $token->save();
58         $token->refresh();
59
60         session()->flash('api-token-secret:' . $token->id, $secret);
61         $this->showSuccessNotification(trans('settings.user_api_token_create_success'));
62         return redirect($user->getEditUrl('/api-tokens/' . $token->id));
63     }
64
65     /**
66      * Show the details for a user API token, with access to edit.
67      */
68     public function edit(int $userId, int $tokenId)
69     {
70         [$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
71         $secret = session()->pull('api-token-secret:' . $token->id, null);
72
73         return view('users.api-tokens.edit', [
74             'user' => $user,
75             'token' => $token,
76             'model' => $token,
77             'secret' => $secret,
78         ]);
79     }
80
81     /**
82      * Update the API token.
83      */
84     public function update(Request $request, int $userId, int $tokenId)
85     {
86         $this->validate($request, [
87             'name' => 'required|max:250',
88             'expires_at' => 'date_format:Y-m-d',
89         ]);
90
91         [$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
92
93         $token->fill($request->all())->save();
94         $this->showSuccessNotification(trans('settings.user_api_token_update_success'));
95         return redirect($user->getEditUrl('/api-tokens/' . $token->id));
96     }
97
98     /**
99      * Show the delete view for this token.
100      */
101     public function delete(int $userId, int $tokenId)
102     {
103         [$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
104         return view('users.api-tokens.delete', [
105             'user' => $user,
106             'token' => $token,
107         ]);
108     }
109
110     /**
111      * Destroy a token from the system.
112      */
113     public function destroy(int $userId, int $tokenId)
114     {
115         [$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
116         $token->delete();
117
118         $this->showSuccessNotification(trans('settings.user_api_token_delete_success'));
119         return redirect($user->getEditUrl('#api_tokens'));
120     }
121
122     /**
123      * Check the permission for the current user and return an array
124      * where the first item is the user in context and the second item is their
125      * API token in context.
126      */
127     protected function checkPermissionAndFetchUserToken(int $userId, int $tokenId): array
128     {
129         $this->checkPermissionOr('users-manage', function () use ($userId) {
130             return $userId === user()->id && userCan('access-api');
131         });
132
133         $user = User::query()->findOrFail($userId);
134         $token = ApiToken::query()->where('user_id', '=', $user->id)->where('id', '=', $tokenId)->firstOrFail();
135         return [$user, $token];
136     }
137
138 }