Skip to content

Adding Google Authenticator #439

Merged
steinkel merged 17 commits into
CakeDC:developfrom
anvyst:master
Nov 2, 2016
Merged

Adding Google Authenticator #439
steinkel merged 17 commits into
CakeDC:developfrom
anvyst:master

Conversation

@anvyst

@anvyst anvyst commented Oct 31, 2016

Copy link
Copy Markdown
Contributor

I've expanded a bit the default LoginTrait to support Google Authenticator mechanism, by using TwoFactorAuth plugin (since version 1.5.2, which became PSR-4 compatible for CakePHP3.x).

In order to function properly, there was also added migration file, to expand users table with secret, secret_verified fields. One holds Base32 shared secret, the second one - makes sure QR-code is shown in verify.ctp until the first successful verification/login into the system.

After the user is authorised from /login, I'm creating temporary session, with holds all required data for me, removing Auth.Users session (to avoid access for restricted url's), and redirect to /verify, where verification process takes place. Once verification is successful, I'm restoring Auth.Users session to let AuthComponent magic to work.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-3.4%) to 77.391% when pulling ffebc26 on anvyst:master into 647ce97 on CakeDC:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-2.4%) to 78.404% when pulling fd2fa2b on anvyst:master into 647ce97 on CakeDC:master.

@steinkel steinkel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check review comments

Comment thread composer.json Outdated
"require": {
"cakephp/cakephp": ">=3.2.9 <4.0.0"
"cakephp/cakephp": ">=3.2.9 <4.0.0",
"robthree/twofactorauth": "^1.5.2"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should not require this lib by default, we are trying to keep the plugin as clean as possible and let every user include the required packages if needed, for example see the oauth related packages

Comment thread composer.json
"league/oauth2-linkedin": "@stable",
"google/recaptcha": "@stable"
"google/recaptcha": "@stable",
"robthree/twofactorauth": "^1.5.2"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed here? or could we leave this as suggest only and add some docs on how to properly setup the 2factor feature?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@steinkel , I've removed it from require param, and left it only in require-dev just to make sure that unit tests will pass.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense, thanks

$table->addColumn('secret', 'string', [
'after' => 'activation_date',
'default' => null,
'limit' => 255,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

secret is using any fixed size or should we use text instead?

Comment thread config/users.php Outdated
'plugin' => 'CakeDC/Users',
'controller' => 'Users',
'action' => 'verify',
'prefix' => null,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to use "false" instead, to prevent issues with login inside a prefixed controller. We had issues with this before setting up the loginAction...

const EVENT_BEFORE_LOGOUT = 'Users.Component.UsersAuth.beforeLogout';
const EVENT_AFTER_LOGOUT = 'Users.Component.UsersAuth.afterLogout';


Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

phpcs

$this->set('secretDataUri', $this->GoogleAuthenticator->getQRCodeImageAsDataUri($temporarySession['email'], $secret));
}

if ($this->request->is('post')) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use request->data() instead

if ($this->request->is('post')) {
$verificationCode = $this->request->data['code'];
$user = $this->request->session()->read('temporarySession');

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check user keys before using them

$this->request->session()->write('Auth.User', $user);

$url = $this->Auth->redirectUrl();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use return

Comment thread src/Controller/Traits/LoginTrait.php Outdated
$this->Flash->error($message, 'default', [], 'auth');

$this->request->session()->destroy();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use return

Comment thread src/Template/Users/verify.ctp Outdated
<?php if (!empty($secretDataUri)):?>
<p class='text-center'><img src="<?php echo $secretDataUri;?>"/></p>
<?php endif;?>
<?= $this->Form->input('code', ['required' => true,'label' => 'Verification Code']) ?>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use __d() for label

@steinkel
steinkel changed the base branch from master to develop November 1, 2016 12:02
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-2.7%) to 78.35% when pulling b1a0057 on anvyst:master into 372ad73 on CakeDC:develop.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-2.7%) to 78.35% when pulling b1a0057 on anvyst:master into 372ad73 on CakeDC:develop.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-2.7%) to 78.35% when pulling a90f722 on anvyst:master into 372ad73 on CakeDC:develop.

@steinkel
steinkel merged commit ebf2957 into CakeDC:develop Nov 2, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants