Skip to content

Commit

Permalink
feature: passport/sanctum validation, refactoring routes and actions
Browse files Browse the repository at this point in the history
  • Loading branch information
ArielMejiaDev committed Jul 15, 2021
1 parent 054215d commit 69f9460
Show file tree
Hide file tree
Showing 23 changed files with 183 additions and 141 deletions.
Binary file modified .DS_Store
Binary file not shown.
12 changes: 12 additions & 0 deletions .idea/json-api-auth.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}
],
"require": {
"php": "^7.1|^8.0",
"php": "^7.4|^8.0",
"illuminate/support": "^6.0|^7.0|^8.0"
},
"require-dev": {
Expand Down
42 changes: 36 additions & 6 deletions src/Console/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,31 @@ class InstallCommand extends Command
*/
protected $description = 'Install the Json Api Authentication controllers and routes.';

public function __construct()
{
parent::__construct();

if (file_exists(config_path('json-api-auth.php')))
{
$this->setHidden(true);
}
}

/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
if (!$this->appHasSanctum() && !$this->appHasPassport()) {
$this->output->error('The package requires some official package to handle api tokens.');
$this->warn('You can choose between Laravel Sanctum (your app would be consumed by mobile & js apps) or Laravel Passport (your app would be consumed by third party apps with oauth).');
$this->info('For Laravel sanctum you can run: composer require laravel/sanctum');
$this->info('For Laravel passport you can run: composer require laravel/passport');
return null;
}

// Controllers...
(new Filesystem)->ensureDirectoryExists(app_path('Http/Controllers/JsonApiAuth'));
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/App/Http/Controllers/JsonApiAuth', app_path('Http/Controllers/JsonApiAuth'));
Expand All @@ -40,10 +58,6 @@ public function handle()
(new Filesystem)->ensureDirectoryExists(app_path('Notifications/JsonApiAuth'));
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/App/Notifications/JsonApiAuth', app_path('Notifications/JsonApiAuth'));

// Actions...
(new Filesystem)->ensureDirectoryExists(app_path('Actions/JsonApiAuth'));
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/App/Actions/JsonApiAuth', app_path('Actions/JsonApiAuth'));

// Translate files...
(new Filesystem)->ensureDirectoryExists(resource_path('lang'));
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/resources/lang', resource_path('lang/en'));
Expand All @@ -53,7 +67,13 @@ public function handle()

// Routes...
copy(__DIR__.'/../../stubs/routes/api.php', base_path('routes/api.php'));
copy(__DIR__.'/../../stubs/routes/auth.php', base_path('routes/json-api-auth.php'));
if($this->appHasSanctum()) {
copy(__DIR__ . '/../../stubs/routes/auth/sanctum.php', base_path('routes/json-api-auth.php'));
}

if($this->appHasPassport()) {
copy(__DIR__ . '/../../stubs/routes/auth/passport.php', base_path('routes/json-api-auth.php'));
}

// Config...
copy(__DIR__.'/../../stubs/config/config.php', base_path('config/json-api-auth.php'));
Expand All @@ -63,13 +83,23 @@ public function handle()
$this->createRoutesTable();
}

protected function appHasSanctum(): bool
{
return class_exists('Laravel\Sanctum\Sanctum');
}

protected function appHasPassport(): bool
{
return class_exists('Laravel\Passport\Passport');
}


