Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Opauth with GoogleStrategy
  • Hi guys,

    I have a small problem with the Opauth.

    Everything has been installed through Compser.

    I create a Controller 'auth' exactly as described here

    I set the config as described here, and everything works perfect with Facebook, Twitter and Linkedin. My path is 'auth/login' and my callback_url is 'auth/callback'

    The problem is with GoogleStrategy, because Google returns a 'code'. With this code I have to make the exchange to a token, and after that I am able to get the needed details. The procedure is described here

    My question is, how can I redirect to the desired method : 'oauth2callback' in place of the normal callback settled in the config (see above : 'auth/callback'), if the provider is google ???
    In case of google, the author says that I have to redirect like this http://path_to_opauth/google/oauth2callback
    But my path_to_opauth is the 'auth' controller now, correct ?
    Do I have in this case to instantiate the class GoogleStrategy and to call the public method oauth2callback ? Idon't think that is the right way.

    Could someone help me with an answer of better code example for cases where it used google AND another provider, in order to understand the logic of swapping the callbacks.

    Thanks
    <b></b>
  • I use the Opauth that's integrated in the Auth package, and have no issues. So I can't help you in this case, it seams to be something specific to that package you use.
  • Hy Harro,

    do you have a code example, where you use it, even with Auth package, but with at least 2 providers (google being one of them) ?
  • There are complete examples in the docs (http://fuelphp.com/docs/packages/auth/examples/opauth.html).

    All you need to do is to add the required strategies to the composer.json file in the Fuel root, and create the config file in app/config, containing the required keys for those strategies. And the rest happens magically, providing you have Auth installed (either SimpleAuth or OrmAuth), as it works integrated with those.
  • p.s. the example code comes from one of our running apps, so I'm very confident it works... ;)
  • Hi Harro,

    I already try it, but it doesn't work at all. Where is my mistake ?

    the auth.php config file :
    return array(
        'driver'                 => 'Ormauth',
        'verify_multiple_logins' => true,
        'salt'                   => 'put_your_salt_here',
        'iterations'             => 10000,
    );

    the opauth.php config file :
    'path' => '/CMS-Sites/funding_funnel/public/index.php/login/oauth',

    'callback_url' => '/CMS-Sites/funding_funnel/public/index.php/login/callback',

    'callback_transport' => 'session',

    'Strategy' => array(

            'Facebook' => array(
                        'app_id'     => '........my app id.........',
                        'app_secret' => '....my app secret....',
                        'scope'      => array('user_birthday', 'email', ),
                 ),

            'Google' => array(
                    'client_id'     => '....my client_id.....',
                    'client_secret' => '...my client secret....',
                ),
    ...

    My login controller :

    class Controller_Login extends Controller
    {

        public function action_oauth($provider = null)
        {
            // bail out if we don't have an OAuth provider to call
            if ($provider === null)
            {
                //\Messages::error(__('login-no-provider-specified'));
                \Response::redirect_back();
            }

            // load Opauth, it will load the provider strategy and redirect to the provider
            \Auth_Opauth::forge();
        }


    }

    I don't even put a callback method yet because I receive an error when I call like this :

    http://kbx3.de/CMS-Sites/funding_funnel/public/index.php/login/oauth/facebook

    The error is :
    Auth\OpauthException [ Error ]: Opauth strategy "" is not supported


    PKGPATH/auth/classes/auth/opauth.php
    @ line 134


    // check if we have a strategy defined for this provider
    132 if ( ! \Config::get('opauth.Strategy.'.$config['provider'], false))
    133 {
    134 throw new \OpauthException('Opauth strategy "'.$config['provider'].'" is not supported');
    135 }


    Backtrace


    1. APPPATH/classes/controller/login.php
      @ line 16



    // load Opauth, it will load the provider strategy and redirect to the provider
    16 \Auth_Opauth::forge();


    What am I doing wrong ??


  • Checked on of the apps I'm working on.

    You should have 'verify_multiple_logins' set to false, unless your using multiple auth drivers. As per the docs, this setting has nothing to do with user logins.

    My opauth.php config file contains only the Strategy definitions, and a custom security_salt, but nothing else. There are no options to define paths, the Auth class will take care of that for you (it is not a config file for the composer Opauth class!).

    Our login controller is in an "admin" module, and is called "auth", but the principe remains the same. So it has the actions as defined in the example in the documentation.

    Note that there were some fixed to Auth after the 1.6.1. release, so you might want to switch to 1.7/develop for the Auth package, to benefit from these fixes (all our apps run on the latest develop).
  • If I don't set a 'path' in the opath.php config file, I receive an error like this :

    Fuel\Core\PhpErrorException [ User Error ]: Unsupported or undefined Opauth strategy - public



    /Volumes/KBX3DataHD/WebKBX3/kbx3.de/htdocs/CMS-Sites/funding_funnel/fuel/vendor/opauth/opauth/lib/Opauth/Opauth.php @ line 127



    122                
    123                $this->Strategy->callAction($this->env['params']['action']);
    124            }
    125            else
    126            {
    127                trigger_error('Unsupported or undefined Opauth strategy - '.$this->env['params']['strategy'], E_USER_ERROR);
    128            }
    129        }
    130        else
    131        {
    132            $sampleStrategy array_pop($this->env['Strategy']);
    ???
  • It looks like you have a very odd non-standard installation, and/or there is something very wrong with your rewrite commands. "public" should NEVER be part of the URI.

    If no path is given, it gets the main request URI, it gets the number of parameters passed to the controller, and from those two, it determines that path.

    So if you URI is "http://mysite/admin/auth/login/parm1, and the current controller has 1 parameter passed, "parm1", then the path will be determined to be "/admin/auth/login", and the callback path will be "/admin/auth/callback".
  • Hi Harro,

    I've made a fresh install with the last FuelPHP version.

    I've setted in the auth.php config file, the driver as being 'Ormauth'. I've runned the migrations and all the tables have been created. Then I've read the docu regarding the Ormauth usage in order to not skip anything.

    I've copied the opauth.php config file from the packages/auth/config/opauth to the app/config/opauth.php and there i've settled my salt and put all the strategies credentials. Nothing else.

    Then I have created a controller named login, which has the method from the example : action_oauth($provider)

    When I go to the browser http://kbx3.de/CMS-Sites/funding_funnel/login/oauth/facebook I receive this error :

    Auth\OpauthException [ Error ]: No providers table configured. At the moment, only SimpleAuth and OrmAuth can be auto-detected.

    Even if in the documentations says that the providers table (which was already created as I said), doesn't have to be set in the config (and as you told me, that you don't do it in your opaut.php config file), I wonder why I receive this error.

    If I set the table name in the opauth.php config file like this : 'table' => 'users_providers', it goes further but I receive immediately another error :

    Fuel\Core\PhpErrorException [ User Error ]: Unsupported or undefined Opauth strategy - login



    DOCROOT/fuel/vendor/opauth/opauth/lib/Opauth/Opauth.php @ line 120



    115                    $this->env['params']['action'] = 'request';
    116                }
    117                
    118                $this->Strategy->callAction($this->env['params']['action']);
    119            } else {
    120                trigger_error('Unsupported or undefined Opauth strategy - '.$this->env['params']['strategy'], E_USER_ERROR);
    121            }
    122        } else {
    123            $sampleStrategy array_pop($this->env['Strategy']);

    Backtrace

    1. COREPATH/bootstrap.php @ line 79


    2. DOCROOT/fuel/vendor/opauth/opauth/lib/Opauth/Opauth.php @ line 120


    3. DOCROOT/fuel/vendor/opauth/opauth/lib/Opauth/Opauth.php @ line 88
    4. ...

    What is wrong now ?

  • If no table is given, it will read the Auth config file to check what you have configured, and from that it will determine the table name to use.

    There was a bug in this mechanism in 1.6.1., so be sure to upgrade Auth to 1.7/develop.

    The issue may have to do with the fact that you have a non-standard setup where the framework is installed inside the docroot, so you have additional URI segments which may cause the count to be incorrect.

    You have two additional URI segments that are not part of the framework URI, so the count comes up two short, and it returns 'login' instead of 'facebook'.

    If you're on 1.7, you can check the auth/classes/auth/opauth.php in the package. on line 95 it will attempt to autodetect the path. And below that the callback path and the provider name. You can add some debugging to verify that my assumption is correct.

    If so, let me know and I'll try to come up with a fix for this kind of install.
  • You can also try to replace line 129 with

    $provider = explode('/', substr(implode('/', \Request::main()->uri->get_segments()), strlen($config['path']) - 1));

    and see if that fixes it?
  • I've checked, and on line 95, the path is empty. After creation it becomes an array like that :
    Array
    (
    [0] => login
    [1] => oauth
    [2] => facebook
    )

    I don't have to modify the line 129, because it returns the correct value. So $config['provider'] will be 'Facebook'.

    The problem is that it acts based on the opauth.php file from the vendor/opauth/opauth/lib/Opauth/Opauth.php not based on the Opauth file from auth/classes/auth/opauth.php.
  • How is tat possible, when I call in the oauth method inside the Login controller like this : \Auth_Opauth::forge();  So it's clear that it has to use the opauth class from the auth namespace ?

    It includes then the one from the vendor and there is a problem with detecting the correct path ?
  • If provider is ok, you can't get that error message. Because that is thrown in the Opauth class in the Auth package, just after it has been determined.

    The Auth_Opauth class is a facade for the Opauth composer package, which takes care of the integration between the Auth functionality and the Opauth features.

    Are you sure you get the error on the initial call, and not on the callback (which I assume will fail because the path is missing your first two segments)?
  • I found that not the provider is the problem, but the strategy.

    On vendor/opauth/opauth/lib/Opauth/Opauth.php, inside function run(), the first line is $this->parseUri();
    and inside the parseUri() method, it says

    if ( ! empty($this->env['params'][0]))
    {
          $this->env['params']['strategy'] = $this->env['params'][0];
    }

    but in my case, the $this->env['params'] looks like this :
    [params] => Array
    (
    [0] => login
    [1] => oauth
    [2] => facebook
    )
    so i've changed to do like this

    if ( ! empty($this->env['params']['strategy']))
    {
          $this->env['params']['strategy'] = $this->env['params']['strategy'];
    }

    now it redirects me to the provider. Let's see what comes next :)
  • It doesn't work :((

    It redirects me to the provider, i sign-up and give the ok to facebook, but after that, I receive an error :

    The requested URL /login/oauth/facebook/int_callback was not found on this server.

    from where this int_callback ??????

    and the url looks like this :

    http://kbx3.de/login/oauth/facebook/int_callback?code=AQAnOeBz8AvwFI7X13aUiB_SavdoCor5eJ4D3VFMIYSVkfDQLky5ZvM179u9INd_0dLbsuRCfXp8-5S0zzQxZ4bb4kHiu3ZlndQekn0OtW3kxj1t6zXvO80gt9W1Jxuz008-yJI8R6dIYj6pKUuEId_D-_GI2hofIvQhdxD9ngsLeMEuQ7ebQkY6J7MnwHFXo-n-FTVzQZFUgGngFSRwf0f-zVjzDvEI546p_AfeKTE_6F_7Aps8i6DhgaQ87mG2qSt-4AF4RJGh7Nb6SltaqmTrkaLx2qaceDgcMshabr6cjO_d-zQZSHOOMWrVNt8Pzyk#_=_

    So the path is truncated to kbx3.de/login/oauth/facebook/int_callback

    not even

    kbx3.de/CMS-Sites/funding_funnel/login/oauth/facebook/int_callback

    what is going on with this package ??


  • It's because of what I wrote earlier.

    Because you have two additional URI segments, everything is shifted by two segments. And that is why $this->env['params'] in Opauth contains 3 entries instead of only one, the strategy.

    This needs to be fixed in the Opauth Auth driver because it's a Fuel issue. I need to setup a similar environment so I can test and fix it. I'll try to find the time in the next few days.
  • Thanks Harro, it will be great if you can manage this 'bug'.

    Now it works with Facebook, Twitter and Linkedin (ONLY if I set the 'path' and the 'callback_url' in the opauth.php config file.

    The problem still remains with Google, because it asks for a different callback. I've tried to insert a 'redirect_uri' in the config, but without success. I will check forward but if you already have a project with google and another provider (fb or twitter), please give me a hint on how to solve it...or it doesn't work because of my segments problem ??

    Now I have another question regarding the Auth and Ormauth. I've installed also Warden, because I want to use some functions like (send_confirmation_instructions, forgot_password, etc...)

    I've decide to use Ormauth as driver, and I want to know how can I extend the Auth_User model. I've tried to create a user.php file in app/classes/model, like this :

    use \Orm\Model;

    class Model_User extends \Auth\Model\Auth_User
    {
        protected static $_properties = array(
           
        );

    }

    but if I say : $user = Model_User::find(4);    //i have a record in users table with id 4

    it return me an error like this :
    Fuel\Core\Database_Exception [ Error ]: SQLSTATE[42S22]: Column not
    found: 1054 Unknown column 't0.remember_token' in 'field list'.....

    It seems that it looks for the user model of Warden, but in my controller i don't use the warden namespace. So how can I extend the Auth_User model and to make use of it, even if I have Warden installed ?

  • I don't know, I've never used Warden.

    I use the registration and password recovery methods are shown in the example code, it would not be difficult to add email verification to that, in our application we also do that.
  • Ok, thanks. It will be great if you would have time to analyze the other problem with Opauth. Looking forward for an answer / solution when you have it.
  • Just to let you know I've got a setup working, and I can recreate your error. I'll try to push a fix for it this weekend.
  • Super. I wait for your solution. Thanks
  • Fix was pushed yesterday.

    As it also contains fixes in the framework (https://github.com/fuel/core/commit/a1ac30d02ff716375ee9093d397c595945a7b152), you should upgrade to 1.7/develop to be able to use this fix now, or wait for the next official release (which isn't planned yet).
  • Hi,

    I've made the upgrade to 1.7/develop.

    I'm sorry to tell you, that I still have the same problem.

    If inside the opauth.php config file, i let the path like this '/', it doesn't work. If I change it back to '/CMS-Sites/funding_funnel/account/oauth/', it works for Linkedin, Facebook and Twitter, but not for googleas I wrote from the beginning of our discussion.

    If I let the path like this '/', I receive this error :

    Fuel\Core\PhpErrorException [ User Error ]: Unsupported or undefined Opauth strategy - CMS-Sites



    DOCROOT/fuel/vendor/opauth/opauth/lib/Opauth/Opauth.php @ line 120



    115                    $this->env['params']['action'] = 'request';
    116                }
    117                
    118                $this->Strategy->callAction($this->env['params']['action']);
    119            } else {
    120                trigger_error('Unsupported or undefined Opauth strategy - '.$this->env['params']['strategy'], E_USER_ERROR);
    121            }
    122        } else {
    123            $sampleStrategy array_pop($this->env['Strategy']);
    124            trigger_error('No strategy is requested. Try going to '.$this->env['complete_path'].$sampleStrategy['strategy_url_name'].' to authenticate with '.$sampleStrategy['strategy_name'], E_USER_NOTICE);
    125        }

    Backtrace



    1. COREPATH/bootstrap.php @ line 79


    2. DOCROOT/fuel/vendor/opauth/opauth/lib/Opauth/Opauth.php @ line 120


    3. DOCROOT/fuel/vendor/opauth/opauth/lib/Opauth/Opauth.php @ line 88


    4. APPPATH/classes/controller/account.php @ line 615


    5. COREPATH/classes/request.php @ line 440


    6. DOCROOT/index.php @ line 49

    So It didn't help me :(
  • You should NOT define any paths anywhere, it should work automatically!

    If it works for the other strategies, but not for Google, then Opauth has a driver bug, which they have to fix.

    Please verify if you have the latest version of the driver, and if so, report the issue here: https://github.com/opauth/google/issues

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion