Add Passport

master
sundayenglish 4 weeks ago
parent 5c55628b83
commit 4ce36683be
  1. 35
      app/Providers/AuthServiceProvider.php
  2. 1
      bootstrap/app.php
  3. 1
      bootstrap/providers.php
  4. 6
      composer.json
  5. 392
      composer.lock
  6. 13
      routes/api.php

@ -0,0 +1,35 @@
<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Laravel\Passport\Bridge\RefreshTokenRepository;
use Laravel\Passport\Bridge\AccessTokenRepository;
use Laravel\Passport\Bridge\ClientRepository;
use Laravel\Passport\Bridge\ScopeRepository;
use Laravel\Passport\Bridge\UserRepository;
use League\OAuth2\Server\AuthorizationServer;
use League\OAuth2\Server\Grant\PasswordGrant;
use DateInterval;
class AuthServiceProvider extends ServiceProvider
{
protected $policies = [];
public function boot(): void
{
$this->registerPolicies();
// Thêm cấu hình hỗ trợ password grant
$this->app->afterResolving(AuthorizationServer::class, function ($server) {
$grant = new PasswordGrant(
app(UserRepository::class),
app(RefreshTokenRepository::class)
);
$grant->setRefreshTokenTTL(new DateInterval('P1M')); // Thời gian sống refresh token: 1 tháng
$server->enableGrantType($grant, new DateInterval('PT1H')); // Access token sống 1 giờ
});
}
}

@ -7,6 +7,7 @@ use Illuminate\Foundation\Configuration\Middleware;
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
api: __DIR__.'/../routes/api.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
)

@ -2,4 +2,5 @@
return [
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
];

