cozy / cozy-proxy

This repository was part of CozyV2 which has been deprecated - Cozy authentication and routing layer
https://blog.cozycloud.cc/post/2016/11/21/On-the-road-to-Cozy-version-3
GNU Affero General Public License v3.0
26 stars 31 forks source link

Two-factors authentication #260

Closed babolivier closed 8 years ago

babolivier commented 8 years ago

Here is the 2FA (2-factors authentication) support for the Cozy proxy. Probably needs polishing, and doesn't have a single test, that's the reason for the WIP tag.

For the 2FA support to be complete in Cozy, I consider that we still need to have:

I'll hopefully be working on both in the next few days.

When complete, will fix #123.

frankrousseau commented 8 years ago

@babolivier Thank you for that awesome pull request. Can you add explanation about what you did, how does it work and what it brings to the platform?

babolivier commented 8 years ago

Sure thing @frankrousseau. Sorry if my initial post wasn't explicit enough (actually it wasn't explicit at all).

As said above, this PR contains support for two-factors authentication in Cozy's authentication proxy.

The proxy can now authenticate using the HOTP (HMAC-based One-Time Password, or OTP with a counter) and TOTP (Timer-based One-Time Password, or OTP with a timer) algorithms through the corresponding Passport strategies in the passport_configurator.coffee file right here.

Quick look at the algorithms and the corresponding strategies:

In the future, the user will be able to chose which algorithme to use via a configuration panel within the Cozy home.

In order to store everything we need in the database, I've added three fields to the User doctype:

The authentication proceeds as such:

The authType also allows one really useful feature: When we generate and send the login page to the user, we can modulate it according to wether or not OTP-based 2FA is enabled. This way, if this feature is disabled, the user will see the usual login page:

capture d ecran de 2016-04-04 18-59-27

But if the OTP-based 2FA feature is enabled (on any of the two algorithms), the user will get this login page:

capture d ecran de 2016-04-04 19-04-00

I've also added a few translations (mainly for OTP-related errors) and edited this error in order for it to be accurate in both simple password authentication and 2FA.

If you have any feedback, remark or question, I'd be more than happy to read and answer them.

frankrousseau commented 8 years ago

Another question. I don't understand how the use get its authentication code. How does he retrieve it?

babolivier commented 8 years ago

Via an app such as Authy or Google Authenticator. These apps will process the base32 of the master key (which is the string we'll give to the user in the configuration panel) and output a 6 digits-long token for a limited time that the user will have to enter in the "Authentication code" field. If the chosen algorithm is HOTP, he can also retrieve the token through a dedicated device such as a Yubikey. I also see discussions about adding a token generation feature in the Cozy Android app, which could be great to make switching to 2FA easier.

To put this in perspective, it's the same process as the Steam Authenticator or Battle.net Authenticator apps (except these two are for specific services). Given the right information (in our case, the base32 of the master key), the app will give us a temporary token to enter.

frankrousseau commented 8 years ago

Ok thank you a lot for the explanation. I would like to ask you two more things:

Is it ok for you?

babolivier commented 8 years ago

Absolutely. I've just added the .md file to my pull request, and will start workin on tests tonight.

babolivier commented 8 years ago

Hi @frankrousseau,

Tests have been added (Travis's displaying them all green, whoohoo!), I've had occasions to try it myself and fix a few things, add comments where I thought them necessary and add a doc, I think the PR is now ready from here! We still need a configuration panel in the home, but that'll come after. (removing the "WIP" tag then)

m4dz commented 8 years ago

Thanks a lot @babolivier for this awesome work :tada:!

@frankrousseau it' ok for me, ready to merge!

babolivier commented 8 years ago

:tada: :tada: :tada:

My pleasure :smile:

frankrousseau commented 8 years ago

@m4dz I will review it before merging. Thank you for the additional review.

frankrousseau commented 8 years ago

OK Can you make sure that all lines are less than 80 chars wide?

babolivier commented 8 years ago

@frankrousseau I made the requested edits, and no line to my knowledge is wider than 80 char. Let me know if you have any more feedback on the commit.

frankrousseau commented 8 years ago

Thank you @babolivier for this beautiful PR. We must now run into the functional testing. We'll provide you feedback soon.

NB: Sorry for all that checking but auth is something we can't do wrong.

babolivier commented 8 years ago

No problem @frankrousseau. Actually, would seem weird if changes to auth didn't require a lot of checkout before rolling out to production. And I also want this feature to be as stable and working as possible!

poupotte commented 8 years ago

@babolivier : when login is wrong, is it possible to differentiate password error and auth code error ?

babolivier commented 8 years ago

@poupotte Well technically we could, but I already looked into it, and, front-end, the proxy always assumes that if there's an error on the login page it's due to the password being wrong. So it would be possible, but we would have to change the whole error management on this view, and making that change wasn't a priority to me.

frankrousseau commented 8 years ago

@poupotte @m4dz can we merge it?

m4dz commented 8 years ago

It's fine for me \o/

frankrousseau commented 8 years ago

@m4dz Did you see the error display issue. Do you think it can be easily fixed?

m4dz commented 8 years ago

I saw the issue, I think we can easily parse an error response for the server, but I ask myself about two points:

  1. should we explicitely return which code is erroneous? It makes it more easy for attackers to identify if the password is correct or not and then concentrate to try to crack the token only
  2. if we decide to be more explicit about the failing reason, I think we can do it in a separate PR, this one seems to be more stable (i.e. it's more an improvement rather then n issue)

Any though about it?

frankrousseau commented 8 years ago

Ok let's merge it. @poupotte will tell if an improvement is required.

frankrousseau commented 8 years ago

Congrats @babolivier for this awesome PR!

babolivier commented 8 years ago

:tada: :tada:

Thanks! :smile: Configuration in cozy-home should have its PR open in a few days at last. :wink: