1 <?php namespace BookStack\Http\Controllers;
3 use BookStack\Actions\ActivityType;
4 use BookStack\Api\ApiToken;
5 use BookStack\Auth\User;
6 use Illuminate\Http\Request;
7 use Illuminate\Support\Facades\Hash;
8 use Illuminate\Support\Str;
10 class UserApiTokenController extends Controller
14 * Show the form to create a new API token.
16 public function create(int $userId)
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);
22 $user = User::query()->findOrFail($userId);
23 return view('users.api-tokens.create', [
29 * Store a new API token in the system.
31 public function store(Request $request, int $userId)
33 $this->checkPermission('access-api');
34 $this->checkPermissionOrCurrentUser('users-manage', $userId);
36 $this->validate($request, [
37 'name' => 'required|max:250',
38 'expires_at' => 'date_format:Y-m-d',
41 $user = User::query()->findOrFail($userId);
42 $secret = Str::random(32);
44 $token = (new ApiToken())->forceFill([
45 'name' => $request->get('name'),
46 'token_id' => Str::random(32),
47 'secret' => Hash::make($secret),
48 'user_id' => $user->id,
49 'expires_at' => $request->get('expires_at') ?: ApiToken::defaultExpiry(),
52 while (ApiToken::query()->where('token_id', '=', $token->token_id)->exists()) {
53 $token->token_id = Str::random(32);
58 session()->flash('api-token-secret:' . $token->id, $secret);
59 $this->showSuccessNotification(trans('settings.user_api_token_create_success'));
60 $this->logActivity(ActivityType::API_TOKEN_CREATE, $token);
62 return redirect($user->getEditUrl('/api-tokens/' . $token->id));
66 * Show the details for a user API token, with access to edit.
68 public function edit(int $userId, int $tokenId)
70 [$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
71 $secret = session()->pull('api-token-secret:' . $token->id, null);
73 return view('users.api-tokens.edit', [
82 * Update the API token.
84 public function update(Request $request, int $userId, int $tokenId)
86 $this->validate($request, [
87 'name' => 'required|max:250',
88 'expires_at' => 'date_format:Y-m-d',
91 [$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
93 'name' => $request->get('name'),
94 'expires_at' => $request->get('expires_at') ?: ApiToken::defaultExpiry(),
97 $this->showSuccessNotification(trans('settings.user_api_token_update_success'));
98 $this->logActivity(ActivityType::API_TOKEN_UPDATE, $token);
99 return redirect($user->getEditUrl('/api-tokens/' . $token->id));
103 * Show the delete view for this token.
105 public function delete(int $userId, int $tokenId)
107 [$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
108 return view('users.api-tokens.delete', [
115 * Destroy a token from the system.
117 public function destroy(int $userId, int $tokenId)
119 [$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
122 $this->showSuccessNotification(trans('settings.user_api_token_delete_success'));
123 $this->logActivity(ActivityType::API_TOKEN_DELETE, $token);
125 return redirect($user->getEditUrl('#api_tokens'));
129 * Check the permission for the current user and return an array
130 * where the first item is the user in context and the second item is their
131 * API token in context.
133 protected function checkPermissionAndFetchUserToken(int $userId, int $tokenId): array
135 $this->checkPermissionOr('users-manage', function () use ($userId) {
136 return $userId === user()->id && userCan('access-api');
139 $user = User::query()->findOrFail($userId);
140 $token = ApiToken::query()->where('user_id', '=', $user->id)->where('id', '=', $tokenId)->firstOrFail();
141 return [$user, $token];