mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
Source files keep their SPDX-License-Identifier and Hanzo copyright line; the Gitea, Gogs and LFS Test Server copyrights they used to repeat now live once in NOTICE, alongside LICENSE. The header lint accepts any copyright line above the SPDX line, so it no longer demands an upstream one. Co-authored-by: Hanzo Dev <[email protected]>
53 lines
1.0 KiB
Go
53 lines
1.0 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package password
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/hanzoai/git/modules/auth/password/pwn"
|
|
"github.com/hanzoai/git/modules/setting"
|
|
)
|
|
|
|
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
|
|
}
|
|
|
|
// IsPwned checks whether a password has been pwned
|
|
// If a password has not been pwned, no error is returned.
|
|
func IsPwned(ctx context.Context, password string) error {
|
|
if !setting.PasswordCheckPwn {
|
|
return nil
|
|
}
|
|
|
|
client := pwn.New(pwn.WithContext(ctx))
|
|
count, err := client.CheckPassword(password, true)
|
|
if err != nil {
|
|
return ErrIsPwnedRequest{err}
|
|
}
|
|
|
|
if count > 0 {
|
|
return ErrIsPwned
|
|
}
|
|
|
|
return nil
|
|
}
|