Laravel Zero OAuth is a library aimed at adding OAuth 2.0 authentication to your Laravel Zero application, leveraging The PHP League's OAuth 2.0 Client library.
Laravel Zero was created by Nuno Nuno Maduro and Owen Voke, and is a micro-framework that provides an elegant starting point for your console application. It's a customized version of Laravel optimized for building command-line applications.
- To get started install it using composer:
composer require hskrasek/laravel-zero-oauth
- Then add
LaravelZeroOAuthProvider
to yourconfig/app.php
file:
'providers' => [
App\Providers\AppServiceProvider::class,
++ HSkrasek\LaravelZeroOAuth\LaravelZeroOAuthProvider::class,
],
To get started you'll want to configure your OAuth provider. This package supports all providers that are supported by The PHP League's OAuth 2.0 Client, and you can find a list of providers and their configuration options here. You can do this by configuring the provider in your .env
file:
OAUTH2_CLIENT_ID=<CLIENT_ID>
OAUTH2_CLIENT_SECRET=<CLIENT_SECRET>
OAUTH2_SCOPES=<SCOPES>
OAUTH2_AUTHORIZE_URI=<AUTHORIZE_URI>
OAUTH2_TOKEN_URI=<TOKEN_URI>
# Your redirect URI needs to match your applications OAuth configuration. It is recommended to use the default value, but you can change it if necessary.
# The package will attempt to correctly start a server for you using PHP's built in server.
# OAUTH2_REDIRECT_URI="http://127.0.0.1:8000"
# By default the package will use the GenericProvider, but you can change it to any provider supported by The PHP League's OAuth 2.0 Client.
# OAUTH2_PROVIDER=League\OAuth2\Client\Provider\GenericProvider::class
Once you've configured your provider, you can use the oauth:login
command to start the OAuth flow:
php your-app-name oauth:login
After you've authenticated with your provider, you'll be able to access the token for requests with the Keyring
class:
use HSkrasek\LaravelZeroOAuth\Auth\Keyring;
// ...
public function handle(Keyring $keyring)
{
$token = $keyring->get('access_token');
// Use the token to make requests to your provider.
Http::withToken($token->accessToken)->get('https://api.example.com');
}
Laravel Zero OAuth is open-sourced software licensed under the MIT license.