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 return view('users.api-tokens.create', [
27 'back' => $this->getRedirectPath($user),
32 * Store a new API token in the system.
34 public function store(Request $request, int $userId)
36 $this->checkPermission('access-api');
37 $this->checkPermissionOrCurrentUser('users-manage', $userId);
39 $this->validate($request, [
40 'name' => ['required', 'max:250'],
41 'expires_at' => ['date_format:Y-m-d'],
44 $user = User::query()->findOrFail($userId);
45 $secret = Str::random(32);
47 $token = (new ApiToken())->forceFill([
48 'name' => $request->get('name'),
49 'token_id' => Str::random(32),
50 'secret' => Hash::make($secret),
51 'user_id' => $user->id,
52 'expires_at' => $request->get('expires_at') ?: ApiToken::defaultExpiry(),
55 while (ApiToken::query()->where('token_id', '=', $token->token_id)->exists()) {
56 $token->token_id = Str::random(32);
61 session()->flash('api-token-secret:' . $token->id, $secret);
62 $this->logActivity(ActivityType::API_TOKEN_CREATE, $token);
64 return redirect($token->getUrl());
68 * Show the details for a user API token, with access to edit.
70 public function edit(Request $request, int $userId, int $tokenId)
72 $this->updateContext($request);
74 [$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
75 $secret = session()->pull('api-token-secret:' . $token->id, null);
77 return view('users.api-tokens.edit', [
82 'back' => $this->getRedirectPath($user),
87 * Update the API token.
89 public function update(Request $request, int $userId, int $tokenId)
91 $this->validate($request, [
92 'name' => ['required', 'max:250'],
93 'expires_at' => ['date_format:Y-m-d'],
96 [$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
98 'name' => $request->get('name'),
99 'expires_at' => $request->get('expires_at') ?: ApiToken::defaultExpiry(),
102 $this->logActivity(ActivityType::API_TOKEN_UPDATE, $token);
104 return redirect($token->getUrl());
108 * Show the delete view for this token.
110 public function delete(int $userId, int $tokenId)
112 [$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
114 return view('users.api-tokens.delete', [
121 * Destroy a token from the system.
123 public function destroy(int $userId, int $tokenId)
125 [$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
128 $this->logActivity(ActivityType::API_TOKEN_DELETE, $token);
130 return redirect($this->getRedirectPath($user));
134 * Check the permission for the current user and return an array
135 * where the first item is the user in context and the second item is their
136 * API token in context.
138 protected function checkPermissionAndFetchUserToken(int $userId, int $tokenId): array
140 $this->checkPermissionOr('users-manage', function () use ($userId) {
141 return $userId === user()->id && userCan('access-api');
144 $user = User::query()->findOrFail($userId);
145 $token = ApiToken::query()->where('user_id', '=', $user->id)->where('id', '=', $tokenId)->firstOrFail();
147 return [$user, $token];
151 * Update the context for where the user is coming from to manage API tokens.
152 * (Track of location for correct return redirects)
154 protected function updateContext(Request $request): void
156 $context = $request->query('context');
158 session()->put('api-token-context', $context);
163 * Get the redirect path for the current api token editing session.
164 * Attempts to recall the context of where the user is editing from.
166 protected function getRedirectPath(User $relatedUser): string
168 $context = session()->get('api-token-context');
169 if ($context === 'settings' || user()->id !== $relatedUser->id) {
170 return $relatedUser->getEditUrl('#api_tokens');
173 return url('/my-account/auth#api_tokens');