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
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
}

View File

@ -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
}

View File

@ -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)
}

View File

@ -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))
}
}