mflor/pwned

一个干净简单的PHP库,用于与HaveIBeenPwned.com的所有API端点交互

4.0.0 2022-05-14 09:21 UTC

This package is auto-updated.

Last update: 2024-09-14 14:18:39 UTC


README

Build Status Coverage Status Total Downloads Latest Version License

一个干净简单的PHP库,用于与HaveIBeenPwned.com的所有 API端点交互

此包将整个HaveIBeenPwned API封装在一个简单易用的PHP库中,可以在任何项目中使用。

许多其他封装API的包,要么是打算在框架中使用,要么只是封装了密码检查器。

安装

使用composer安装库

composer require mflor/pwned

授权

需要对所有启用通过电子邮件地址搜索HIBP的API进行授权,即检索账户的所有违规记录检索账户的所有粘贴。需要HIBP订阅密钥才能进行授权调用,可以在API密钥页面上获得。

设置

<?php

// Require composers autoloader
require_once './vendor/autoload.php';

// Initiate a new instance of the Pwned class
// It can be instantiated without an API key
// but then account-specific braches and pastes
// will result in unauthorized exceptions.
$pwned = new \MFlor\Pwned\Pwned($apiKey = null);

用法

违规记录

// Get all breaches
$pwned->breaches()->getAll();
// Returns an array of Breach Models (see MFlor/Pwned/Models/Breach.php)

// Get all breaches by a specific domain
$pwned->breaches()->byDomain('adobe.com');
// Returns an array of Breach Models (see MFlor/Pwned/Models/Breach.php)

// Get a breach by its name
$pwned->breaches()->byName('Adobe');
// Returns a Breach Model (see MFlor/Pwned/Models/Breach.php)

// Get breaches by account (Requires API key)
$pwned->breaches()->byAccount('test@example.com');
// Returns an array of Breach Models (see MFlor/Pwned/Models/Breach.php)

// Options for breaches by account:
$options = [
    'truncateResponse' => true // Show full breach or just the name (Default: true)
    'domain' => 'adobe.com' // Filter results by a domain (Default: null)
    'includeUnverified' => true // Include unverified breaches (Default: false)
];
$pwned->breaches()->byAccount('test@example.com', $options);

// Get all data classes
$pwned->breaches()->getDataClasses();
// ["Account balances","Address book contacts","Age groups","Ages"...]

粘贴

// Get all pastes by account (Requires API key)
$pwned->pastes()->byAccount('test@example.com')
// Returns an array of Paste Models (see MFlor/Pwned/Models/Paste.php)

密码

// Search for passwords (By the first five characters of a sha1 hash)
$pwned->passwords()->search('e38ad');
// Returns an array of Password Models (see MFlor/Pwned/Models/Password.php)

$pwned->passwords()->occurrences('password1');
// Returns the number occurrences of the given password has been found in leaks

搜索和发生情况都接受一个布尔参数,用于禁用请求的填充。请注意,这比启用填充(默认)要安全得多。有关填充的更多信息,请参阅Troy Hunt的博客文章

异常

如果发生客户端错误,此包将抛出自定义异常。

在状态码400时抛出\Mflor\Pwned\Exceptions\BadRequestException

在状态码401时抛出\Mflor\Pwned\Exceptions\UnauthorizedException

在状态码403时抛出\Mflor\Pwned\Exceptions\ForbiddenException

在状态码404时抛出\Mflor\Pwned\Exceptions\NotFoundException

在状态码429时抛出\Mflor\Pwned\Exceptions\TooManyRequestsException

在状态码503时抛出\Mflor\Pwned\Exceptions\ServiceUnavailableException

测试

您可以通过执行以下任一命令来运行所有测试

$ composer test
$ ./vendor/bin/phpunit
# If you already have phpunit installed globally
$ phpunit

许可证

MIT