PHPackages                             raiolanetworks/simple-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. raiolanetworks/simple-oauth2-client

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

raiolanetworks/simple-oauth2-client
===================================

Easily add OAuth2 access to your projects

2.0.0(3mo ago)4457↓57.1%[1 PRs](https://github.com/RaiolaNetworks/simple-oauth2-client/pulls)MITPHPPHP ^8.2|^8.3|^8.4CI failing

Since Sep 20Pushed 2w ago2 watchersCompare

[ Source](https://github.com/RaiolaNetworks/simple-oauth2-client)[ Packagist](https://packagist.org/packages/raiolanetworks/simple-oauth2-client)[ Docs](https://github.com/RaiolaNetworks/simple-oauth2-client)[ GitHub Sponsors](https://github.com/RaiolaNetworks)[ RSS](/packages/raiolanetworks-simple-oauth2-client/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (9)Dependencies (28)Versions (16)Used By (0)

Easily add OAuth2 access to your projects
=========================================

[](#easily-add-oauth2-access-to-your-projects)

[![Latest Version on Packagist](https://camo.githubusercontent.com/8bd71fd7c863e58208a8db0a6b9ac487967b1b560a161e9a42b7fd0d4e2d68d3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7261696f6c616e6574776f726b732f6f617574682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/raiolanetworks/simple-oauth2-client)[![GitHub Tests Action Status](https://camo.githubusercontent.com/3863e314cf4ab41766c02651d67c1edeaca8fc4f98bf8beaf0b1285365919842/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7261696f6c616e6574776f726b732f6f617574682f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/raiolanetworks/oauth/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/fe8a50b1ae9f2084b6ee55da7647bc01f2edbb9ed2f1a46cad079b829d8452d3/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7261696f6c616e6574776f726b732f6f617574682f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/raiolanetworks/oauth/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/0fa54354b18b0cbf180463d45596383007dd4c2662a58ce181cd089f301141fa/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7261696f6c616e6574776f726b732f6f617574682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/raiolanetworks/simple-oauth2-client)

This OAuth for Laravel package provides a simple and reusable integration for implementing OAuth authentication in Laravel projects. The main goal is to allow developers to extend and customize their application's authentication system without needing to modify their existing user models.

The package is designed to work flexibly with any user model that implements the Authenticatable interface, ensuring that it can be easily adapted to various projects without direct dependencies on a specific user model.

Get to know us
--------------

[](#get-to-know-us)

[![](https://camo.githubusercontent.com/a2a13aedf73f874d9f95dd4a2e9c0505a43a8e0934ef8dd225b8935fe0ad1192/68747470733a2f2f63646e2d6173736574732e7261696f6c616e6574776f726b732e636f6d2f646973742f696d616765732f6c6f676f732f6c6f676f2d626c75652e737667)](https://raiolanetworks.com)

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

[](#installation)

You can install the package via composer:

```
composer require raiolanetworks/simple-oauth2-client
```

The next step is to configure the package using this command:

```
php artisan oauth:install
```

When this command is executed, the user will be guided through a series of steps to properly configure the necessary variables in both the configuration file and the environment file.

#### Steps in the installation process:

[](#steps-in-the-installation-process)

### Setting variables in the configuration file

[](#setting-variables-in-the-configuration-file)

- **Authenticatable model name**: Here you need to enter the name of the user management model used in the project, which must implement the `Authenticatable` interface.
- **Main guard name**: You should specify the name of the guard that handles the login process in the project.
- **Login route**: You need to provide the route defined in the project where the login process takes place.
- **Route name when callback is OK**: Here you indicate the name of your project path where the redirection will be made after a correct response from the provider.
- **Will you use the refresh token system in your app?**: Checking 'Yes' will allow the 'offline\_access' scope to be added to the provider configuration. Which will allow the use of refresh token (as long as it is enabled in your OAuth provider).

### Creation of variables in the .env file

[](#creation-of-variables-in-the-env-file)

- **OAuth base URL**: Enter the base URL of the OAuth provider, which will be used for authorization and authentication requests.
- **OAuth client ID**: Provide the unique identifier of the OAuth client, issued by the OAuth authentication service being used.
- **OAuth client secret key**: Enter the secret key associated with the OAuth client, which is used to validate the authentication between the client and the OAuth server.
- **OAuth admin group name**: Specify the name of the user group with administrative privileges that will be managed within the OAuth system.
- **OAuth mode**: Select the mode of operation of the OAuth system, which will allow 3 modes: “OAUTH”, “PASSWORD” or “BOTH”.

IMPORTANT
---------

[](#important)

```
If the process is not completed correctly or is aborted, the implementation and use of the package will result in errors, such as:

- Missing the new database table required to store OAuth user data.
- Incorrectly configured configuration file.
- Environment variables that are improperly defined or missing.

```

Once all steps are completed, the migrations will be automatically executed and the configuration file will be published.

You can publish different files:

#### Migrations

[](#migrations)

```
php artisan vendor:publish --tag="oauth-migrations"
```

#### Config file

[](#config-file)

```
php artisan vendor:publish --tag="oauth-config"
```

#### Translations

[](#translations)

```
php artisan vendor:publish --tag="oauth-translations"
```

Implementing the Package in the Project
---------------------------------------

[](#implementing-the-package-in-the-project)

Before starting to develop the workflow, it is recommended to understand how the package works when creating or modifying users and groups.

To achieve this, two interfaces have been created: [OAuthUserHandlerInterface](src/Contracts/OAuthUserHandlerInterface.php) and [OAuthGroupHandlerInterface](src/Contracts/OAuthGroupHandlerInterface.php). These interfaces can be implemented in the user model of your application, allowing you to override the `handleUser()` and `handleGroup()` methods, respectively.

There are also two predefined classes: [BaseOAuthUserHandler](src/Handlers/BaseOAuthUserHandler.php) and [BaseOAuthGroupHandler](src/Handlers/BaseOAuthGroupHandler.php), which implement these interfaces with default logic. These will serve as an example for the developer and will also help, if it is a simple application, for the package to work without having to overwrite anything.

**IMPORTANT**

It is likely necessary to implement these interfaces to override the logic for handling the users and groups returned by the OAuth service.

However, **do not forget** to override the `user_handler` and `group_handler` variables in the [configuration file](config/oauth.php), specifying which model will override the interface methods.

```
return [
    ...

    'user_handler'  => App\Models\User::class,
    'group_handler' => App\Models\User::class,
];
```

---

Once you have installed the package in your project, the next step is to configure your own login flow. This can be done through a button, link, or any other interface element that triggers a function in a controller. In this function, you'll implement the package and call the `request()` function:

```
$authController = new OAuthController;
$authController->request();
```

### 1. Create a Controller to Handle OAuth Authentication

[](#1-create-a-controller-to-handle-oauth-authentication)

First, you'll need to create a controller that handles the OAuth authentication logic. You can use the `OAuthController` provided by the package or create your own controller. The main goal is to call the `request()` method from the package to start the OAuth authentication process.

##### Example Controller:

[](#example-controller)

```
