Skip to content

[Proposal] Own AbstractProviders with Custom Configuration #26

@ghost

Description

So I quickly put something together which makes it possible to have a fluent interface for the config which at the moment is somewhat clunky in my opinion - https://github.com/SocialiteProviders/StackExchange/issues/2#issuecomment-186761408

These changes would require the following changes to all Providers:

  • Provider.php has to extend SocialiteProviders\Manager\OAuth1\AbstractProvider instead of the Laravel\Socialite\One\AbstractProvider
  • Provider.php has to extend SocialiteProviders\Manager\OAuth2\AbstractProvider instead of the Laravel\Socialite\Two\AbstractProvider

Additionally we probably also should introduce our own User classes for both OAuth1 and OAuth2 to gain finer control over what is happening and the response data, which you already did for OAuth2.


OAuth2/AbstractProvider.php

<?php
namespace SocialiteProviders\Manager\OAuth2;

use GuzzleHttp\ClientInterface;
use Laravel\Socialite\Two\InvalidStateException;
use SocialiteProviders\Manager\SocialiteWasCalled;

abstract class AbstractProvider extends \Laravel\Socialite\Two\AbstractProvider
{
    // all the code of the current AbstractProvider in the OAuth2 folder...

    public function config(array $additionalConfig) {
        $identifier = static::PROVIDER_IDENTIFIER;

        $this->config = array_merge([
            'client_id' => env("{$identifier}_KEY"),
            'client_secret' => env("{$identifier}_SECRET"),
            'redirect' => env("{$identifier}_REDIRECT_URI"),
        ], $additionalConfig);

        return $this;
    }

    protected function getFromConfig($key)
    {
        $identifier = strtolower(static::PROVIDER_IDENTIFIER);

        return app()['config']["services.{$identifier}"][$key];
    }
}

StackExchange/Provider.php

<?php

namespace SocialiteProviders\StackExchange;

use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;
use Laravel\Socialite\Two\ProviderInterface;

/**
 * https://api.stackexchange.com/docs/authentication
 * Class Provider.
 */
class Provider extends AbstractProvider implements ProviderInterface
{
    const PROVIDER_IDENTIFIER = 'STACKEXCHANGE';

    // all the code we have in the Provider.php file of each package at the moment...
}

routes.php

Route::group(['middleware' => ['web']], function () {
    \Route::get('/', function() {
        return \Socialite::with('stackexchange')->config([
            'key' => 'yourkeyfortheservice', 'site' => 'stackoverflow'
        ])->redirect();
    });

    \Route::get('/callback', function () {
        $user = \Socialite::with('stackexchange')->user();

        dd($user);
    });
});

If this seems like it goes into the right direction I will finish it up.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions