Integrating custom SSO client logic into Pyro's login process
Created 4 years ago by dxtrs

Hey all

I'm trying to integrate a Laravel SSO package (jasny/sso) into Pyro's login process and have the Pyro installation act as a client. I've already created the SSO server (we'll be using our own one) and two dummy clients in Laravel to test and they work.

The integration happened in the LoginController.php. So in Pyro I was trying to override the login controller in order to add functionality upon login. I need to get the request()->get('email') and request()->get('password') but they're null when the request happens. Upon further investigation, I didn't see a POST request to /login when I inspect the traffic - instead I saw that the action on the login form was to form/handle/{key} - how does that work and how can I then inject my own logic into the login process?

I am following the example for the client and server logic from the Github page https://github.com/awnali/SSO-laravel-5.

I look forward to and appreciate anyone's help,

ryanthompson  —  4 years ago

So there are a couple ways you can tackle this:

Callbacks:

You could add an onPosting callback on the builder from a theme service provider or something. Inject the login form $builder and use something like the following (or other trigger like ready or something).

$builder->on('posting', $callback);

You can also override the form entirely by extending it with one of your own - and binding it to my.login_form or something in your service provider's $bindings property. You would need to make sure any handlers or classes surrounding the original LoginFormBuilder though have stuff manually listed like protected $handler = LoginFormHandler::class since they're no longer next to each other.

dxtrs  —  4 years ago

Thanks for the feedback Ryan - much appreciated.

I like the first option. When you say "on the builder", which builder and how do I get access to it in a service provider? Could you please provide a small snippet?

Are there login success and failure events that I could hook into? Is there a list of events somewhere I could look at?

Thanks,

ryanthompson  —  4 years ago

@dxtrs you would use the LoginFormBuilder. Here's a better code example:

<?php namespace Anomaly\NewishTheme;

use Anomaly\Streams\Platform\Addon\AddonServiceProvider;
use Anomaly\UsersModule\User\Login\LoginFormBuilder;

class NewishThemeServiceProvider extends AddonServiceProvider
{

    /**
     * Boot the addon.
     */
    public function boot(LoginFormBuilder $builder)
    {
        $builder->on(
            'posting',
            function () use ($builder) {

                if ($builder->getFormValue('email') == request('email')) {
                    dd('Yes!');
                }

                // Do stuff.. 
            }
        );
    }

}
dxtrs  —  4 years ago

Thanks Ryan!

And if I wanted to go the first route? Where I would create a custom page that POSTs to a custom route and controller - what are all the things I need to cater for/run/setup in my own login controller?

dxtrs  —  4 years ago

@ryanthompson Should I be using an AddonServiceProvider or could I use a normal ServiceProvider? When I use the latter, the 'posting' event doesn't seem to trigger upon login submit.

If I need to use the former, do I create the AddonServiceProvider with the Artisan command and have all that boilerplate?

Thanks Ryan,

ryanthompson  —  4 years ago

@dxtrs You would use your own addon service provider. Usually when you generate an addon (like a theme for your project for example) you have one available that's generated for you. Otherwise you can create your own per documentation.

dxtrs  —  4 years ago

@ryanthompson Thanks for the help Ryan. I've created an AddonServiceProvider and included it in my theme's DefaultThemeServiceProvider's $providers array. But I get the error: Type error: Too few arguments to function Anomaly\Streams\Platform\Addon\AddonServiceProvider::__construct(), 1 passed in /var/www/sbt.local/vendor/laravel/framework/src/Illuminate/Foundation/Application.php on line 635 and exactly 2 expected. Am I including it correctly?

dxtrs  —  4 years ago

@ryanthompson Thanks for the help Ryan. I've created an AddonServiceProvider and included it in my theme's DefaultThemeServiceProvider's $providers array. But I get the error: Type error: Too few arguments to function Anomaly\Streams\Platform\Addon\AddonServiceProvider::__construct(), 1 passed in /var/www/sbt.local/vendor/laravel/framework/src/Illuminate/Foundation/Application.php on line 635 and exactly 2 expected. Am I including it correctly?

dxtrs  —  4 years ago

@ryanthompson Thanks for the help Ryan. I've created an AddonServiceProvider and included it in my theme's DefaultThemeServiceProvider's $providers array. But I get the error: Type error: Too few arguments to function Anomaly\Streams\Platform\Addon\AddonServiceProvider::__construct(), 1 passed in /var/www/sbt.local/vendor/laravel/framework/src/Illuminate/Foundation/Application.php on line 635 and exactly 2 expected. Am I including it correctly?