forked from laravel/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request laravel#37 from eurides-eu/feature/add-user-authen…
…tication-endpoints Add user + client credentials authentication endpoints
- Loading branch information
Showing
30 changed files
with
920 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands\Organizations; | ||
|
||
use App\Organizations\Commands\CreateOrganization as CreateOrganizationCommand; | ||
use Illuminate\Console\Command; | ||
use Madewithlove\Tactician\Traits\DispatchesJobs; | ||
|
||
class CreateOrganization extends Command | ||
{ | ||
use DispatchesJobs; | ||
|
||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'eurides:organization:create | ||
{name : The organization name} | ||
{reference? : The organization internal reference (optional)}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Create an organization. Arguments: {name} {reference?}'; | ||
|
||
/** | ||
* Create a new command instance. | ||
*/ | ||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return mixed | ||
*/ | ||
public function handle() | ||
{ | ||
$name = $this->argument('name'); | ||
|
||
$referenceCode = $this->argument('reference'); | ||
|
||
$organization = $this->dispatch(new CreateOrganizationCommand($name, $referenceCode)); | ||
|
||
$this->info("Organization created successfully => ID {$organization->id}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands\Users; | ||
|
||
use App\Users\Commands\CreateUser as CreateUserCommand; | ||
use Illuminate\Console\Command; | ||
use Madewithlove\Tactician\Traits\DispatchesJobs; | ||
|
||
class CreateUser extends Command | ||
{ | ||
use DispatchesJobs; | ||
|
||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'eurides:user:create {name} {email} {organization_id}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Create a user for an organization. Arguments: {name} {email} {organization_id}'; | ||
|
||
/** | ||
* Create a new command instance. | ||
*/ | ||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return mixed | ||
*/ | ||
public function handle() | ||
{ | ||
$name = $this->argument('name'); | ||
$email = $this->argument('email'); | ||
$organizationId = $this->argument('organization_id'); | ||
|
||
$user = $this->dispatch(new CreateUserCommand($name, $email, $organizationId)); | ||
|
||
$this->info("User created successfully => ID {$user->id}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\oAuth; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Http\Requests\oAuth\Users\AuthorizeFromCodeRequest; | ||
use App\Http\Requests\oAuth\Users\SendLoginCodeRequest; | ||
use App\Users\Commands\AuthorizeFromLoginCode; | ||
use App\Users\Commands\IssueLoginCode; | ||
|
||
class UserController extends Controller | ||
{ | ||
/** | ||
* Get personal access token for this user and send via email. | ||
* | ||
* @param SendLoginCodeRequest $request | ||
* | ||
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response | ||
*/ | ||
public function sendLoginCode(SendLoginCodeRequest $request) | ||
{ | ||
$email = $request->get('email'); | ||
|
||
$command = new IssueLoginCode($email); | ||
|
||
$this->dispatch($command); | ||
|
||
return response([]); | ||
} | ||
|
||
/** | ||
* Validate active login code and return an access token. | ||
* | ||
* @param AuthorizeFromCodeRequest $request | ||
* | ||
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response | ||
*/ | ||
public function authorizeFromCode(AuthorizeFromCodeRequest $request) | ||
{ | ||
$code = $request->get('code'); | ||
|
||
$accessToken = $this->dispatch(new AuthorizeFromLoginCode($code)); | ||
|
||
return response(['accessToken' => $accessToken]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
app/Http/Requests/oAuth/Users/AuthorizeFromCodeRequest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\oAuth\Users; | ||
|
||
use App\Rules\ActiveLoginCode; | ||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class AuthorizeFromCodeRequest extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
* | ||
* @return bool | ||
*/ | ||
public function authorize() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
'code' => ['string', 'required', new ActiveLoginCode()], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\oAuth\Users; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class SendLoginCodeRequest extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
* | ||
* @return bool | ||
*/ | ||
public function authorize() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
'email' => 'string|email|required|exists:users,email', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace App\Mail; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Mail\Mailable; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class UserLoginCode extends Mailable | ||
{ | ||
use Queueable, SerializesModels; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $code; | ||
|
||
/** | ||
* Create a new message instance. | ||
*/ | ||
public function __construct($code) | ||
{ | ||
$this->code = $code; | ||
} | ||
|
||
/** | ||
* Build the message. | ||
* | ||
* @return $this | ||
*/ | ||
public function build() | ||
{ | ||
return $this->view('emails.user_login') | ||
->with('code', $this->code) | ||
->with('link', config('eurides.app_user_login_url')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use App\Traits\UuidModel; | ||
use App\User; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class LoginCode extends Model | ||
{ | ||
use UuidModel; | ||
|
||
/** | ||
* Indicates if the model should be timestamped. | ||
* | ||
* @var bool | ||
*/ | ||
public $timestamps = false; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $fillable = [ | ||
'code', | ||
'user_id', | ||
'expired_at', | ||
]; | ||
|
||
/** | ||
* The attributes that should be mutated to dates. | ||
* | ||
* @var array | ||
*/ | ||
protected $dates = [ | ||
'created_at', | ||
'expired_at', | ||
]; | ||
|
||
/** | ||
* Get the user that owns the code. | ||
*/ | ||
public function user() | ||
{ | ||
return $this->belongsTo(User::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace App\Organizations\CommandHandlers; | ||
|
||
use App\Models\Organization; | ||
use App\Organizations\Commands\CreateOrganization as Job; | ||
|
||
class CreateOrganization | ||
{ | ||
public function handle(Job $job) | ||
{ | ||
return Organization::create([ | ||
'name' => $job->getName(), | ||
'reference_code' => $job->getReferenceCode(), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace App\Organizations\Commands; | ||
|
||
class CreateOrganization | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $name; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $referenceCode; | ||
|
||
/** | ||
* CreateOrganization constructor. | ||
* | ||
* @param $name | ||
* @param $referenceCode | ||
*/ | ||
public function __construct($name, $referenceCode) | ||
{ | ||
$this->name = $name; | ||
$this->referenceCode = $referenceCode; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getReferenceCode() | ||
{ | ||
return $this->referenceCode; | ||
} | ||
} |
Oops, something went wrong.