Skip to content

Commit e364862

Browse files
authored
Merge pull request #7 from RaiolaNetworks/develop
Release versión 1.0.0
2 parents 40ee3e4 + 4b1fc23 commit e364862

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1143
-155
lines changed

.github/ISSUE_TEMPLATE/feature_request.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,4 @@ body:
3939
label: "Feature priority"
4040
options:
4141
- label: "Critical"
42-
- label: "Important"
43-
- label: "Nice to have"
42+

.github/workflows/fix-php-code-style-issues.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ on:
55
paths:
66
- '**.php'
77

8+
workflow_dispatch:
9+
810
permissions:
911
contents: write
1012

.github/workflows/phpstan.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ on:
44
push:
55
paths:
66
- '**.php'
7-
- 'phpstan.neon'
7+
- 'phpstan.neon.dist'
88
- '.github/workflows/phpstan.yml'
9+
- 'composer.json'
10+
- 'composer.lock'
11+
12+
workflow_dispatch:
913

1014
jobs:
1115
phpstan:
@@ -18,7 +22,7 @@ jobs:
1822
- name: Setup PHP
1923
uses: shivammathur/setup-php@v2
2024
with:
21-
php-version: '8.2'
25+
php-version: '8.3'
2226
coverage: none
2327

2428
- name: Install composer dependencies

.github/workflows/run-tests.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: run-tests
1+
name: Pest
22

33
on:
44
push:
@@ -9,14 +9,16 @@ on:
99
- 'composer.json'
1010
- 'composer.lock'
1111

12+
workflow_dispatch:
13+
1214
jobs:
1315
test:
1416
runs-on: ${{ matrix.os }}
1517
timeout-minutes: 5
1618
strategy:
1719
fail-fast: true
1820
matrix:
19-
os: [ubuntu-latest, windows-latest]
21+
os: [ubuntu-latest]
2022
php: [8.3, 8.2]
2123
laravel: [11.*, 10.*]
2224
stability: [prefer-lowest, prefer-stable]

.github/workflows/update-changelog.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ on:
44
release:
55
types: [released]
66

7+
workflow_dispatch:
8+
79
permissions:
810
contents: write
911

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ phpstan.neon
1010
testbench.yaml
1111
vendor
1212
node_modules
13+
pest-coverage-results

README.md

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,9 @@ This OAuth for Laravel package provides a simple and reusable integration for im
99

1010
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.
1111

12-
<!-- ## Support us
12+
## Get to know us
1313

14-
[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/oauth.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/oauth)
15-
16-
We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).
17-
18-
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards). -->
14+
[<img src="https://cdn-assets.raiolanetworks.com/dist/images/logos/logo-blue.svg" width="419px" />](https://raiolanetworks.com)
1915

2016
## Installation
2117

@@ -33,7 +29,7 @@ php artisan oauth:install
3329

3430
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.
3531

36-
Steps in the installation process:
32+
#### Steps in the installation process:
3733

3834
### Setting variables in the configuration file
3935
- **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.
@@ -42,6 +38,10 @@ Steps in the installation process:
4238

4339
- **Login route**: You need to provide the route defined in the project where the login process takes place.
4440

41+
- **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.
42+
43+
- **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).
44+
4545
### Creation of variables in the .env file
4646
- **OAuth base URL**: Enter the base URL of the OAuth provider, which will be used for authorization and authentication requests.
4747

@@ -53,6 +53,16 @@ Steps in the installation process:
5353

5454
- **OAuth mode**: Select the mode of operation of the OAuth system, which will allow 3 modes: “OAUTH”, “PASSWORD” or “BOTH”.
5555

56+
## IMPORTANT
57+
58+
~~~
59+
If the process is not completed correctly or is aborted, the implementation and use of the package will result in errors, such as:
60+
61+
- Missing the new database table required to store OAuth user data.
62+
- Incorrectly configured configuration file.
63+
- Environment variables that are improperly defined or missing.
64+
~~~
65+
5666
Once all steps are completed, the migrations will be automatically executed and the configuration file will be published.
5767

5868
You can publish different files:
@@ -74,14 +84,38 @@ php artisan vendor:publish --tag="oauth-translations"
7484

7585
## Implementing the Package in the Project
7686

87+
88+
Before starting to develop the workflow, it is recommended to understand how the package works when creating or modifying users and groups.
89+
90+
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.
91+
92+
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.
93+
94+
**IMPORTANT**
95+
96+
It is likely necessary to implement these interfaces to override the logic for handling the users and groups returned by the OAuth service.
97+
98+
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.
99+
100+
```php
101+
return [
102+
...
103+
104+
'user_handler' => App\Models\User::class,
105+
'group_handler' => App\Models\User::class,
106+
];
107+
```
108+
109+
---
110+
77111
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:
78112

79113
```php
80114
$authController = new OAuthController;
81115
$authController->request();
82116
```
83117

84-
#### 1. Create a Controller to Handle OAuth Authentication
118+
### 1. Create a Controller to Handle OAuth Authentication
85119

86120
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.
87121

@@ -104,7 +138,7 @@ class AuthController extends Controller
104138
}
105139
```
106140

107-
#### 2. Set Up a Login Route
141+
### 2. Set Up a Login Route
108142

109143
In your routes file (`routes/web.php`), define a route that points to the controller you just created. This route will trigger the OAuth authentication process when the user interacts with the login button or link.
110144

@@ -114,7 +148,7 @@ use App\Http\Controllers\AuthController;
114148
Route::get('/login/oauth', [AuthController::class, 'loginWithOAuth'])->name('login.oauth');
115149
```
116150

117-
#### 3. Create a Login Button or Link in the View
151+
### 3. Create a Login Button or Link in the View
118152

119153
In your application's view (e.g., `resources/views/auth/login.blade.php`), add a button or link that points to the route you defined in the previous step. When the user clicks the button, the OAuth authentication process will begin.
120154

@@ -124,14 +158,24 @@ In your application's view (e.g., `resources/views/auth/login.blade.php`), add a
124158
</a>
125159
```
126160

127-
#### 4. Authentication Process
161+
### 4. Authentication Process
128162

129163
When the user clicks the button or link, a request will be sent to the `login()` function of the `AuthController`. From there, the controller will call the `request()` method of the package's `OAuthController`, which handles the OAuth authentication flow by redirecting the user to the OAuth provider for authorization.
130164

131165
Once the user completes the authorization process, the OAuth provider will redirect back to your application, where you can handle the response and authenticate the user in your system.
132166

133167
This will complete the integration of the OAuth package into your project, allowing you to set up a login flow that triggers the OAuth authentication process with a button or link.
134168

169+
### Other features
170+
171+
This section talks about certain functions of the package, which are good to know.
172+
173+
#### Renew tokens
174+
175+
For token renewal, simply set the 'offline_access' variable in [the configuration file](config/oauth.php) to `true`; the package will handle the rest.
176+
177+
A [middleware](src/Middleware/OAuthTokenRenewal.php) is available that calls the `renew()` function of the `OAuthController`. This function checks whether the authenticated user has an OAuth token and if the expiration time has not passed. If the token has expired, it will determine if a refresh token is being handled. It will either generate a new token or reject and unauthenticate the user's session accordingly.
178+
135179
## Testing
136180

137181
```bash

composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"laravel",
77
"oauth"
88
],
9-
"homepage": "https://github.com/raiolanetworks/oauth",
9+
"homepage": "https://github.com/RaiolaNetworks/simple-oauth2-client",
1010
"license": "MIT",
1111
"authors": [
1212
{
@@ -24,6 +24,7 @@
2424
"require": {
2525
"php": "^8.2",
2626
"illuminate/contracts": "^10.0||^11.0",
27+
"laravel/framework": "11.*",
2728
"laravel/prompts": "^0.1.25",
2829
"league/oauth2-client": "^2.7",
2930
"livewire/livewire": "^3.5",
@@ -32,11 +33,13 @@
3233
"require-dev": {
3334
"larastan/larastan": "^2.0",
3435
"laravel/pint": "^1.14",
36+
"mockery/mockery": "^1.6",
3537
"nunomaduro/collision": "^8.1.1||^7.10.0",
3638
"orchestra/testbench": "^9.4",
3739
"pestphp/pest": "^2.34",
3840
"pestphp/pest-plugin-arch": "^2.7",
39-
"pestphp/pest-plugin-laravel": "^2.3"
41+
"pestphp/pest-plugin-laravel": "^2.3",
42+
"pestphp/pest-plugin-type-coverage": "^2.8"
4043
},
4144
"autoload": {
4245
"psr-4": {
@@ -47,6 +50,7 @@
4750
"autoload-dev": {
4851
"psr-4": {
4952
"Raiolanetworks\\OAuth\\Tests\\": "tests/",
53+
"Raiolanetworks\\OAuth\\Tests\\Database\\Factories\\": "tests/database/factories/",
5054
"Workbench\\App\\": "workbench/app/",
5155
"Workbench\\Database\\Factories\\": "workbench/database/factories/",
5256
"Workbench\\Database\\Seeders\\": "workbench/database/seeders/"

config/oauth.php

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,62 @@
33
declare(strict_types=1);
44

55
return [
6-
// URL of the chosen system for login via OAuth
7-
'base_url' => env('OAUTH_BASE_URL', ''),
8-
// Client ID of the OAuth system
9-
'client_id' => env('OAUTH_CLIENT_ID', ''),
10-
// Client secret key of the OAuth system
11-
'client_secret' => env('OAUTH_CLIENT_SECRET', ''),
12-
// Route of the project receiving the callback
13-
'callback' => env('OAUTH_CALLBACK_URI', ''),
14-
// Name of administration group in the OAuth system
15-
'admin_group' => env('OAUTH_ADMIN_GROUP', ''),
6+
/**
7+
* Environment variables to configuration the OAuth services.
8+
*
9+
* base_url: URL of the chosen system for login via OAuth
10+
* client_id: Client ID of the OAuth system
11+
* client_secret: Client secret key of the OAuth system
12+
* callback: Route of the project receiving the callback
13+
* admin_group: Name of administration group in the OAuth system
14+
* mode: Preferred login mode in your project. Allow 3 types:
15+
* PASSWORD -> Show login with username and password
16+
* OAUTH -> Show login with oauth
17+
* BOTH -> Show both login types
18+
*/
19+
'base_url' => env('OAUTH_BASE_URL', ''),
20+
'client_id' => env('OAUTH_CLIENT_ID', ''),
21+
'client_secret' => env('OAUTH_CLIENT_SECRET', ''),
22+
'callback' => env('OAUTH_CALLBACK_URI', ''),
23+
'admin_group' => env('OAUTH_ADMIN_GROUP', ''),
24+
'mode' => env('OAUTH_MODE', 'OAUTH'),
1625

1726
/**
18-
* Preferred login mode in your project
27+
* Integration configuration variables.
1928
*
20-
* Allow 3 types:
21-
* PASSWORD -> Show login with username and password
22-
* OAUTH -> Show login with oauth
23-
* BOTH -> Show both login types
29+
* user_model_name: Model name in your project of the Authenticatable users (default: \App\Models\User)
30+
* guard_name: Client ID of the OAuth system
31+
* login_route_name: Client secret key of the OAuth system
32+
* redirect_route_name_callback_ok: Route of the project receiving the callback
2433
*/
25-
'mode' => env('OAUTH_MODE', 'OAUTH'),
34+
'user_model_name' => '\App\Models\User'::class,
35+
'guard_name' => 'web',
36+
'login_route_name' => 'login',
37+
'redirect_route_name_callback_ok' => 'home',
2638

27-
// Model name in your project of the Authenticatable users (default: \App\Models\User)
28-
'user_model_name' => '\App\Models\User'::class,
29-
// Guard name who is in charge of this logging in your project
30-
'guard_name' => 'web',
39+
/**
40+
* Handler classes configuration
41+
*
42+
* Here the classes in charge of managing the handler classes of your
43+
* application are declared.
44+
*
45+
* By default, the base classes are defined for managing users and groups.
46+
* But they can be customized to meet the needs of the project.
47+
*
48+
* For example:
49+
* 'user_handler' => App\Models\User::class,
50+
* 'group_handler' => App\Models\User::class
51+
*
52+
* As long as the interfaces have been implemented in these models.
53+
*/
54+
'user_handler' => Raiolanetworks\OAuth\Handlers\BaseOAuthUserHandler::class,
55+
'group_handler' => Raiolanetworks\OAuth\Handlers\BaseOAuthGroupHandler::class,
3156

32-
// Route to redirect when callback response is Ok
33-
'login_route' => '/login',
34-
// Route to redirect when callback response is Ok
35-
'redirect_route_callback_ok' => '/',
57+
/**
58+
* Allow refresh tokens in your app
59+
*
60+
* If the value is true, it will add the 'offline_access' scope in the OAuth
61+
* provider configuration.
62+
*/
63+
'offline_access' => true,
3664
];
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Raiolanetworks\OAuth\Database\Factories;
6+
7+
use Carbon\Carbon;
8+
use Illuminate\Database\Eloquent\Factories\Factory;
9+
use Raiolanetworks\OAuth\Models\OAuth;
10+
11+
/**
12+
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\Raiolanetworks\OAuth\Models\OAuth>
13+
*/
14+
class OAuthFactory extends Factory
15+
{
16+
/**
17+
* The name of the factory's corresponding model.
18+
*
19+
* @var class-string<OAuth>
20+
*/
21+
protected $model = OAuth::class;
22+
23+
/**
24+
* Define the model's default state.
25+
*
26+
* @return array<string, mixed>
27+
*/
28+
public function definition(): array
29+
{
30+
return [
31+
'oauth_id' => 'oauth_id',
32+
'oauth_token' => 'oauth_token',
33+
'oauth_refresh_token' => 'oauth_refresh_token',
34+
'oauth_token_expires_at' => Carbon::now()->addHours(2)->timestamp,
35+
];
36+
}
37+
}

0 commit comments

Comments
 (0)