captcha.php
1 year ago
common.php
1 year ago
dashboard.php
1 year ago
disable-access-system-file.php
7 months ago
disable-author-query.php
1 year ago
disable-login.php
1 year ago
disable-restapi.php
1 year ago
disable-xmlrpc.php
1 year ago
login-log-table.php
1 year ago
login-log.php
1 year ago
login-notification.php
1 year ago
rename-login-page.php
1 year ago
restrict-admin-page.php
1 year ago
server-error-notification.php
1 year ago
server-error-table.php
1 year ago
two-factor-authentication-registration.php
5 months ago
two-factor-authentication.php
1 year ago
unify-messages.php
1 year ago
update-notice.php
1 year ago
waf-table.php
1 year ago
waf.php
7 months ago
two-factor-authentication-registration.php
231 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | class CloudSecureWP_Admin_Two_Factor_Authentication_Registration extends CloudSecureWP_Admin_Common { |
| 8 | private $two_factor_authentication; |
| 9 | /** |
| 10 | * 管理画面にレンダリングするキー |
| 11 | * |
| 12 | * @var string |
| 13 | */ |
| 14 | private $default_key; |
| 15 | /** |
| 16 | * 秘密鍵が登録済みかどうか |
| 17 | * |
| 18 | * @var bool |
| 19 | */ |
| 20 | private $is_registered; |
| 21 | |
| 22 | function __construct( array $info, CloudSecureWP_Two_Factor_Authentication $two_factor_authentication ) { |
| 23 | parent::__construct( $info ); |
| 24 | $this->two_factor_authentication = $two_factor_authentication; |
| 25 | $this->prepare_view_data(); |
| 26 | $this->render(); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * 画面表示用のデータを準備 |
| 31 | */ |
| 32 | public function prepare_view_data(): void { |
| 33 | $stored_secret = get_user_option( 'cloudsecurewp_two_factor_authentication_secret' ); |
| 34 | $this->is_registered = ! empty( $stored_secret ); |
| 35 | $this->default_key = ''; |
| 36 | if ( ! empty( $_POST ) && check_admin_referer( $this->two_factor_authentication->get_feature_key() . '_csrf' ) ) { |
| 37 | if ( ! empty( $_POST['key'] ) ) { |
| 38 | $this->default_key = sanitize_text_field( $_POST['key'] ); |
| 39 | } |
| 40 | if ( empty( $_POST['key'] ) ) { |
| 41 | $this->errors[] = 'セットアップキーを保存できませんでした。<br />セットアップキーを生成してください。'; |
| 42 | |
| 43 | return; |
| 44 | } |
| 45 | if ( empty( $_POST['google_authenticator_code'] ) ) { |
| 46 | $this->errors[] = 'セットアップキーを保存できませんでした。<br />QRコードをGoogle Authenticator アプリケーションでスキャンし、認証コードを� |
| 47 | �力してください。'; |
| 48 | |
| 49 | return; |
| 50 | } |
| 51 | $key = sanitize_text_field( $_POST['key'] ); |
| 52 | $google_authenticator_code = sanitize_text_field( $_POST['google_authenticator_code'] ); |
| 53 | if ( CloudSecureWP_Time_Based_One_Time_Password::verify_code( $key, $google_authenticator_code, 2 ) ) { |
| 54 | update_user_option( get_current_user_id(), 'cloudsecurewp_two_factor_authentication_secret', $key ); |
| 55 | $this->messages[] = 'セットアップキーを保存しました。'; |
| 56 | // 保存後は秘密鍵を非表示にする |
| 57 | $this->default_key = ''; |
| 58 | $this->is_registered = true; |
| 59 | } else { |
| 60 | $this->errors[] = 'セットアップキーを保存できませんでした。<br />認証コードが間違っています。'; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * ディスクリプション |
| 67 | */ |
| 68 | protected function admin_description(): void { |
| 69 | ?> |
| 70 | <div class="title-block mb-12"> |
| 71 | <h1 class="title-block-title">デバイス登録 - 2段階認証</h1> |
| 72 | <p class="title-block-small-text">この機能のマニュアルは<a class="title-block-link" target="_blank" |
| 73 | href="https://wpplugin.cloudsecure.ne.jp/cloudsecure_wp_security/two_factor_authentication.php">こちら</a> |
| 74 | </p> |
| 75 | </div> |
| 76 | <div class="title-bottom-text"> |
| 77 | 2段階認証機能をアクティブにするには、セットアップキーを生成し、<a class="title-block-link" href="https://play.google.com/store/apps/details?id=com.google.android.apps.authenticator2" target="_blank">Google Authenticator</a> アプリケーションでQRコードを読み込んでください。<br /> |
| 78 | QRコードを読み込めない場合、<a class="title-block-link" href="https://play.google.com/store/apps/details?id=com.google.android.apps.authenticator2" target="_blank">Google Authenticator</a> アプリケーションにセットアップキーを� |
| 79 | �力してください。 |
| 80 | </div> |
| 81 | <?php |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * ページコンテンツ |
| 86 | */ |
| 87 | protected function page(): void { |
| 88 | ?> |
| 89 | <form method="post"> |
| 90 | <div class="box"> |
| 91 | <div class="box-bottom"> |
| 92 | <div class="box-row flex-start"> |
| 93 | <div class="box-row-title not-label pt-12"> |
| 94 | <label for="key">設定状況</label> |
| 95 | </div> |
| 96 | <div class="box-row-content"> |
| 97 | <div class="flex"> |
| 98 | <?php if ( $this->is_registered ) : ?> |
| 99 | <p>設定済</p> |
| 100 | <?php else : ?> |
| 101 | <p>未設定</p> |
| 102 | <?php endif; ?> |
| 103 | </div> |
| 104 | </div> |
| 105 | </div> |
| 106 | <div class="box-row flex-start"> |
| 107 | <div class="box-row-title not-label pt-12"> |
| 108 | <label for="key">セットアップキー</label> |
| 109 | </div> |
| 110 | <div class="box-row-content"> |
| 111 | <div class="flex"> |
| 112 | <input type="text" id="key" name="key" |
| 113 | value="<?php echo esc_attr( $this->default_key ); ?>" maxlength="16" |
| 114 | readonly/> |
| 115 | <button id="generate_key" type="button" class="button button-large"> |
| 116 | セットアップキーを生成 |
| 117 | </button> |
| 118 | </div> |
| 119 | <div id="qrcode_container" hidden> |
| 120 | <div id="qrcode"></div> |
| 121 | <div class="flex onetime-password-container"> |
| 122 | <label for="google_authenticator_code">認証コード:</label> |
| 123 | <input type="text" id="google_authenticator_code" name="google_authenticator_code" |
| 124 | maxlength="6"/> |
| 125 | </div> |
| 126 | </div> |
| 127 | </div> |
| 128 | </div> |
| 129 | </div> |
| 130 | </div> |
| 131 | <div id="submit-btn-area"> |
| 132 | <?php $this->nonce_wp( $this->two_factor_authentication->get_feature_key() ); ?> |
| 133 | <p id="guide_message" hidden>アプリケーションに表示された6桁の認証コードを� |
| 134 | �力し、「変更を保存」ボタンをクリックしてください。</p> |
| 135 | <?php $this->submit_button_wp(); ?> |
| 136 | </div> |
| 137 | </form> |
| 138 | <style> |
| 139 | #generate_key { |
| 140 | margin-left: 16px; |
| 141 | } |
| 142 | |
| 143 | #qrcode { |
| 144 | margin: 32px 32px 16px; |
| 145 | } |
| 146 | |
| 147 | .onetime-password-container { |
| 148 | margin-left: -20px; |
| 149 | } |
| 150 | </style> |
| 151 | <script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js" |
| 152 | type="text/javascript"></script> |
| 153 | <script type="text/javascript"> |
| 154 | function quintetCount(buff) { |
| 155 | const quintets = Math.floor(buff.length / 5); |
| 156 | return buff.length % 5 === 0 ? quintets : quintets + 1; |
| 157 | } |
| 158 | |
| 159 | function bufferToBase32(plain) { |
| 160 | let i = 0; |
| 161 | let j = 0; |
| 162 | let shiftIndex = 0; |
| 163 | let digit = 0; |
| 164 | const charTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; |
| 165 | const encoded = new Array(quintetCount(plain) * 8); |
| 166 | |
| 167 | /* byte by byte は、quintet by quintet ほどきれいではありませんが、テストは少し速くなります。 再訪する� |
| 168 | 要があります。 */ |
| 169 | while (i < plain.length) { |
| 170 | const current = plain[i]; |
| 171 | |
| 172 | if (shiftIndex > 3) { |
| 173 | digit = current & (0xff >> shiftIndex); |
| 174 | shiftIndex = (shiftIndex + 5) % 8; |
| 175 | digit = (digit << shiftIndex) | ((i + 1 < plain.length) ? |
| 176 | plain[i + 1] : 0) >> (8 - shiftIndex); |
| 177 | i++; |
| 178 | } else { |
| 179 | digit = (current >> (8 - (shiftIndex + 5))) & 0x1f; |
| 180 | shiftIndex = (shiftIndex + 5) % 8; |
| 181 | if (shiftIndex === 0) { |
| 182 | i++; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | encoded[j] = charTable[digit]; |
| 187 | j++; |
| 188 | } |
| 189 | |
| 190 | for (i = j; i < encoded.length; i++) { |
| 191 | encoded[i] = '='; |
| 192 | } |
| 193 | |
| 194 | return encoded.join(''); |
| 195 | } |
| 196 | |
| 197 | function generateKey() { |
| 198 | return bufferToBase32(crypto.getRandomValues(new Uint8Array(10))) |
| 199 | } |
| 200 | |
| 201 | const qrcode = new QRCode('qrcode', {correctLevel: QRCode.CorrectLevel.M}) |
| 202 | |
| 203 | function showQRCode(key) { |
| 204 | document.getElementById('qrcode_container').hidden = false |
| 205 | document.getElementById('guide_message').hidden = false |
| 206 | const name = document.querySelector('.display-name').textContent |
| 207 | |
| 208 | // https://github.com/google/google-authenticator/wiki/Key-Uri-Format |
| 209 | qrcode.makeCode(`otpauth://totp/${encodeURIComponent(name)}?secret=${key}&issuer=${location.hostname}`) |
| 210 | |
| 211 | document.getElementById('google_authenticator_code').focus() |
| 212 | } |
| 213 | |
| 214 | // ページ読み込み時に秘密鍵が� |
| 215 | �力されている場合はQRコードを表示 |
| 216 | // (認証コードエラー時の再表示のため) |
| 217 | const secretKey = document.getElementById('key').value |
| 218 | if (secretKey) { |
| 219 | showQRCode(secretKey) |
| 220 | } |
| 221 | |
| 222 | document.getElementById('generate_key').addEventListener('click', () => { |
| 223 | const newKey = generateKey() |
| 224 | document.getElementById('key').value = newKey |
| 225 | showQRCode(newKey) |
| 226 | }) |
| 227 | </script> |
| 228 | <?php |
| 229 | } |
| 230 | } |
| 231 |