Check passwords against HaveIBeenPwned (#12716)

* Implement pwn

Signed-off-by: jolheiser <john.olheiser@gmail.com>

* Update module

Signed-off-by: jolheiser <john.olheiser@gmail.com>

* Apply suggestions mrsdizzie

Co-authored-by: mrsdizzie <info@mrsdizzie.com>

* Add link to HIBP

Signed-off-by: jolheiser <john.olheiser@gmail.com>

* Add more details to admin command

Signed-off-by: jolheiser <john.olheiser@gmail.com>

* Add context to pwn

Signed-off-by: jolheiser <john.olheiser@gmail.com>

* Consistency and making some noise ;)

Signed-off-by: jolheiser <john.olheiser@gmail.com>

Co-authored-by: mrsdizzie <info@mrsdizzie.com>
Co-authored-by: zeripath <art27@cantab.net>
This commit is contained in:
John Olheiser
2020-09-08 17:06:39 -05:00
committed by GitHub
parent bea343ce09
commit c6e4bc53aa
22 changed files with 309 additions and 8 deletions

6
vendor/go.jolheiser.com/pwn/.gitignore generated vendored Normal file
View File

@ -0,0 +1,6 @@
# GoLand
.idea/
# Binaries
/pwn
/pwn.exe

7
vendor/go.jolheiser.com/pwn/LICENSE generated vendored Normal file
View File

@ -0,0 +1,7 @@
Copyright 2020 John Olheiser
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

18
vendor/go.jolheiser.com/pwn/Makefile generated vendored Normal file
View File

@ -0,0 +1,18 @@
GO ?= go
VERSION ?= $(shell git describe --tags --always | sed 's/-/+/' | sed 's/^v//')
.PHONY: fmt
fmt:
$(GO) fmt ./...
.PHONY: test
test:
$(GO) test -race ./...
.PHONY: vet
vet:
$(GO) vet ./...
.PHONY: build
build:
$(GO) build -ldflags '-s -w -X "main.Version=$(VERSION)"' go.jolheiser.com/pwn/cmd/pwn

13
vendor/go.jolheiser.com/pwn/README.md generated vendored Normal file
View File

@ -0,0 +1,13 @@
# Have I Been Pwned
Go library for interacting with [HaveIBeenPwned](https://haveibeenpwned.com/).
Implemented:
* [ ] Breaches
* [ ] Pastes
* [x] Passwords
## License
[MIT](LICENSE)

15
vendor/go.jolheiser.com/pwn/error.go generated vendored Normal file
View File

@ -0,0 +1,15 @@
package pwn
// ErrEmptyPassword is an empty password error
type ErrEmptyPassword struct{}
// Error fulfills the error interface
func (e ErrEmptyPassword) Error() string {
return "password cannot be empty"
}
// IsErrEmptyPassword checks if an error is ErrEmptyPassword
func IsErrEmptyPassword(err error) bool {
_, ok := err.(ErrEmptyPassword)
return ok
}

3
vendor/go.jolheiser.com/pwn/go.mod generated vendored Normal file
View File

@ -0,0 +1,3 @@
module go.jolheiser.com/pwn
go 1.15

62
vendor/go.jolheiser.com/pwn/password.go generated vendored Normal file
View File

@ -0,0 +1,62 @@
package pwn
import (
"crypto/sha1"
"encoding/hex"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
)
const passwordURL = "https://api.pwnedpasswords.com/range/"
// CheckPassword returns the number of times a password has been compromised
// Adding padding will make requests more secure, however is also slower
// because artificial responses will be added to the response
// For more information, see https://www.troyhunt.com/enhancing-pwned-passwords-privacy-with-padding/
func (c *Client) CheckPassword(pw string, padding bool) (int, error) {
if strings.TrimSpace(pw) == "" {
return -1, ErrEmptyPassword{}
}
sha := sha1.New()
sha.Write([]byte(pw))
enc := hex.EncodeToString(sha.Sum(nil))
prefix, suffix := enc[:5], enc[5:]
req, err := newRequest(c.ctx, http.MethodGet, fmt.Sprintf("%s%s", passwordURL, prefix), nil)
if err != nil {
return -1, nil
}
if padding {
req.Header.Add("Add-Padding", "true")
}
resp, err := c.http.Do(req)
if err != nil {
return -1, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return -1, err
}
defer resp.Body.Close()
for _, pair := range strings.Split(string(body), "\n") {
parts := strings.Split(pair, ":")
if len(parts) != 2 {
continue
}
if strings.EqualFold(suffix, parts[0]) {
count, err := strconv.ParseInt(strings.TrimSpace(parts[1]), 10, 64)
if err != nil {
return -1, err
}
return int(count), nil
}
}
return 0, nil
}

58
vendor/go.jolheiser.com/pwn/pwn.go generated vendored Normal file
View File

@ -0,0 +1,58 @@
package pwn
import (
"context"
"io"
"net/http"
)
const (
libVersion = "0.0.3"
userAgent = "go.jolheiser.com/pwn v" + libVersion
)
// Client is a HaveIBeenPwned client
type Client struct {
ctx context.Context
http *http.Client
}
// New returns a new HaveIBeenPwned Client
func New(options ...ClientOption) *Client {
client := &Client{
ctx: context.Background(),
http: http.DefaultClient,
}
for _, opt := range options {
opt(client)
}
return client
}
// ClientOption is a way to modify a new Client
type ClientOption func(*Client)
// WithHTTP will set the http.Client of a Client
func WithHTTP(httpClient *http.Client) func(pwnClient *Client) {
return func(pwnClient *Client) {
pwnClient.http = httpClient
}
}
// WithContext will set the context.Context of a Client
func WithContext(ctx context.Context) func(pwnClient *Client) {
return func(pwnClient *Client) {
pwnClient.ctx = ctx
}
}
func newRequest(ctx context.Context, method, url string, body io.ReadCloser) (*http.Request, error) {
req, err := http.NewRequestWithContext(ctx, method, url, body)
if err != nil {
return nil, err
}
req.Header.Add("User-Agent", userAgent)
return req, nil
}

3
vendor/modules.txt vendored
View File

@ -760,6 +760,9 @@ github.com/yuin/goldmark-highlighting
github.com/yuin/goldmark-meta
# go.etcd.io/bbolt v1.3.5
go.etcd.io/bbolt
# go.jolheiser.com/pwn v0.0.3
## explicit
go.jolheiser.com/pwn
# go.mongodb.org/mongo-driver v1.3.5
go.mongodb.org/mongo-driver/bson
go.mongodb.org/mongo-driver/bson/bsoncodec