2021-09-22 16:20:54 +02:00
|
|
|
package hibp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-10-29 15:32:12 +02:00
|
|
|
// PasteAPI is a HIBP pastes API client
|
|
|
|
type PasteAPI struct {
|
2021-09-22 16:20:54 +02:00
|
|
|
hibp *Client // References back to the parent HIBP client
|
|
|
|
}
|
|
|
|
|
|
|
|
// Paste represents a JSON response structure of the pastes API
|
|
|
|
type Paste struct {
|
|
|
|
// Source is the paste service the record was retrieved from. Current values are: Pastebin,
|
|
|
|
// Pastie, Slexy, Ghostbin, QuickLeak, JustPaste, AdHocUrl, PermanentOptOut, OptOut
|
|
|
|
Source string `json:"Source"`
|
|
|
|
|
2022-10-29 15:32:12 +02:00
|
|
|
// ID of the paste as it was given at the source service. Combined with the "Source" attribute, this
|
2021-09-22 16:20:54 +02:00
|
|
|
// can be used to resolve the URL of the paste
|
2022-10-29 15:32:12 +02:00
|
|
|
ID string `json:"ID"`
|
2021-09-22 16:20:54 +02:00
|
|
|
|
|
|
|
// Title of the paste as observed on the source site. This may be null and if so will be omitted from
|
|
|
|
// the response
|
|
|
|
Title string `json:"Title"`
|
|
|
|
|
|
|
|
// Date is the date and time (precision to the second) that the paste was posted. This is taken directly
|
|
|
|
// from the paste site when this information is available but may be null if no date is published
|
|
|
|
Date time.Time `json:"Date"`
|
|
|
|
|
|
|
|
// EmailCount is number of emails that were found when processing the paste. Emails are extracted by
|
|
|
|
// using the regular expression \b[a-zA-Z0-9\.\-_\+]+@[a-zA-Z0-9\.\-_]+\.[a-zA-Z]+\b
|
|
|
|
EmailCount int `json:"EmailCount"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// PastedAccount returns a single breached site based on its name
|
2022-10-29 15:32:12 +02:00
|
|
|
func (p *PasteAPI) PastedAccount(a string) ([]*Paste, *http.Response, error) {
|
2021-09-22 16:20:54 +02:00
|
|
|
if a == "" {
|
2022-12-22 09:57:57 +01:00
|
|
|
return nil, nil, ErrNoAccountID
|
2021-09-22 16:20:54 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 09:57:57 +01:00
|
|
|
au := fmt.Sprintf("%s/pasteaccount/%s", BaseURL, a)
|
|
|
|
hb, hr, err := p.hibp.HTTPResBody(http.MethodGet, au, nil)
|
2021-09-22 16:20:54 +02:00
|
|
|
if err != nil {
|
2022-12-22 09:57:57 +01:00
|
|
|
return nil, hr, err
|
2021-09-22 16:20:54 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 09:57:57 +01:00
|
|
|
var pd []*Paste
|
|
|
|
if err := json.Unmarshal(hb, &pd); err != nil {
|
2021-09-22 16:20:54 +02:00
|
|
|
return nil, hr, err
|
|
|
|
}
|
|
|
|
|
2022-12-22 09:57:57 +01:00
|
|
|
return pd, hr, nil
|
2021-09-22 16:20:54 +02:00
|
|
|
}
|