Git Product home page Git Product logo

two-factor-authentication's Introduction

Build Status Scrutinizer Code Quality StyleCI License

Laravel Two Factor Authentication (2FA)

Two

Two Factor Authentication or 2-Step Verification provides stronger security for your Account by requiring a second step of verification when you sign in. In addition to your password, you’ll also need a code generated by the Google Authenticator app on your phone. This package implements TOTP defined in RFC 6238

Requirements

  • PHP >= 7.1
  • Laravel >= 5.3
  • Google Authenticator Android - iOS (Recommended) or Authy mobile app

Installation

1. Composer Install

$ composer require thecodework/two-factor-authentication

Note - If your're using Laravel 5.5 or newer version then auto-discovery-pacakge would automatically update the providers and you could skip to Step 3

2. Add Service Provider

After requiring the package add TwoFactorAuthenticationServiceProvider::class into providors array in app.php confi file

[
 'providers' => [
    //...
    Thecodework\TwoFactorAuthentication\TwoFactorAuthenticationServiceProvider::class
  ]
]

3. Publish the ConfigFile

Publish config file

$ php artisan vendor:publish --provider="Thecodework\TwoFactorAuthentication\TwoFactorAuthenticationServiceProvider" --tag=config

Once the config file is published you can navigate to config directory of your application and look for 2fa-config.php file and change configuration as you want.

4. Run Migrations

Now run the migration

$ php artisan migrate

It will use the default User model and adds two columns is_2fa_enabled and secret_key.

5. Add AuthenticatesUserWith2FA trait in the LoginController

Now the config file is placed. The last thing to do is addding AuthenticatesUsersWith2FA trait in the Http/Controllers/Auth/LoginController.php file which helps to stop user at verify-2fa page to enter TOTP token after each login.

The final snippet will look like this.

use AuthenticatesUsers, AuthenticatesUsersWith2FA {
    AuthenticatesUsersWith2FA::authenticated insteadof AuthenticatesUsers;
}

Note: Don't forget to include use statement use Thecodework\TwoFactorAuthentication\AuthenticatesUsersWith2FA in the header.

6. Setup 2FA for user

• Enable 2FA

Now login to the application and visit /setup-2fa/ route, which will show a barcode which can be scanned either using Google Authenticator or Authy mobile application as described above. Scan that code and click Enable Two Factor Authentication.

• Disable 2FA

To disable Two Factor, visit /setup-2fa route, which will now show a Disable Two Factor Authentication button. Click to disable 2FA for your account.

7. Testing 2FA

Now to test 2FA, perform logout and log back in again, it will ask you to enter Token which can be obtain from the authenticator mobile application. Enter the token and you're logged in.

Additionally

If you want to publish views, and migration as well along with config file then run

$ php artisan vendor:publish --provider="Thecodework\TwoFactorAuthentication\TwoFactorAuthenticationServiceProvider"

Contribution

Feel free to create issues, submit PRs and talk about features and enhancement through proposing issue. If you find any security consideration, instead of creating an issue send an email to [email protected].

two-factor-authentication's People

Contributors

devfelixdorn avatar eliesauveterre avatar imrealashu avatar mukuled2 avatar sarnodeep avatar scrutinizer-auto-fixer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

two-factor-authentication's Issues

upgrade to laravel 5.5 bug - "Class 'Base32\Base32' not found"

Hello, sinse i update to laravel 5.5 im getting this error with this packeg ;

Class 'Base32\Base32' not found

Symfony\Component\Debug\Exception\FatalThrowableError
…/vendor/thecodework/two-factor-authentication/src/Http/Controllers/TwoFactorAuthenticationController.php152

private function base32EncodedString($length = 30):
    string
    {
        return Base32::encode($this->strRandom($length));
    }

Use not too generic name for db columns

Currently we're using is_2fa_enabled and secret_key column to make 2FA work. Instead of using too common name we can use some unique non-collidable names.

Can we send token via SMS?

I imagine this feature is not yet here.
but I ask you if in someway when we are at 'enter otp' page we can send an sms to the mobile phone of the user instead of let user use the authenticator app.

Some very old user are still not able to perform a 'read qr' -> user app flow

Inconsistencies in class naming (2FA vs TwoFactorAuthentication)

Hello! (again xD)

I would like to discuss something.

Sometimes, you use 2FA in classes, methods and sometimes it's TwoFactorAuthentication. I suggest we choose one or the other to remove these (little) inconsistencies.

