Coder Social home page Coder Social logo

How to get the secret? about twofactor HOT 4 CLOSED

1e4 avatar 1e4 commented on June 14, 2024
How to get the secret?

from twofactor.

Comments (4)

DarkGhostHunter avatar DarkGhostHunter commented on June 14, 2024

How can I get the secret for use outside the Laravel application?

If you are wondering about the Shared Secret, you can easily get it from the user model relation itself. Pair that with a controller on a route and you can retrieve it.

use App\Models\User;

Route::get('user/{user:id}/shared_secret', function (User $user) {
    return $user->twoFactorAuth->shared_secret;
});

When passing in shared_secret to verify the 2fa code they gave it always returns false.

The Shared Secret is only given to the user once, at setup. After that, they need to confirm by generating a 2FA code on their device, which is returned to the app and checked against the code generated by the same server. If they're equal, it's true.

Route::get('user/{user:id}/confirm_2fa', function (User $user) {
    return $user->confirmTwoFactorAuth(request('2fa_code')); // true|false
});

Most common problems for this to fail are wrong server time and/or device time.

So how can I derive the secret from the shared_secret?

The shared_secret is encrypted on the database. Save yourself some lines and use the model itself from the first example to transmit it. It decrypts to a Base32 uppercased string.

Route::get('user/{user:id}/shared_secret', function (User $user) {
    return $user->twoFactorAuth->shared_secret;
});

from twofactor.

1e4 avatar 1e4 commented on June 14, 2024

I understand how to get it in Laravel, but I'm using it outside of Laravel, I looked at the internals but couldn't figure out how to get it from the database

from twofactor.

DarkGhostHunter avatar DarkGhostHunter commented on June 14, 2024

You can just query the database and decrypt the shared secret. You should receive a Base 32 in uppercase string.

If you don't have the ID of the row, you will need to query both model class name and model id:

$secret = DB::table('two_factor_authentications')
    ->where([
        'authenticatable_type' => 'App/Models/User',
        'authenticatable_id' => 4
    ])
    ->first('shared_secret');

return Crypt::decrypt($secret);

from twofactor.

1e4 avatar 1e4 commented on June 14, 2024

Thanks, solved it. Now to understand why the session isn't being flashed which seems related to #21

from twofactor.

Related Issues (20)

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.