Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Expand and rebuild Passport 13 views.
James Cole3235e1c86761
diff --git a/app/Http/Controllers/Profile/OAuthController.php b/app/Http/Controllers/Profile/OAuthController.phpindex 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.phpindex 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.phpindex 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.vueindex 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 @@</div><div class="box-body"><!-- Current Clients -->- <p v-if="clients.length === 0" class="mb-0">- {{ $t('firefly.profile_oauth_no_clients') }}- </p>- <p class="mb-2">+ <p>+ {{ $t('firefly.profile_oauth_clients_explain') }}+ </p>+ <p>{{ $t('firefly.profile_oauth_clients_external_auth') }}</p>+ <p v-if="clients.length === 0">+ {{ $t('firefly.profile_oauth_no_clients') }}+ </p><table v-if="clients.length > 0" class="table table-responsive table-borderless mb-0"><caption>{{ $t('firefly.profile_oauth_clients_header') }}</caption><thead>diff --git a/resources/assets/v1/src/components/passport/PersonalAccessTokens.vue b/resources/assets/v1/src/components/passport/PersonalAccessTokens.vueindex e6b7615ab4..8dd9a9b1c9 100644--- a/resources/assets/v1/src/components/passport/PersonalAccessTokens.vue+++ b/resources/assets/v1/src/components/passport/PersonalAccessTokens.vue@@ -20,305 +20,326 @@<style scoped>.action-link {- cursor: pointer;+ cursor: pointer;}</style><template>- <div><div>- <div class="box box-default">- <div class="box-header">- <h3 class="box-title">{{ $t('firefly.profile_personal_access_tokens') }}</h3>- <a class="btn btn-default pull-right" tabindex="-1" @click="showCreateTokenForm">- {{ $t('firefly.profile_create_new_token') }}- </a>- </div>+ <div>+ <div class="box box-default">+ <div class="box-header">+ <h3 class="box-title">{{ $t('firefly.profile_personal_access_tokens') }}</h3>+ <a class="btn btn-default pull-right" tabindex="-1" @click="showCreateTokenForm">+ {{ $t('firefly.profile_create_new_token') }}+ </a>+ </div>- <div class="box-body">- <!-- No Tokens Notice -->- <p v-if="tokens.length === 0" class="mb-0">- {{ $t('firefly.profile_no_personal_access_token') }}- </p>-- <!-- Personal Access Tokens -->- <table v-if="tokens.length > 0" class="table table-responsive table-borderless mb-0">- <caption style="display:none;">{{ $t('firefly.profile_personal_access_tokens') }}</caption>- <thead>- <tr>- <th scope="col">{{ $t('firefly.name') }}</th>- <th scope="col">{{ $t('firefly.expires_at') }}</th>- <th scope="col"></th>- </tr>- </thead>-- <tbody>- <tr v-for="token in tokens">- <!-- Client Name -->- <td style="vertical-align: middle;">- {{ token.name }}- </td>- <!-- expires at -->- <td style="vertical-align: middle;">- {{ new Date(token.expires_at).toLocaleString() }}- </td>-- <!-- Delete Button -->- <td style="vertical-align: middle;">- <a class="action-link text-danger" @click="revoke(token)">- {{ $t('firefly.delete') }}- </a>- </td>- </tr>- </tbody>- </table>- </div>- <div class="box-footer">- <a class="btn btn-default pull-right" tabindex="-1" @click="showCreateTokenForm">- {{ $t('firefly.profile_create_new_token') }}- </a>+ <div class="box-body">+ <p>+ {{ $t('firefly.explain_pats') }}+ </p>++ <p v-if="tokens.length === 0" class="mb-0">+ {{ $t('firefly.profile_no_personal_access_token') }}+ </p>++ <!-- Personal Access Tokens -->+ <table v-if="tokens.length > 0" class="table table-responsive table-borderless mb-0">+ <caption style="display:none;">{{ $t('firefly.profile_personal_access_tokens') }}</caption>+ <thead>+ <tr>+ <th scope="col">{{ $t('firefly.name') }}</th>+ <th scope="col">{{ $t('firefly.expires_at') }}</th>+ <th scope="col"></th>+ </tr>+ </thead>++ <tbody>+ <tr v-for="token in tokens">+ <!-- Client Name -->+ <td style="vertical-align: middle;">+ {{ token.name }}+ </td>+ <!-- expires at -->+ <td style="vertical-align: middle;">+ {{ new Date(token.expires_at).toLocaleString() }}+ </td>++ <!-- Delete Button -->+ <td style="vertical-align: middle;">+ <a class="action-link text-danger" @click="revoke(token)">+ {{ $t('firefly.delete') }}+ </a>+ </td>+ </tr>+ </tbody>+ </table>+ </div>+ <div class="box-footer">+ <a class="btn btn-default pull-right" tabindex="-1" @click="showCreateTokenForm">+ {{ $t('firefly.profile_create_new_token') }}+ </a>+ </div>+ </div></div>- </div>- </div>- <!-- Create Token Modal -->- <div id="modal-create-token" class="modal fade" role="dialog" tabindex="-1">- <div class="modal-dialog">- <div class="modal-content">- <div class="modal-header">- <h4 class="modal-title">- {{ $t('firefly.profile_create_token') }}- </h4>-- <button aria-hidden="true" class="close" data-dismiss="modal" type="button">×</button>- </div>-- <div class="modal-body">- <!-- Form Errors -->- <div v-if="form.errors.length > 0" class="alert alert-danger">- <p class="mb-0"><strong>{{ $t('firefly.profile_whoops') }}</strong>- {{ $t('firefly.profile_something_wrong') }}</p>- <br>- <ul>- <li v-for="error in form.errors">- {{ error }}- </li>- </ul>- </div>+ <!-- Create Token Modal -->+ <div id="modal-create-token" class="modal fade" role="dialog" tabindex="-1">+ <div class="modal-dialog">+ <div class="modal-content">+ <div class="modal-header">+ <h4 class="modal-title">+ {{ $t('firefly.profile_create_token') }}+ </h4>- <!-- Create Token Form -->- <form role="form" @submit.prevent="store">- <!-- Name -->- <div class="form-group row">- <label class="col-md-4 col-form-label">{{ $t('firefly.name') }}</label>+ <button aria-hidden="true" class="close" data-dismiss="modal" type="button">×</button>+ </div>- <div class="col-md-6">- <input id="create-token-name" v-model="form.name" class="form-control" name="name" type="text" spellcheck="false">- </div>- </div>-- <!-- Scopes -->- <div v-if="scopes.length > 0" class="form-group row">- <label class="col-md-4 col-form-label">{{ $t('firefly.profile_scopes') }}</label>-- <div class="col-md-6">- <div v-for="scope in scopes">- <div class="checkbox">- <label>- <input :checked="scopeIsAssigned(scope.id)"- type="checkbox"- @click="toggleScope(scope.id)">-- {{ scope.id }}- </label>+ <div class="modal-body">+ <!-- Form Errors -->+ <div v-if="form.errors.length > 0" class="alert alert-danger">+ <p class="mb-0"><strong>{{ $t('firefly.profile_whoops') }}</strong>+ {{ $t('firefly.profile_something_wrong') }}</p>+ <br>+ <ul>+ <li v-for="error in form.errors">+ {{ error }}+ </li>+ </ul>+ </div>++ <!-- Create Token Form -->+ <form role="form" @submit.prevent="store">+ <!-- Name -->+ <div class="form-group row">+ <label class="col-md-4 col-form-label">{{ $t('firefly.name') }}</label>++ <div class="col-md-6">+ <input id="create-token-name" v-model="form.name" class="form-control" name="name"+ type="text" spellcheck="false">+ </div>+ </div>++ <!-- Scopes -->+ <div v-if="scopes.length > 0" class="form-group row">+ <label class="col-md-4 col-form-label">{{ $t('firefly.profile_scopes') }}</label>++ <div class="col-md-6">+ <div v-for="scope in scopes">+ <div class="checkbox">+ <label>+ <input :checked="scopeIsAssigned(scope.id)"+ type="checkbox"+ @click="toggleScope(scope.id)">++ {{ scope.id }}+ </label>+ </div>+ </div>+ </div>+ </div>+ </form>+ </div>++ <!-- Modal Actions -->+ <div class="modal-footer">+ <button class="btn btn-secondary" data-dismiss="modal" type="button">{{+ $t('firefly.close')+ }}+ </button>++ <button class="btn btn-primary" type="button" @click="store">+ Create+ </button></div>- </div></div>- </div>- </form>- </div>-- <!-- Modal Actions -->- <div class="modal-footer">- <button class="btn btn-secondary" data-dismiss="modal" type="button">{{ $t('firefly.close') }}</button>-- <button class="btn btn-primary" type="button" @click="store">- Create- </button>- </div>+ </div></div>- </div>- </div>- <!-- Access Token Modal -->- <div id="modal-access-token" class="modal fade" role="dialog" tabindex="-1">- <div class="modal-dialog">- <div class="modal-content">- <div class="modal-header">- <h4 class="modal-title">- {{ $t('firefly.profile_personal_access_token') }}- </h4>-- <button aria-hidden="true" class="close" data-dismiss="modal" type="button">×</button>- </div>-- <div class="modal-body">- <p>- {{ $t('firefly.profile_personal_access_token_explanation') }}- </p>- <textarea class="form-control" readonly rows="20" style="width:100%;">{{ accessToken }}</textarea>- </div>-- <!-- Modal Actions -->- <div class="modal-footer">- <button class="btn btn-secondary" data-dismiss="modal" type="button">{{ $t('firefly.close') }}</button>- </div>+ <!-- Access Token Modal -->+ <div id="modal-access-token" class="modal fade" role="dialog" tabindex="-1">+ <div class="modal-dialog">+ <div class="modal-content">+ <div class="modal-header">+ <h4 class="modal-title">+ {{ $t('firefly.profile_personal_access_token') }}+ </h4>++ <button aria-hidden="true" class="close" data-dismiss="modal" type="button">×</button>+ </div>++ <div class="modal-body">+ <p>+ {{ $t('firefly.profile_personal_access_token_explanation') }}+ </p>+ <textarea class="form-control" id="token_box" readonly rows="20" style="width:100%;">{{ accessToken }}</textarea>+ </div>++ <!-- Modal Actions -->+ <div class="modal-footer">+ <button class="btn btn-secondary" data-dismiss="modal" type="button">{{+ $t('firefly.close')+ }}+ </button>+ </div>+ </div>+ </div></div>- </div></div>- </div></template><script>export default {- /*- * The component's data.- */- data() {- return {- accessToken: null,-- tokens: [],- scopes: [],-- form: {- name: '',- scopes: [],- errors: []- }- };- },-- /**- * Prepare the component (Vue 1.x).- */- ready() {- this.prepareComponent();- },-- /**- * Prepare the component (Vue 2.x).- */- mounted() {- this.prepareComponent();- },-- methods: {- /**- * Prepare the component.+ /*+ * The component's data.*/- prepareComponent() {- this.getTokens();- this.getScopes();+ data() {+ return {+ accessToken: null,- $('#modal-create-token').on('shown.bs.modal', () => {- $('#create-token-name').focus();- });- },+ tokens: [],+ scopes: [],- /**- * Get all of the personal access tokens for the user.- */- getTokens() {- axios.get('./oauth/personal-access-tokens')- .then(response => {- this.tokens = response.data;- });+ form: {+ name: '',+ scopes: [],+ errors: []+ }+ };},/**- * Get all the available scopes.+ * Prepare the component (Vue 1.x).*/- getScopes() {- axios.get('./oauth/scopes')- .then(response => {- this.scopes = response.data;- });+ ready() {+ this.prepareComponent();},/**- * Show the form for creating new tokens.+ * Prepare the component (Vue 2.x).*/- showCreateTokenForm() {- $('#modal-create-token').modal('show');+ mounted() {+ this.prepareComponent();},- /**- * Create a new personal access token.- */- store() {- this.accessToken = null;-- this.form.errors = [];+ methods: {+ /**+ * Prepare the component.+ */+ prepareComponent() {+ this.getTokens();+ this.getScopes();++ $('#modal-create-token').on('shown.bs.modal', () => {+ $('#create-token-name').focus();+ });+ const textBox = document.getElementById("token_box");+ textBox.onfocus = function () {+ textBox.select();++ // Work around Chrome's little problem+ textBox.onmouseup = function () {+ // Prevent further mouseup intervention+ textBox.onmouseup = null;+ return false;+ };+ };+ },++ /**+ * Get all of the personal access tokens for the user.+ */+ getTokens() {+ axios.get('./oauth/personal-access-tokens')+ .then(response => {+ this.tokens = response.data;+ });+ },++ /**+ * Get all the available scopes.+ */+ getScopes() {+ axios.get('./oauth/scopes')+ .then(response => {+ this.scopes = response.data;+ });+ },++ /**+ * Show the form for creating new tokens.+ */+ showCreateTokenForm() {+ $('#modal-create-token').modal('show');+ },++ /**+ * Create a new personal access token.+ */+ store() {+ this.accessToken = null;- axios.post('./oauth/personal-access-tokens', this.form)- .then(response => {- this.form.name = '';- this.form.scopes = [];this.form.errors = [];- this.tokens.push(response.data.token);-- this.showAccessToken(response.data.accessToken);- })- .catch(error => {- if (typeof error.response.data === 'object') {- this.form.errors = _.flatten(_.toArray(error.response.data.errors));+ axios.post('./oauth/personal-access-tokens', this.form)+ .then(response => {+ this.form.name = '';+ this.form.scopes = [];+ this.form.errors = [];++ this.getTokens();+ this.showAccessToken(response.data.accessToken);++ })+ .catch(error => {+ if (typeof error.response.data === 'object') {+ this.form.errors = _.flatten(_.toArray(error.response.data.errors));+ } else {+ this.form.errors = ['Something went wrong. Please try again.'];+ }+ });+ },++ /**+ * Toggle the given scope in the list of assigned scopes.+ */+ toggleScope(scope) {+ if (this.scopeIsAssigned(scope)) {+ this.form.scopes = _.reject(this.form.scopes, s => s == scope);} else {- this.form.errors = ['Something went wrong. Please try again.'];+ this.form.scopes.push(scope);}- });- },-- /**- * Toggle the given scope in the list of assigned scopes.- */- toggleScope(scope) {- if (this.scopeIsAssigned(scope)) {- this.form.scopes = _.reject(this.form.scopes, s => s == scope);- } else {- this.form.scopes.push(scope);- }- },-- /**- * Determine if the given scope has been assigned to the token.- */- scopeIsAssigned(scope) {- return _.indexOf(this.form.scopes, scope) >= 0;- },-- /**- * Show the given access token to the user.- */- showAccessToken(accessToken) {- $('#modal-create-token').modal('hide');-- this.accessToken = accessToken;-- $('#modal-access-token').modal('show');- },-- /**- * Revoke the given token.- */- revoke(token) {- axios.delete('./oauth/personal-access-tokens/' + token.id)- .then(response => {- this.getTokens();- });+ },++ /**+ * Determine if the given scope has been assigned to the token.+ */+ scopeIsAssigned(scope) {+ return _.indexOf(this.form.scopes, scope) >= 0;+ },++ /**+ * Show the given access token to the user.+ */+ showAccessToken(accessToken) {+ $('#modal-create-token').modal('hide');++ this.accessToken = accessToken;++ $('#modal-access-token').modal('show');+ },++ /**+ * Revoke the given token.+ */+ revoke(token) {+ axios.delete('./oauth/personal-access-tokens/' + token.id)+ .then(response => {+ this.getTokens();+ });+ }}- }}</script>diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.phpindex e34b6592c7..b9f19ed373 100644--- a/resources/lang/en_US/firefly.php+++ b/resources/lang/en_US/firefly.php@@ -1639,9 +1639,11 @@ return ['delete_local_info_only' => "Because Firefly III isn't responsible for user management or authentication handling, this function will only delete local Firefly III information.",'oauth' => 'OAuth','oauth_tokens' => 'Remote access and tokens',- 'profile_oauth_clients' => 'OAuth Clients',- 'profile_oauth_no_clients' => 'You have not created any OAuth clients.',- 'profile_oauth_clients_external_auth' => 'If you\'re using an external authentication provider like Authelia, OAuth Clients will not work. You can use Personal Access Tokens only.',+ 'profile_oauth_clients' => 'OAuth Clients and Applications',+ 'explain_pats' => 'Personal Access Tokens are long lived (with a maximum of 1 year) keys that allow direct and unlimited access to your Firefly III data. Tools like the Firefly III Data Importer and the Firefly III integration in Home Assistant use such tokens to connect to Firefly III and do their thing. When you create a token, it is only visible once. The token is also very long.',+ 'profile_oauth_no_clients' => 'You have not created any OAuth clients or applications.',+ 'profile_oauth_clients_external_auth' => 'Please note that if you\'re using an external authentication provider like Authelia, OAuth Clients will not work. You can use Personal Access Tokens only.',+ 'profile_oauth_clients_explain' => 'An OAuth client can be used to connect "smart" applications to Firefly III: applications that are capable of redirecting you to your Firefly III, get your permission, and return you back. The Firefly III Data Importer is such an application. OAuth clients can be generated with or without a "secret". This secret is used to authenticate the client. Since not all clients are capable of storing the secret, so you have the option to generate a client without one.','profile_oauth_clients_header' => 'Clients','profile_oauth_client_id' => 'Client ID','profile_oauth_client_name' => 'Name',diff --git a/routes/web.php b/routes/web.phpindex 6ec8fb7b6b..2d8d39074f 100644--- a/routes/web.php+++ b/routes/web.php@@ -29,6 +29,28 @@ if (!defined('DATEFORMAT')) {define('DATEFORMAT', '(19|20)[0-9]{2}-?[0-9]{2}-?[0-9]{2}');}+// new Passport routes.+Route::group(+ [+ 'as' => 'passport.',+ 'prefix' => 'oauth',+ // 'namespace' => 'FireflyIII\Http\Controllers\OAuth',+ ],+ function (): void {+ // routes with no extra middleware+ // Route::post('/token', ['uses' => '\Laravel\Passport\Http\Controllers\AccessTokenController@issueToken', 'as' => 'token', 'middleware' => 'throttle']);+ // Route::get('/authorize', ['uses' => 'AuthorizationController@authorize', 'as' => 'authorizations.authorize', 'middleware' => 'user-full-auth']);++ // personal access tokens:+ Route::post('/personal-access-tokens', ['uses' => 'FireflyIII\Http\Controllers\Profile\OAuthController@storePersonalAccessToken', 'as' => 'personal.tokens.store']);+ Route::get('/personal-access-tokens', ['uses' => 'FireflyIII\Http\Controllers\Profile\OAuthController@listPersonalAccessTokens', 'as' => 'personal.tokens.index']);+ Route::delete('/personal-access-tokens/{token_id}', ['uses' => 'FireflyIII\Http\Controllers\Profile\OAuthController@destroyPersonalAccessToken', 'as' => 'personal.tokens.destroy']);++ // clients:+ Route::get('/clients', ['uses' => 'FireflyIII\Http\Controllers\Profile\OAuthController@listClients', 'as' => 'clients.index']);+ }+);+// laravel passport routes//Route::group(// [