krak/doctrine-oauth2

Doctrine 和 Laravel OAuth2 集成

v0.1.2 2018-03-17 04:42 UTC

This package is auto-updated.

Last update: 2024-09-18 17:42:54 UTC


README

这个库为 Doctrine 和 Laravel 提供了 OAuth2 集成。它使用 league/oauth2-server 包来完成所有繁重的工作,并使用 Doctrine ORM 作为后端。

安装

使用 composer 在 krak/doctrine-oauth2 上安装

Laravel 使用方法

<?php

$app = new Laravel\Lumen\Application(__DIR__ . '/..');

$app->register(LaravelDoctrine\ORM\DoctrineServiceProvider::class);
$app->register(LaravelDoctrine\Migrations\MigrationsServiceProvider::class);
$app->register(Krak\DoctrineOAuth2\OAuth2ServiceProvider::class);

$app['oauth2.seeds']->push(function($em, $logger) {
    $scopes = [
        new Scope('basic', 'Basic', 'Allows basic access to the API.'),
        new Scope('user', 'User', 'Allows user access to the API.'),
    ];

    $logger->info("Creating local client");
    $client = new Client('local', 'local', 'local123', '', $scopes);
    $em->persist($client);
    foreach ($scopes as $scope) {
        $logger->info("Creating scope: ". json_encode($scope, JSON_PRETTY_PRINT));
        $em->persist($scope);
    }
});

$app->router->group(['middleware' => 'oauth2'], function($router) {
    $router->get('/', function(Illuminate\Http\Request $req) {
        return $req->attributes->all();
    });
});
<?php

// config/oauth2.php

return [
    'grants' => [
        'refresh_token',
        'password',
        'client_credentials',
        'authorization_code',
        'implicit'
    ],
    'client_credentials' => [
        'access_token_ttl' => new DateInterval('P1Y'),
    ],
    'access_token_ttl' => new DateInterval('PT2H'),
    'refresh_token_ttl' => new DateInterval('P2Y'),
    'private_key' => resource_path('oauth-private.key'),
    'public_key' => resource_path('oauth-public.key'),
];

您需要运行 ./artisan oauth2:generate-keys 来创建 OAuth 密钥。您可以选择运行 ./artisan oauth2:seed 来运行您为 OAuth2 包定义的任何种子。