Giter Site home page Giter Site logo

Comments (14)

adamwathan avatar adamwathan commented on June 9, 2024

You can register a custom provider like so:

// MyCustomProvider.php

use AdamWathan\EloquentOAuth\Providers\ProviderInterface;

class MyCustomProvider implements ProviderInterface
{
    // ...
}


// In your own service provider, or bootstrap/global.php, or routes.php, or wherever you want to put your bootstrapping code...
$provider = new MyCustomProvider();
OAuth::registerProvider('my-custom-provider', $provider);

The ProviderInterface is very simple (just two methods) but there's a lot of work that goes into implementing that completely from scratch. If you're trying to create a custom provider for an external service that follows the OAuth2 spec reasonably closely, using AdamWathan\EloquentOAuth\Providers\Provider as a base class will give you a lot of stuff for free. Just check out one of the provided provider classes that implement that base class to see how it can be used.

from eloquent-oauth.

syphernl avatar syphernl commented on June 9, 2024

Awesome, thanks! Editing the core-files wasn't a great success with updates ;-)

from eloquent-oauth.

adamwathan avatar adamwathan commented on June 9, 2024

No prob man!

from eloquent-oauth.

syphernl avatar syphernl commented on June 9, 2024

Hrm doesn't look like its working but I'll probably have to fiddle some more.

from eloquent-oauth.

adamwathan avatar adamwathan commented on June 9, 2024

Here's a minimal working example that I just stuck in my routes file. Working in the sense that it proves the provider gets registered and can be used at least. Hitting http://myproject.dev/foobar/authorize will redirect to Google.

class CustomProvider implements AdamWathan\EloquentOAuth\Providers\ProviderInterface {
    public function authorizeUrl($state)
    {
        return 'http://www.google.com';
    }
    public function getUserDetails()
    {
    }
}

OAuth::registerProvider('foobar', new CustomProvider);

Route::get('{provider}/authorize', function($provider) {
    return OAuth::authorize($provider);
});

from eloquent-oauth.

syphernl avatar syphernl commented on June 9, 2024

Ok, that does seem to work (better) than what I had.
But what about the access token / userdataurl functionality? As soon as someone hits the authorize link the oAuth provider claims there is an issue with the client_id parameter

from eloquent-oauth.

adamwathan avatar adamwathan commented on June 9, 2024

I think the best thing to do would be look at something like the GitHub provider as an example implementation and go from there. What provider are you trying to implement? If it's not a pretty standard OAuth provider, than the base provider class isn't going to do you any good and you are gonna have to write a lot of code from scratch, nothing that can really be done about it unfortunately.

The OAuthManager class just cares that the provider implements that interface and gives it the data it needs when it asks for it; it isn't concerned at all with how it gets that data, so the bulk of the work for communicating with the external service lives in the provider.

from eloquent-oauth.

syphernl avatar syphernl commented on June 9, 2024

I am using a custom oAuth server (using oauth2-server-laravel. This uses league/oauth2-server which - AFAIK - follows the standards.

from eloquent-oauth.

adamwathan avatar adamwathan commented on June 9, 2024

Is it possible to share your code that you've written for your custom provider?

from eloquent-oauth.

syphernl avatar syphernl commented on June 9, 2024

The first approach I had actually cloned the one for Facebook, replaced the URL's and it was nearly working. I did have to modify the EloquentOAuthServiceProvider.php in order to get it to work (include in the search list)

Second and third approach were based on your codes. I do not have the code right here, if its still desired I can add that tomorrow morning.

from eloquent-oauth.

adamwathan avatar adamwathan commented on June 9, 2024

If you are extending the base provider then your CustomProvider class of course needs to take a bunch of stuff in the constructor and be bootstrapped the same way the Facebook one was for example.

class CustomProvider extends AdamWathan\EloquentOAuth\Providers\Provider {
    // implement all the abstract methods just like the Facebook one does
}

$config = $this->app['config']['eloquent-oauth::providers.foobar'];
$provider = new CustomProvider($config, new Guzzle\Http\Client, App::make('request'));

OAuth::registerProvider('foobar', $provider);

Route::get('{provider}/authorize', function($provider) {
    return OAuth::authorize($provider);
});

And then of course change the URLs you need to from the Facebook example, rewrite anything that parses the response in a different format if it comes back differently, etc.

The initial example I gave was just to show how you register the providers. How you actually build a provider and what dependencies the provider has depends on how the provider works of course, which might usually mean it needs Guzzle (and you write the code to make Guzzle do the right requests), it needs the current Request object to get any parameters that came back from the provider itself, etc. etc. etc.

from eloquent-oauth.

syphernl avatar syphernl commented on June 9, 2024

Close: the $this->app may not be used in start/global.php or the routes but Config::Get can. However, without the change to EloquentOAuthServiceProvider I am still getting an Undefined index: foobar

from eloquent-oauth.

syphernl avatar syphernl commented on June 9, 2024

It needs an additional check whether the key is set, since it doesn't exist during initialization. PR on its way.

from eloquent-oauth.

syphernl avatar syphernl commented on June 9, 2024

#13

from eloquent-oauth.

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.