FriendsOfSymfony / FOSOAuthServerBundle

A server side OAuth2 Bundle for Symfony
1.09k stars 451 forks source link

[GrantType][Password] Add element in return json #307

Open Ciloe opened 9 years ago

Ciloe commented 9 years ago

Hi,

I implement the Fos Bundle, but I have a question. I want to implement an api_key user connexion. I have create a service and a new GrantExtension like in this url

But in my WebService, when I generate a token with the grant_type=password, I want with the acess_token the user api_key. How can I do that ? It is possible ? It is implemented ?

Thank you for your response.

Ciloe commented 9 years ago

I have see in the function

    public function createAccessToken(IOAuth2Client $client, $data, $scope = null, $access_token_lifetime = null, $issue_refresh_token = true, $refresh_token_lifetime = null)
    {
        $token = array(
            "access_token" => $this->genAccessToken(),
            "expires_in" => ($access_token_lifetime ?: $this->getVariable(self::CONFIG_ACCESS_LIFETIME)),
            "token_type" => $this->getVariable(self::CONFIG_TOKEN_TYPE),
            "scope" => $scope,
        );

        $this->storage->createAccessToken(
            $token["access_token"],
            $client,
            $data,
            time() + ($access_token_lifetime ?: $this->getVariable(self::CONFIG_ACCESS_LIFETIME)),
            $scope
        );

        // Issue a refresh token also, if we support them
        if ($this->storage instanceof IOAuth2RefreshTokens && $issue_refresh_token === true) {
            $token["refresh_token"] = $this->genAccessToken();
            $this->storage->createRefreshToken(
                $token["refresh_token"],
                $client,
                $data,
                time() + ($refresh_token_lifetime ?: $this->getVariable(self::CONFIG_REFRESH_LIFETIME)),
                $scope
            );

            // If we've granted a new refresh token, expire the old one
            if (null !== $this->oldRefreshToken) {
                $this->storage->unsetRefreshToken($this->oldRefreshToken);
                $this->oldRefreshToken = null;
            }
        }

        if ($this->storage instanceof IOAuth2GrantCode) {
            if (null !== $this->usedAuthCode) {
                $this->storage->markAuthCodeAsUsed($this->usedAuthCode->getToken());
                $this->usedAuthCode = null;
            }
        }

        return $token;
    }

It is possible to add an external parameter with a

token += array(EXTERNAL_PARAM);

If it's possible I can return an array with the api_key ?

For example in the config.yml :

fos_oauth_server:
    ....
    service:
        options:
            external_access_value:
                 key: api_key
                 value: Acme\Bundle\UserBundle\Entity\User:api_key_attribute
Spomky commented 9 years ago

A better way could be to use dispatcher to allow listeners to add custom parameters.

johnpancoast commented 9 years ago

Hi @Spomky, any update on this? We have similar needs. I might look into this is if time permits and you haven't done anything yet.

gtrias commented 8 years ago

+1 I would like to modify the AccessToken response as well. Any update about this? Any workaround? I think this bundle has a lack in event dispatchers to allow changing some behaviours.

Thanks

georgesamy commented 8 years ago

Same issue here

DariuszLuber commented 6 years ago

Any solution? I'm also want to extend return data.