|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * https://haveibeenpwned.com/API/v3 |
| 4 | + */ |
| 5 | + |
| 6 | +use RainLoop\Model\Account; |
| 7 | +use MailSo\Imap\ImapClient; |
| 8 | +use MailSo\Imap\Settings as ImapSettings; |
| 9 | +use MailSo\Sieve\SieveClient; |
| 10 | +use MailSo\Sieve\Settings as SieveSettings; |
| 11 | +use MailSo\Smtp\SmtpClient; |
| 12 | +use MailSo\Smtp\Settings as SmtpSettings; |
| 13 | +use MailSo\Mime\Message as MimeMessage; |
| 14 | + |
| 15 | +class HaveibeenpwnedPlugin extends \RainLoop\Plugins\AbstractPlugin |
| 16 | +{ |
| 17 | +// use \MailSo\Log\Inherit; |
| 18 | + |
| 19 | + const |
| 20 | + NAME = 'Have i been pwned', |
| 21 | + AUTHOR = 'SnappyMail', |
| 22 | + URL = 'https://snappymail.eu/', |
| 23 | + VERSION = '0.1', |
| 24 | + RELEASE = '2024-04-22', |
| 25 | + REQUIRED = '2.14.0', |
| 26 | + CATEGORY = 'General', |
| 27 | + LICENSE = 'MIT', |
| 28 | + DESCRIPTION = 'Check if your passphrase or email address is in a data breach'; |
| 29 | + |
| 30 | + public function Init() : void |
| 31 | + { |
| 32 | +// $this->UseLangs(true); |
| 33 | + $this->addJs('hibp.js'); |
| 34 | + $this->addJsonHook('HibpCheck'); |
| 35 | + } |
| 36 | + |
| 37 | + public function HibpCheck() |
| 38 | + { |
| 39 | +// $oAccount = $this->Manager()->Actions()->GetAccount(); |
| 40 | + $oAccount = $this->Manager()->Actions()->getAccountFromToken(); |
| 41 | +// $oAccount = \RainLoop\Api::Actions()->getAccountFromToken(); |
| 42 | + |
| 43 | + $HTTP = \SnappyMail\HTTP\Request::factory(); |
| 44 | + |
| 45 | + $breached = null; |
| 46 | + $api_key = \trim($this->Config()->Get('plugin', 'hibp-api-key', '')); |
| 47 | + if ($api_key) { |
| 48 | + $breached = $HTTP->doRequest('GET', "https://haveibeenpwned.com/api/v3/breachedaccount/{$oAccount->Email()}", null, [ |
| 49 | + 'hibp-api-key' => $api_key |
| 50 | + ]); |
| 51 | + } |
| 52 | + |
| 53 | + $pass = \sha1($oAccount->ImapPass()); |
| 54 | + $response = $HTTP->doRequest('GET', 'https://api.pwnedpasswords.com/range/' . \substr($pass, 0, 5)); |
| 55 | + $passwords = []; |
| 56 | + foreach (\preg_split('/\\R/', $response->body) as $entry) { |
| 57 | + if ($entry) { |
| 58 | + $entry = \explode(':', $entry); |
| 59 | + $passwords[$entry[0]] = (int) $entry[1]; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return $this->jsonResponse(__FUNCTION__, array( |
| 64 | + 'pwned' => isset($passwords[$pass]) ? $passwords[$pass] : 0, |
| 65 | +// 'passwords' => $passwords, |
| 66 | + 'breached' => $breached ? [ |
| 67 | + 'request_uri' => $breached->request_uri, |
| 68 | + 'final_uri' => $breached->final_uri, |
| 69 | + 'status' => $breached->status, |
| 70 | + 'headers' => $breached->headers, |
| 71 | + 'body' => $breached->body |
| 72 | + ] : [] |
| 73 | + )); |
| 74 | + } |
| 75 | + |
| 76 | + public function configMapping() : array |
| 77 | + { |
| 78 | + return [ |
| 79 | + \RainLoop\Plugins\Property::NewInstance("hibp-api-key") |
| 80 | + ->SetLabel('API key') |
| 81 | + ->SetDescription('https://haveibeenpwned.com/API/Key') |
| 82 | + ->SetType(\RainLoop\Enumerations\PluginPropertyType::STRING) |
| 83 | + ]; |
| 84 | + } |
| 85 | +} |
0 commit comments