Git Product home page Git Product logo

passport-custom-request-grant's Introduction

Install

Install with composer... composer require mikemclin/passport-custom-request-grant

Versions

  • Laravel 5.4 - Use version ^1.0
  • Laravel 5.3 - Use version ^0.1

Setup

  • Add MikeMcLin\Passport\CustomRequestGrantProvider to your list of providers after Laravel\Passport\PassportServiceProvider.
  • Add getUserEntityByRequest($request) method to your User model (or whatever model you have configured to work with Passport).
    • The method should accept an Illuminate\Http\Request object.
    • You should authorize and retrieve user based on this request
    • If you find that the request met your requirement, return the User model.
    • If the request did not satisfy your requirement, return null

How to use

  • Make a POST request to https://your-site.com/oauth/token, just like you would a Password or Refresh grant.
  • The POST body should contain grant_type = custom_request.
  • The request will get routed to your User::getUserEntityByRequest() function, where you will determine if access should be granted or not.
  • An access_token and refresh_token will be returned if successful.

Example

Here is what a User::byPassportCustomRequest() method might look like...

/**
 * Verify and retrieve user by custom token request.
 *
 * @param \Illuminate\Http\Request $request
 *
 * @return \Illuminate\Database\Eloquent\Model|null
 * @throws \League\OAuth2\Server\Exception\OAuthServerException
 */
public function byPassportCustomRequest(Request $request)
{
    try {
        if ($request->get('sso_token')) {
            return $this->bySsoToken($request->get('sso_token'));
        }
    } catch (\Exception $e) {
        throw OAuthServerException::accessDenied($e->getMessage());
    }
    return null;
}

In this example, the app is able to authenticate a user based on an sso_token property from a submitted JSON payload. The bySsoToken is this app's way of doing that. It will return null or a user object. It also might throw exceptions explaining why the token is invalid. The byPassportCustomRequest catches any of those exceptions and converts them to appropriate OAuth exception type. If an ssoToken is not present on the request payload, then we return null which returns an invalid_credentials error response:

{
  "error": "invalid_credentials",
  "message": "The user credentials were incorrect."
}

passport-custom-request-grant's People

Contributors

mikemclin avatar se1exin avatar

Watchers

 avatar  avatar

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.