wocao
This commit is contained in:
+32
-23
@@ -1,14 +1,3 @@
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
package state
|
||||
|
||||
import (
|
||||
@@ -19,21 +8,43 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
|
||||
"simplegit/common"
|
||||
)
|
||||
|
||||
// AccessByUserPswd authenticates a namespace-scoped User. The username is the
|
||||
// current Namespace.Name; the User row itself deliberately has no name. A
|
||||
// successful login owns that namespace and therefore authorizes its repos.
|
||||
func (s *LocalState) AccessByUserPswd(ctx context.Context, username, password, owner, name string, perm common.Perm) (bool, error) {
|
||||
r, err := s.findRepo(owner, name)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrRepoNotFound) {
|
||||
return false, ErrDenied
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
if username == "" || password == "" || username != owner {
|
||||
return false, ErrDenied
|
||||
}
|
||||
|
||||
var ns Namespace
|
||||
has, err := s.engine.Where("name = ?", username).Get(&ns)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("lookup user namespace: %w", err)
|
||||
}
|
||||
if !has || ns.ID != r.NamespaceID {
|
||||
return false, ErrDenied
|
||||
}
|
||||
|
||||
if ns.PasswordHash == "" || bcrypt.CompareHashAndPassword([]byte(ns.PasswordHash), []byte(password)) != nil {
|
||||
return false, ErrInvalidCredentials
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
const patPrefix = "sgp_"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
func (s *LocalState) AccessByPAT(ctx context.Context, pat, owner, name string, perm common.Perm) (bool, error) {
|
||||
r, err := s.findRepo(owner, name)
|
||||
if err != nil {
|
||||
@@ -64,8 +75,6 @@ func (s *LocalState) AccessByPAT(ctx context.Context, pat, owner, name string, p
|
||||
return true, nil
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (s *LocalState) lookupPAT(token string) (*PAT, error) {
|
||||
sum := sha256.Sum256([]byte(token))
|
||||
var pat PAT
|
||||
|
||||
Reference in New Issue
Block a user