From c21ca252a3a857d63d2d70f2fd117dffae8970f9 Mon Sep 17 00:00:00 2001 From: "tom.cat" Date: Mon, 14 Mar 2016 20:03:39 +0900 Subject: [PATCH] =?UTF-8?q?=ED=95=B4=EC=8B=9C=ED=95=A8=EC=88=98=20?= =?UTF-8?q?=EC=A0=9C=EC=9E=91=EC=A4=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 2 +- util/ldap.go | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 238bc5d..89ba852 100644 --- a/main.go +++ b/main.go @@ -92,7 +92,7 @@ func main() { graceful.Run(":8080", 10*time.Second, r) - util.Log().Error("byez") + util.Log().Error("bye!") } func setMaxProcs() { diff --git a/util/ldap.go b/util/ldap.go index f4e4c41..b8464f3 100644 --- a/util/ldap.go +++ b/util/ldap.go @@ -2,11 +2,52 @@ package util import ( + "crypto/rand" + "crypto/sha1" + "encoding/base64" "fmt" + "strings" "github.com/nmcclain/ldap" ) +type pwdStor string + +func (p pwdStor) GetSaltedSha1() string { + source := strings.TrimSpace(string(p)) + Log().Errorf("source %s", source) + + var err error + salt := make([]byte, 4) + + _, err = rand.Read(salt) + + if err != nil { + panic(err) + } + + hasher := sha1.New() + + hasher.Write([]byte(source)) + + if err != nil { + panic(err) + } + + hasher.Write(salt) + + if err != nil { + panic(err) + } + + encoded := base64.StdEncoding.EncodeToString(append(hasher.Sum(nil), salt...)) + tag := "{SSHA}" + + formatted := tag + encoded + Log().Errorf("sha1 %s", formatted) + return formatted +} + type ldapUnit struct { dn string filter string @@ -23,12 +64,14 @@ type ldapSource struct { type AccountSource interface { Connect() + Bind(uid string, password pwdStor) Search() Close() } //ldapSourceBool func (l *ldapSource) Connect() { + l.Bind("test", "testpw") addr := fmt.Sprintf("%s:%d", l.host, l.port) conn, err := ldap.DialTLS("tcp", addr, nil) if err == nil { @@ -56,7 +99,7 @@ func (l ldapSource) Search() { nil, nil) searchResults, err := l.connection.Search(search) - // be sure to add error checking! + if err == nil { for _, v := range searchResults.Entries { @@ -72,6 +115,10 @@ func (l ldapSource) Search() { } } +func (l ldapSource) Bind(uid string, password pwdStor) { + password.GetSaltedSha1() +} + func NewAccoauntSource() AccountSource { user := ldapUnit{ dn: "ou=User,dc=amuz,dc=es",