add redis

This commit is contained in:
Jay
2018-04-18 14:16:29 +08:00
parent 4fd4c7018e
commit 6419162471
48 changed files with 9952 additions and 14 deletions
+60 -2
View File
@@ -1,11 +1,20 @@
package account
import (
"crypto/rand"
"crypto/sha512"
"encoding/hex"
"encoding/json"
"log"
"reflect"
"strings"
"git.trj.tw/golang/go-gallery/modules/memstore"
"git.trj.tw/golang/go-gallery/models"
"git.trj.tw/golang/go-gallery/modules/context"
"git.trj.tw/golang/go-gallery/modules/utils"
"golang.org/x/crypto/pbkdf2"
)
// UserLogin route
@@ -17,7 +26,7 @@ func UserLogin(c *context.Context) {
Account: "",
Password: "",
}
err := c.ShouldBind(&loginArg)
err := c.BindData(&loginArg)
if err != nil {
c.DataFormat(nil)
return
@@ -35,5 +44,54 @@ func UserLogin(c *context.Context) {
return
}
c.Success(utils.ToMap(acc))
strs := strings.Split(acc.Password, ".")
if len(strs) != 2 {
c.ServerError("store pass format error")
return
}
b, err := hex.DecodeString(strs[0])
if err != nil {
c.ServerError(nil)
return
}
hashPass, err := hex.DecodeString(strs[1])
if err != nil {
c.ServerError(nil)
return
}
enc := pbkdf2.Key([]byte(loginArg.Password), b, 2048, 64, sha512.New)
if enc == nil || !reflect.DeepEqual(enc, hashPass) {
c.DataFormat("password error")
return
}
res := utils.ToMap(acc)
m := make(map[string]interface{})
m["user"] = res
jsonStr, err := json.Marshal(m)
if err != nil {
c.ServerError(nil)
return
}
tByte := make([]byte, 20)
_, err = rand.Read(tByte)
if err != nil {
c.ServerError(nil)
return
}
err = memstore.RedisSet("golang", hex.EncodeToString(tByte), string(jsonStr), 600)
if err != nil {
c.ServerError(nil)
}
m["token"] = hex.EncodeToString(tByte)
c.Success(m)
}