Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Airlock Auth Provider #1700

Open
simondotwhite opened this issue Jan 18, 2020 · 2 comments
Open

Airlock Auth Provider #1700

simondotwhite opened this issue Jan 18, 2020 · 2 comments

Comments

@simondotwhite
Copy link

Q A
Bug? no
New Feature? yes
Framework Laravel
Framework version 6.2
Package version 2.4.5
PHP version 7.2.15

Would possible to get an airlock (https://github.com/laravel/airlock) auth provider?

@simondotwhite
Copy link
Author

I have this so far as a provider, but I feel there is definitely a better way to do it. If you want, I can PR this in?

<?php

namespace App\Providers;

use Illuminate\Http\Request;
use Dingo\Api\Routing\Route;
use Dingo\Api\Auth\Provider\Authorization;
use Illuminate\Support\Facades\Auth as LaravelAuth;
use Laravel\Airlock\PersonalAccessToken;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;

/**
 * Class AirlockAuthProvider
 *
 * @package App\Providers
 */
class AirlockAuthProvider extends Authorization
{
    /**
     * @param Request $request
     * @param Route   $route
     * @return mixed
     */
    public function authenticate(Request $request, Route $route)
    {
        // Validate 
        $this->validateAuthorizationHeader($request);

        // Grab the token
        $token = $this->getToken($request);

        // Lookup token
        $foundToken = PersonalAccessToken::query()->where('token', $token)->first();
        if (empty($foundToken)) {
            throw new UnauthorizedHttpException('airlock', 'Unable to authenticate with invalid token.');
        }

        // Manually auth the user
        LaravelAuth::loginUsingId($foundToken->user_id);

        // Return User Model
        return $foundToken->user;
    }

    /**
     * Authorization Header Prefix
     * 
     * @return string
     */
    public function getAuthorizationMethod()
    {
        return 'bearer';
    }

    /**
     * Get the token value from the request
     * 
     * @param Request $request
     * @return string
     */
    public function getToken(Request $request)
    {
        return trim(str_replace(ucfirst($this->getAuthorizationMethod()), '', $request->headers->get('authorization')));
    }
}

@specialtactics
Copy link
Member

Hi

My advice is that Tymon JWT Auth is a superior solution (way more functionality), however if you want to add airlock, I don't really mind.

My only request is that you include unit testing in your PR, just like for current providers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants