Skip to content
This repository was archived by the owner on Nov 4, 2024. It is now read-only.

Commit c9d3f3d

Browse files
author
the-djmaze
committed
Example haveibeenpwned integration
1 parent 027ce6c commit c9d3f3d

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

plugins/haveibeenpwned/hibp.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
(doc => {
2+
3+
addEventListener('rl-view-model.create', event => {
4+
if ('SettingsSecurity' === event.detail.viewModelTemplateID) {
5+
const template = doc.getElementById('SettingsSecurity'),
6+
details = doc.createElement('details'),
7+
summary = doc.createElement('summary'),
8+
button = doc.createElement('button');
9+
summary.textContent = "Have i been pwned?"
10+
button.dataset.bind = "click:HibpCheck";
11+
button.textContent = "Check";
12+
details.append(summary, button);
13+
template.content.append(details);
14+
15+
event.detail.HibpCheck = () => {
16+
// JsonHibpCheck
17+
rl.pluginRemoteRequest((iError, oData) => {
18+
console.dir({iError, oData});
19+
}, 'HibpCheck');
20+
21+
};
22+
}
23+
});
24+
25+
})(document);

plugins/haveibeenpwned/index.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)