Laravel Passport custom hasher to create token - laravel

I have used a SHA1 hash for a password like this:
https://arjunphp.com/laravel-5-sha1-encryption-instead-of-bcrypt/
Now I am using passport API to create a token, but it is not allowing me to create a token as the hasher is changed now.
Symfony\Component\Debug\Exception\FatalThrowableError: Argument 1 passed to Laravel\Passport\Bridge\UserRepository::__construct() must be an instance of Illuminate\Hashing\HashManager, instance of App\Libraries\ShaHash\SHAHasher given in file C:\xampp1\htdocs\coursekartv2\vendor\laravel\passport\src\Bridge\UserRepository.php on line 26
How can I override UserRepository to use SHAHasher instead of HashManager? Or any other help to overcome this issue.

we came across the same problem as the one described here, I am working on a Laravel API that needs to handle Passport and also has it own custom Hasher (SHA1). like the one in here
Our fix for this was not only make our class ShaHasher extends HashManager
like so:
class ShaHasher extends HashManager implements Hasher { ..... }
you also need to make sure that your provider for this Hasher get and instance of the $app container in the constructor like so:
<?php
namespace App\Providers;
use Illuminate\Hashing\HashServiceProvider;
use Psytech\ShaHasher;
class ShaHashServiceProvider extends HashServiceProvider {
public function register()
{
$this->app->singleton('hash', function () {
return new ShaHasher($this->app);
});
}
}
Hope this helps someone!

Found the solution:
I was using my custom hasher (SHAHasher) instead of Passport hashManager, now extend HashManager of passport instead of complete new hasher (SHAHasher). So now even i am sending SHAHasher (custom) it is accepting as my SHAHasher extends hashManager.
Extend hashManager inside custom hasher library.

You can also create a custom hasher and extend the existing HashManager with your custom hashing using the extend method on the HashManager
In the AuthServiceProvider:
$this->app->get('hash')->extend('custom_hasher', function(){
return new CustomHasher();
});
In the hash.php config file you can then change the hash driver:
'driver' => 'custom_hasher',

Related

Laravel sanctum custom model

I'm using Laravel sanctum to authenticate my API, and I wanted to customize personal access token model so I did the following:
I created new model named PersonalAccessToken in App namespace.
I override model used in sanctum to be my new model by adding this line to my AppServiceProvider boot() method.
Sanctum::usePersonalAccessTokenModel(PersonalAccessToken::class);
but when I create a token it works fine and insert it into DB but this line throw exception
return new NewAccessToken($token, $token->id.'|'.$plainTextToken);
and that's because it's type hinted to be an instance of Laravel\Sanctum\PersonalAccessToken
how can I fix that
If you are not extending the default PersonalAccessToken that maybe your issue.
Instead of extending Model extend use Laravel\Sanctum\PersonalAccessToken
use Laravel\Sanctum\PersonalAccessToken as Model;
class CustomPersonalAccessToken extends Model
{
// Add you customisation here
}

How can I create routes similar to Auth::routes()

I created a laravel package with routes. I want to do something similar to Laravel's authentication scaffolding Auth::routes(), where they are injected into whatever file you want to include them (i.e. api.php or web.php).
I am currently using
public function boot()
{
...
$this->loadRoutesFrom(__DIR__.'/routes/api.php');
...
}
But this makes the routes available from anywhere, which I do not want.
I understand I should use a Service Container, but this is my first package and first time creating my own Service Provider, so I am not too sure how to do so, and I couldn't find documentation on how to use these for routes.
Instead, I want to be able to do something along the lines of:
//routes/api.php
Route::group(['prefix'=>'v1', 'middleware:auth-api'], function(){
Logging::routes(); //<-----
...
});
Simple solution, make a class that has a static method that declares your routes.
// your/package/Logging.php
class Logging
{
public static method routes()
{
...your routes...
}
}
// routes/web.php
use Your/Package/Logging;
Logging::routes();

Saving an object into the session or cookie

I'm using Instagram API library to connect user to Instagram profile and then do smth with it. So, as Instagram API wiki says:
Once you have initialized the InstagramAPI class, you must login to an account.
$ig = new \InstagramAPI\Instagram();
$ig->login($username, $password); // Will resume if a previous session exists.
I have initialized InstagramAPI class and then I called $ig->login('username', 'password');. But I have to call it in every function where I need to work with Instagram.
So how could I save this object $ig for using it in the future in other controllers without calling login() any more? Can I save $ig object into the session or cookie file?
P.S. I think saving into the session is not safe way to solve the issue.
UPD: I was trying to save $ig object into the session, however the size is large and session become stop working as well.
Regarding the register method you asked in the comments section, all you need to create a new service provider class in your app\providers directory and declare the register method in there for example:
namespace App\Providers;
use InstagramAPI\Instagram;
use Illuminate\Support\ServiceProvider;
class InstagramServiceProvider extends ServiceProvider
{
public function register()
{
// Use singleton because, always you need the same instance
$this->app->singleton(Instagram::class, function ($app) {
return new Instagram();
});
}
}
Then, add your newly created InstagramServiceProvider class in providers array inside the config/app.php file for example:
'providers' => [
// Other ...
App\Providers\InstagramServiceProvider::class,
]
Now on, in any controller class, whenever you need the Instagram instance, all you need to call the App::make('InstagramAPI\Instagram') or simply call the global function app('InstagramAPI\Instagram') or even you can typehint the class in any method/constructor etc. Some examples:
$ig = App::make('InstagramAPI\Instagram');
$ig = App::make(Instagram::class); // if has use statement at the top fo the class
$ig = app('...');
In a class method as a dependency:
public function someMethod(Instagram $ig)
{
// You can use $ig here
}
Hope this helps but read the documentation properly, there will get everything documented.

