From a971bd52d2f8bf9a36dccc0209a188c2de47307e Mon Sep 17 00:00:00 2001 From: Jay Date: Wed, 18 Apr 2018 18:21:29 +0800 Subject: [PATCH] update --- models/account.go | 31 ++++++++++++----- models/models.go | 1 + routers/account/account.go | 68 ++++++++++++++++++++++++++++++++++++-- routers/routes/routes.go | 1 + 4 files changed, 91 insertions(+), 10 deletions(-) diff --git a/models/account.go b/models/account.go index 6126c10..896d7c3 100644 --- a/models/account.go +++ b/models/account.go @@ -6,13 +6,13 @@ import ( // Account - Account table struct type Account struct { - ID string `xorm:"id" cc:"id"` + ID string `xorm:"id <-" cc:"id"` Account string `xorm:"account" cc:"account"` Password string `xorm:"password" cc:"-"` Nick string `xorm:"nick" cc:"nick"` Email string `xorm:"email" cc:"email"` - Ctime time.Time `xorm:"ctime" cc:"ctime"` - Mtime time.Time `xorm:"mtime" cc:"mtime"` + Ctime time.Time `xorm:"ctime created" cc:"ctime"` + Mtime time.Time `xorm:"mtime updated" cc:"mtime"` } // GetAllAccount - all account @@ -27,13 +27,28 @@ func GetAccount(account string) (acc *Account, err error) { acc = &Account{ Account: account, } - ok, err := x.Table("account").Get(acc) - if err != nil { + + ok, err := acc.Get() + + if err != nil || !ok { return nil, err } - if !ok { - return nil, nil - } return } + +// Get - +func (a *Account) Get() (bool, error) { + ok, err := x.Table("account").Get(a) + if err != nil { + return false, err + } + + return ok, nil +} + +// Create - +func (a *Account) Create() error { + _, err := x.Table("account").Insert(a) + return err +} diff --git a/models/models.go b/models/models.go index d391595..c4117e1 100644 --- a/models/models.go +++ b/models/models.go @@ -13,5 +13,6 @@ var x *xorm.Engine func NewDB() (*xorm.Engine, error) { var err error x, err = xorm.NewEngine("postgres", fmt.Sprintf("postgres://%s@%s/%s?sslmode=disable", "postgres", "localhost", "gallery")) + x.ShowSQL(true) return x, err } diff --git a/routers/account/account.go b/routers/account/account.go index 2bae102..54a1f85 100644 --- a/routers/account/account.go +++ b/routers/account/account.go @@ -17,6 +17,11 @@ import ( "golang.org/x/crypto/pbkdf2" ) +var passIterator = 2048 +var passLen = 64 +var passSaltLen = 16 +var passHash = sha512.New + // UserLogin route func UserLogin(c *context.Context) { loginArg := struct { @@ -60,7 +65,7 @@ func UserLogin(c *context.Context) { return } - enc := pbkdf2.Key([]byte(loginArg.Password), b, 2048, 64, sha512.New) + enc := pbkdf2.Key([]byte(loginArg.Password), b, passIterator, passLen, passHash) if enc == nil || !reflect.DeepEqual(enc, hashPass) { c.DataFormat("password error") @@ -99,7 +104,6 @@ func UserLogin(c *context.Context) { // UserLogout route func UserLogout(c *context.Context) { token := c.GetHeader("X-Auth-Token") - // token, ok := c.C["token"] if len(token) == 0 { c.DataFormat("token not found") return @@ -113,3 +117,63 @@ func UserLogout(c *context.Context) { c.Success(nil) } + +// UserSignup route +func UserSignup(c *context.Context) { + singupObj := struct { + Account string `json:"account" binding:"required"` + Password string `json:"password" binding:"required"` + Nick string `json:"nick"` + Email string `json:"email" binding:"required"` + }{} + + err := c.BindData(&singupObj) + if err != nil { + c.DataFormat(nil) + return + } + + salt := make([]byte, passSaltLen) + _, err = rand.Read(salt) + if err != nil { + c.ServerError(nil) + return + } + + passBuf := pbkdf2.Key([]byte(singupObj.Password), salt, passIterator, passLen, passHash) + passStr := hex.EncodeToString(salt) + "." + hex.EncodeToString(passBuf) + + dat, err := models.GetAccount(singupObj.Account) + if err != nil { + c.ServerError(nil) + return + } + if dat != nil { + c.DataFormat("account exists") + return + } + dat = nil + + acc := &models.Account{} + acc.Account = singupObj.Account + acc.Nick = singupObj.Nick + acc.Password = passStr + acc.Email = singupObj.Email + + err = acc.Create() + if err != nil { + c.ServerError(nil) + return + } + + ok, err := acc.Get() + + if !ok { + c.ServerError(nil) + return + } + + log.Println(acc.ID) + + c.Success(nil) +} diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 3d1c491..44a23be 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -41,5 +41,6 @@ func SetDefaultRoutes(r *gin.Engine) { { accountAPI.POST("/login", context.PatchContext(account.UserLogin)) accountAPI.POST("/logout", context.PatchContext(account.UserLogout)) + accountAPI.POST("/signup", context.PatchContext(account.UserSignup)) } }