Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 12 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| keys | |
0.00% |
0 / 12 |
|
0.00% |
0 / 2 |
42 | |
0.00% |
0 / 1 |
| get | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
| public_b64u | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | // This file is part of Moodle - https://moodle.org/ |
| 3 | // |
| 4 | // Moodle is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // Moodle is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with Moodle. If not, see <https://www.gnu.org/licenses/>. |
| 16 | |
| 17 | namespace message_webpush\local; |
| 18 | |
| 19 | /** |
| 20 | * The site's VAPID key pair (RFC 8292), made once and kept in the plugin config. |
| 21 | * |
| 22 | * @package message_webpush |
| 23 | * @copyright 2026 DevLMS |
| 24 | * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 25 | */ |
| 26 | class keys { |
| 27 | /** |
| 28 | * The key pair, made on first use. |
| 29 | * |
| 30 | * @return array|null ['public' => 65 raw bytes, 'private' => PEM], or null when PHP OpenSSL cannot make P-256 keys |
| 31 | */ |
| 32 | public static function get(): ?array { |
| 33 | $public = (string)get_config('message_webpush', 'vapidpublic'); |
| 34 | $private = (string)get_config('message_webpush', 'vapidprivate'); |
| 35 | if ($public !== '' && $private !== '') { |
| 36 | return ['public' => crypto::b64u_decode($public), 'private' => $private]; |
| 37 | } |
| 38 | $keys = crypto::generate_keys(); |
| 39 | if ($keys === null) { |
| 40 | return null; |
| 41 | } |
| 42 | set_config('vapidpublic', crypto::b64u_encode($keys['public']), 'message_webpush'); |
| 43 | set_config('vapidprivate', $keys['private'], 'message_webpush'); |
| 44 | return $keys; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * The public key as browsers want it for applicationServerKey. |
| 49 | * |
| 50 | * @return string base64url, or '' when there are no keys |
| 51 | */ |
| 52 | public static function public_b64u(): string { |
| 53 | $keys = self::get(); |
| 54 | return $keys ? crypto::b64u_encode($keys['public']) : ''; |
| 55 | } |
| 56 | } |