Laravel 5.3 SubstituteBindings middleware with withoutMiddleware issue

Since Laravel 5.3, the route implicit binding works as middleware called SubstituteBindings. I used to work with Laravel 5.2 and upgraded to 5.3.
I have some custom middlewares in my application and in my tests I need to disable them. So, until now I used $this->withoutMiddleware() in the test methods. But since the update to Laravel 5.3, withoutMiddleware stops the route implicit binding, and all my tests fails.
I don't know if this should be considered as bug, but it is a huge problem for me.
Is there any way to set the SubstituteBindings middleware as mandatory middleware? How can I still use implicit binding and test my tests without other middlewares?
Building on my comment above I had a look at registering a custom router which always adds SubstituteBindings to the list of middleware if middleware was disabled. You can achieve it by registering a custom RoutingServiceProvider and registering your own Router class. Unfortunately since the route is created fairly early on in the app bootstrap process you also need to create a custom App class and use that in bootstrap/app.php too.
RoutingServiceProvider
<?php namespace App\Extensions\Providers;
use Illuminate\Routing\RoutingServiceProvider as IlluminateRoutingServiceProvider;
use App\Extensions\ExtendedRouter;
class RoutingServiceProvider extends IlluminateRoutingServiceProvider
{
protected function registerRouter()
{
$this->app['router'] = $this->app->share(function ($app) {
return new ExtendedRouter($app['events'], $app);
});
}
}
Custom router
This adds the middleware, it just extends the default router but overrides the runRouteWithinStack method and, instead of returning an empty array if $this->container->make('middleware.disable') is true, it returns an array containing the SubstituteBindings class.
<?php namespace App\Extensions;
use Illuminate\Routing\Router;
use Illuminate\Routing\Route;
use Illuminate\Routing\Pipeline;
use Illuminate\Http\Request;
class ExtendedRouter extends Router {
protected function runRouteWithinStack(Route $route, Request $request)
{
$shouldSkipMiddleware = $this->container->bound('middleware.disable') &&
$this->container->make('middleware.disable') === true;
// Make sure SubstituteBindings is always used as middleware
$middleware = $shouldSkipMiddleware ? [
\Illuminate\Routing\Middleware\SubstituteBindings::class
] : $this->gatherRouteMiddleware($route);
return (new Pipeline($this->container))
->send($request)
->through($middleware)
->then(function ($request) use ($route) {
return $this->prepareResponse(
$request, $route->run($request)
);
});
}
}
Custom App Class
<?php namespace App;
use App\Extensions\Providers\RoutingServiceProvider;
class MyCustomApp extends Application
{
protected function registerBaseServiceProviders()
{
parent::registerBaseServiceProviders();
$this->register(new RoutingServiceProvider($this));
}
Using the custom app class
In bootstrap/app.php change the line where the app is instantiated to:
$app = new App\MyCustomApp(
realpath(__DIR__.'/../')
);
--
Warning! I haven't fully tested this, my app loads and my tests pass but there could be issues that I haven't discovered. It's also quite brittle since if the Laravel base Router class changes you might find things break randomly on future upgrades.
--
You might also want to refactor this so the list of middleware in the custom router always contains the SubstituteBindings class so there isn't so much of a difference in behaviour if middleware is disabled.

How does laravel 5 know which contract implementation I want to use?

I'm kind of confused of how to use contracts. I think that's because I haven't used unit-testing so that it's not obvious for me how contracts work.
Let's have look at this code:
use Illuminate\Contracts\Auth\Guard;
...
public function __construct(Guard $auth)
{
$this->auth = $auth;
$this->middleware('guest', ['except' => 'getLogout']);
}
public function postRegister(RegisterRequest $request)
{
// Registration form is valid, create user...
$this->auth->login($user);
return redirect('/');
}
So how do I know which class implements login method of contract in this line: $this->auth->login($user) ? And how can I change the class if I want to use my own?
In laravel 4 I wrote Auth::user() as an example and I used it everywhere in any controller and it worked. Now I should inject a contract inside a controller method and use it like $auth->user?
Also, If I get it right, contracts are used for making an abstraction. Okay, so, if I want to build a new interface for my own class and then have multiple classes that implement my interface, where should I write the code? I can't think of an example but lets imagine I need to implement an interface for enabling/disabling a lamp, and I have two methods like on() and off() and I have multiple ways to do that. Do I need to create new contract for that?
I hope I can make this a bit clearer for you...
Ad.1. You can check default binding at /vendor/laravel/framework/src/Illuminate/Foundation/Application.php (method registerCoreContainerAliases around line 792). If you want to create your own class or extend existing I recommend looking at How to extend Laravel's Auth Guard class? or http://laravel.com/docs/master/extending (this one is more about Laravel 4.x but might give you an idea).
Ad.2. Actually you can still use Auth::user() but I inject a contract in constructor or a method and call it like $this->auth->user or $auth->user.
Ad.3. I have a /app/Repositories folder where I put my interfaces and implementations, so to follow your example I would create subfolder Lamp and I would create LampInterface with on() and off() methods, then I would create something like Lamp.php that implements LampInterface. Next I would create a service provider in /app/Providers, like LampServiceProvider.php with binding:
namespace Apps\Providers;
use Illuminate\Support\ServiceProvider;
class LampServiceProvider extends ServiceProvider {
/**
* Register the application services.
*
* #return void
*/
public function register()
{
$this->app->singleton(
'App\Repositories\Lamp\LampInterface',
'App\Repositories\Lamp\Lamp'
);
}
}
After that I would register new service provider in /app/config/app.php and finally I can inject my interface like:
public function switchLampOn(App\Repository\Lamp\LampInterface $lamp)
{
$lamp->on();
}

Resources