/**
* At command runtime the routes files are not available yet, so its necessary to build it manually
*/
public function createRoutesTable()
{
$headers = ['Method', 'URI', 'Name'];
$headers = ['METHOD', 'URI', 'NAME'];

$routes = [
[
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
<?php

namespace App\Actions\JsonApiAuth;
namespace App\Http\Controllers\JsonApiAuth\Actions;

class AuthKit
{
const PASSPORT_AUTH_KIT = 'passport';
const SANCTUM_AUTH_KIT = 'sanctum';

public static function getMiddleware()
{
if(static::isSanctum()) {
return 'auth:sanctum';
}

return 'auth:api';
}

public static function getGuard()
public static function getGuard(): string
{
if(static::isSanctum()) {
return 'sanctum';
Expand All @@ -25,7 +16,7 @@ public static function getGuard()
return 'api';
}

public static function isPassport()
public static function isPassport(): bool
{
$passportVendorName = 'Laravel\Passport\Passport';
if (class_exists($passportVendorName)) {
Expand All @@ -34,7 +25,7 @@ public static function isPassport()
return false;
}

public static function isSanctum()
public static function isSanctum(): bool
{
$sanctumVendorName = 'Laravel\Sanctum\Sanctum';
if(class_exists($sanctumVendorName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@

namespace App\Http\Controllers\JsonApiAuth;

use App\Actions\JsonApiAuth\AuthKit;
use App\Http\Controllers\JsonApiAuth\Actions\AuthKit;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\ValidationException;

class ConfirmablePasswordController
{
/**
* Confirm the user's password.
*
* @param \Illuminate\Http\Request $request
* @return mixed
*/
public function __invoke(Request $request)
/** Confirm the user's password.*/
public function __invoke(Request $request): JsonResponse
{
if (! Hash::check($request->get('password'), $request->user(AuthKit::getGuard())->password)) {
throw ValidationException::withMessages([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

namespace App\Http\Controllers\JsonApiAuth;

use App\Actions\JsonApiAuth\AuthKit;
use App\Http\Controllers\JsonApiAuth\Actions\AuthKit;
use App\Notifications\JsonApiAuth\VerifyEmailNotification;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Http\Response;

class EmailVerificationNotificationController extends Controller
class EmailVerificationNotificationController
{
/**
* Resend the email verification notification.
Expand All @@ -31,6 +30,4 @@ public function __invoke(Request $request)
'message' => __('json-api-auth.email_sent'),
], 200);
}


}
10 changes: 5 additions & 5 deletions stubs/App/Http/Controllers/JsonApiAuth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@

namespace App\Http\Controllers\JsonApiAuth;

use App\Http\Controllers\Controller;
use App\Http\Controllers\JsonApiAuth\Traits\HasToShowApiTokens;
use App\Http\Requests\JsonApiAuth\LoginRequest;
use App\Models\User;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
class LoginController
{
use HasToShowApiTokens;

public function __invoke(LoginRequest $request)
public function __invoke(LoginRequest $request): JsonResponse
{
try {

if(Auth::attempt($request->only(['email', 'password']))) {
return $this->showCredentials(Auth::user());
}

} catch (\Exception $exception) {
} catch (Exception $exception) {

return response()->json([
'message' => $exception->getMessage()
Expand Down
20 changes: 6 additions & 14 deletions stubs/App/Http/Controllers/JsonApiAuth/LogoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,24 @@

namespace App\Http\Controllers\JsonApiAuth;

use App\Http\Controllers\Controller;
use App\Http\Controllers\JsonApiAuth\Revokers\RevokerFactory;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Str;
use App\Models\User;

class LogoutController extends Controller
class LogoutController
{
/**
* @param Request $request
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
*/
public function __invoke(Request $request)
public function __invoke(Request $request): Response
{
(new RevokerFactory)->make()->{$this->applyRevokeStrategy()}();

return Response([
return response([
'message' => __('json-api-auth.logout'),
], 200);
}

/**
* It guess what method is going to use on logout based on the package config file
* @return string
*/
public function applyRevokeStrategy()
/** It guess what method is going to use on logout based on the package config file. */
public function applyRevokeStrategy(): string
{
$methods = [
'revoke_only_current_token',
Expand Down
22 changes: 7 additions & 15 deletions stubs/App/Http/Controllers/JsonApiAuth/NewPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,19 @@

namespace App\Http\Controllers\JsonApiAuth;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Http\Requests\JsonApiAuth\NewPasswordRequest;
use App\Models\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class NewPasswordController extends Controller
class NewPasswordController
{
/**
* Handle an incoming new password request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse
*
* @throws \Illuminate\Validation\ValidationException
*/
public function __invoke(NewPasswordRequest $request)
/** Handle an incoming new password request. */
public function __invoke(NewPasswordRequest $request): JsonResponse
{
$user = $request->getUser($request);
/** @var User $user */
$user = $request->getUser();

$user->update(['password' => Hash::make($request->get('password'))]);

Expand All @@ -30,6 +24,4 @@ public function __invoke(NewPasswordRequest $request)
'message' => __('json-api-auth.password_updated'),
]);
}


}
Loading

0 comments on commit 69f9460

Please sign in to comment.