mirror of
https://github.com/netfun2000/rttys_zhaojh329.git
synced 2026-02-27 09:53:24 +08:00
pwauth: Optimize code
Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
This commit is contained in:
@@ -47,8 +47,7 @@ func httpAuth(w http.ResponseWriter, r *http.Request) bool {
|
||||
}
|
||||
|
||||
func httpLogin(cfg *RttysConfig, creds *Credentials) bool {
|
||||
ok := pwauth.Auth(creds.Username, creds.Password)
|
||||
if ok {
|
||||
if err := pwauth.Auth(creds.Username, creds.Password); err == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package pwauth
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
// Need to be implemented
|
||||
func Auth(username, password string) bool {
|
||||
return false
|
||||
func auth(username, password string) error {
|
||||
return errors.New("not implemented")
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/user"
|
||||
"strings"
|
||||
|
||||
"github.com/GehirnInc/crypt"
|
||||
@@ -37,16 +38,20 @@ func getPassword(name string) (string, error) {
|
||||
return "", errors.New("Not found")
|
||||
}
|
||||
|
||||
func Auth(username, password string) bool {
|
||||
func auth(username, password string) error {
|
||||
if os.Getuid() != 0 {
|
||||
return false
|
||||
return errors.New("Cannot possibly work without effective root")
|
||||
}
|
||||
|
||||
if _, err := user.Lookup(username); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pw, err := getPassword(username)
|
||||
if err != nil {
|
||||
return false
|
||||
return err
|
||||
}
|
||||
|
||||
c := crypt.NewFromHash(pw)
|
||||
return c.Verify(pw, []byte(password)) == nil
|
||||
return c.Verify(pw, []byte(password))
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/user"
|
||||
"strings"
|
||||
|
||||
"github.com/GehirnInc/crypt"
|
||||
@@ -37,16 +38,20 @@ func getPassword(name string) (string, error) {
|
||||
return "", errors.New("Not found")
|
||||
}
|
||||
|
||||
func Auth(username, password string) bool {
|
||||
func auth(username, password string) error {
|
||||
if os.Getuid() != 0 {
|
||||
return false
|
||||
return errors.New("Cannot possibly work without effective root")
|
||||
}
|
||||
|
||||
if _, err := user.Lookup(username); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pw, err := getPassword(username)
|
||||
if err != nil {
|
||||
return false
|
||||
return err
|
||||
}
|
||||
|
||||
c := crypt.NewFromHash(pw)
|
||||
return c.Verify(pw, []byte(password)) == nil
|
||||
return c.Verify(pw, []byte(password))
|
||||
}
|
||||
|
||||
+11
-2
@@ -1,6 +1,7 @@
|
||||
package pwauth
|
||||
|
||||
import (
|
||||
"os/user"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
@@ -31,12 +32,20 @@ func LogonUserW(username, domain, password *uint16, logonType, logonProvider uin
|
||||
return token, nil
|
||||
}
|
||||
|
||||
func Auth(username, password string) bool {
|
||||
func auth(username, password string) error {
|
||||
if _, err := user.Lookup(username); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pUsername, _ := syscall.UTF16PtrFromString(username)
|
||||
pDomain, _ := syscall.UTF16PtrFromString(".")
|
||||
pPassword, _ := syscall.UTF16PtrFromString(password)
|
||||
|
||||
_, err := LogonUserW(pUsername, pDomain, pPassword, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT)
|
||||
|
||||
return err == nil || err == errERROR_ACCOUNT_RESTRICTION
|
||||
if err == errERROR_ACCOUNT_RESTRICTION {
|
||||
return nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package pwauth
|
||||
|
||||
// Auth check the validity of the username/password pair.If the
|
||||
// credentials are not valid, this function will return an error.
|
||||
func Auth(username, password string) error {
|
||||
return auth(username, password)
|
||||
}
|
||||
Reference in New Issue
Block a user