mirror of https://github.com/go-gitea/gitea
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>pull/12771/head^2
parent
bea343ce09
commit
c6e4bc53aa
@ -0,0 +1,30 @@ |
||||
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package password |
||||
|
||||
import ( |
||||
"context" |
||||
|
||||
"code.gitea.io/gitea/modules/setting" |
||||
|
||||
"go.jolheiser.com/pwn" |
||||
) |
||||
|
||||
// IsPwned checks whether a password has been pwned
|
||||
// NOTE: This func returns true if it encounters an error under the assumption that you ALWAYS want to check against
|
||||
// HIBP, so not getting a response should block a password until it can be verified.
|
||||
func IsPwned(ctx context.Context, password string) (bool, error) { |
||||
if !setting.PasswordCheckPwn { |
||||
return false, nil |
||||
} |
||||
|
||||
client := pwn.New(pwn.WithContext(ctx)) |
||||
count, err := client.CheckPassword(password, true) |
||||
if err != nil { |
||||
return true, err |
||||
} |
||||
|
||||
return count > 0, nil |
||||
} |
@ -0,0 +1,6 @@ |
||||
# GoLand |
||||
.idea/ |
||||
|
||||
# Binaries |
||||
/pwn |
||||
/pwn.exe |
@ -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. |
@ -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
|
@ -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) |
@ -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 |
||||
} |
@ -0,0 +1,3 @@ |
||||
module go.jolheiser.com/pwn |
||||
|
||||
go 1.15 |
@ -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 |
||||
} |
@ -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 |
||||
} |
Loading…
Reference in new issue