diff --git a/app/Http/Controllers/Profile/OAuthController.php b/app/Http/Controllers/Profile/OAuthController.php index ac2bb7a65f..774d7e11b2 100644 --- a/app/Http/Controllers/Profile/OAuthController.php +++ b/app/Http/Controllers/Profile/OAuthController.php @@ -23,17 +23,27 @@ namespace FireflyIII\Http\Controllers\Profile; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Http\Middleware\IsDemoUser; +use Illuminate\Contracts\Validation\Factory as ValidationFactory; +use Illuminate\Http\JsonResponse; +use Illuminate\Http\Request; +use Illuminate\Http\Response; +use Illuminate\Support\Facades\Date; +use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; +use Laravel\Passport\ClientRepository; +use Laravel\Passport\Token; +use Laravel\Passport\TokenRepository; class OAuthController extends Controller { protected bool $internalAuth; - public function __construct() + + public function __construct(protected TokenRepository $tokenRepository,protected ValidationFactory $validation) { parent::__construct(); $this->middleware(static function ($request, $next) { - app('view')->share('title', (string) trans('firefly.oauth_tokens')); + app('view')->share('title', (string)trans('firefly.oauth_tokens')); app('view')->share('mainTitleIcon', 'fa-user'); return $next($request); @@ -45,7 +55,60 @@ class OAuthController extends Controller $this->middleware(IsDemoUser::class)->except(['index']); } - public function index() { + public function index() + { + $count = DB::table('oauth_clients')->where('grant_types', '["personal_access"]')->whereNull('owner_id')->count(); + + if (0 === $count) { + /** @var ClientRepository $repository */ + $repository = app(ClientRepository::class); + $repository->createPersonalAccessGrantClient('Firefly III Personal Access Grant Client', null); + } + return view('profile.oauth.index'); } + + public function listClients(): JsonResponse + { + // Retrieving all the OAuth app clients that belong to the user... + $clients = auth()->user()->oauthApps()->get(); + return response()->json($clients); + } + + public function storePersonalAccessToken(Request $request): JsonResponse + { + $this->validation->make($request->all(), [ + 'name' => ['required', 'max:255']])->validate(); + + return response()->json($request->user()->createToken($request->name)); + } + + public function destroyPersonalAccessToken(Request $request, string $tokenId): Response + { + $token = $this->tokenRepository->findForUser( + $tokenId, $request->user() + ); + + if (is_null($token)) { + return new Response('', 404); + } + + $token->revoke(); + + return new Response('', Response::HTTP_NO_CONTENT); + } + + public function listPersonalAccessTokens(): JsonResponse + { + // Retrieving all the OAuth app clients that belong to the user... + $tokens = auth()->user()->tokens() + ->with('client') + ->where('revoked', false) + ->where('expires_at', '>', Date::now()) + ->get() + ->filter(fn(Token $token) => $token->client->hasGrantType('personal_access')); + return response()->json($tokens); + } + + } diff --git a/app/Http/Controllers/ProfileController.php b/app/Http/Controllers/ProfileController.php index c764095bc3..d8cd15abce 100644 --- a/app/Http/Controllers/ProfileController.php +++ b/app/Http/Controllers/ProfileController.php @@ -38,6 +38,7 @@ use FireflyIII\Support\Http\Controllers\CreateStuff; use FireflyIII\User; use Illuminate\Auth\AuthenticationException; use Illuminate\Contracts\Auth\Guard; +use Illuminate\Contracts\Validation\Factory as ValidationFactory; use Illuminate\Contracts\View\Factory; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; @@ -67,7 +68,7 @@ final class ProfileController extends Controller /** * ProfileController constructor. */ - public function __construct() + public function __construct(protected ValidationFactory $validation,) { parent::__construct(); diff --git a/config/translations.php b/config/translations.php index 2bb39e82d9..6c6fcb857d 100644 --- a/config/translations.php +++ b/config/translations.php @@ -139,6 +139,7 @@ return [ ], 'v1' => [ 'firefly' => [ + 'explain_pats', 'administrations_page_title', 'administrations_index_menu', 'expires_at', diff --git a/resources/assets/v1/src/components/passport/Clients.vue b/resources/assets/v1/src/components/passport/Clients.vue index c28ba672e2..17e8a776e5 100644 --- a/resources/assets/v1/src/components/passport/Clients.vue +++ b/resources/assets/v1/src/components/passport/Clients.vue @@ -37,12 +37,15 @@
- {{ $t('firefly.profile_oauth_no_clients') }} -
-+
+ {{ $t('firefly.profile_oauth_clients_explain') }} +
+{{ $t('firefly.profile_oauth_clients_external_auth') }}
++ {{ $t('firefly.profile_oauth_no_clients') }} +
| {{ $t('firefly.name') }} | -{{ $t('firefly.expires_at') }} | -- | |||
|---|---|---|---|---|---|
| - {{ token.name }} - | - -- {{ new Date(token.expires_at).toLocaleString() }} - | + +
| {{ $t('firefly.name') }} | +{{ $t('firefly.expires_at') }} | ++ | - - {{ $t('firefly.delete') }} - - | - - -
|---|