Skip to content

Commit

Permalink
ShipSaaS Laravel JWKS
Browse files Browse the repository at this point in the history
  • Loading branch information
sethsandaru committed Oct 15, 2023
1 parent 1c181ee commit 763fa86
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
Binary file added .github/laravel-jwks-diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@ Documentation: [ShipSaaS Laravel JWKS](https://laravel-jwks.shipsaas.tech)

## Sample use cases

Ever thinking of microservices? One of the biggest challenges is the Authentication & Authorization.
Ever thinking of microservices? One of the biggest challenges is having the Authentication (& Authorization) service.

However, you don't have to spend enormous time to build a brand new AuthService and migrate the current users.

Your current app = the core, the heart of everything. Let's build satellite services around that.

< diagram here >
![laravel-jwks-diagram.png](.github/laravel-jwks-diagram.png)

With JWKS, satellite services can simply verify the signed JWT token before handling the actual requests.
With (Laravel) JWKS, we will have:

JWKS is supported in multiple languages e.g.: Node.js, go,...
- The main app exposes the JWKs internally for the satellite microservices.
- The satellite services can simply obtain the JWKs and verify the signed JWT token before handling the actual requests.

JWKS is supported in multiple languages e.g.: Node.js, Go,...

## Installation

Expand Down
10 changes: 10 additions & 0 deletions src/Configs/jwks.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@
*/
'use_default_jwks_route' => true,

/**
* The middleware that you want to apply before accessing the handler
*
* This would come in handy if you want to have your own custom authentication (basic token or something)
*/
'default_jwks_route_middlewares' => [
// 'base_auth',
// AuthMiddleware::class,
],

/**
* The JWT Algorithm of your current application
*
Expand Down
3 changes: 2 additions & 1 deletion src/Routes/jwks_routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
use ShipSaasLaravelJwks\Http\Controllers\JwksController;

if (config('jwks.use_default_jwks_route')) {
Route::get('auth/jwks', [JwksController::class, 'index']);
Route::get('auth/jwks', [JwksController::class, 'index'])
->middleware(config('jwks.default_jwks_route_middlewares'));
}
37 changes: 37 additions & 0 deletions tests/Features/JwksControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace ShipSaasLaravelJwks\Tests\Features;

use ShipSaasLaravelJwks\Tests\TestCase;

class JwksControllerTest extends TestCase
{
public function testIndexReturnsTheDefaultJwks()
{
config([
'jwks.default_keys_path' => [
__DIR__ . '/../__fixtures__/public-key.pub'
],
]);

$this->json('GET', '/auth/jwks')
->assertOk()
->assertJsonIsArray()
->assertJsonFragment([
'kty' => 'RSA',
'alg' => 'RS256',
]);
}

public function testIndexReturnsEmptyOnNoKey()
{
config([
'jwks.default_keys_path' => [],
]);

$this->json('GET', '/auth/jwks')
->assertOk()
->assertJsonIsArray()
->assertJsonCount(0);
}
}

0 comments on commit 763fa86

Please sign in to comment.