-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding well-known configuration and JWT routes in Laravel
When using Laravel with Passport, we have enough knowledge of the environment to be able to provide 2 useful routes our of the box: - a discovery endpoint at `/.well-known/openid-configuration`. - a JWKS endpoint at `/oauth/jwks`. This greatly eases integrating with Laravel since clients can now use auto-discovery to integrate with Laravel + PassPort + OpenID.
- Loading branch information
Showing
5 changed files
with
119 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace OpenIDConnect\Laravel; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Route; | ||
|
||
class DiscoveryController | ||
{ | ||
public function discovery(Request $request) | ||
{ | ||
$response = [ | ||
'issuer' => url('/'), | ||
'authorization_endpoint' => route('passport.authorizations.authorize'), | ||
'token_endpoint' => route('passport.token'), | ||
'jwks_uri' => route('openid.jwks'), | ||
'response_types_supported' => [ | ||
'code', | ||
'token', | ||
'id_token', | ||
'code token', | ||
'code id_token', | ||
'token id_token', | ||
'code token id_token', | ||
'none', | ||
], | ||
'subject_types_supported' => [ | ||
'public', | ||
], | ||
'id_token_signing_alg_values_supported' => [ | ||
'RS256', | ||
], | ||
'scopes_supported' => config('openid.passport.tokens_can'), | ||
'token_endpoint_auth_methods_supported' => [ | ||
'client_secret_basic', | ||
'client_secret_post', | ||
], | ||
]; | ||
|
||
if (Route::has('openid.userinfo')) { | ||
$response['userinfo_endpoint'] = route('openid.userinfo'); | ||
} | ||
|
||
return response()->json($response, 200, [], JSON_PRETTY_PRINT); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace OpenIDConnect\Laravel; | ||
|
||
use Illuminate\Config\Repository as Config; | ||
use Laravel\Passport\Passport; | ||
|
||
class JwksController | ||
{ | ||
public function jwks() { | ||
$publicKey = $this->getPublicKey(); | ||
|
||
// Source: https://www.tuxed.net/fkooman/blog/json_web_key_set.html | ||
$keyInfo = openssl_pkey_get_details(openssl_pkey_get_public($publicKey)); | ||
|
||
$jsonData = [ | ||
'keys' => [ | ||
[ | ||
'kty' => 'RSA', | ||
'n' => rtrim(str_replace(['+', '/'], ['-', '_'], base64_encode($keyInfo['rsa']['n'])), '='), | ||
'e' => rtrim(str_replace(['+', '/'], ['-', '_'], base64_encode($keyInfo['rsa']['e'])), '='), | ||
], | ||
], | ||
]; | ||
|
||
return response()->json($jsonData, 200, [], JSON_PRETTY_PRINT); | ||
} | ||
|
||
private function getPublicKey(): string { | ||
$publicKey = str_replace('\\n', "\n", app()->make(Config::class)->get('passport.public_key') ?? ''); | ||
|
||
if (!$publicKey) { | ||
$publicKey = 'file://'.Passport::keyPath('oauth-public.key'); | ||
} | ||
|
||
return $publicKey; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php | ||
Route::get('/oauth/jwks', \OpenIDConnect\Laravel\JwksController::class."@jwks")->name('openid.jwks'); | ||
Route::get('/.well-known/openid-configuration', \OpenIDConnect\Laravel\DiscoveryController::class."@discovery")->name('openid.discovery'); |