Skip to content

How to implement JWT custom payload? A full example? #890

@lucavicidomini

Description

@lucavicidomini

Hi, I'm struggling with the new feature in release 1.10. I'm using the library in a Laravel project.

The register function creates the server and returns it. Without the commented line (the one beginning with OOOPS) the server successfully returns a JWT token, but whenever I un-comment that line the server answers with "authorization code grant type not supported". I thought about a storage misconfiguration, but I couldn't fix it. I'm using MySQL as storage, but Jwt token, as other developers already have pointed out, is too big to be stored in it.

Any advice is really appreciated.

class MyJwtToken extends \OAuth2\ResponseType\JwtAccessToken { }

class OAuth2ServiceProvider extends ServiceProvider {

    public function register()
    {
        $this->app->singleton( 'oauth2' , function ( $app ) {
            $storage = new Pdo( App::make( 'db' )->getPdo() );
            $server = new \Oauth2\Server( $storage );

            if (setting('jwt_enable') && setting('key_private') && setting('key_public')) {
                $publicKey  = file_get_contents( setting('key_public' ) );
                $privateKey = file_get_contents( setting('key_private' ) );
                $keyStorage = new \OAuth2\Storage\Memory([
                    'keys' => [
                        'public_key'  => $publicKey,
                        'private_key' => $privateKey,
                    ],
                ]);

                $server->addStorage($keyStorage, 'access_token');
                $server->addStorage($keyStorage, 'public_key');
                $server->setConfig('use_jwt_access_tokens', true);

                $jwtTokenResponseType = new App\RtpaJwt($storage);
                // OOOPS $server->addResponseType($jwtTokenResponseType, 'token');
            }

            $server->addGrantType( new ClientCredentials( $storage ) );
            $server->addGrantType( new UserCredentials( $storage ) );
            $server->addGrantType( new RefreshToken( $storage ), ['always_issue_new_refresh_token' => true] );

            return $server;
        });
    }

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions