This commit is contained in:
Jay 2018-04-18 18:21:29 +08:00
parent d86e8e47f4
commit a971bd52d2
4 changed files with 91 additions and 10 deletions

View File

@ -6,13 +6,13 @@ import (
// Account - Account table struct // Account - Account table struct
type Account struct { type Account struct {
ID string `xorm:"id" cc:"id"` ID string `xorm:"id <-" cc:"id"`
Account string `xorm:"account" cc:"account"` Account string `xorm:"account" cc:"account"`
Password string `xorm:"password" cc:"-"` Password string `xorm:"password" cc:"-"`
Nick string `xorm:"nick" cc:"nick"` Nick string `xorm:"nick" cc:"nick"`
Email string `xorm:"email" cc:"email"` Email string `xorm:"email" cc:"email"`
Ctime time.Time `xorm:"ctime" cc:"ctime"` Ctime time.Time `xorm:"ctime created" cc:"ctime"`
Mtime time.Time `xorm:"mtime" cc:"mtime"` Mtime time.Time `xorm:"mtime updated" cc:"mtime"`
} }
// GetAllAccount - all account // GetAllAccount - all account
@ -27,13 +27,28 @@ func GetAccount(account string) (acc *Account, err error) {
acc = &Account{ acc = &Account{
Account: account, Account: account,
} }
ok, err := x.Table("account").Get(acc)
if err != nil { ok, err := acc.Get()
if err != nil || !ok {
return nil, err return nil, err
} }
if !ok {
return nil, nil
}
return 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
}

View File

@ -13,5 +13,6 @@ var x *xorm.Engine
func NewDB() (*xorm.Engine, error) { func NewDB() (*xorm.Engine, error) {
var err error var err error
x, err = xorm.NewEngine("postgres", fmt.Sprintf("postgres://%s@%s/%s?sslmode=disable", "postgres", "localhost", "gallery")) x, err = xorm.NewEngine("postgres", fmt.Sprintf("postgres://%s@%s/%s?sslmode=disable", "postgres", "localhost", "gallery"))
x.ShowSQL(true)
return x, err return x, err
} }

View File

@ -17,6 +17,11 @@ import (
"golang.org/x/crypto/pbkdf2" "golang.org/x/crypto/pbkdf2"
) )
var passIterator = 2048
var passLen = 64
var passSaltLen = 16
var passHash = sha512.New
// UserLogin route // UserLogin route
func UserLogin(c *context.Context) { func UserLogin(c *context.Context) {
loginArg := struct { loginArg := struct {
@ -60,7 +65,7 @@ func UserLogin(c *context.Context) {
return 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) { if enc == nil || !reflect.DeepEqual(enc, hashPass) {
c.DataFormat("password error") c.DataFormat("password error")
@ -99,7 +104,6 @@ func UserLogin(c *context.Context) {
// UserLogout route // UserLogout route
func UserLogout(c *context.Context) { func UserLogout(c *context.Context) {
token := c.GetHeader("X-Auth-Token") token := c.GetHeader("X-Auth-Token")
// token, ok := c.C["token"]
if len(token) == 0 { if len(token) == 0 {
c.DataFormat("token not found") c.DataFormat("token not found")
return return
@ -113,3 +117,63 @@ func UserLogout(c *context.Context) {
c.Success(nil) 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)
}

View File

@ -41,5 +41,6 @@ func SetDefaultRoutes(r *gin.Engine) {
{ {
accountAPI.POST("/login", context.PatchContext(account.UserLogin)) accountAPI.POST("/login", context.PatchContext(account.UserLogin))
accountAPI.POST("/logout", context.PatchContext(account.UserLogout)) accountAPI.POST("/logout", context.PatchContext(account.UserLogout))
accountAPI.POST("/signup", context.PatchContext(account.UserSignup))
} }
} }