Code Monkey home page Code Monkey logo

openid-connect's Introduction

OpenID Connect

OpenID Connect support to the PHP League's OAuth2 Server.

Compatible with Laravel Passport!

Requirements

Installation

composer require ronvanderheijden/openid-connect

Keys

To sign and encrypt the tokens, we need a private and a public key.

mkdir -m 700 -p tmp

openssl genrsa -out tmp/private.key 2048
openssl rsa -in tmp/private.key -pubout -out tmp/public.key

chmod 600 tmp/private.key
chmod 644 tmp/public.key

Example

I recommend to read this first.

To enable OpenID Connect, follow these simple steps

$privateKeyPath = 'tmp/private.key';

// create the response_type
$responseType = new IdTokenResponse(
    new IdentityRepository(),
    new ClaimExtractor(),
    Configuration::forSymmetricSigner(
        new Sha256(),
        InMemory::file($privateKeyPath),
    ),
);

$server = new \League\OAuth2\Server\AuthorizationServer(
    $clientRepository,
    $accessTokenRepository,
    $scopeRepository,
    $privateKeyPath,
    $encryptionKey,
    // add the response_type
    $responseType,
);

Now when calling the /authorize endpoint, provide the openid scope to get an id_token.
Provide more scopes (e.g. openid profile email) to receive additional claims in the id_token.

For a complete implementation, visit the OAuth2 Server example.

Laravel Passport

You can use this package with Laravel Passport in 2 simple steps.

1.) add the service provider

# config/app.php
'providers' => [
    /*
     * Package Service Providers...
     */
    OpenIDConnect\Laravel\PassportServiceProvider::class,
],

2.) create an entity

Create an entity class in app/Entities/ named IdentityEntity or UserEntity. This entity is used to collect the claims.

# app/Entities/IdentityEntity.php
namespace App\Entities;

use League\OAuth2\Server\Entities\Traits\EntityTrait;
use OpenIDConnect\Claims\Traits\WithClaims;
use OpenIDConnect\Interfaces\IdentityEntityInterface;

class IdentityEntity implements IdentityEntityInterface
{
    use EntityTrait;
    use WithClaims;

    /**
     * The user to collect the additional information for
     */
    protected User $user;

    /**
     * The identity repository creates this entity and provides the user id
     * @param mixed $identifier
     */
    public function setIdentifier($identifier): void
    {
        $this->identifier = $identifier;
        $this->user = User::findOrFail($identifier);
    }

    /**
     * When building the id_token, this entity's claims are collected
     */
    public function getClaims(): array
    {
        return [
            'email' => $this->user->email,
        ];
    }
}

Publishing the config

In case you want to change the default scopes, add custom claim sets or change the repositories, you can publish the openid config using:

php artisan vendor:publish --tag=openid

Discovery and JWKS

The Laravel Passport integration also provides:

  • a discovery endpoint at /.well-known/openid-configuration.
  • a JWKS endpoint at /oauth/jwks.

Those 2 endpoints are automatically added to the Laravel routes and can be disabled from the config (using the openid.routes.discovery and openid.routes.jwks keys).

Laravel Passport does not provide a userinfo endpoint by default. If you provide one, you can add it to the discovery document by naming the route openid.userinfo.

Route::get('/oauth/userinfo', 'YourController@userinfo')->middleware('xxx')->name('openid.userinfo');

Support

Found a bug? Got a feature request? Create an issue.

License

OpenID Connect is open source and licensed under the MIT licence.

openid-connect's People

Contributors

ben-power avatar dellanx avatar moufmouf avatar ronvanderheijden avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

openid-connect's Issues

Feature Request: Configuration option to disable tokens_can registration

I have a large amount of scopes that I want to register, which I serialize into a json file.
This isn't something I want to load and process in my AuthServiceProvider.

To allow for this implementation, I need to disable the tokens_can registration that this package is doing.

I think I might just make a modification to the service provider, if the tokens_can array isn't defined, don't execute it in the this package's service provider.

Implict flow not supported

Issue previously raised in thephpleague/oauth2-server#1374

When I use the authorisation code grant, this works as expected.

However, when I try to do an implict flow by setting response_type=token id_token or response_type=id_token, the server always rejects the request because the following check does not match the request:
https://github.com/thephpleague/oauth2-server/blob/ab7714d073844497fd222d5d0a217629089936bc/src/Grant/ImplicitGrant.php#L105-L109

Are there any recommended ways to bypass this issue?

I am using Laravel Passport.

Need help for the maintenance of this project?

Hey @ronvanderheijden ,

Your package is exactly what I needed and I'm using (a fork of) it in production now.
So first of all, thanks for the hard work.

I've noticed you are not maintaining it anymore (I'm not sure you even noticed the 4 PRs I opened :) )

In the case you are seeing this message, do you need help in maintaining it? I'm considering forking it but I would really like to avoid that as much as possible as fragmenting the packages is never a good idea. If you need some help to review the PRs, I'm volunteering to help maintain the package! Let me know!

HTTP scheme not available in IdTokenResponse

The HTTPS-schema is hard coded here:

->issuedBy('https://' . $_SERVER['HTTP_HOST'])

It would be very useful to use HTTP-scehma during local development and testing. Is it possibly to make it configurable?

I've been using it together with Laravel Passport, and since the Issuer is always set to "https://localhost:8000" I get an error in the client that's trying to validate the Id Token, since it tries to match it with the client with URL "http://localhost:8000".

Error in signature of methods __construct and getBuilder from OpenIDConnect\IdTokenResponse.php

Both methods's last arguments contain a trailing comma in the end that raises an error.

public function __construct(
        IdentityRepositoryInterface $identityRepository,
        ClaimExtractor $claimExtractor,
        Configuration $config,
 )

and

protected function getBuilder(
        AccessTokenEntityInterface $accessToken,
        IdentityEntityInterface $userEntity,
    )

After removing the commas, the errors are gone.

Public and Private Keys with Laravel Passport

In this section you describe how to generate the keys for later encryption of the ID Token. Can I simply use the ones generated by the laravel artisan passport:keys command? If so, where do I store them for the openid provider to pick it up? Right now they don't appear in the token being generated.
image

But if I add them , the signature becomes valid.

image

key is invalid excepion

when OAuth private key be setting in laravel/.env, not laravel/storage/oauth-private.key

requesting oauth/authorize will occur key is invalid excepion

Update composer packages

The composer packages are outdated, we should update and test.

composer outdated --direct

guzzlehttp/psr7               1.9.1 -> 2.6.2
lcobucci/jwt                  4.3.0 -> 5.3.0
overtrue/phplint              2.4.1 -> 9.1.2 
phpunit/phpunit               9.6.19 -> 11.1.3
slevomat/coding-standard      6.4.1 -> 8.15.0
symplify/easy-coding-standard 9.4.70 -> 12.1.14

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.