|
| 1 | +<?php |
| 2 | +namespace ProcessWire; |
| 3 | + |
| 4 | +/** |
| 5 | + * TfaWebAuthn (1.0.0) |
| 6 | + * Adds WebAuthn/FIDO2 as a TFA option |
| 7 | + * |
| 8 | + * @author Adam Blunt |
| 9 | + * |
| 10 | + * ProcessWire 3.x |
| 11 | + * Copyright (C) 2011 by Ryan Cramer |
| 12 | + * Licensed under GNU/GPL v2, see LICENSE.TXT |
| 13 | + * |
| 14 | + * http://www.processwire.com |
| 15 | + * http://www.ryancramer.com |
| 16 | + * |
| 17 | + */ |
| 18 | +require_once 'WebAuthn/WebAuthn.php'; |
| 19 | +//use lbuchs\WebAuthn\Binary\ByteBuffer; |
| 20 | +class TfaWebAuthn extends Tfa implements Module, ConfigurableModule |
| 21 | +{ |
| 22 | + public function __construct() |
| 23 | + { |
| 24 | + parent::__construct(); |
| 25 | + $this->WebAuthn = new \lbuchs\WebAuthn\WebAuthn('WebAuthn Library', $this->wire('config')->httpHost); // $formats |
| 26 | + $this->userVerification = "discouraged"; |
| 27 | + $this->crossPlatformAttachment = null; // True only crossplatfrm aka USB keys, False only single device like TPM/Winows Hellow, Null allow both |
| 28 | + $this->typeUsb = true; |
| 29 | + $this->typeNfc = true; |
| 30 | + $this->typeBle = true; |
| 31 | + $this->typeInt = true; |
| 32 | + } |
| 33 | + |
| 34 | + public function init() |
| 35 | + { |
| 36 | + if(config()->version('3.0.165')) { |
| 37 | + // 3.0.165 and newer have an init() function in the Tfa class. previous versions did not |
| 38 | + // Strangely this init() function while not needed before is now required in new versions even if you dont use the auto-enable feature |
| 39 | + parent::init(); |
| 40 | + } |
| 41 | + $this->addHookBefore('Tfa::getUserSettingsInputfields', $this, 'addScripts'); |
| 42 | + $this->addHookBefore('Tfa::render', $this, 'addScripts'); |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + public function addScripts() |
| 47 | + { |
| 48 | + $this->config->scripts->add($this->wire('config')->urls->$this . "WebAuthn.js"); |
| 49 | + } |
| 50 | + |
| 51 | + public function enabledForUser(User $user, array $settings) |
| 52 | + { |
| 53 | + return $settings['enabled'] === true; |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + public function isValidUserCode(User $user, $code, array $settings) |
| 58 | + { |
| 59 | + if (!strlen($code)) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + $authreg = $this->session->authreg; |
| 64 | + $authreq = $this->session->authreq; |
| 65 | + $this->session->authreq = null; |
| 66 | + $this->session->authreg = null; |
| 67 | + |
| 68 | + $code = json_decode($code); |
| 69 | + |
| 70 | + |
| 71 | + $clientDataJSON = base64_decode($code->clientDataJSON); |
| 72 | + $authenticatorData = base64_decode($code->authenticatorData); |
| 73 | + $signature = base64_decode($code->signature); |
| 74 | + $userHandle = base64_decode($code->userHandle); |
| 75 | + $id = base64_decode($code->id); |
| 76 | + $challenge = $this->session->authchallange; |
| 77 | + $credentialPublicKey = null; |
| 78 | + // Get the public key for the given credential |
| 79 | + if (is_array($authreg)) { |
| 80 | + foreach ($authreg as $reg) { |
| 81 | + $o = (object) $reg; |
| 82 | + if ($o->credentialId === bin2hex($id)) { |
| 83 | + $credentialPublicKey = $o->credentialPublicKey; |
| 84 | + break; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + try { |
| 90 | + if ($credentialPublicKey === null) { |
| 91 | + exit('Public Key for credential ID not found!'); |
| 92 | + } |
| 93 | + // process the get request. throws WebAuthnException if it fails |
| 94 | + $this->WebAuthn->processGet($clientDataJSON, $authenticatorData, $signature, $credentialPublicKey, $challenge, null, $this->userVerification === 'required'); |
| 95 | + return true; |
| 96 | + } catch (Exception $e) { |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + // Create settings page for server |
| 105 | + public function ___getUserSettingsInputfields(User $user, InputfieldWrapper $fieldset, $settings) |
| 106 | + { |
| 107 | + parent::___getUserSettingsInputfields($user, $fieldset, $settings); |
| 108 | + |
| 109 | + if ($this->enabledForUser($user, $settings)) { |
| 110 | + } elseif ($this->wire('input')->requestMethod('POST')) { |
| 111 | + $fieldset->new('text', 'regdata') |
| 112 | + ->attr('maxlength', 20480); |
| 113 | + } else { |
| 114 | + $createArgs = $this->WebAuthn->getCreateArgs(\hex2bin($user->id), $user->name, $user->name, 20, false, $this->userVerification, $this->crossPlatformAttachment); |
| 115 | + $fieldset->new('hidden', 'createData') |
| 116 | + ->attr('id', 'TfaWebAuthn_createData') |
| 117 | + ->attr('value', json_encode($createArgs)); |
| 118 | + |
| 119 | + $fieldset->new('hidden', 'regdata') |
| 120 | + ->attr('id', 'TfaWebAuthn_regdata') |
| 121 | + ->attr('maxlength', 20480); |
| 122 | + |
| 123 | + $fieldset->new('button', 'addKey', 'Enable two-factor authentication') |
| 124 | + ->attr('id', 'TfaWebAuthn_button') |
| 125 | + ->attr('onclick', "TfaWebAuthn_addkey()") |
| 126 | + ->value("Add WebAuthn Credential"); |
| 127 | + |
| 128 | + $fieldset->new('markup') |
| 129 | + ->attr('value', "<span id='TfaWebAuthn_msg'>Click the button to register a new credential.</span>"); |
| 130 | + $_SESSION["chal"] = (string) $this->WebAuthn->getChallenge(); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + // Save registration data from the User settings |
| 135 | + public function ___processUserSettingsInputfields(User $user, InputfieldWrapper $fieldset, $settings, $settingsPrev) |
| 136 | + { |
| 137 | + $settings = parent::___processUserSettingsInputfields($user, $fieldset, $settings, $settingsPrev); |
| 138 | + try { |
| 139 | + $challenge = $_SESSION["chal"]; |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + $aryreg = json_decode("[".$settings['regdata']."]"); |
| 144 | + $data = array(); |
| 145 | + foreach ($aryreg as $reg) { |
| 146 | + |
| 147 | + $clientdata = base64_decode($reg->clientDataJSON); |
| 148 | + $attestationObject = base64_decode($reg->attestationObject); |
| 149 | + $d = $this->WebAuthn->processCreate($clientdata, $attestationObject, $challenge, $this->userVerification === 'required', true, false); |
| 150 | + array_push($data, (array) $d ); |
| 151 | + } |
| 152 | + $sd = serialize($data); |
| 153 | + $settings['regkeys'] = $sd; |
| 154 | + $settings['enabled'] = true; |
| 155 | + $settings['challenge'] = null; |
| 156 | + $settings['regdata'] = null; |
| 157 | + $this->message("Success! Your account is now secured with two-factor authentication"); |
| 158 | + } catch (Exception $e) { |
| 159 | + $settings['enabled'] = false; |
| 160 | + $this->error("That did not work " . $e); |
| 161 | + } |
| 162 | + |
| 163 | + return $settings; |
| 164 | + } |
| 165 | + |
| 166 | + |
| 167 | + protected function getDefaultUserSettings(User $user) |
| 168 | + { |
| 169 | + return array( |
| 170 | + 'enabled' => false, |
| 171 | + 'regkeys' => '' |
| 172 | + ); |
| 173 | + } |
| 174 | + |
| 175 | + // Display the tFA form |
| 176 | + public function buildAuthCodeForm() |
| 177 | + { |
| 178 | + $user = $this->getUser(); |
| 179 | + $settings = $this->getUserSettings($user); |
| 180 | + |
| 181 | + $authreg = unserialize($settings['regkeys']); |
| 182 | + $ids = array(); |
| 183 | + $this->session->authreg = $authreg; |
| 184 | + |
| 185 | + |
| 186 | + $ids = array(); |
| 187 | + if (is_array($authreg)) { |
| 188 | + foreach ($authreg as $reg) { |
| 189 | + $o = (object) $reg; |
| 190 | + array_push($ids, $o->credentialId); |
| 191 | + } |
| 192 | + } |
| 193 | + if (count($ids) === 0) { |
| 194 | + throw new Exception('no WebAuthn registrations for userId ' . $user->id); |
| 195 | + } |
| 196 | + |
| 197 | + |
| 198 | + /* |
| 199 | + * Check if the authreq is null. if so make a new challenge |
| 200 | + * ProcessWire annoingly calls builtAuthCodeForm twice. once to build the form and a 2nd time when the user submits the form |
| 201 | + * thus validation was failing as the was non-matching challanges |
| 202 | + */ |
| 203 | + if (is_null($this->session->authreq)) { |
| 204 | + $req = $this->WebAuthn->getGetArgs($ids, 20, $this->typeUsb, $this->typeNfc, $this->typeBle, $this->typeInt, $this->userVerification); |
| 205 | + $this->session->authreq = json_encode($req); |
| 206 | + $this->session->authchallange = (string) $this->WebAuthn->getChallenge(); |
| 207 | + } |
| 208 | + |
| 209 | + |
| 210 | + |
| 211 | + $form = $this->modules->get('InputfieldForm'); |
| 212 | + $form->attr('action', "./?$this->keyName=" . $this->getSessionKey(true)) |
| 213 | + ->attr('id', "tfaform"); |
| 214 | + $form->new('markup') |
| 215 | + ->attr('value', "<img style='height:80px' onload='TfaWebAuthn_authKey()' src='" . $this->wire('config')->urls->$this . "assets\bluekeyside.png'><br> |
| 216 | + You need to use your security key to login.<br> |
| 217 | + Insert it now and tap/click the key to verify its you. If your key does not have a button just unplug it and then plug it back in.<br> |
| 218 | + <div uk-alert class='uk-alert-danger' style='display:none' id='TfaWebAuthn_error'></div>"); |
| 219 | + $form->new('hidden', 'authreq') |
| 220 | + ->attr('id', 'TfaWebAuthn_authreq') |
| 221 | + ->attr('value', $this->session->authreq); |
| 222 | + $form->new('hidden', 'tfa_code') |
| 223 | + ->attr('id', 'TfaWebAuthn_authresponse') |
| 224 | + ->attr('required', 'required'); |
| 225 | + $form->new('button', 'authKey', 'Start two-factor authentication') |
| 226 | + ->attr('id', 'TfaWebAuthn_button') |
| 227 | + ->attr('onclick', "TfaWebAuthn_authKey()") |
| 228 | + ->value("Use Security Key"); |
| 229 | + return $form; |
| 230 | + } |
| 231 | +} |
0 commit comments