]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/UserApiTokenController.php
Built out interfaces & endpoints for API token managment
[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('manage-users', $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('manage-users', $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             'client_id' => Str::random(32),
48             'client_secret' => Hash::make($secret),
49             'user_id' => $user->id,
50             'expires_at' => $expiry
51         ]);
52
53         while (ApiToken::query()->where('client_id', '=', $token->client_id)->exists()) {
54             $token->client_id = Str::random(32);
55         }
56
57         $token->save();
58         // TODO - Notification and activity?
59         session()->flash('api-token-secret:' . $token->id, $secret);
60         return redirect($user->getEditUrl('/api-tokens/' . $token->id));
61     }
62
63     /**
64      * Show the details for a user API token, with access to edit.
65      */
66     public function edit(int $userId, int $tokenId)
67     {
68         [$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
69         $secret = session()->pull('api-token-secret:' . $token->id, null);
70
71         return view('users.api-tokens.edit', [
72             'user' => $user,
73             'token' => $token,
74             'model' => $token,
75             'secret' => $secret,
76         ]);
77     }
78
79     /**
80      * Update the API token.
81      */
82     public function update(Request $request, int $userId, int $tokenId)
83     {
84         $this->validate($request, [
85             'name' => 'required|max:250',
86             'expires_at' => 'date_format:Y-m-d',
87         ]);
88
89         [$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
90
91         $token->fill($request->all())->save();
92         // TODO - Notification and activity?
93         return redirect($user->getEditUrl('/api-tokens/' . $token->id));
94     }
95
96     /**
97      * Show the delete view for this token.
98      */
99     public function delete(int $userId, int $tokenId)
100     {
101         [$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
102         return view('users.api-tokens.delete', [
103             'user' => $user,
104             'token' => $token,
105         ]);
106     }
107
108     /**
109      * Destroy a token from the system.
110      */
111     public function destroy(int $userId, int $tokenId)
112     {
113         [$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
114         $token->delete();
115
116         // TODO - Notification and activity?, Might have text in translations already (user_api_token_delete_success)
117         return redirect($user->getEditUrl('#api_tokens'));
118     }
119
120     /**
121      * Check the permission for the current user and return an array
122      * where the first item is the user in context and the second item is their
123      * API token in context.
124      */
125     protected function checkPermissionAndFetchUserToken(int $userId, int $tokenId): array
126     {
127         $this->checkPermission('access-api');
128         $this->checkPermissionOrCurrentUser('manage-users', $userId);
129
130         $user = User::query()->findOrFail($userId);
131         $token = ApiToken::query()->where('user_id', '=', $user->id)->where('id', '=', $tokenId)->firstOrFail();
132         return [$user, $token];
133     }
134
135 }