From ae8eb98f1c032a9bc6fa96babd31d959b8c988a0 Mon Sep 17 00:00:00 2001 From: norareidy Date: Fri, 23 Aug 2024 14:27:27 -0400 Subject: [PATCH 01/10] DOCSP-42794: Laravel Passport --- docs/user-authentication.txt | 48 ++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/docs/user-authentication.txt b/docs/user-authentication.txt index d02b8b089..e760ffdd4 100644 --- a/docs/user-authentication.txt +++ b/docs/user-authentication.txt @@ -107,6 +107,7 @@ This section describes how to use the following features to customize the MongoD authentication process: - :ref:`laravel-user-auth-sanctum` +- :ref:`laravel-user-auth-passport` - :ref:`laravel-user-auth-reminders` .. _laravel-user-auth-sanctum: @@ -154,6 +155,53 @@ in the Laravel Sanctum guide. To learn more about the ``DocumentModel`` trait, see :ref:`laravel-third-party-model` in the Eloquent Model Class guide. +.. _laravel-user-auth-passport: + +Laravel Passport +~~~~~~~~~~~~~~~~ + +Laravel Passport is an OAuth 2.0 server implementation that offers +API authentication for Laravel applications. The Laravel MongoDB Passport +service provider enables Laravel Passport support for applications that +use MongoDB. + +To install Laravel MongoDB Passport, run the following command from +your project root: + +.. code-block:: bash + + composer require designmynight/laravel-mongodb-passport + +Next, navigate to your application's ``config/app.php`` file and +add the following service provider to the ``providers`` array: + +.. code-block:: php + + DesignMyNight\Mongodb\MongodbPassportServiceProvider::class, + +Then, ensure your ``User`` class extends ``DesignMyNight\Mongodb\Auth\User.php`` +instead of the default ``Illuminate\Foundation\Auth\User``. The following code +shows how to modify your ``app\Models\User.php`` file to extend ``DesignMyNight\Mongodb\Auth\User.php``: + +.. code-block:: php + + `__ in the +Laravel documentation. + .. _laravel-user-auth-reminders: Password Reminders From 93a3d9054241870e99ee079764268ef8d7adbeb6 Mon Sep 17 00:00:00 2001 From: norareidy Date: Tue, 27 Aug 2024 11:38:45 -0400 Subject: [PATCH 02/10] update instructions --- docs/user-authentication.txt | 74 ++++++++++++++++++++++++++---------- 1 file changed, 54 insertions(+), 20 deletions(-) diff --git a/docs/user-authentication.txt b/docs/user-authentication.txt index e760ffdd4..d6b039be6 100644 --- a/docs/user-authentication.txt +++ b/docs/user-authentication.txt @@ -161,27 +161,20 @@ Laravel Passport ~~~~~~~~~~~~~~~~ Laravel Passport is an OAuth 2.0 server implementation that offers -API authentication for Laravel applications. The Laravel MongoDB Passport -service provider enables Laravel Passport support for applications that -use MongoDB. +API authentication for Laravel applications. Use Laravel Passport if +your application requires OAuth2 support. -To install Laravel MongoDB Passport, run the following command from -your project root: +To install Laravel Passport and run the database migrations required +to store OAuth2 clients, run the following command from your project root: .. code-block:: bash - composer require designmynight/laravel-mongodb-passport + php artisan install:api --passport -Next, navigate to your application's ``config/app.php`` file and -add the following service provider to the ``providers`` array: - -.. code-block:: php - - DesignMyNight\Mongodb\MongodbPassportServiceProvider::class, - -Then, ensure your ``User`` class extends ``DesignMyNight\Mongodb\Auth\User.php`` -instead of the default ``Illuminate\Foundation\Auth\User``. The following code -shows how to modify your ``app\Models\User.php`` file to extend ``DesignMyNight\Mongodb\Auth\User.php``: +Next, navigate to your ``User`` model and add the ``Laravel\Passport\HasApiTokens`` +trait. This trait provides helper methods that allow you to inspect a user's +authentication token and scopes. The following code shows how to add ``Laravel\Passport\HasApiTokens`` +to your ``app\Models\User.php`` file: .. code-block:: php @@ -189,16 +182,57 @@ shows how to modify your ``app\Models\User.php`` file to extend ``DesignMyNight\ namespace App\Models; - use Illuminate\Notifications\Notifiable; - use DesignMyNight\Mongodb\Auth\User as Authenticatable; + use MongoDB\Laravel\Auth\User as Authenticatable; + use Laravel\Passport\HasApiTokens; class User extends Authenticatable { - use Notifiable; + use HasApiTokens; ... } -You can now use Laravel Passport in your application. For more information, see +Then, define an ``api`` authentication guard in your ``config\auth.php`` +file and set the ``driver`` option to ``passport``. This instructs your +application to use Laravel Passport's ``TokenGuard`` class to authenticate +API requests. The following example adds the ``api`` authentication guard +to the ``guards`` array: + +.. code-block:: php + :emphasize-lines: 7-10 + + 'guards' => [ + 'web' => [ + 'driver' => 'session', + 'provider' => 'users', + ], + + 'api' => [ + 'driver' => 'passport', + 'provider' => 'users', + ], + ], + +Lastly, you must override the default Laravel Passport models by defining a custom model +that extends the corresponding Laravel Passport model. by including the ``DocumentModel`` trait. This trait allows you +to make Laravel Passport compatible with MongoDB. The following code defines +a series of model classes that extend the corresponding ``Laravel\Passport`` models: + +.. code-block:: php + + class MongoDB\Laravel\Passport\AuthCode extends Laravel\Passport\AuthCode + { + use MongoDB\Laravel\Eloquent\DocumentModel; + + protected $primaryKey = '_id'; + protected $keyType = 'string'; + } + + class MongoDB\Laravel\Passport\Client extends Laravel\Passport\Client; + class MongoDB\Laravel\Passport\PersonalAccessClient extends Laravel\Passport\PersonalAccessClient; + class MongoDB\Laravel\Passport\RefreshToken extends Laravel\Passport\RefreshToken; + class MongoDB\Laravel\Passport\Token extends Laravel\Passport\Token; + +You can now use Laravel Passport and MongoDB in your application. For more information, see `Laravel Passport `__ in the Laravel documentation. From fd30e09358b5d1892d42f23b14125741fb44d783 Mon Sep 17 00:00:00 2001 From: norareidy Date: Tue, 27 Aug 2024 13:38:41 -0400 Subject: [PATCH 03/10] edits, code file --- docs/includes/auth/AuthServiceProvider.php | 25 +++++++++++++++ docs/user-authentication.txt | 36 +++++++++++++++++----- 2 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 docs/includes/auth/AuthServiceProvider.php diff --git a/docs/includes/auth/AuthServiceProvider.php b/docs/includes/auth/AuthServiceProvider.php new file mode 100644 index 000000000..e6423a4b8 --- /dev/null +++ b/docs/includes/auth/AuthServiceProvider.php @@ -0,0 +1,25 @@ +`__ in the From be4a2b2277a1988a5397315dd37ddb50364baf20 Mon Sep 17 00:00:00 2001 From: norareidy Date: Tue, 27 Aug 2024 17:39:31 +0000 Subject: [PATCH 04/10] apply phpcbf formatting --- docs/includes/auth/AuthServiceProvider.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/includes/auth/AuthServiceProvider.php b/docs/includes/auth/AuthServiceProvider.php index e6423a4b8..3f6832e42 100644 --- a/docs/includes/auth/AuthServiceProvider.php +++ b/docs/includes/auth/AuthServiceProvider.php @@ -12,7 +12,6 @@ class AppServiceProvider extends ServiceProvider */ public function register(): void { - // } /** @@ -22,4 +21,4 @@ public function boot(): void { Passport::useAuthCodeModel(AuthCode::class); } -} \ No newline at end of file +} From 388db353540882fb465a44c844ae520ed65c0e9f Mon Sep 17 00:00:00 2001 From: norareidy Date: Tue, 27 Aug 2024 13:42:40 -0400 Subject: [PATCH 05/10] more edits --- docs/user-authentication.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/user-authentication.txt b/docs/user-authentication.txt index f92001ef3..a8d67cf19 100644 --- a/docs/user-authentication.txt +++ b/docs/user-authentication.txt @@ -215,8 +215,8 @@ to the ``guards`` array: ], ], -Use Laravel Passport with {+odm-short+} -``````````````````````````````````````` +Use Laravel Passport with Laravel MongoDB +````````````````````````````````````````` After installing Laravel Passport, you must enable Passport compatibility with MongoDB by defining custom {+odm-short+} models that extend the corresponding Passport models. @@ -247,7 +247,7 @@ After defining a custom model, instruct Passport to use the model in the ``boot` method of your application's ``App\Providers\AppServiceProvider`` class, as shown in the following code: -.. literalinclude:: /includes/auth/PersonalAccessToken.php +.. literalinclude:: /includes/auth/AuthServiceProvider.php :language: php :emphasize-lines: 6,23 :dedent: From bffbc4345fcb2ea4ea89356ccd07c1537f04fabe Mon Sep 17 00:00:00 2001 From: norareidy Date: Tue, 27 Aug 2024 13:46:54 -0400 Subject: [PATCH 06/10] code highlight --- docs/user-authentication.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/user-authentication.txt b/docs/user-authentication.txt index a8d67cf19..97561f4b8 100644 --- a/docs/user-authentication.txt +++ b/docs/user-authentication.txt @@ -249,7 +249,7 @@ in the following code: .. literalinclude:: /includes/auth/AuthServiceProvider.php :language: php - :emphasize-lines: 6,23 + :emphasize-lines: 6,22 :dedent: You can now use Laravel Passport and MongoDB in your application. For more information, see From d1afb7b94dc7eee906b5bb8fc7d989aa00ed25f5 Mon Sep 17 00:00:00 2001 From: norareidy Date: Tue, 27 Aug 2024 13:54:04 -0400 Subject: [PATCH 07/10] file name --- .../auth/{AuthServiceProvider.php => AppServiceProvider.php} | 0 docs/user-authentication.txt | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename docs/includes/auth/{AuthServiceProvider.php => AppServiceProvider.php} (100%) diff --git a/docs/includes/auth/AuthServiceProvider.php b/docs/includes/auth/AppServiceProvider.php similarity index 100% rename from docs/includes/auth/AuthServiceProvider.php rename to docs/includes/auth/AppServiceProvider.php diff --git a/docs/user-authentication.txt b/docs/user-authentication.txt index 97561f4b8..bb463ebb2 100644 --- a/docs/user-authentication.txt +++ b/docs/user-authentication.txt @@ -247,7 +247,7 @@ After defining a custom model, instruct Passport to use the model in the ``boot` method of your application's ``App\Providers\AppServiceProvider`` class, as shown in the following code: -.. literalinclude:: /includes/auth/AuthServiceProvider.php +.. literalinclude:: /includes/auth/AppServiceProvider.php :language: php :emphasize-lines: 6,22 :dedent: From 130cce854357a1dcc49a3f912c2a94aaba66451d Mon Sep 17 00:00:00 2001 From: norareidy Date: Wed, 28 Aug 2024 16:12:48 -0400 Subject: [PATCH 08/10] JT feedback --- docs/includes/auth/AppServiceProvider.php | 8 ++++++++ docs/user-authentication.txt | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/docs/includes/auth/AppServiceProvider.php b/docs/includes/auth/AppServiceProvider.php index 3f6832e42..24ee1802c 100644 --- a/docs/includes/auth/AppServiceProvider.php +++ b/docs/includes/auth/AppServiceProvider.php @@ -4,6 +4,10 @@ use Illuminate\Support\ServiceProvider; use MongoDB\Laravel\Passport\AuthCode; +use MongoDB\Laravel\Passport\Client; +use MongoDB\Laravel\Passport\PersonalAccessClient; +use MongoDB\Laravel\Passport\RefreshToken; +use MongoDB\Laravel\Passport\Token; class AppServiceProvider extends ServiceProvider { @@ -20,5 +24,9 @@ public function register(): void public function boot(): void { Passport::useAuthCodeModel(AuthCode::class); + Passport::useClientModel(Client::class); + Passport::usePersonalAccessClientModel(PersonalAccessClient::class); + Passport::useRefreshTokenModel(RefreshToken::class); + Passport::useTokenModel(Token::class); } } diff --git a/docs/user-authentication.txt b/docs/user-authentication.txt index bb463ebb2..c3c97d138 100644 --- a/docs/user-authentication.txt +++ b/docs/user-authentication.txt @@ -243,13 +243,13 @@ the ``DocumentModel`` trait: protected $keyType = 'string'; } -After defining a custom model, instruct Passport to use the model in the ``boot`` +After defining each custom model, instruct Passport to use the models in the ``boot()`` method of your application's ``App\Providers\AppServiceProvider`` class, as shown in the following code: .. literalinclude:: /includes/auth/AppServiceProvider.php :language: php - :emphasize-lines: 6,22 + :emphasize-lines: 26-30 :dedent: You can now use Laravel Passport and MongoDB in your application. For more information, see From 0b959f889606b9bcd15c5e897fac38092f5ff422 Mon Sep 17 00:00:00 2001 From: norareidy Date: Thu, 29 Aug 2024 12:21:29 -0400 Subject: [PATCH 09/10] RR feedback --- docs/user-authentication.txt | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/docs/user-authentication.txt b/docs/user-authentication.txt index c3c97d138..52c04012b 100644 --- a/docs/user-authentication.txt +++ b/docs/user-authentication.txt @@ -164,6 +164,16 @@ Laravel Passport is an OAuth 2.0 server implementation that offers API authentication for Laravel applications. Use Laravel Passport if your application requires OAuth2 support. +.. tip:: + + To learn more about Laravel Passport and the OAuth 2.0 protocol, see + the following resources: + + - `Laravel Passport `__ in the + Laravel documentation. + + - `OAuth 2.0 `__ on the OAuth website. + Install Laravel Passport ```````````````````````` @@ -201,14 +211,13 @@ API requests. The following example adds the ``api`` authentication guard to the ``guards`` array: .. code-block:: php - :emphasize-lines: 7-10 + :emphasize-lines: 6-9 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], - 'api' => [ 'driver' => 'passport', 'provider' => 'users', @@ -229,8 +238,8 @@ You can define the following {+odm-short+} model classes: - ``MongoDB\Laravel\Passport\RefreshToken``, which extends ``Laravel\Passport\RefreshToken`` - ``MongoDB\Laravel\Passport\Token``, which extends ``Laravel\Passport\Token`` -For example, the following code overrides the default ``Laravel\Passport\AuthCode`` -model class by defining a ``MongoDB\Laravel\Passport\AuthCode`` class and including +The following example code extends the default ``Laravel\Passport\AuthCode`` +model class when defining a ``MongoDB\Laravel\Passport\AuthCode`` class and includes the ``DocumentModel`` trait: .. code-block:: php @@ -243,9 +252,10 @@ the ``DocumentModel`` trait: protected $keyType = 'string'; } -After defining each custom model, instruct Passport to use the models in the ``boot()`` -method of your application's ``App\Providers\AppServiceProvider`` class, as shown -in the following code: +After defining custom models that extend each ``Laravel\Passport`` class, instruct +Passport to use the models in the ``boot()`` method of your application's +``App\Providers\AppServiceProvider`` class. The following example adds each custom +model to the ``boot()`` method: .. literalinclude:: /includes/auth/AppServiceProvider.php :language: php From dafc058581509b863a52b5e4a212db3ebf254c59 Mon Sep 17 00:00:00 2001 From: norareidy Date: Thu, 29 Aug 2024 13:48:16 -0400 Subject: [PATCH 10/10] edit --- docs/user-authentication.txt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/docs/user-authentication.txt b/docs/user-authentication.txt index 52c04012b..8976581c3 100644 --- a/docs/user-authentication.txt +++ b/docs/user-authentication.txt @@ -262,9 +262,7 @@ model to the ``boot()`` method: :emphasize-lines: 26-30 :dedent: -You can now use Laravel Passport and MongoDB in your application. For more information, see -`Laravel Passport `__ in the -Laravel documentation. +Then, you can use Laravel Passport and MongoDB in your application. .. _laravel-user-auth-reminders: