pwauth: Optimize code

Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
This commit is contained in:
Jianhui Zhao
2019-05-01 17:35:29 +08:00
parent c5ee722a71
commit 7de126d52c
6 changed files with 43 additions and 14 deletions
+1 -2
View File
@@ -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
}
+6 -2
View File
@@ -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")
}
+9 -4
View File
@@ -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))
}
+9 -4
View File
@@ -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
View File
@@ -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
}
+7
View File
@@ -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)
}