├── CHANGELOG.md ├── LICENCE.txt ├── README.md ├── bin └── create_keys ├── composer.json └── src ├── ClaimExtractor.php ├── Claims ├── ClaimSet.php ├── ClaimSetInterface.php ├── Claimable.php ├── Scopable.php └── Traits │ ├── WithClaims.php │ └── WithScope.php ├── Entities ├── AccessTokenEntity.php ├── AuthCodeEntity.php ├── ClientEntity.php ├── IdentityEntity.php ├── RefreshTokenEntity.php └── ScopeEntity.php ├── Exceptions └── ProtectedScopeException.php ├── Grant └── AuthCodeGrant.php ├── IdTokenResponse.php ├── Interfaces ├── CurrentRequestServiceInterface.php ├── IdentityEntityInterface.php └── IdentityRepositoryInterface.php ├── Laravel ├── DiscoveryController.php ├── JwksController.php ├── LaravelCurrentRequestService.php ├── PassportServiceProvider.php ├── config │ └── openid.php └── routes │ └── web.php ├── Repositories ├── AccessTokenRepository.php ├── AuthCodeRepository.php ├── ClientRepository.php ├── IdentityRepository.php └── RefreshTokenRepository.php └── Services ├── CurrentRequestService.php └── IssuedByGetter.php /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## 1.0.0 (2021/../..) 4 | * Initial version 5 | -------------------------------------------------------------------------------- /LICENCE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Ron van der Heijden 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | [](https://github.com/jeremy379/laravel-openid-connect/actions/workflows/php82.yml) 3 | 4 | # OpenID Connect for Laravel 5 | 6 | OpenID Connect support to the PHP League's OAuth2 Server. 7 | 8 | This is a fork of [ronvanderheijden/openid-connect](https://github.com/ronvanderheijden/openid-connect). 9 | 10 | It's made to support only Laravel and [Laravel Passport](https://laravel.com/docs/10.x/passport). 11 | 12 | ## Requirements 13 | 14 | * Requires PHP version `^8.2`. 15 | * [lcobucci/jwt](https://github.com/lcobucci/jwt) version `^4.0`. 16 | * [league/oauth2-server](https://github.com/thephpleague/oauth2-server) `^8.2`. 17 | * Laravel 10 to 12 18 | * Laravel Passport installed and configured 19 | 20 | ## Installation 21 | 22 | ```sh 23 | composer require jeremy379/laravel-openid-connect 24 | ``` 25 | 26 | Now when calling the `oauth/authorize` endpoint, provide the `openid` scope to get an `id_token`. 27 | Provide more scopes (e.g. `openid profile email`) to receive additional claims in the `id_token`. 28 | 29 | The id_token will be returned after the call to the `oauth/token` endpoint. 30 | 31 | ### Laravel 11 32 | 33 | On Laravel 11 you may need to register the package: https://github.com/jeremy379/laravel-openid-connect/issues/31 34 | 35 | ## Configuration 36 | 37 | ### 1.) Add the scope in your AuthServiceProvider in boot() method. 38 | 39 | ```php 40 | Passport::tokensCan(config('openid.passport.tokens_can')); 41 | ```` 42 | 43 | You may want to combine existing scope and oauth implementation with the open ID connect. 44 | 45 | ```php 46 | $scopes = array_merge($yourScope, config('openid.passport.tokens_can')); 47 | Passport::tokensCan($scopes); 48 | ```` 49 | 50 | ### 2.) create an entity 51 | Create an entity class in `app/Entities/` named `IdentityEntity` or `UserEntity`. This entity is used to collect the claims. 52 | 53 | You can customize the entity setup by using another IdentityRepository, this is customizable in the config file. 54 | 55 | ```php 56 | # app/Entities/IdentityEntity.php 57 | namespace App\Entities; 58 | 59 | use League\OAuth2\Server\Entities\Traits\EntityTrait; 60 | use OpenIDConnect\Claims\Traits\WithClaims; 61 | use OpenIDConnect\Interfaces\IdentityEntityInterface; 62 | 63 | class IdentityEntity implements IdentityEntityInterface 64 | { 65 | use EntityTrait; 66 | use WithClaims; 67 | 68 | /** 69 | * The user to collect the additional information for 70 | */ 71 | protected User $user; 72 | 73 | /** 74 | * The identity repository creates this entity and provides the user id 75 | * @param mixed $identifier 76 | */ 77 | public function setIdentifier($identifier): void 78 | { 79 | $this->identifier = $identifier; 80 | $this->user = User::findOrFail($identifier); 81 | } 82 | 83 | /** 84 | * When building the id_token, this entity's claims are collected 85 | */ 86 | public function getClaims(): array 87 | { 88 | return [ 89 | 'email' => $this->user->email, 90 | ]; 91 | } 92 | } 93 | ``` 94 | 95 | ### The id token is a JWT and the client should verify the signature. 96 | 97 | Here is an example to verify the signature with lcobucci/jwt 98 | 99 | ```php 100 | $config = Configuration::forSymmetricSigner( 101 | new \Lcobucci\JWT\Signer\Rsa\Sha256(), 102 | InMemory::file(base_path('oauth-public.key')) //This is the public key generate by passport. You need to share it. 103 | ); 104 | 105 | //Parse the token 106 | 107 | $token = $config->parser()->parse($idtoken); 108 | 109 | $signatureValid = $config->validator()->validate($token, new \Lcobucci\JWT\Validation\Constraint\SignedWith($config->signer(), $config->signingKey())); 110 | ``` 111 | 112 | ### Publishing the config 113 | In case you want to change the default scopes, add custom claim sets or change the repositories, you can publish the openid config using: 114 | ```sh 115 | php artisan vendor:publish --tag=openid 116 | ``` 117 | 118 | ### Using nonce 119 | 120 | When `nonce` is required, you need to pass it as a query parameter to `passport.authorizations.approve` during authorization step. 121 | 122 | Example based on default Passport's `authorize.blade.php`: 123 | ``` 124 |