2021-06-26 11:23:15 -04:00
|
|
|
<?php
|
|
|
|
|
2023-05-17 12:56:55 -04:00
|
|
|
namespace BookStack\Api;
|
2019-12-29 08:02:26 -05:00
|
|
|
|
2023-05-17 12:56:55 -04:00
|
|
|
use BookStack\Activity\ActivityType;
|
2023-05-18 15:53:39 -04:00
|
|
|
use BookStack\Http\Controller;
|
2023-05-17 12:56:55 -04:00
|
|
|
use BookStack\Users\Models\User;
|
2019-12-29 08:02:26 -05:00
|
|
|
use Illuminate\Http\Request;
|
2019-12-29 12:03:52 -05:00
|
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
use Illuminate\Support\Str;
|
2019-12-29 08:02:26 -05:00
|
|
|
|
|
|
|
class UserApiTokenController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Show the form to create a new API token.
|
|
|
|
*/
|
2023-10-19 06:31:45 -04:00
|
|
|
public function create(Request $request, int $userId)
|
2019-12-29 08:02:26 -05:00
|
|
|
{
|
|
|
|
$this->checkPermission('access-api');
|
2019-12-29 14:46:46 -05:00
|
|
|
$this->checkPermissionOrCurrentUser('users-manage', $userId);
|
2023-10-19 06:31:45 -04:00
|
|
|
$this->updateContext($request);
|
2019-12-29 08:02:26 -05:00
|
|
|
|
2019-12-29 12:03:52 -05:00
|
|
|
$user = User::query()->findOrFail($userId);
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2019-12-29 12:03:52 -05:00
|
|
|
return view('users.api-tokens.create', [
|
|
|
|
'user' => $user,
|
2023-10-19 06:31:45 -04:00
|
|
|
'back' => $this->getRedirectPath($user),
|
2019-12-29 12:03:52 -05:00
|
|
|
]);
|
2019-12-29 08:02:26 -05:00
|
|
|
}
|
|
|
|
|
2019-12-29 12:03:52 -05:00
|
|
|
/**
|
|
|
|
* Store a new API token in the system.
|
|
|
|
*/
|
|
|
|
public function store(Request $request, int $userId)
|
|
|
|
{
|
|
|
|
$this->checkPermission('access-api');
|
2019-12-29 14:46:46 -05:00
|
|
|
$this->checkPermissionOrCurrentUser('users-manage', $userId);
|
2019-12-29 12:03:52 -05:00
|
|
|
|
|
|
|
$this->validate($request, [
|
2021-11-04 20:26:55 -04:00
|
|
|
'name' => ['required', 'max:250'],
|
|
|
|
'expires_at' => ['date_format:Y-m-d'],
|
2019-12-29 12:03:52 -05:00
|
|
|
]);
|
|
|
|
|
|
|
|
$user = User::query()->findOrFail($userId);
|
|
|
|
$secret = Str::random(32);
|
2019-12-29 15:18:37 -05:00
|
|
|
|
2019-12-29 12:03:52 -05:00
|
|
|
$token = (new ApiToken())->forceFill([
|
2021-06-26 11:23:15 -04:00
|
|
|
'name' => $request->get('name'),
|
|
|
|
'token_id' => Str::random(32),
|
|
|
|
'secret' => Hash::make($secret),
|
|
|
|
'user_id' => $user->id,
|
2019-12-30 09:51:28 -05:00
|
|
|
'expires_at' => $request->get('expires_at') ?: ApiToken::defaultExpiry(),
|
2019-12-29 12:03:52 -05:00
|
|
|
]);
|
|
|
|
|
2019-12-29 15:07:28 -05:00
|
|
|
while (ApiToken::query()->where('token_id', '=', $token->token_id)->exists()) {
|
|
|
|
$token->token_id = Str::random(32);
|
2019-12-29 12:03:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$token->save();
|
2019-12-29 14:46:46 -05:00
|
|
|
|
2019-12-29 12:03:52 -05:00
|
|
|
session()->flash('api-token-secret:' . $token->id, $secret);
|
2020-11-20 13:53:01 -05:00
|
|
|
$this->logActivity(ActivityType::API_TOKEN_CREATE, $token);
|
|
|
|
|
2023-10-19 06:31:45 -04:00
|
|
|
return redirect($token->getUrl());
|
2019-12-29 12:03:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the details for a user API token, with access to edit.
|
|
|
|
*/
|
2023-10-19 06:31:45 -04:00
|
|
|
public function edit(Request $request, int $userId, int $tokenId)
|
2019-12-29 12:03:52 -05:00
|
|
|
{
|
2023-10-19 06:31:45 -04:00
|
|
|
$this->updateContext($request);
|
|
|
|
|
2019-12-29 12:03:52 -05:00
|
|
|
[$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
|
|
|
|
$secret = session()->pull('api-token-secret:' . $token->id, null);
|
|
|
|
|
|
|
|
return view('users.api-tokens.edit', [
|
2021-06-26 11:23:15 -04:00
|
|
|
'user' => $user,
|
|
|
|
'token' => $token,
|
|
|
|
'model' => $token,
|
2019-12-29 12:03:52 -05:00
|
|
|
'secret' => $secret,
|
2023-10-19 06:31:45 -04:00
|
|
|
'back' => $this->getRedirectPath($user),
|
2019-12-29 12:03:52 -05:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the API token.
|
|
|
|
*/
|
|
|
|
public function update(Request $request, int $userId, int $tokenId)
|
|
|
|
{
|
2019-12-30 09:51:28 -05:00
|
|
|
$this->validate($request, [
|
2021-11-04 20:26:55 -04:00
|
|
|
'name' => ['required', 'max:250'],
|
|
|
|
'expires_at' => ['date_format:Y-m-d'],
|
2019-12-29 12:03:52 -05:00
|
|
|
]);
|
|
|
|
|
|
|
|
[$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
|
2019-12-30 09:51:28 -05:00
|
|
|
$token->fill([
|
2021-06-26 11:23:15 -04:00
|
|
|
'name' => $request->get('name'),
|
2019-12-30 09:51:28 -05:00
|
|
|
'expires_at' => $request->get('expires_at') ?: ApiToken::defaultExpiry(),
|
|
|
|
])->save();
|
2019-12-29 12:03:52 -05:00
|
|
|
|
2020-11-20 13:53:01 -05:00
|
|
|
$this->logActivity(ActivityType::API_TOKEN_UPDATE, $token);
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2023-10-19 06:31:45 -04:00
|
|
|
return redirect($token->getUrl());
|
2019-12-29 12:03:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the delete view for this token.
|
|
|
|
*/
|
|
|
|
public function delete(int $userId, int $tokenId)
|
|
|
|
{
|
|
|
|
[$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2019-12-29 12:03:52 -05:00
|
|
|
return view('users.api-tokens.delete', [
|
2021-06-26 11:23:15 -04:00
|
|
|
'user' => $user,
|
2019-12-29 12:03:52 -05:00
|
|
|
'token' => $token,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy a token from the system.
|
|
|
|
*/
|
|
|
|
public function destroy(int $userId, int $tokenId)
|
|
|
|
{
|
|
|
|
[$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
|
|
|
|
$token->delete();
|
|
|
|
|
2020-11-20 13:53:01 -05:00
|
|
|
$this->logActivity(ActivityType::API_TOKEN_DELETE, $token);
|
|
|
|
|
2023-10-19 06:31:45 -04:00
|
|
|
return redirect($this->getRedirectPath($user));
|
2019-12-29 12:03:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check the permission for the current user and return an array
|
|
|
|
* where the first item is the user in context and the second item is their
|
|
|
|
* API token in context.
|
|
|
|
*/
|
|
|
|
protected function checkPermissionAndFetchUserToken(int $userId, int $tokenId): array
|
|
|
|
{
|
2019-12-29 14:46:46 -05:00
|
|
|
$this->checkPermissionOr('users-manage', function () use ($userId) {
|
|
|
|
return $userId === user()->id && userCan('access-api');
|
|
|
|
});
|
2019-12-29 12:03:52 -05:00
|
|
|
|
|
|
|
$user = User::query()->findOrFail($userId);
|
|
|
|
$token = ApiToken::query()->where('user_id', '=', $user->id)->where('id', '=', $tokenId)->firstOrFail();
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2019-12-29 12:03:52 -05:00
|
|
|
return [$user, $token];
|
|
|
|
}
|
2023-10-19 06:31:45 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the context for where the user is coming from to manage API tokens.
|
|
|
|
* (Track of location for correct return redirects)
|
|
|
|
*/
|
|
|
|
protected function updateContext(Request $request): void
|
|
|
|
{
|
|
|
|
$context = $request->query('context');
|
|
|
|
if ($context) {
|
|
|
|
session()->put('api-token-context', $context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the redirect path for the current api token editing session.
|
|
|
|
* Attempts to recall the context of where the user is editing from.
|
|
|
|
*/
|
|
|
|
protected function getRedirectPath(User $relatedUser): string
|
|
|
|
{
|
|
|
|
$context = session()->get('api-token-context');
|
2023-10-19 09:18:42 -04:00
|
|
|
if ($context === 'settings' || user()->id !== $relatedUser->id) {
|
2023-10-19 06:31:45 -04:00
|
|
|
return $relatedUser->getEditUrl('#api_tokens');
|
|
|
|
}
|
|
|
|
|
|
|
|
return url('/my-account/auth#api_tokens');
|
|
|
|
}
|
2019-12-29 08:02:26 -05:00
|
|
|
}
|