2022-05-08 12:44:20 +02:00
|
|
|
// Package hibp provides Go binding to all 3 APIs of the "Have I Been Pwned" by Troy Hunt
|
2021-09-21 11:21:04 +02:00
|
|
|
package hibp
|
2021-09-19 18:10:12 +02:00
|
|
|
|
|
|
|
import (
|
2021-09-21 19:46:48 +02:00
|
|
|
"bytes"
|
2021-09-21 11:21:04 +02:00
|
|
|
"crypto/tls"
|
2022-12-22 11:55:56 +01:00
|
|
|
"errors"
|
2021-09-22 15:00:29 +02:00
|
|
|
"fmt"
|
2021-09-21 19:46:48 +02:00
|
|
|
"io"
|
2021-09-22 15:00:29 +02:00
|
|
|
"log"
|
2021-09-19 18:10:12 +02:00
|
|
|
"net/http"
|
2021-09-21 11:21:04 +02:00
|
|
|
"net/url"
|
2021-09-19 18:10:12 +02:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-09-21 11:21:04 +02:00
|
|
|
// Version represents the version of this package
|
2022-12-22 11:55:56 +01:00
|
|
|
const Version = "1.0.5"
|
2021-09-19 18:10:12 +02:00
|
|
|
|
2022-12-22 15:59:48 +01:00
|
|
|
// BaseURL is the base URL for the majority of API endpoints
|
2022-10-29 15:32:12 +02:00
|
|
|
const BaseURL = "https://haveibeenpwned.com/api/v3"
|
2021-09-21 18:21:23 +02:00
|
|
|
|
2022-12-22 15:59:48 +01:00
|
|
|
// PasswdBaseURL is the base URL for the pwned passwords API endpoints
|
|
|
|
const PasswdBaseURL = "https://api.pwnedpasswords.com"
|
|
|
|
|
2021-09-22 13:59:22 +02:00
|
|
|
// DefaultUserAgent defines the default UA string for the HTTP client
|
|
|
|
// Currently the URL in the UA string is comment out, as there is a bug in the HIBP API
|
|
|
|
// not allowing multiple slashes
|
2022-12-22 11:55:56 +01:00
|
|
|
const DefaultUserAgent = `go-hibp/` + Version + ` (+https://github.com/wneessen/go-hibp)`
|
|
|
|
|
|
|
|
// DefaultTimeout is the default timeout value for the HTTP client
|
|
|
|
const DefaultTimeout = time.Second * 5
|
|
|
|
|
|
|
|
// List of common errors
|
|
|
|
var (
|
|
|
|
// ErrNoAccountID is returned if no account ID is given to the corresponding API method
|
|
|
|
ErrNoAccountID = errors.New("no account ID given")
|
|
|
|
|
|
|
|
// ErrNoName is returned if no name is given to the corresponding API method
|
|
|
|
ErrNoName = errors.New("no name given")
|
|
|
|
|
|
|
|
// ErrNonPositiveResponse should be returned if a HTTP request failed with a non HTTP-200 status
|
|
|
|
ErrNonPositiveResponse = errors.New("non HTTP-200 response for HTTP request")
|
2022-12-22 15:59:48 +01:00
|
|
|
|
|
|
|
// ErrPrefixLengthMismatch should be used if a given prefix does not match the
|
|
|
|
// expected length
|
|
|
|
ErrPrefixLengthMismatch = errors.New("password hash prefix must be 5 characters long")
|
|
|
|
|
|
|
|
// ErrSHA1LengthMismatch should be used if a given SHA1 checksum does not match the
|
|
|
|
// expected length
|
|
|
|
ErrSHA1LengthMismatch = errors.New("SHA1 hash size needs to be 160 bits")
|
|
|
|
|
2023-02-09 17:07:20 +01:00
|
|
|
// ErrNTLMLengthMismatch should be used if a given NTLM hash does not match the
|
|
|
|
// expected length
|
|
|
|
ErrNTLMLengthMismatch = errors.New("NTLM hash size needs to be 128 bits")
|
|
|
|
|
2022-12-22 15:59:48 +01:00
|
|
|
// ErrSHA1Invalid should be used if a given string does not represent a valid SHA1 hash
|
|
|
|
ErrSHA1Invalid = errors.New("not a valid SHA1 hash")
|
2023-02-09 17:07:20 +01:00
|
|
|
|
|
|
|
// ErrNTLMInvalid should be used if a given string does not represent a valid NTLM hash
|
|
|
|
ErrNTLMInvalid = errors.New("not a valid NTLM hash")
|
|
|
|
|
|
|
|
// ErrUnsupportedHashMode should be used if a given hash mode is not supported
|
|
|
|
ErrUnsupportedHashMode = errors.New("hash mode not supported")
|
2022-12-22 11:55:56 +01:00
|
|
|
)
|
2021-09-22 13:59:22 +02:00
|
|
|
|
2021-09-21 11:21:04 +02:00
|
|
|
// Client is the HIBP client object
|
|
|
|
type Client struct {
|
2021-09-22 15:25:27 +02:00
|
|
|
hc *http.Client // HTTP client to perform the API requests
|
|
|
|
to time.Duration // HTTP client timeout
|
|
|
|
ak string // HIBP API key
|
|
|
|
ua string // User agent string for the HTTP client
|
|
|
|
|
|
|
|
// If set to true, the HTTP client will sleep instead of failing in case the HTTP 429
|
|
|
|
// rate limit hits a request
|
|
|
|
rlSleep bool
|
2021-09-19 18:10:12 +02:00
|
|
|
|
2022-10-29 15:32:12 +02:00
|
|
|
PwnedPassAPI *PwnedPassAPI // Reference to the PwnedPassAPI API
|
|
|
|
PwnedPassAPIOpts *PwnedPasswordOptions // Additional options for the PwnedPassAPI API
|
2021-09-21 18:21:23 +02:00
|
|
|
|
2022-10-29 15:32:12 +02:00
|
|
|
BreachAPI *BreachAPI // Reference to the BreachAPI
|
|
|
|
PasteAPI *PasteAPI // Reference to the PasteAPI
|
2021-09-21 11:21:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Option is a function that is used for grouping of Client options.
|
|
|
|
type Option func(*Client)
|
|
|
|
|
|
|
|
// New creates and returns a new HIBP client object
|
2021-09-22 15:46:36 +02:00
|
|
|
func New(options ...Option) Client {
|
|
|
|
c := Client{}
|
2021-09-21 11:21:04 +02:00
|
|
|
|
|
|
|
// Set defaults
|
2022-12-22 11:55:56 +01:00
|
|
|
c.to = DefaultTimeout
|
2023-02-09 17:07:20 +01:00
|
|
|
c.PwnedPassAPIOpts = &PwnedPasswordOptions{
|
|
|
|
HashMode: HashModeSHA1,
|
|
|
|
WithPadding: false,
|
|
|
|
}
|
2021-09-22 13:59:22 +02:00
|
|
|
c.ua = DefaultUserAgent
|
2021-09-21 11:21:04 +02:00
|
|
|
|
|
|
|
// Set additional options
|
|
|
|
for _, opt := range options {
|
2021-09-22 13:59:22 +02:00
|
|
|
if opt == nil {
|
|
|
|
continue
|
|
|
|
}
|
2021-09-22 15:46:36 +02:00
|
|
|
opt(&c)
|
2021-09-21 11:21:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add a http client to the Client object
|
|
|
|
c.hc = httpClient(c.to)
|
|
|
|
|
|
|
|
// Associate the different HIBP service APIs with the Client
|
2023-02-09 17:07:20 +01:00
|
|
|
c.PwnedPassAPI = &PwnedPassAPI{
|
|
|
|
hibp: &c,
|
|
|
|
ParamMap: make(map[string]string),
|
|
|
|
}
|
2022-10-29 15:32:12 +02:00
|
|
|
c.BreachAPI = &BreachAPI{hibp: &c}
|
|
|
|
c.PasteAPI = &PasteAPI{hibp: &c}
|
2021-09-21 11:21:04 +02:00
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2022-10-29 15:32:12 +02:00
|
|
|
// WithHTTPTimeout overrides the default http client timeout
|
|
|
|
func WithHTTPTimeout(t time.Duration) Option {
|
2021-09-21 11:21:04 +02:00
|
|
|
return func(c *Client) {
|
|
|
|
c.to = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-29 15:32:12 +02:00
|
|
|
// WithAPIKey set the optional API key to the Client object
|
|
|
|
func WithAPIKey(k string) Option {
|
2021-09-21 11:21:04 +02:00
|
|
|
return func(c *Client) {
|
|
|
|
c.ak = k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-21 18:21:23 +02:00
|
|
|
// WithPwnedPadding enables padding-mode for the PwnedPasswords API client
|
|
|
|
func WithPwnedPadding() Option {
|
|
|
|
return func(c *Client) {
|
2022-10-29 15:32:12 +02:00
|
|
|
c.PwnedPassAPIOpts.WithPadding = true
|
2021-09-21 18:21:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-22 13:59:22 +02:00
|
|
|
// WithUserAgent sets a custom user agent string for the HTTP client
|
|
|
|
func WithUserAgent(a string) Option {
|
|
|
|
if a == "" {
|
2022-04-12 22:52:24 +02:00
|
|
|
return nil
|
2021-09-22 13:59:22 +02:00
|
|
|
}
|
|
|
|
return func(c *Client) {
|
|
|
|
c.ua = a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-22 15:25:27 +02:00
|
|
|
// WithRateLimitSleep let's the HTTP client sleep in case the API rate limiting hits (Defaults to fail)
|
|
|
|
func WithRateLimitSleep() Option {
|
2021-09-22 15:00:29 +02:00
|
|
|
return func(c *Client) {
|
2021-09-22 15:25:27 +02:00
|
|
|
c.rlSleep = true
|
2021-09-22 15:00:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 17:07:20 +01:00
|
|
|
// WithPwnedNTLMHash sets the hash mode for the PwnedPasswords API to NTLM hashes
|
|
|
|
//
|
|
|
|
// Note: This option only affects the generic methods like PwnedPassAPI.CheckPassword
|
|
|
|
// or PwnedPassAPI.ListHashesPassword. For any specifc method with the hash type in
|
|
|
|
// the method name, this option is ignored and the hash type of the function is
|
|
|
|
// forced
|
|
|
|
func WithPwnedNTLMHash() Option {
|
|
|
|
return func(c *Client) {
|
|
|
|
c.PwnedPassAPIOpts.HashMode = HashModeNTLM
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-29 15:32:12 +02:00
|
|
|
// HTTPReq performs an HTTP request to the corresponding API
|
|
|
|
func (c *Client) HTTPReq(m, p string, q map[string]string) (*http.Request, error) {
|
2021-09-21 11:21:04 +02:00
|
|
|
u, err := url.Parse(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-21 19:46:48 +02:00
|
|
|
if m == http.MethodGet {
|
|
|
|
uq := u.Query()
|
|
|
|
for k, v := range q {
|
|
|
|
uq.Add(k, v)
|
|
|
|
}
|
|
|
|
u.RawQuery = uq.Encode()
|
|
|
|
}
|
|
|
|
|
2021-09-21 11:21:04 +02:00
|
|
|
hr, err := http.NewRequest(m, u.String(), nil)
|
2021-09-19 18:10:12 +02:00
|
|
|
if err != nil {
|
2021-09-21 11:21:04 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-21 19:46:48 +02:00
|
|
|
|
|
|
|
if m == http.MethodPost {
|
|
|
|
pd := url.Values{}
|
|
|
|
for k, v := range q {
|
|
|
|
pd.Add(k, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
rb := io.NopCloser(bytes.NewBufferString(pd.Encode()))
|
|
|
|
hr.Body = rb
|
|
|
|
}
|
|
|
|
|
2021-09-21 11:21:04 +02:00
|
|
|
hr.Header.Set("Accept", "application/json")
|
2021-09-22 13:59:22 +02:00
|
|
|
hr.Header.Set("user-agent", c.ua)
|
2021-09-21 11:21:04 +02:00
|
|
|
if c.ak != "" {
|
2021-09-21 18:21:23 +02:00
|
|
|
hr.Header.Set("hibp-api-key", c.ak)
|
|
|
|
}
|
2022-10-29 15:32:12 +02:00
|
|
|
if c.PwnedPassAPIOpts.WithPadding {
|
2021-09-21 18:21:23 +02:00
|
|
|
hr.Header.Set("Add-Padding", "true")
|
2021-09-19 18:10:12 +02:00
|
|
|
}
|
|
|
|
|
2021-09-21 11:21:04 +02:00
|
|
|
return hr, nil
|
|
|
|
}
|
|
|
|
|
2022-10-29 15:32:12 +02:00
|
|
|
// HTTPResBody performs the API call to the given path and returns the response body as byte array
|
|
|
|
func (c *Client) HTTPResBody(m string, p string, q map[string]string) ([]byte, *http.Response, error) {
|
|
|
|
hreq, err := c.HTTPReq(m, p, q)
|
2021-09-22 15:00:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
hr, err := c.hc.Do(hreq)
|
|
|
|
if err != nil {
|
|
|
|
return nil, hr, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
_ = hr.Body.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
hb, err := io.ReadAll(hr.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, hr, err
|
|
|
|
}
|
|
|
|
|
2021-09-22 15:25:27 +02:00
|
|
|
if hr.StatusCode == 429 && c.rlSleep {
|
2021-09-22 15:00:29 +02:00
|
|
|
headerDelay := hr.Header.Get("Retry-After")
|
|
|
|
delayTime, err := time.ParseDuration(headerDelay + "s")
|
|
|
|
if err != nil {
|
|
|
|
return nil, hr, err
|
|
|
|
}
|
|
|
|
log.Printf("API rate limit hit. Retrying request in %s", delayTime.String())
|
|
|
|
time.Sleep(delayTime)
|
2022-10-29 15:32:12 +02:00
|
|
|
return c.HTTPResBody(m, p, q)
|
2021-09-22 15:00:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if hr.StatusCode != 200 {
|
2022-12-22 11:55:56 +01:00
|
|
|
return nil, hr, fmt.Errorf("HTTP %s: %w", hr.Status, ErrNonPositiveResponse)
|
2021-09-22 15:00:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return hb, hr, nil
|
|
|
|
}
|
|
|
|
|
2021-09-21 11:21:04 +02:00
|
|
|
// httpClient returns a custom http client for the HIBP Client object
|
|
|
|
func httpClient(to time.Duration) *http.Client {
|
2022-12-22 11:55:56 +01:00
|
|
|
tc := &tls.Config{
|
2021-09-21 11:21:04 +02:00
|
|
|
MaxVersion: tls.VersionTLS13,
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
}
|
2022-12-22 11:55:56 +01:00
|
|
|
ht := &http.Transport{TLSClientConfig: tc}
|
|
|
|
hc := &http.Client{
|
|
|
|
Transport: ht,
|
|
|
|
Timeout: DefaultTimeout,
|
2021-09-19 18:10:12 +02:00
|
|
|
}
|
2021-09-21 11:21:04 +02:00
|
|
|
if to.Nanoseconds() > 0 {
|
2022-12-22 11:55:56 +01:00
|
|
|
hc.Timeout = to
|
2021-09-19 18:10:12 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 11:55:56 +01:00
|
|
|
return hc
|
2021-09-19 18:10:12 +02:00
|
|
|
}
|