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(Request $request, int $userId)
19 $this->checkPermission('access-api');
20 $this->checkPermissionOrCurrentUser('users-manage', $userId);
21 $this->updateContext($request);
23 $user = User::query()->findOrFail($userId);
25 $this->setPageTitle(trans('settings.user_api_token_create'));
27 return view('users.api-tokens.create', [
29 'back' => $this->getRedirectPath($user),
34 * Store a new API token in the system.
36 public function store(Request $request, int $userId)
38 $this->checkPermission('access-api');
39 $this->checkPermissionOrCurrentUser('users-manage', $userId);
41 $this->validate($request, [
42 'name' => ['required', 'max:250'],
43 'expires_at' => ['date_format:Y-m-d'],
46 $user = User::query()->findOrFail($userId);
47 $secret = Str::random(32);
49 $token = (new ApiToken())->forceFill([
50 'name' => $request->get('name'),
51 'token_id' => Str::random(32),
52 'secret' => Hash::make($secret),
53 'user_id' => $user->id,
54 'expires_at' => $request->get('expires_at') ?: ApiToken::defaultExpiry(),
57 while (ApiToken::query()->where('token_id', '=', $token->token_id)->exists()) {
58 $token->token_id = Str::random(32);
63 session()->flash('api-token-secret:' . $token->id, $secret);
64 $this->logActivity(ActivityType::API_TOKEN_CREATE, $token);
66 return redirect($token->getUrl());
70 * Show the details for a user API token, with access to edit.
72 public function edit(Request $request, int $userId, int $tokenId)
74 $this->updateContext($request);
76 [$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
77 $secret = session()->pull('api-token-secret:' . $token->id, null);
79 $this->setPageTitle(trans('settings.user_api_token'));
81 return view('users.api-tokens.edit', [
86 'back' => $this->getRedirectPath($user),
91 * Update the API token.
93 public function update(Request $request, int $userId, int $tokenId)
95 $this->validate($request, [
96 'name' => ['required', 'max:250'],
97 'expires_at' => ['date_format:Y-m-d'],
100 [$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
102 'name' => $request->get('name'),
103 'expires_at' => $request->get('expires_at') ?: ApiToken::defaultExpiry(),
106 $this->logActivity(ActivityType::API_TOKEN_UPDATE, $token);
108 return redirect($token->getUrl());
112 * Show the delete view for this token.
114 public function delete(int $userId, int $tokenId)
116 [$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
118 $this->setPageTitle(trans('settings.user_api_token_delete'));
120 return view('users.api-tokens.delete', [
127 * Destroy a token from the system.
129 public function destroy(int $userId, int $tokenId)
131 [$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
134 $this->logActivity(ActivityType::API_TOKEN_DELETE, $token);
136 return redirect($this->getRedirectPath($user));
140 * Check the permission for the current user and return an array
141 * where the first item is the user in context and the second item is their
142 * API token in context.
144 protected function checkPermissionAndFetchUserToken(int $userId, int $tokenId): array
146 $this->checkPermissionOr('users-manage', function () use ($userId) {
147 return $userId === user()->id && userCan('access-api');
150 $user = User::query()->findOrFail($userId);
151 $token = ApiToken::query()->where('user_id', '=', $user->id)->where('id', '=', $tokenId)->firstOrFail();
153 return [$user, $token];
157 * Update the context for where the user is coming from to manage API tokens.
158 * (Track of location for correct return redirects)
160 protected function updateContext(Request $request): void
162 $context = $request->query('context');
164 session()->put('api-token-context', $context);
169 * Get the redirect path for the current api token editing session.
170 * Attempts to recall the context of where the user is editing from.
172 protected function getRedirectPath(User $relatedUser): string
174 $context = session()->get('api-token-context');
175 if ($context === 'settings' || user()->id !== $relatedUser->id) {
176 return $relatedUser->getEditUrl('#api_tokens');
179 return url('/my-account/auth#api_tokens');