@ -8,8 +8,10 @@
"require": {
"php": "^8.2",
"laravel/framework": "^12.0",
"laravel/passport": "^13.0",
"laravel/tinker": "^2.10.1"
"laravel/passport": "^12.4",
"laravel/tinker": "^2.10.1",
"nyholm/psr7": "^1.8",
"symfony/psr-http-message-bridge": "^7.3"
},
"require-dev": {
"fakerphp/faker": "^1.23",

392
composer.lock generated

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "55bc8c3889cd9347dacfaa3e64f15721",
"content-hash": "45d1af3b23edb54650d3e407d72a6ca6",
"packages": [
{
"name": "brick/math",
@ -1401,44 +1401,43 @@
},
{
"name": "laravel/passport",
"version": "v13.0.5",
"version": "v12.4.2",
"source": {
"type": "git",
"url": "https://github.com/laravel/passport.git",
"reference": "9a3b47bc784dc9334aad67f690b13230b0434960"
"reference": "65a885607b62d361aedaeb10a946bc6b5a954262"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/laravel/passport/zipball/9a3b47bc784dc9334aad67f690b13230b0434960",
"reference": "9a3b47bc784dc9334aad67f690b13230b0434960",
"url": "https://api.github.com/repos/laravel/passport/zipball/65a885607b62d361aedaeb10a946bc6b5a954262",
"reference": "65a885607b62d361aedaeb10a946bc6b5a954262",
"shasum": ""
},
"require": {
"ext-json": "*",
"ext-openssl": "*",
"firebase/php-jwt": "^6.4",
"illuminate/auth": "^11.35|^12.0",
"illuminate/console": "^11.35|^12.0",
"illuminate/container": "^11.35|^12.0",
"illuminate/contracts": "^11.35|^12.0",
"illuminate/cookie": "^11.35|^12.0",
"illuminate/database": "^11.35|^12.0",
"illuminate/encryption": "^11.35|^12.0",
"illuminate/http": "^11.35|^12.0",
"illuminate/support": "^11.35|^12.0",
"league/oauth2-server": "^9.2",
"php": "^8.2",
"php-http/discovery": "^1.20",
"phpseclib/phpseclib": "^3.0",
"psr/http-factory-implementation": "*",
"symfony/console": "^7.1",
"symfony/psr-http-message-bridge": "^7.1"
"illuminate/auth": "^9.21|^10.0|^11.0|^12.0",
"illuminate/console": "^9.21|^10.0|^11.0|^12.0",
"illuminate/container": "^9.21|^10.0|^11.0|^12.0",
"illuminate/contracts": "^9.21|^10.0|^11.0|^12.0",
"illuminate/cookie": "^9.21|^10.0|^11.0|^12.0",
"illuminate/database": "^9.21|^10.0|^11.0|^12.0",
"illuminate/encryption": "^9.21|^10.0|^11.0|^12.0",
"illuminate/http": "^9.21|^10.0|^11.0|^12.0",
"illuminate/support": "^9.21|^10.0|^11.0|^12.0",
"lcobucci/jwt": "^4.3|^5.0",
"league/oauth2-server": "^8.5.3",
"nyholm/psr7": "^1.5",
"php": "^8.0",
"phpseclib/phpseclib": "^2.0|^3.0",
"symfony/console": "^6.0|^7.0",
"symfony/psr-http-message-bridge": "^2.1|^6.0|^7.0"
},
"require-dev": {
"mockery/mockery": "^1.6",
"orchestra/testbench": "^9.9|^10.0",
"phpstan/phpstan": "^2.0",
"phpunit/phpunit": "^11.5|^12.0"
"mockery/mockery": "^1.0",
"orchestra/testbench": "^7.35|^8.14|^9.0|^10.0",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9.3|^10.5|^11.5"
},
"type": "library",
"extra": {
@ -1474,7 +1473,7 @@
"issues": "https://github.com/laravel/passport/issues",
"source": "https://github.com/laravel/passport"
},
"time": "2025-06-12T15:12:08+00:00"
"time": "2025-02-12T16:11:33+00:00"
},
{
"name": "laravel/prompts",
@ -1990,34 +1989,29 @@
},
{
"name": "league/event",
"version": "3.0.3",
"version": "2.3.0",
"source": {
"type": "git",
"url": "https://github.com/thephpleague/event.git",
"reference": "ec38ff7ea10cad7d99a79ac937fbcffb9334c210"
"reference": "062ebb450efbe9a09bc2478e89b7c933875b0935"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/thephpleague/event/zipball/ec38ff7ea10cad7d99a79ac937fbcffb9334c210",
"reference": "ec38ff7ea10cad7d99a79ac937fbcffb9334c210",
"url": "https://api.github.com/repos/thephpleague/event/zipball/062ebb450efbe9a09bc2478e89b7c933875b0935",
"reference": "062ebb450efbe9a09bc2478e89b7c933875b0935",
"shasum": ""
},
"require": {
"php": ">=7.2.0",
"psr/event-dispatcher": "^1.0"
},
"provide": {
"psr/event-dispatcher-implementation": "1.0"
"php": ">=7.1.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.16",
"phpstan/phpstan": "^0.12.45",
"phpunit/phpunit": "^8.5"
"henrikbjorn/phpspec-code-coverage": "~1.0.1",
"phpspec/phpspec": "^2.2"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.0-dev"
"dev-master": "2.2-dev"
}
},
"autoload": {
@ -2043,9 +2037,9 @@
],
"support": {
"issues": "https://github.com/thephpleague/event/issues",
"source": "https://github.com/thephpleague/event/tree/3.0.3"
"source": "https://github.com/thephpleague/event/tree/2.3.0"
},
"time": "2024-09-04T16:06:53+00:00"
"time": "2025-03-14T19:51:10+00:00"
},
{
"name": "league/flysystem",
@ -2237,46 +2231,38 @@
},
{
"name": "league/oauth2-server",
"version": "9.2.0",
"version": "8.5.5",
"source": {
"type": "git",
"url": "https://github.com/thephpleague/oauth2-server.git",
"reference": "00323013403e1a1e0f424affafca56c28b60c22c"
"reference": "cc8778350f905667e796b3c2364a9d3bd7a73518"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/thephpleague/oauth2-server/zipball/00323013403e1a1e0f424affafca56c28b60c22c",
"reference": "00323013403e1a1e0f424affafca56c28b60c22c",
"url": "https://api.github.com/repos/thephpleague/oauth2-server/zipball/cc8778350f905667e796b3c2364a9d3bd7a73518",
"reference": "cc8778350f905667e796b3c2364a9d3bd7a73518",
"shasum": ""
},
"require": {
"defuse/php-encryption": "^2.4",
"ext-json": "*",
"defuse/php-encryption": "^2.3",
"ext-openssl": "*",
"lcobucci/clock": "^2.3 || ^3.0",
"lcobucci/jwt": "^5.0",
"league/event": "^3.0",
"league/uri": "^7.0",
"php": "~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0",
"psr/http-message": "^2.0",
"psr/http-server-middleware": "^1.0"
"lcobucci/clock": "^2.2 || ^3.0",
"lcobucci/jwt": "^4.3 || ^5.0",
"league/event": "^2.2",
"league/uri": "^6.7 || ^7.0",
"php": "^8.0",
"psr/http-message": "^1.0.1 || ^2.0"
},
"replace": {
"league/oauth2server": "*",
"lncd/oauth2": "*"
},
"require-dev": {
"laminas/laminas-diactoros": "^3.5",
"php-parallel-lint/php-parallel-lint": "^1.3.2",
"phpstan/extension-installer": "^1.3.1",
"phpstan/phpstan": "^1.12",
"phpstan/phpstan-deprecation-rules": "^1.1.4",
"phpstan/phpstan-phpunit": "^1.3.15",
"phpstan/phpstan-strict-rules": "^1.5.2",
"phpunit/phpunit": "^9.6.21",
"roave/security-advisories": "dev-master",
"slevomat/coding-standard": "^8.14.1",
"squizlabs/php_codesniffer": "^3.8"
"laminas/laminas-diactoros": "^3.0.0",
"phpstan/phpstan": "^0.12.57",
"phpstan/phpstan-phpunit": "^0.12.16",
"phpunit/phpunit": "^9.6.6",
"roave/security-advisories": "dev-master"
},
"type": "library",
"autoload": {
@ -2321,7 +2307,7 @@
],
"support": {
"issues": "https://github.com/thephpleague/oauth2-server/issues",
"source": "https://github.com/thephpleague/oauth2-server/tree/9.2.0"
"source": "https://github.com/thephpleague/oauth2-server/tree/8.5.5"
},
"funding": [
{
@ -2329,7 +2315,7 @@
"type": "github"
}
],
"time": "2025-02-15T00:49:10+00:00"
"time": "2024-12-20T23:06:10+00:00"
},
{
"name": "league/uri",
@ -3006,6 +2992,84 @@
],
"time": "2025-05-08T08:14:37+00:00"
},
{
"name": "nyholm/psr7",
"version": "1.8.2",
"source": {
"type": "git",
"url": "https://github.com/Nyholm/psr7.git",
"reference": "a71f2b11690f4b24d099d6b16690a90ae14fc6f3"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Nyholm/psr7/zipball/a71f2b11690f4b24d099d6b16690a90ae14fc6f3",
"reference": "a71f2b11690f4b24d099d6b16690a90ae14fc6f3",
"shasum": ""
},
"require": {
"php": ">=7.2",
"psr/http-factory": "^1.0",
"psr/http-message": "^1.1 || ^2.0"
},
"provide": {
"php-http/message-factory-implementation": "1.0",
"psr/http-factory-implementation": "1.0",
"psr/http-message-implementation": "1.0"
},
"require-dev": {
"http-interop/http-factory-tests": "^0.9",
"php-http/message-factory": "^1.0",
"php-http/psr7-integration-tests": "^1.0",
"phpunit/phpunit": "^7.5 || ^8.5 || ^9.4",
"symfony/error-handler": "^4.4"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.8-dev"
}
},
"autoload": {
"psr-4": {
"Nyholm\\Psr7\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Tobias Nyholm",
"email": "tobias.nyholm@gmail.com"
},
{
"name": "Martijn van der Ven",
"email": "martijn@vanderven.se"
}
],
"description": "A fast PHP7 implementation of PSR-7",
"homepage": "https://tnyholm.se",
"keywords": [
"psr-17",
"psr-7"
],
"support": {
"issues": "https://github.com/Nyholm/psr7/issues",
"source": "https://github.com/Nyholm/psr7/tree/1.8.2"
},
"funding": [
{
"url": "https://github.com/Zegnat",
"type": "github"
},
{
"url": "https://github.com/nyholm",
"type": "github"
}
],
"time": "2024-09-09T07:06:30+00:00"
},
{
"name": "paragonie/constant_time_encoding",
"version": "v3.0.0",
@ -3123,85 +3187,6 @@
},
"time": "2020-10-15T08:29:30+00:00"
},
{
"name": "php-http/discovery",
"version": "1.20.0",
"source": {
"type": "git",
"url": "https://github.com/php-http/discovery.git",
"reference": "82fe4c73ef3363caed49ff8dd1539ba06044910d"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-http/discovery/zipball/82fe4c73ef3363caed49ff8dd1539ba06044910d",
"reference": "82fe4c73ef3363caed49ff8dd1539ba06044910d",
"shasum": ""
},
"require": {
"composer-plugin-api": "^1.0|^2.0",
"php": "^7.1 || ^8.0"
},
"conflict": {
"nyholm/psr7": "<1.0",
"zendframework/zend-diactoros": "*"
},
"provide": {
"php-http/async-client-implementation": "*",
"php-http/client-implementation": "*",
"psr/http-client-implementation": "*",
"psr/http-factory-implementation": "*",
"psr/http-message-implementation": "*"
},
"require-dev": {
"composer/composer": "^1.0.2|^2.0",
"graham-campbell/phpspec-skip-example-extension": "^5.0",
"php-http/httplug": "^1.0 || ^2.0",
"php-http/message-factory": "^1.0",
"phpspec/phpspec": "^5.1 || ^6.1 || ^7.3",
"sebastian/comparator": "^3.0.5 || ^4.0.8",
"symfony/phpunit-bridge": "^6.4.4 || ^7.0.1"
},
"type": "composer-plugin",
"extra": {
"class": "Http\\Discovery\\Composer\\Plugin",
"plugin-optional": true
},
"autoload": {
"psr-4": {
"Http\\Discovery\\": "src/"
},
"exclude-from-classmap": [
"src/Composer/Plugin.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Márk Sági-Kazár",
"email": "mark.sagikazar@gmail.com"
}
],
"description": "Finds and installs PSR-7, PSR-17, PSR-18 and HTTPlug implementations",
"homepage": "http://php-http.org",
"keywords": [
"adapter",
"client",
"discovery",
"factory",
"http",
"message",
"psr17",
"psr7"
],
"support": {
"issues": "https://github.com/php-http/discovery/issues",
"source": "https://github.com/php-http/discovery/tree/1.20.0"
},
"time": "2024-10-02T11:20:13+00:00"
},
{
"name": "phpoption/phpoption",
"version": "1.9.3",
@ -3698,119 +3683,6 @@
},
"time": "2023-04-04T09:54:51+00:00"
},
{
"name": "psr/http-server-handler",
"version": "1.0.2",
"source": {
"type": "git",
"url": "https://github.com/php-fig/http-server-handler.git",
"reference": "84c4fb66179be4caaf8e97bd239203245302e7d4"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/http-server-handler/zipball/84c4fb66179be4caaf8e97bd239203245302e7d4",
"reference": "84c4fb66179be4caaf8e97bd239203245302e7d4",
"shasum": ""
},
"require": {
"php": ">=7.0",
"psr/http-message": "^1.0 || ^2.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Http\\Server\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "https://www.php-fig.org/"
}
],
"description": "Common interface for HTTP server-side request handler",
"keywords": [
"handler",
"http",
"http-interop",
"psr",
"psr-15",
"psr-7",
"request",
"response",
"server"
],
"support": {
"source": "https://github.com/php-fig/http-server-handler/tree/1.0.2"
},
"time": "2023-04-10T20:06:20+00:00"
},
{
"name": "psr/http-server-middleware",
"version": "1.0.2",
"source": {
"type": "git",
"url": "https://github.com/php-fig/http-server-middleware.git",
"reference": "c1481f747daaa6a0782775cd6a8c26a1bf4a3829"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/http-server-middleware/zipball/c1481f747daaa6a0782775cd6a8c26a1bf4a3829",
"reference": "c1481f747daaa6a0782775cd6a8c26a1bf4a3829",
"shasum": ""
},
"require": {
"php": ">=7.0",
"psr/http-message": "^1.0 || ^2.0",
"psr/http-server-handler": "^1.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Http\\Server\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "https://www.php-fig.org/"
}
],
"description": "Common interface for HTTP server-side middleware",
"keywords": [
"http",
"http-interop",
"middleware",
"psr",
"psr-15",
"psr-7",
"request",
"response"
],
"support": {
"issues": "https://github.com/php-fig/http-server-middleware/issues",
"source": "https://github.com/php-fig/http-server-middleware/tree/1.0.2"
},
"time": "2023-04-11T06:14:47+00:00"
},
{
"name": "psr/log",
"version": "3.0.2",

@ -0,0 +1,13 @@
<?php
use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;
use Psr\Http\Message\ServerRequestInterface;
use Laravel\Passport\Bridge\AccessTokenRepository;
use Laravel\Passport\TokenRepository;
use Laravel\Passport\Passport;
Route::post('/oauth/token', function (ServerRequestInterface $serverRequest) {
return app()->make(\Laravel\Passport\Http\Controllers\AccessTokenController::class)
->issueToken($serverRequest);
});
Loading…
Cancel
Save