I prefer 2FA as it keeps classes name shorter.

Add API support

Add support so that user can extend this library for generating qr code and verifying token using api end point.

Support for custom columns

Hello! (again, again)

I would like to discuss adding custom properties in the config to allow custom columns names.

We just need an extra class.

What do you think about that?

I can submit a PR.

Do not regenerate barcode image on each refresh of the page.

When the secret is generated for barcode image, don't change it on each refresh unless user requests to change the secret key so the barcode image. this will help user if they accidentally delete application account from Google Authenticator or so and they'd be able to retrieve it easily.

Use config url in view not hard-coded one

This is actually a bug not a feature request.

In views there is things like {{ url('/enable-2fa'). I think it should be instead {{ route('enable-2fa') or something and add this in the routes.php.

I can submit a PR.

Barcode not valid in iOS

Hello, I have found an issue with Google Authenticator (iOS), with Android it works well:

The base32EncodedString Function in TwoFactorAuthenticationController.php should be min. length of 10 and not be dependent on the config('2fa-config.number_of_digits') value. Otherwise the base32 Function will pad with "=" at the end of the string. It turns out, that Google Authenticator (iOS) doesn't accept "=" chars in Barcode URL.

Source: https://stackoverflow.com/questions/29173288/google-authenticator-on-apple-devices-certain-secrets-are-not-valid

Rename views, migration and config to 2fa-*

Hello!

To make this library better, i suggest that instead of naming config config, views : views, ... We name them 2fa-config, 2fa-views... Because when publishing one of them their name are just views,... which is not really useful

What do you think ?

Extends Controller class from default new project namespace

Hi,
Minor complaint.

\Thecodework\TwoFactorAuthentication\Http\Controllers\TwoFactorAuthenticationController

I have renamed my Laravel project's namespace, and so the Controller class TwoFactorAuthenticationController extends is no longer found in the \App\Http\Controllers namespace.

So either I have to rename my project namespace back to "App" or I have to make a copy of the TwoFactorAuthenticationController in my project's controller directory with my namespace.

Just something people need to be aware of if they are using their own namespace.

Token is not valid?

Hello again.
i have installed the package and everything works great!
but it seems that i cant make a login, every time i got the same message.
"Token is not valid"
Do i need to sync my server time to some time zone?
why this could happen?

Suggestion: submit issuer and user email with the barcode

I use the library in laravel and found that in the authenticator app I was missing my email address. I made an overwrite for the TwoFactorAuthenticationController as follows:

    public function setupTwoFactorAuthentication(Request $request)
    {
        $user = $this->getUser();
        $totp = TOTP::create(
            $this->base32EncodedString(),
            config('2fa-config.period'),
            config('2fa-config.digest_algorithm'),
            config('2fa-config.number_of_digits')
        );

//here is my change
        $totp->setLabel($user->email);
        $totp->setIssuer(config('2fa-config.account_name'));
//end of change

        $this->updateUserWithProvisionedUri($totp->getProvisioningUri());
        $barcode = $totp->getQrCodeUri();
        if ($request->ajax()) {
            return $barcode;
        }

        return view('2fa::setup', compact('barcode', 'user'));
    }

That way it is easier to see for which email address it is used for

Barcode is invalid, migrations fail

This package works very well for intercepting the auth request after login and has great setup instructions.

However, two issues for a fresh install using the instructions provided on this repo:

(1) I keep getting "QR code is invalid" (or similar) on both Google Auth and Authy. In case this additional info helps, the secret key generated in the user's table upon a fresh install seems to be adding an arbitrary number of equals signs at the end: "MFWGK6CAOVZXA4TPORSWG2BOMNXW2===".

(2) A RequiredFields error is thrown for migrations: [2017-04-01 17:14:40] local.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: Class 'RequiredFields' not found in /Users/[...redacted...]

two_factor_provisioned_uri field not found

Hey, currently there is an issue in the new version 0.1.7

the new field "two_factor_provisioned_uri" is in the Migration but the Controller TwoFactorAuthenticationController is still accessing the field "two_factor_secret_key" (line 103).

Greetings

Support for custom qr code generator

Hello!
Would love to have that.
I think we could do something in the config like

return [
// ...
  'qrcode_generator' => \App\2fa\MyCustomQrCodeGenerator::class
// ...
];

MyCustom could extends of a QrCodeGeneratorInterface that could be called here

I can, and if you agree with that proposition, will submit a PR.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.