2020-09-09 01:06:39 +03:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 21:20:29 +03:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-09-09 01:06:39 +03:00
|
|
|
|
|
|
|
package password
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-02-04 16:29:09 +03:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-09-09 01:06:39 +03:00
|
|
|
|
2023-02-19 10:35:20 +03:00
|
|
|
"code.gitea.io/gitea/modules/auth/password/pwn"
|
2020-09-09 01:06:39 +03:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
)
|
|
|
|
|
2024-02-04 16:29:09 +03:00
|
|
|
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 01:06:39 +03:00
|
|
|
// IsPwned checks whether a password has been pwned
|
2024-02-04 16:29:09 +03:00
|
|
|
// If a password has not been pwned, no error is returned.
|
|
|
|
func IsPwned(ctx context.Context, password string) error {
|
2020-09-09 01:06:39 +03:00
|
|
|
if !setting.PasswordCheckPwn {
|
2024-02-04 16:29:09 +03:00
|
|
|
return nil
|
2020-09-09 01:06:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
client := pwn.New(pwn.WithContext(ctx))
|
|
|
|
count, err := client.CheckPassword(password, true)
|
|
|
|
if err != nil {
|
2024-02-04 16:29:09 +03:00
|
|
|
return ErrIsPwnedRequest{err}
|
|
|
|
}
|
|
|
|
|
|
|
|
if count > 0 {
|
|
|
|
return ErrIsPwned
|
2020-09-09 01:06:39 +03:00
|
|
|
}
|
|
|
|
|
2024-02-04 16:29:09 +03:00
|
|
|
return nil
|
2020-09-09 01:06:39 +03:00
|
|
|
}
|