PHPackages                             lantosbro/laravel-oauth2-client - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. lantosbro/laravel-oauth2-client

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

lantosbro/laravel-oauth2-client
===============================

Laravel OAuth2 Client

010PHP

Since Aug 31Pushed 2y agoCompare

[ Source](https://github.com/LantosBro/laravel-oauth2-client)[ Packagist](https://packagist.org/packages/lantosbro/laravel-oauth2-client)[ RSS](/packages/lantosbro-laravel-oauth2-client/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel OAuth2 Client
=====================

[](#laravel-oauth2-client)

Laravel package for OAuth 2 Client Authentication
-------------------------------------------------

[](#laravel-package-for-oauth-2-client-authentication)

[![Header Image](https://github.com/MacsiDigital/repo-design/raw/master/laravel-oauth2-client/header.png)](https://github.com/MacsiDigital/repo-design/raw/master/laravel-oauth2-client/header.png)

 [![tests badge](https://github.com/MacsiDigital/laravel-oauth2-client/workflows/Tests/badge.svg)](https://github.com/MacsiDigital/laravel-oauth2-client/actions?query=workflow%3ATests) [![version badge](https://camo.githubusercontent.com/f21e9ba81610c94e7842cb4c5ee872b8e54a2cff1fe4884b1b3c1037b559b9b6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c616e746f7362726f2f6c61726176656c2d6f61757468322d636c69656e742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/lantosbro/laravel-oauth2-client) [![downloads badge](https://camo.githubusercontent.com/d2dbc0ed7b03e9eb542405d78108945dbe969e7f5d4f3444a822c1217be18ca2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c616e746f7362726f2f6c61726176656c2d6f61757468322d636c69656e742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/lantosbro/laravel-oauth2-client)

A little OAuth2 Client Authentication Library

Support us
----------

[](#support-us)

We invest a lot in creating [open source packages](https://macsidigital.co.uk/open-source), and would be grateful for a [sponsor](https://github.com/sponsors/MacsiDigital) if you make money from your product that uses them.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require lantosbro/laravel-oauth2-client
```

Package helper
--------------

[](#package-helper)

The package helper can be used to return the package version

Usage
-----

[](#usage)

The main aim of this library is to handle the authentication requirements of OAuth2. Then you should have a token which you can use in an API client.

There are Token Drivers for both File and Database.

### File

[](#file)

The file driver will save a file in storage/app/oauth2, which will keep the token details required to communicate with the OAuth2 Server.

### Database

[](#database)

#### Config

[](#config)

If you want to use the DB driver and would like to customise teh table name then you can publish the config file and amend the table\_name column

```
php artisan vendor:publish --provider="LantosBro\OAuth2\Providers\OAuth2ServiceProvider" --tag="integration-config"
```

#### Migrations

[](#migrations)

If using DB driver you will need to publish migrations.

```
php artisan vendor:publish --provider="LantosBro\OAuth2\Providers\OAuth2ServiceProvider" --tag="integration-migrations"
```

Then you will need to run migrations

```
php artisan migrate
```

### Integration Configuration

[](#integration-configuration)

The majority of the setup can be found in the config file, which needs to be copied and placed in the laravel config directory

```
return [
	'oauth2' => [
		'clientId' => '',
		'clientSecret' => '',
	],
	'options' => [
		'scope' => ['openid email profile offline_access accounting.settings accounting.transactions accounting.contacts accounting.journals.read accounting.reports.read accounting.attachments']
	],
	'tokenProcessor' => '\LantosBro\OAuth2\Support\AuthorisationProcessor',
	'tokenModel' => '\LantosBro\OAuth2\Support\Token\File',
	'authorisedRedirect' => '',
	'failedRedirect' => '',
];
```

(Todo: Create a command to automatically publish the config file)

As the primary focus of the library is in packages, this needs to be loaded into laravel with an integration name through a service provider. So for xero:-

```
$this->mergeConfigFrom(__DIR__.'/../../config/config.php', 'xero');
```

You also need to check the credential requirements for the oauth2 server and add to config as required.

Authorising &amp; the AuthorisationProcessor
--------------------------------------------

[](#authorising--the-authorisationprocessor)

There are routes pre-defined to connect to the Oauth2 server, the named routes are 'oauth2.authorise' &amp; 'oauth2.callback' and both need passing in the integration. So for xero:-

```
route('oauth2.authorise', ['integration' => 'xero']); // will return /oauth2/xero/authorise
```

If you are using a simple straight forward Server and if all setup is done correctly we should be linking the account in no time.

However, some API's will have custom processing requirements, for example Xero needs a tenant id.

In these cases we need to create a custom AuthorisationProcessor, which is passed the League/Oauth2-client AccessToken and the integration name so that the config can be pulled.

So this is how it would look for Xero:-

```
