aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/go.jolheiser.com
diff options
context:
space:
mode:
authorJohn Olheiser <john.olheiser@gmail.com>2020-09-08 17:06:39 -0500
committerGitHub <noreply@github.com>2020-09-08 17:06:39 -0500
commitc6e4bc53aad371210f0cb670e36c57132087b230 (patch)
treeef2eecef855a4257a22eb61aefd5439be23a770e /vendor/go.jolheiser.com
parentbea343ce0997262e61c5d83812a270090896afbf (diff)
downloadgitea-c6e4bc53aad371210f0cb670e36c57132087b230.tar.gz
gitea-c6e4bc53aad371210f0cb670e36c57132087b230.zip
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>
Diffstat (limited to 'vendor/go.jolheiser.com')
-rw-r--r--vendor/go.jolheiser.com/pwn/.gitignore6
-rw-r--r--vendor/go.jolheiser.com/pwn/LICENSE7
-rw-r--r--vendor/go.jolheiser.com/pwn/Makefile18
-rw-r--r--vendor/go.jolheiser.com/pwn/README.md13
-rw-r--r--vendor/go.jolheiser.com/pwn/error.go15
-rw-r--r--vendor/go.jolheiser.com/pwn/go.mod3
-rw-r--r--vendor/go.jolheiser.com/pwn/password.go62
-rw-r--r--vendor/go.jolheiser.com/pwn/pwn.go58
8 files changed, 182 insertions, 0 deletions
diff --git a/vendor/go.jolheiser.com/pwn/.gitignore b/vendor/go.jolheiser.com/pwn/.gitignore
new file mode 100644
index 0000000000..b73cf3849a
--- /dev/null
+++ b/vendor/go.jolheiser.com/pwn/.gitignore
@@ -0,0 +1,6 @@
+# GoLand
+.idea/
+
+# Binaries
+/pwn
+/pwn.exe \ No newline at end of file
diff --git a/vendor/go.jolheiser.com/pwn/LICENSE b/vendor/go.jolheiser.com/pwn/LICENSE
new file mode 100644
index 0000000000..0f02641b29
--- /dev/null
+++ b/vendor/go.jolheiser.com/pwn/LICENSE
@@ -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. \ No newline at end of file
diff --git a/vendor/go.jolheiser.com/pwn/Makefile b/vendor/go.jolheiser.com/pwn/Makefile
new file mode 100644
index 0000000000..7f54744011
--- /dev/null
+++ b/vendor/go.jolheiser.com/pwn/Makefile
@@ -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 \ No newline at end of file
diff --git a/vendor/go.jolheiser.com/pwn/README.md b/vendor/go.jolheiser.com/pwn/README.md
new file mode 100644
index 0000000000..9b3c37eb23
--- /dev/null
+++ b/vendor/go.jolheiser.com/pwn/README.md
@@ -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) \ No newline at end of file
diff --git a/vendor/go.jolheiser.com/pwn/error.go b/vendor/go.jolheiser.com/pwn/error.go
new file mode 100644
index 0000000000..2c73ed5cee
--- /dev/null
+++ b/vendor/go.jolheiser.com/pwn/error.go
@@ -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
+}
diff --git a/vendor/go.jolheiser.com/pwn/go.mod b/vendor/go.jolheiser.com/pwn/go.mod
new file mode 100644
index 0000000000..46b73c8e5c
--- /dev/null
+++ b/vendor/go.jolheiser.com/pwn/go.mod
@@ -0,0 +1,3 @@
+module go.jolheiser.com/pwn
+
+go 1.15
diff --git a/vendor/go.jolheiser.com/pwn/password.go b/vendor/go.jolheiser.com/pwn/password.go
new file mode 100644
index 0000000000..848ac49188
--- /dev/null
+++ b/vendor/go.jolheiser.com/pwn/password.go
@@ -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
+}
diff --git a/vendor/go.jolheiser.com/pwn/pwn.go b/vendor/go.jolheiser.com/pwn/pwn.go
new file mode 100644
index 0000000000..399284b487
--- /dev/null
+++ b/vendor/go.jolheiser.com/pwn/pwn.go
@@ -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
+}