Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. diff --git a/mysite/code/CustomAuthenticator.php b/mysite/code/CustomAuthenticator.php
  2. new file mode 100644
  3. index 0000000..141a9f1
  4. --- /dev/null
  5. +++ b/mysite/code/CustomAuthenticator.php
  6. @@ -0,0 +1,14 @@
  7. +<?php
  8. +
  9. +use SilverStripe\Security\MemberAuthenticator\Authenticator;
  10. +
  11. +class CustomAuthenticator extends Authenticator
  12. +{
  13. + /**
  14. + * @inherit
  15. + */
  16. + public function getLoginHandler($link)
  17. + {
  18. + return CustomLoginHandler::create($link, $this);
  19. + }
  20. +}
  21. diff --git a/mysite/code/CustomLoginHandler.php b/mysite/code/CustomLoginHandler.php
  22. new file mode 100644
  23. index 0000000..dbfbca3
  24. --- /dev/null
  25. +++ b/mysite/code/CustomLoginHandler.php
  26. @@ -0,0 +1,76 @@
  27. +<?php
  28. +
  29. +use SilverStripe\Security\MemberAuthenticator\LoginHandler;
  30. +use SilverStripe\Security\Member;
  31. +use SilverStripe\Control\Session;
  32. +use SilverStripe\Forms\Form;
  33. +use SilverStripe\Forms\FormAction;
  34. +use SilverStripe\Forms\FieldList;
  35. +use SilverStripe\Forms\TextField;
  36. +
  37. +use SilverStripe\Dev\Debug;
  38. +
  39. +class CustomLoginHandler extends LoginHandler
  40. +{
  41. +
  42. + private static $allowed_actions = [
  43. + 'step2',
  44. + 'secondStepForm',
  45. + ];
  46. +
  47. + public function doLogin($data, $formHandler)
  48. + {
  49. + if ($member = $this->checkLogin($data)) {
  50. + Session::set('CustomLoginHandler.MemberID', $member->ID);
  51. + Session::set('CustomLoginHandler.Data', $data);
  52. + return $this->redirect($this->link('step2'));
  53. + }
  54. +
  55. + // Fail to login redirects back to form
  56. + return $this->redirectBack();
  57. + }
  58. +
  59. + public function step2()
  60. + {
  61. + return [
  62. + "Form" => $this->secondStepForm()
  63. + ];
  64. + }
  65. +
  66. + public function secondStepForm()
  67. + {
  68. + return new Form(
  69. + $this,
  70. + "secondStepForm",
  71. + new FieldList(
  72. + new TextField('SecondFactor', 'Your 2FA (12345)')
  73. + ),
  74. + new FieldList(
  75. + new FormAction('completeSecondStep', 'Login in')
  76. + )
  77. + );
  78. + }
  79. +
  80. + public function completeSecondStep($data)
  81. + {
  82. + if ($this->checkSecondFactor($data)) {
  83. + $memberID = Session::get('CustomLoginHandler.MemberID');
  84. + $member = Member::get()->byID($memberID);
  85. + $data = Session::get('CustomLoginHandler.Data');
  86. + if (!$member) {
  87. + return $this->redirectBack();
  88. + }
  89. + $this->performLogin($member, $data);
  90. + return $this->redirectAfterSuccessfulLogin();
  91. + }
  92. +
  93. +
  94. + // Fail to login redirects back to form
  95. + return $this->redirectBack();
  96. + }
  97. +
  98. + protected function checkSecondFactor($data)
  99. + {
  100. + return $data['SecondFactor'] === '12345';
  101. + }
  102. +}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement