Laravel 5.1 + lucadegasperi/oauth2-server-laravel

I am attempting to setup an oauth2 server in Laravel 5.1 but I am new and I am not quite sure what needs to happen during a specific step of the configuration.

I am following the documentation for lucadegasperi's package but I am not sure what to do here:
Implementing an Authorization Server with the Password Grant

I completed the initial installation steps and proceeded to setup the grants. I used the example in my oauth2 config file:
'grant_types' => [
    'password' => [
        'class' => '\League\OAuth2\Server\Grant\PasswordGrant',
        'callback' => '\App\PasswordVerifier@verify',
        'access_token_ttl' => 3600
    ]
]

Open in new window


I am stuck on step two, where you setup the callback (Create a class with a verify method where you check if the provided user is a valid one).

The example shows:
use Illuminate\Support\Facades\Auth;

class PasswordGrantVerifier
{
  public function verify($username, $password)
  {
      $credentials = [
        'email'    => $username,
        'password' => $password,
      ];

      if (Auth::once($credentials)) {
          return Auth::user()->id;
      }

      return false;
  }
}

Open in new window


But I am not sure where to place that code, can someone help me complete this step?
ITNCAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Why not use Laravel's built-in authentication?  Unless this is required for national security reasons, OAuth usually creates more troubles than it solves!  The built-in almost always works right out of the box.
http://laravel.com/docs/5.1/authentication

There are other ways, of course, including social media authentication, etc.  All of them are easier to get right than OAuth.  A Google search for "laravel 5.1 client authentication" will turn up several good links.
0
ITNCAuthor Commented:
I am currently using the built-in authentication but I wanted another layer of protection so I decided to setup the oauth2 server as that extra layer.

I was able to get this working but it's kind of a hack of that package.

I first replaced my grant_type code with this:
'grant_types' => [
        'password' => [
            'class' => '\League\OAuth2\Server\Grant\PasswordGrant',
            'callback' => function($username, $password)
            {
                if( Auth::validate(['email' => $username, 'password' => $password]))
                {
                    $user = User::where('email',$username)->first();
                    return $user->id;
                }
                else
                {
                    return false;
                }
            },
            'access_token_ttl' => 3600
        ]
    ]

Open in new window


I then modified the service provider code and changed the callback function to this:

if (array_key_exists('callback', $grantParams)) {
                    $grant->setVerifyCredentialsCallback($grantParams['callback']);
                }

Open in new window


And finally I used this user's middleware code :
Middleware Code

After that oauth is working perfect. This may not be the best fix but the authentication is working with oauth2 + the built-in authentication.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ITNCAuthor Commented:
I found several tutorials and videos on this subject and pieced together a working solution based on those materials.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Laravel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.