https://github.com/mariojgt/castle
This laravel package is a very simple way to implement 2 steps verification in your current application with almost zero setup.
https://github.com/mariojgt/castle
2fa 2factor 2steps easy-to-use laravel php safety two-factor-authentication two-steps-authentication verification
Last synced: 3 months ago
JSON representation
This laravel package is a very simple way to implement 2 steps verification in your current application with almost zero setup.
- Host: GitHub
- URL: https://github.com/mariojgt/castle
- Owner: mariojgt
- License: mit
- Created: 2021-11-04T23:09:32.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2025-01-08T15:05:04.000Z (over 1 year ago)
- Last Synced: 2025-03-30T18:01:42.886Z (about 1 year ago)
- Topics: 2fa, 2factor, 2steps, easy-to-use, laravel, php, safety, two-factor-authentication, two-steps-authentication, verification
- Language: CSS
- Homepage:
- Size: 6.15 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

# Castle
This Laravel package help you quickly add 2fa authentication in you existing application, simular to google authentication.
## Features
- Demo with the example application flow you need.
- 2 steps autentication.
- middleware protection.
## Badges
[](https://choosealicense.com/licenses/mit/)
[](http://www.gnu.org/licenses/agpl-3.0)
## Installation
Install my-project with composer
```bash
composer require mariojgt/castle
php artisan install::castle
```
## Usage/Examples
1: You need to assign the trait to you user model table in order to use the 2steps verification and have access to the backup codes.
```php
use Mariojgt\Castle\Trait\Castle;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable, Castle;
/**
* The attributes that are mass assignable.
*
* @var string[]
*/
protected $fillable = [
'name',
'email',
'password',
];
```
this will insure you have access to the backup codes
2: In order to sync the user you need to first generate the authenticator secret using the helper normally when you register or with a controller method to sync the authenticator
```php
use Mariojgt\Castle\Helpers\AuthenticatorHandle;
class myController
{
public register () {
// Start the class that handle most of the logic
$handle = new AuthenticatorHandle();
// Generate the code
$codeInfo = $handle->generateCode($userEmail);
// Sync that code with the user using the trait
Auth()->user()->syncAuthenticator($codeInfo['secret']);
}
```
3: At this point the authenticator is enabled against that user, now you need to protect the middleware here is a example
```php
// Auth Route Example
Route::group([
'middleware' => ['web', '2fa'], // note you can use (2fa:admin) for admin guard or leave empty for web as default
], function () {
// Example page required to be login
Route::get('/castle-try', [HomeContoller::class, 'protected'])->name('castle.try');
});
```
4: Display the user codes, normaly you only display the backup codes once you can use the following example
```php
Auth()->user()->getCodes; // this will return the backup codes for that user
```
5: using backup codes see the example
```php
use Mariojgt\Castle\Helpers\AuthenticatorHandle;
myclass {
public myFunction () {
// Start the class that handle most of the logic
$handle = new AuthenticatorHandle();
// the encryption is using the normal laravel encrypt fuction // example encrypt('user_secret')
$handle->useBackupCode($codeYouType, $encryptauthenticatorSecret); // The second parameter is not required
}
}
```
## Tech Stack
**Client:** TailwindCSS, vuejs, blade
**Server:** 2fa, Laravel