laravel-passport

How to test authentication via API with Laravel Passport?

喜夏-厌秋 提交于 2019-12-02 20:50:46
I'm trying to test the authentication with Laravel's Passport and there's no way... always received a 401 of that client is invalid, I'll leave you what I've tried: My phpunit configuration is the one that comes from base with laravel tests/TestCase.php abstract class TestCase extends BaseTestCase { use CreatesApplication, DatabaseTransactions; protected $client, $user, $token; public function setUp() { parent::setUp(); $clientRepository = new ClientRepository(); $this->client = $clientRepository->createPersonalAccessClient( null, 'Test Personal Access Client', '/' ); DB::table('oauth_personal

How can I simulate logging out after using Passport::actingAs in a unit or integration test?

我是研究僧i 提交于 2019-12-02 03:22:45
问题 I have a test where I simulate logging in, then afterwards I want to test things as if I was not logged in. Like so. // log in as user with id 2 $id = 2; Passport::actingAs(User::findOrFail($id)); testSomeStuff() ... // now I want to test things as if I was not logged in Is there a way to do this in one test function? I'm using Laravel 5.6 and Passport 5. 回答1: Try this in your test $this->refreshApplication(); Had the same issue and this is the only thing that worked for me EDIT: It seems to

Laravel 5.6 - How to get auth()->user() or $response->user() in api controller?

北城以北 提交于 2019-12-01 21:30:20
问题 In api.php routes file below, there are public routes and private routes: Route::group(['namespace' => 'API'], function() { // Public routes (auth not required) Route::group([], function() { Route::get('/testauth1', 'TestController@testauth1'); // more public routes... }); // Private routes (auth required) Route::group(['middleware' => 'auth:api'], function() { Route::get('/testauth2', 'TestController@testauth2'); // more private routes... }); }); In the TestContoller these are the 2 methods

Laravel Passport middleware protected routes “Unauthenticated” problem

萝らか妹 提交于 2019-12-01 14:56:06
问题 I am using Laravel Passport for the authentication, so I put my routes into middleware protection. UPDATED To make it clear, I am adding UsersController too. public function getUser() { $users = Auth::user(); return response()->json($users); } // Route::group(['prefix' => 'v1', 'middleware' => 'auth:api'], function () { /* users */ Route::get('/users', 'Api\UsersController@getUser'); /* fetch */ Route::get('/articles', 'Api\ArticlesController@allArticles'); Route::get('/article/{id}', 'Api

How to add status in default response passport

会有一股神秘感。 提交于 2019-12-01 09:38:53
I am developing and application in web as well as App. For App's API I have use Passport in laravel. Using passport i can create token and using that token I verify user for other api access. But if token in invalid then it return Error message like "Unauthorized" without any status. Can any one tell me what to do if i want to add error status like 401 with message "Unauthorized" . Where I have to change in code so that my web code is not affect. I want json response like below with 2 fields in json. status:401 message:Unauthorized You could create a new exception handler middleware to catch

laravel passport custom password column

走远了吗. 提交于 2019-12-01 06:54:44
How can I use Laravel's Passport package to authenticate a different password column. If i want to authenticate from a different 'username' column, it can be done with the following code: public function findForPassport($username) { return $this->where('id', $username)->first(); } It will take Id, as the column. What if I want to use a different 'password' column. A column in the table with a different name such as 'uid_token'. There's a method the Passport/Bridge asks for called validateForPassportPasswordGrant($password) that you can override in your user model, if you don't override this it

laravel passport custom password column

纵然是瞬间 提交于 2019-12-01 04:09:47
问题 How can I use Laravel's Passport package to authenticate a different password column. If i want to authenticate from a different 'username' column, it can be done with the following code: public function findForPassport($username) { return $this->where('id', $username)->first(); } It will take Id, as the column. What if I want to use a different 'password' column. A column in the table with a different name such as 'uid_token'. 回答1: There's a method the Passport/Bridge asks for called

Laravel Passport token lifetime

*爱你&永不变心* 提交于 2019-11-30 23:22:43
问题 I don't get what I'm doing wrong. I can't set token expiration time. <?php namespace App\Providers; class AuthServiceProvider extends ServiceProvider { public function boot() { $this->registerPolicies(); Passport::tokensExpireIn(Carbon::now()->addDays(1)); Passport::refreshTokensExpireIn(Carbon::now()->addDays(30)); } } BUT when I call $user->createToken() , for example like this: <?php // as a demo namespace App\Http\Middleware; class ParseSpecialToken { public function handle($request,

Laravel Passport Get Client ID By Access Token

半腔热情 提交于 2019-11-30 18:45:23
I'm writing a tiny sms gateway to be consumed by a couple of projects, I implemented laravel passport authentication ( client credentials grant token ) Then I've added CheckClientCredentials to api middleware group: protected $middlewareGroups = [ 'web' => [ ... ], 'api' => [ 'throttle:60,1', 'bindings', \Laravel\Passport\Http\Middleware\CheckClientCredentials::class ], ]; The logic is working fine, now in my controller I need to get client associated with a valid token. routes.php Route::post('/sms', function(Request $request) { // save the sms along with the client id and send it $client_id

Custom Laravel Passport BearerTokenResponse

倾然丶 夕夏残阳落幕 提交于 2019-11-30 14:23:32
问题 Currently I have a Laravel installation using Laravel Passport (which uses league/oauth2-server for the server implementation). I would like to return the user id when a oauth2 token is granted, so I can use it to identify the authenticated user in my EmberJS app. The suggested method to do this is: Create my own class: use League\OAuth2\Server\ResponseTypes\BearerTokenResponse; use League\OAuth2\Server\Entities\AccessTokenEntityInterface; class UserIdBearerTokenResponse extends