2020-09-09 03:36:39 +05:30
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2020-09-09 03:36:39 +05:30
|
|
|
|
|
|
|
package password
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-02-04 18:59:09 +05:30
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-09-09 03:36:39 +05:30
|
|
|
|
2023-02-19 13:05:20 +05:30
|
|
|
"code.gitea.io/gitea/modules/auth/password/pwn"
|
2020-09-09 03:36:39 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
)
|
|
|
|
|
2024-02-04 18:59:09 +05:30
|
|
|
var ErrIsPwned = errors.New("password has been pwned")
|
|
|
|
|
|
|
|
type ErrIsPwnedRequest struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsErrIsPwnedRequest(err error) bool {
|
|
|
|
_, ok := err.(ErrIsPwnedRequest)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err ErrIsPwnedRequest) Error() string {
|
|
|
|
return fmt.Sprintf("using Have-I-Been-Pwned service failed: %v", err.err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err ErrIsPwnedRequest) Unwrap() error {
|
|
|
|
return err.err
|
|
|
|
}
|
|
|
|
|
2020-09-09 03:36:39 +05:30
|
|
|
// IsPwned checks whether a password has been pwned
|
2024-02-04 18:59:09 +05:30
|
|
|
// If a password has not been pwned, no error is returned.
|
|
|
|
func IsPwned(ctx context.Context, password string) error {
|
2020-09-09 03:36:39 +05:30
|
|
|
if !setting.PasswordCheckPwn {
|
2024-02-04 18:59:09 +05:30
|
|
|
return nil
|
2020-09-09 03:36:39 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
client := pwn.New(pwn.WithContext(ctx))
|
|
|
|
count, err := client.CheckPassword(password, true)
|
|
|
|
if err != nil {
|
2024-02-04 18:59:09 +05:30
|
|
|
return ErrIsPwnedRequest{err}
|
|
|
|
}
|
|
|
|
|
|
|
|
if count > 0 {
|
|
|
|
return ErrIsPwned
|
2020-09-09 03:36:39 +05:30
|
|
|
}
|
|
|
|
|
2024-02-04 18:59:09 +05:30
|
|
|
return nil
|
2020-09-09 03:36:39 +05:30
|
|
|
}
|