limoncello-php / app

Quick start JSON API application
MIT License
83 stars 7 forks source link

is it capable to return user identity information via the /token authentication endpoint? #21

Closed dreamsbond closed 7 years ago

dreamsbond commented 7 years ago

other than standard attribute from oauth2 is it capable to return user identity information via the /token authentication endpoint

beside, is a /revoke endpoint available?

neomerx commented 7 years ago

Not sure if I got your question however you can get current account/user from container with

        /** @var AccountManagerInterface $manager */
        /** @var PassportAccountInterface $account */
        $manager = $container->get(AccountManagerInterface::class);
        $account = $manager->getAccount();

passport has some predifined user properties such is identity howerver it also has all user properties which could be accessed with getProperty()

        $firstName = $account->getProperty('first_name');

image

As for /revoke endpoint, there is no such 'standard' entry, however you can get TokenRepositoryInterface from container and disable a token

        /** @var TokenRepositoryInterface $repo */
        $repo = $container->get(TokenRepositoryInterface::class);
        $repo->readByCode('...', 3600);
        $repo->readByValue('...', 3600);
        $token = $repo->readByRefresh('...', 3600);
        $repo->disable($token->getIdentifier());
dreamsbond commented 7 years ago

with createBodyTokenResponse in BasePassportServer, token information returned as { "access_token": "b0e6c4c9933236439101c55e90a52154594a2f0913d5d", "token_type": "bearer", "expires_in": 3600, "refresh_token": "2796b9cace09892ee2ec56ac9dc8781e594a2f0913d98", "scope": "manage_users" }

i am thinking if it could return corresponding user identity information like: { "id_user": "1", "access_token": "b0e6c4c9933236439101c55e90a52154594a2f0913d5d", "token_type": "bearer", "expires_in": 3600, "refresh_token": "2796b9cace09892ee2ec56ac9dc8781e594a2f0913d98", "scope": "manage_users" }

for /revoke, do you mean i could create an endpoint to do so manually?

neomerx commented 7 years ago

Added feature https://github.com/limoncello-php/framework/issues/52

neomerx commented 7 years ago

It needs some extra functionality in Passport module. I've added preliminary implementation which I'm planning to finalize later today.

neomerx commented 7 years ago

While testing the code I've found a way to improve the code so finalizing takes a bit more.

neomerx commented 7 years ago

Passport settings could have a methods for adding extra properties

    /**
     * @inheritdoc
     */
    protected function getTokenCustomPropertiesProvider()
    {
        return [self::class,  'tokenCustomPropertiesProvider'];
    }

    public static function tokenCustomPropertiesProvider(ContainerInterface $container, TokenInterface $token): array
    {
        return [
            'user_id' => $token->getUserIdentifier(),
        ];
    }
neomerx commented 7 years ago

added to 0.6.17 I'm closing the question. Feel free to contact if you have any further questions.

dreamsbond commented 7 years ago

noted ^^

dreamsbond commented 7 years ago

tried and tested:

    assert($customPropsProvider === null || $this->checkPublicStaticCallable(
        $customPropsProvider,
        [ContainerInterface::class, TokenInterface::class],
        'array'
    ));

failed at the point of $customPropsProvider === null and checkPublicStaticCallable

neomerx commented 7 years ago

@dreamsbond check the method signature. This line ensures the method must have 2 typed parameters and typed return type as array.

dreamsbond commented 7 years ago

@neomerx yes, having the method typed return as array resolved the issue. but asserting $customPropsProvider === null failed

dreamsbond commented 7 years ago

@neomerx keep telling ErrorException: assert(): assert($customPropsProvider === null) failed

neomerx commented 7 years ago

@dreamsbond it does not have a check for signature AND null. It checks either for null OR for a method with the proper signature.

dreamsbond commented 7 years ago

it is quite rare to me..... after i tried composer dump-autoload

the issue gone..........