Git Product home page Git Product logo

Comments (6)

stevenmaguire avatar stevenmaguire commented on August 20, 2024

You will need to implement each of those methods for this class to function properly. Otherwise, you will need to find one of the existing provider specific implementations to use as your base class then override the single method you are interested in at this time.

from oauth1-client.

 avatar commented on August 20, 2024

Well the thing is I need to override the getTokenCredentials-method completely and my Server class should replace the League\OAuth1\Client\Server\Server.

My Providers would then extend MyNamespace\OAuth1\Server instead of League\OAuth1\Client\Server\Server.

So how do I replace the League Server with my own Server class to prevent this stupid solution I have at the moment?

from oauth1-client.

stevenmaguire avatar stevenmaguire commented on August 20, 2024

Can you describe your use case here a bit more? How many provider specific implementations are you planning to build? Do they share some logic and behavior? If so, what?

from oauth1-client.

 avatar commented on August 20, 2024

This would be used for all the OAuth1 Providers at https://github.com/SocialiteProviders. All OAuth1 Providers look pretty much like this.

<?php

namespace SocialiteProviders\Twitter;

use League\OAuth1\Client\Credentials\CredentialsException;
use SocialiteProviders\Manager\OAuth1\User;

class Provider extends AbstractProvider
{
    /**
     * Unique Provider Identifier.
     */
    const PROVIDER_IDENTIFIER = 'TWITTER';

    /**
     * {@inheritdoc}
     */
    protected function mapUserToObject(array $user)
    {
        return (new User())->setRaw($user['extra'])->map(
            [
                'id' => $user['id'],
                'nickname' => $user['nickname'],
                'name' => $user['name'],
                'email' => $user['email'],
                'avatar' => $user['avatar'],
            ]
        );
    }
}
<?php

namespace SocialiteProviders\Manager\OAuth1;

use GuzzleHttp\ClientInterface;
use Laravel\Socialite\One\InvalidStateException;
use SocialiteProviders\Manager\ConfigTrait;
use SocialiteProviders\Manager\SocialiteWasCalled;

abstract class AbstractProvider extends \Laravel\Socialite\One\AbstractProvider
{
    protected $credentialsResponseBody;

    public function user()
    {
        if (!$this->hasNecessaryVerifier()) {
            throw new \InvalidArgumentException('Invalid request. Missing OAuth verifier.');
        }

        $token = $this->getToken();
        $tokenCredentials = $token['tokenCredentials'];

        $user = $this->mapUserToObject((array) $this->server->getUserDetails($tokenCredentials));
        $user->setToken($tokenCredentials->getIdentifier(), $tokenCredentials->getSecret());

        if ($user instanceof User) {
            parse_str($token['credentialsResponseBody'], $credentialsResponseBody);

            if (!$credentialsResponseBody || !is_array($credentialsResponseBody)) {
                throw new CredentialsException('Unable to parse token credentials response.');
            }

            $user->setAccessTokenResponseBody($credentialsResponseBody);
        }

        return $user;
    }
}
<?php

namespace SocialiteProviders\Manager\OAuth1;

use GuzzleHttp\Exception\BadResponseException;
use League\OAuth1\Client\Credentials\TemporaryCredentials;
use League\OAuth1\Client\Credentials\TokenCredentials;

class Server extends \League\OAuth1\Client\Server\Server
{
    public function getTokenCredentials(TemporaryCredentials $temporaryCredentials, $temporaryIdentifier, $verifier)
    {
        if ($temporaryIdentifier !== $temporaryCredentials->getIdentifier()) {
            throw new \InvalidArgumentException(
                'Temporary identifier passed back by server does not match that of stored temporary credentials.
                Potential man-in-the-middle.'
            );
        }

        $uri = $this->urlTokenCredentials();
        $bodyParameters = array('oauth_verifier' => $verifier);

        $client = $this->createHttpClient();

        $headers = $this->getHeaders($temporaryCredentials, 'POST', $uri, $bodyParameters);

        try {
            $response = $client->post($uri, $headers, $bodyParameters)->send();
        } catch (BadResponseException $e) {
            return $this->handleTokenCredentialsBadResponse($e);
        }

        return [
            'tokenCredentials' => $this->createTokenCredentials($response->getBody()),
            'credentialsResponseBody' => $response->getBody()
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function urlTemporaryCredentials()
    {
        //
    }

    /**
     * {@inheritdoc}
     */
    public function urlAuthorization()
    {
        //
    }

    /**
     * {@inheritdoc}
     */
    public function urlTokenCredentials()
    {
        //
    }

    /**
     * {@inheritdoc}
     */
    public function urlUserDetails()
    {
        //
    }

    /**
     * {@inheritdoc}
     */
    public function userDetails($data, TokenCredentials $tokenCredentials)
    {
        //
    }

    /**
     * {@inheritdoc}
     */
    public function userUid($data, TokenCredentials $tokenCredentials)
    {
        //
    }

    /**
     * {@inheritdoc}
     */
    public function userEmail($data, TokenCredentials $tokenCredentials)
    {
        //
    }

    /**
     * {@inheritdoc}
     */
    public function userScreenName($data, TokenCredentials $tokenCredentials)
    {
        //
    }
}

I need the custom Server class because I need to grab the response received after requesting the access token.

P. S.: All of the above code is working at the moment but I find this solution nasty because of all the empty methods.

from oauth1-client.

bencorlett avatar bencorlett commented on August 20, 2024

Hi @DraperStudio, I am trying to understand the issue here.

As I understand it, your problem is that you have a bunch of empty methods in your implementations?

If so, there's really not much we can do about that in v1 as this would seriously break BC. In V2 this has been refactored and it might just be worth waiting out for this.

from oauth1-client.

bencorlett avatar bencorlett commented on August 20, 2024

As far as replacing the League's server class, I'm not sure if I'm missing what you're asking here as well. I don't believe anything else would be required other than you extending that class in your own custom implementation (for the getTokenCredentials() method you described).

Going to close off the issue now, feel free to reopen if you need further clarification or I've missed what you're asking :)

from oauth1-client.

Related Issues (20)

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.