3 namespace BookStack\Api;
5 use BookStack\Activity\ActivityType;
6 use BookStack\Http\Controller;
7 use BookStack\Users\Models\User;
8 use Illuminate\Http\Request;
9 use Illuminate\Support\Facades\Hash;
10 use Illuminate\Support\Str;
12 class UserApiTokenController extends Controller
15 * Show the form to create a new API token.
17 public function create(int $userId)
19 // Ensure user is has access-api permission and is the current user or has permission to manage the current user.
20 $this->checkPermission('access-api');
21 $this->checkPermissionOrCurrentUser('users-manage', $userId);
23 $user = User::query()->findOrFail($userId);
25 return view('users.api-tokens.create', [
31 * Store a new API token in the system.
33 public function store(Request $request, int $userId)
35 $this->checkPermission('access-api');
36 $this->checkPermissionOrCurrentUser('users-manage', $userId);
38 $this->validate($request, [
39 'name' => ['required', 'max:250'],
40 'expires_at' => ['date_format:Y-m-d'],
43 $user = User::query()->findOrFail($userId);
44 $secret = Str::random(32);
46 $token = (new ApiToken())->forceFill([
47 'name' => $request->get('name'),
48 'token_id' => Str::random(32),
49 'secret' => Hash::make($secret),
50 'user_id' => $user->id,
51 'expires_at' => $request->get('expires_at') ?: ApiToken::defaultExpiry(),
54 while (ApiToken::query()->where('token_id', '=', $token->token_id)->exists()) {
55 $token->token_id = Str::random(32);
60 session()->flash('api-token-secret:' . $token->id, $secret);
61 $this->logActivity(ActivityType::API_TOKEN_CREATE, $token);
63 return redirect($user->getEditUrl('/api-tokens/' . $token->id));
67 * Show the details for a user API token, with access to edit.
69 public function edit(int $userId, int $tokenId)
71 [$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
72 $secret = session()->pull('api-token-secret:' . $token->id, null);
74 return view('users.api-tokens.edit', [
83 * Update the API token.
85 public function update(Request $request, int $userId, int $tokenId)
87 $this->validate($request, [
88 'name' => ['required', 'max:250'],
89 'expires_at' => ['date_format:Y-m-d'],
92 [$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
94 'name' => $request->get('name'),
95 'expires_at' => $request->get('expires_at') ?: ApiToken::defaultExpiry(),
98 $this->logActivity(ActivityType::API_TOKEN_UPDATE, $token);
100 return redirect($user->getEditUrl('/api-tokens/' . $token->id));
104 * Show the delete view for this token.
106 public function delete(int $userId, int $tokenId)
108 [$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
110 return view('users.api-tokens.delete', [
117 * Destroy a token from the system.
119 public function destroy(int $userId, int $tokenId)
121 [$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
124 $this->logActivity(ActivityType::API_TOKEN_DELETE, $token);
126 return redirect($user->getEditUrl('#api_tokens'));
130 * Check the permission for the current user and return an array
131 * where the first item is the user in context and the second item is their
132 * API token in context.
134 protected function checkPermissionAndFetchUserToken(int $userId, int $tokenId): array
136 $this->checkPermissionOr('users-manage', function () use ($userId) {
137 return $userId === user()->id && userCan('access-api');
140 $user = User::query()->findOrFail($userId);
141 $token = ApiToken::query()->where('user_id', '=', $user->id)->where('id', '=', $tokenId)->firstOrFail();
143 return [$user, $token];