Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Google Authenticator #439

Merged
merged 17 commits into from
Nov 2, 2016
Merged

Adding Google Authenticator #439

merged 17 commits into from
Nov 2, 2016

Conversation

anvyst
Copy link
Contributor

@anvyst anvyst commented Oct 31, 2016

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

Coverage Status

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

@coveralls
Copy link

Coverage Status

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

Copy link
Member

@steinkel steinkel left a comment

Choose a reason for hiding this comment

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

Please check review comments

@@ -27,7 +27,8 @@
"source": "https://github.com/CakeDC/users"
},
"require": {
"cakephp/cakephp": ">=3.2.9 <4.0.0"
"cakephp/cakephp": ">=3.2.9 <4.0.0",
"robthree/twofactorauth": "^1.5.2"
Copy link
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

@@ -36,15 +37,17 @@
"league/oauth2-instagram": "@stable",
"league/oauth2-google": "@stable",
"league/oauth2-linkedin": "@stable",
"google/recaptcha": "@stable"
"google/recaptcha": "@stable",
"robthree/twofactorauth": "^1.5.2"
Copy link
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
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
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
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?

'plugin' => 'CakeDC/Users',
'controller' => 'Users',
'action' => 'verify',
'prefix' => null,
Copy link
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...

@@ -32,6 +32,7 @@ class UsersAuthComponent extends Component
const EVENT_BEFORE_LOGOUT = 'Users.Component.UsersAuth.beforeLogout';
const EVENT_AFTER_LOGOUT = 'Users.Component.UsersAuth.afterLogout';


Copy link
Member

Choose a reason for hiding this comment

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

phpcs

}

if ($this->request->is('post')) {
$verificationCode = $this->request->data['code'];
Copy link
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

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

$codeVerified = $this->GoogleAuthenticator->verifyCode($user['secret'], $verificationCode);
Copy link
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


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

$this->redirect($url);
Copy link
Member

Choose a reason for hiding this comment

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

use return


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

$this->redirect(Configure::read('Auth.loginAction'));
Copy link
Member

Choose a reason for hiding this comment

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

use return

<?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
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

Coverage Status

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

@coveralls
Copy link

Coverage Status

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

@coveralls
Copy link

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