Skip to content

Commit 1274c5e

Browse files
committed
WIP
1 parent 2d48f3c commit 1274c5e

File tree

5 files changed

+114
-5
lines changed

5 files changed

+114
-5
lines changed

.phpunit.cache/test-results

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

README.md

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,67 @@ The package provides built-in routes for OAuth authentication:
114114
1. **Redirect to Bexio**: `/bexio/redirect`
115115
2. **OAuth Callback**: `/bexio/callback`
116116

117-
You can customize the route prefix in your config file:
117+
You can customize the route prefix and middleware in your config file:
118118

119119
```php
120120
// config/bexio.php
121121
'route_prefix' => 'custom-bexio-prefix',
122+
123+
// Add custom middleware to OAuth routes (in addition to 'web' middleware)
124+
'route_middleware' => ['auth', 'verified'],
125+
```
126+
127+
#### OAuth Callback Response
128+
129+
After the OAuth callback is processed, the user will be redirected to the URL specified in your configuration (`config('bexio.redirect_url')` or `/` by default) with flash session data indicating the result:
130+
131+
**Success Response:**
132+
```php
133+
// When OAuth authentication is successful
134+
session()->get('bexio_oauth_success'); // true
135+
session()->get('bexio_oauth_message'); // 'Successfully authenticated with Bexio.'
136+
```
137+
138+
**Error Responses:**
139+
```php
140+
// When user rejects authorization or OAuth returns an error
141+
session()->get('bexio_oauth_success'); // false
142+
session()->get('bexio_oauth_message'); // 'OAuth authorization failed: access_denied'
143+
144+
// When required parameters (code or state) are missing
145+
session()->get('bexio_oauth_success'); // false
146+
session()->get('bexio_oauth_message'); // 'Missing required parameters: code or state.'
147+
```
148+
149+
**Handling the callback in your controller:**
150+
```php
151+
<?php
152+
153+
namespace App\Http\Controllers;
154+
155+
use Illuminate\Http\Request;
156+
157+
class DashboardController extends Controller
158+
{
159+
public function index(Request $request)
160+
{
161+
if (session()->has('bexio_oauth_success')) {
162+
$success = session()->get('bexio_oauth_success');
163+
$message = session()->get('bexio_oauth_message');
164+
165+
if ($success) {
166+
// OAuth authentication was successful
167+
// You can now use the BexioConnector to make API calls
168+
return view('dashboard.index')->with('success', $message);
169+
} else {
170+
// OAuth authentication failed
171+
return view('dashboard.index')->with('error', $message);
172+
}
173+
}
174+
175+
return view('dashboard.index');
176+
}
177+
}
122178
```
123179

124180
### Multi-Tenant Authentication
@@ -342,8 +398,42 @@ You can specify a custom cache store for OAuth token storage:
342398
// config/bexio.php
343399
'route_prefix' => 'api/bexio', // Custom route prefix
344400
'redirect_url' => '/dashboard', // Where to redirect after OAuth Callback
401+
402+
// Add custom middleware to OAuth routes (in addition to 'web' middleware)
403+
'route_middleware' => ['auth', 'verified'],
404+
```
405+
406+
#### Route Middleware
407+
408+
The OAuth routes (`/bexio/redirect` and `/bexio/callback`) automatically include the `web` middleware group by default. You can add additional middleware using the `route_middleware` configuration:
409+
410+
**Examples:**
411+
412+
```php
413+
// Require authentication for OAuth routes
414+
'route_middleware' => ['auth'],
415+
416+
// Require authentication and email verification
417+
'route_middleware' => ['auth', 'verified'],
418+
419+
// Add custom middleware
420+
'route_middleware' => ['auth', 'custom-middleware'],
421+
422+
// Multiple middleware with parameters
423+
'route_middleware' => ['auth:api', 'throttle:60,1'],
345424
```
346425

426+
**Common Use Cases:**
427+
428+
- **Authentication Required**: Use `['auth']` to ensure only authenticated users can initiate OAuth flow
429+
- **Email Verification**: Use `['auth', 'verified']` for applications requiring email verification
430+
- **Rate Limiting**: Use `['throttle:10,1']` to limit OAuth attempts
431+
- **Custom Authorization**: Add your own middleware to control who can access OAuth routes
432+
433+
The middleware will be applied to both OAuth routes:
434+
- `GET /bexio/redirect` - Initiates OAuth flow
435+
- `GET /bexio/callback` - Handles OAuth callback from Bexio
436+
347437
## Basic Usage
348438

349439
After setting up authentication, create a connector instance:

config/bexio.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
// The prefix for the authentication routes
2121
'route_prefix' => null,
2222

23-
// Redirect after successful authentication
23+
// Middleware for the authentication routes 'web' is included by default.
24+
// You can add your own middleware here, e.g. ['auth', 'verified']
25+
'route_middleware' => [],
26+
27+
// Redirect URL after OAuth callback
2428
'redirect_url' => env('BEXIO_REDIRECT_URL', ''),
2529
];

routes/bexio.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* The default prefix is 'bexio'. It can be customized via 'route_prefix' in config/bexio.php.
1010
* If you change route names, update the connector accordingly.
1111
*/
12-
Route::middleware(['web'])->prefix(config('bexio.route_prefix') ?? 'bexio')->group(function () {
12+
Route::middleware(array_merge(['web'], config('bexio.route_middleware')))->prefix(config('bexio.route_prefix') ?? 'bexio')->group(function () {
1313
Route::get('/redirect', [BexioOAuthController::class, 'redirect'])->name('bexio.oauth.redirect');
1414
Route::get('/callback', [BexioOAuthController::class, 'callback'])->name('bexio.oauth.callback');
1515
});

src/Http/Controllers/BexioOAuthController.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ public function redirect(): RedirectResponse
4444
*/
4545
public function callback(Request $request): RedirectResponse
4646
{
47+
// Handle OAuth errors (like user rejection)
48+
if ($request->has('error')) {
49+
return Redirect::to(config('bexio.redirect_url', '/'))
50+
->with('bexio_oauth_success', false)
51+
->with('bexio_oauth_message', 'OAuth authorization failed: ' . $request->get('error'));
52+
}
53+
54+
if ($request->missing('code') || $request->missing('state')) {
55+
return Redirect::to(config('bexio.redirect_url', '/'))
56+
->with('bexio_oauth_success', false)
57+
->with('bexio_oauth_message', 'Missing required parameters: code or state.');
58+
}
59+
4760
$authenticator = $this->connector()->getAccessToken(
4861
code: $request->get('code'),
4962
state: $request->get('state'),
@@ -53,6 +66,8 @@ public function callback(Request $request): RedirectResponse
5366
App::make(BexioOAuthAuthenticationStoreResolver::class)
5467
->put(authenticator: $authenticator); // @phpstan-ignore-line
5568

56-
return Redirect::to(config('bexio.redirect_url', '/'));
69+
return Redirect::to(config('bexio.redirect_url', '/'))
70+
->with('bexio_oauth_success', true)
71+
->with('bexio_oauth_message', 'Successfully authenticated with Bexio.');
5772
}
5873
}

0 commit comments

Comments
 (0)