site.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. use oxusmedia\webApp\controller;
  3. use oxusmedia\webApp\MyWebauthn;
  4. use oxusmedia\webApp\form;
  5. use oxusmedia\webApp\column;
  6. use oxusmedia\webApp\input;
  7. use oxusmedia\webApp\password;
  8. use oxusmedia\webApp\button;
  9. class site extends controller
  10. {
  11. const LOGIN_METHOD_PASSWORD = 'P';
  12. const LOGIN_METHOD_WEBAUTHN = 'W';
  13. public function index()
  14. {
  15. $this->webApp()->requireLoginRedir();
  16. $this->titulo = 'Bienvenido';
  17. $this->render('index');
  18. }
  19. public function login()
  20. {
  21. if ($this->webApp()->isLoggedIn())
  22. $this->redirect($this->webApp()->getSite());
  23. else {
  24. $form = new form('login', array(
  25. new column(array(
  26. ($this->webApp()->getConfig('LOGIN_WITH_EMAIL')
  27. ? new input('email', array(
  28. 'rules' => array(
  29. 'required' => true,
  30. 'email' => true
  31. ),
  32. 'htmlOptions' => array(
  33. 'placeholder' => 'Email',
  34. 'class' => 'step1'
  35. ),
  36. 'value' => $_COOKIE['login_email'] ?? ''
  37. ))
  38. : new input('usuario', array(
  39. 'rules' => array(
  40. 'required' => true
  41. ),
  42. 'htmlOptions' => array(
  43. 'placeholder' => 'Usuario',
  44. 'class' => 'step1'
  45. )
  46. ))
  47. ),
  48. new password('pass', array(
  49. 'htmlOptions' => array(
  50. 'placeholder' => 'Contraseña',
  51. 'class' => 'step2'
  52. )
  53. ))
  54. ))
  55. ), array(
  56. 'buttons' => array(
  57. new button('siguiente', button::SUBMIT, button::PRIMARY),
  58. new button('cancelar', button::BUTTON, button::SECONDARY, array(
  59. 'htmlOptions' => array(
  60. 'class' => 'step2'
  61. )
  62. ))
  63. )
  64. ));
  65. if (isset($_POST['login'])) {
  66. $form->setAtributes($_POST['login']);
  67. if ($form->validate()) {
  68. $param = $form->getAtributes();
  69. if ($this->webApp()->login($param['email'], $param['pass'])) {
  70. $this->webApp()->setCookie('login_email', $param['email']);
  71. $this->webApp()->setCookie('login_method', $this::LOGIN_METHOD_PASSWORD);
  72. $this->redirect($this->getMethodUrl('registerWebauthn'));
  73. return;
  74. }
  75. }
  76. }
  77. $this->titulo = 'Iniciar sesión';
  78. MyWebauthn::initialize();
  79. $this->addCss($this->webApp()->getUrlAssets() . 'css/login.css');
  80. $this->addJs($this->webApp()->getUrlAssets() . 'webapp/js/jquery.validate.min.js');
  81. $this->addJs($this->webApp()->getUrlAssets() . 'webapp/js/additional-methods.min.js');
  82. $this->addJs($this->webApp()->getUrlAssets() . 'webapp/js/jquery.validate.messages_es_AR.js');
  83. $this->render('login', array(
  84. 'method' => $_COOKIE['login_method'] ?? $this::LOGIN_METHOD_PASSWORD,
  85. 'form' => $form
  86. ));
  87. }
  88. }
  89. public function loginWebauthnStep1()
  90. {
  91. if (isset($_GET['email']))
  92. $this->returnJson(
  93. MyWebauthn::loginStep1($_GET['email'])
  94. );
  95. }
  96. public function loginWebauthnStep2()
  97. {
  98. $post = trim(file_get_contents('php://input'));
  99. if ($post and isset($_GET['email'])) {
  100. $return = MyWebauthn::loginStep2($post, $_GET['email']);
  101. if ($return->success) {
  102. $this->webApp()->setCookie('login_email', $_GET['email']);
  103. $this->webApp()->setCookie('login_method', $this::LOGIN_METHOD_WEBAUTHN);
  104. }
  105. $this->returnJson($return);
  106. }
  107. }
  108. public function registerWebauthn()
  109. {
  110. $this->titulo = 'Bienvenido';
  111. MyWebauthn::initialize();
  112. $this->addJs($this->webApp()->getUrlAssets() . 'webapp/js/jquery.validate.min.js');
  113. $this->addJs($this->webApp()->getUrlAssets() . 'webapp/js/additional-methods.min.js');
  114. $this->addJs($this->webApp()->getUrlAssets() . 'webapp/js/jquery.validate.messages_es_AR.js');
  115. $form = new form('register', array(
  116. new column(array(
  117. new input('dispositivo', array(
  118. 'label' => '',
  119. 'htmlOptions' => array(
  120. 'placeholder' => 'Dispositivo'
  121. )
  122. ))
  123. ))
  124. ), array(
  125. 'buttons' => array(
  126. new button('siguiente', button::SUBMIT, button::PRIMARY),
  127. new button('cancelar', button::BUTTON, button::SECONDARY)
  128. )
  129. ));
  130. $this->render('webauthn', array(
  131. 'form' => $form
  132. ));
  133. }
  134. public function registerWebauthnStep1()
  135. {
  136. $this->webApp()->requireLogin();
  137. $this->returnJson(
  138. MyWebauthn::registerStep1($this->webApp()->getUsuarioId())
  139. );
  140. }
  141. public function registerWebauthnStep2()
  142. {
  143. $this->webApp()->requireLogin();
  144. $post = trim(file_get_contents('php://input'));
  145. if ($post) {
  146. $return = MyWebauthn::registerStep2($post, $_GET['device']);
  147. if ($return->success)
  148. $this->webApp()->setCookie('login_method', $this::LOGIN_METHOD_WEBAUTHN);
  149. $this->returnJson($return);
  150. }
  151. }
  152. public function logout()
  153. {
  154. $this->webApp()->logout();
  155. $this->redirect($this->getMethodUrl('login'));
  156. }
  157. public function downloadCertificates()
  158. {
  159. $return = MyWebauthn::downloadFidoCertificates();
  160. $this->returnJson($return);
  161. }
  162. }