-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
95 lines (81 loc) · 2.38 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package passcheck
import (
"bufio"
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"runtime"
"strings"
"github.com/pkg/errors"
)
// API is an interface representing a thin wrapper of the Pwned Passwords API
type API interface {
Range(string) (RangeMap, error)
}
// NewPwnedPasswords creates a new PwnedPasswords with the given base URL or
// throws an error if the given base URL is invalid.
func NewPwnedPasswords(baseURL string) (*PwnedPasswords, error) {
uri, err := url.Parse(baseURL)
if err != nil {
return nil, err
}
ua := fmt.Sprintf("%s/passcheck-%s (%s/%s)", runtime.Version(), version, runtime.GOOS, runtime.GOARCH)
return &PwnedPasswords{BaseURL: uri, userAgent: ua}, nil
}
// PwnedPasswords is a thin wrapper for the Pwned Passwords API
type PwnedPasswords struct {
BaseURL *url.URL
userAgent string
}
func (pp PwnedPasswords) get(path string) (io.ReadCloser, error) {
parsedPath, err := url.Parse(path)
if err != nil {
return nil, err
}
uri := pp.BaseURL.ResolveReference(parsedPath)
resp, err := http.Get(uri.String())
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
defer resp.Body.Close()
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
return nil, fmt.Errorf("received status code %d; body: <%s>", resp.StatusCode, buf)
}
return resp.Body, nil
}
// RangeMap represents a parsed response object from the PwnedPassword API’s
// `/range` endpoint. The keys are suffixes of password hashes with int values
// representing the count of ‘pwnage’s of the specific password represented by
// the combined hash prefix + suffix
type RangeMap map[string]int
// Range calls the PwnedPassword API’s `/range` endpoint and returns a parsed
// version of that endpoint’s response
func (pp PwnedPasswords) Range(prefix string) (RangeMap, error) {
path := fmt.Sprintf("/range/%s", prefix)
rspBody, err := pp.get(path)
if err != nil {
return nil, err
}
defer rspBody.Close()
return parseRangeResponse(rspBody)
}
func parseRangeResponse(r io.Reader) (RangeMap, error) {
rm := make(map[string]int)
scanner := bufio.NewScanner(r)
var key string
var count int
for scanner.Scan() {
line := strings.ReplaceAll(scanner.Text(), ":", " ")
// fmt.Printf("line: <%s>\n", line)
fmt.Sscanf(line, "%s %d", &key, &count)
rm[key] = count
}
if err := scanner.Err(); err != nil {
return nil, errors.Wrap(err, "error parsing /range response")
}
return rm, nil
}