使用Crypto加密, 找回密码, 修改密码修复

This commit is contained in:
lealife
2015-09-07 15:39:46 +08:00
parent 5794f76b0a
commit c568756d16
4 changed files with 78 additions and 53 deletions

22
app/lea/Pwd.go Normal file
View File

@ -0,0 +1,22 @@
package lea
// 对比密码是否一致
// 因为之前密码是用md5加密的, 所以通过密码长度来判断
// rawPwd 原始, 用户输入的密码
func ComparePwd(rawPwd, dbPwd string) bool {
if len(dbPwd) == 32 {
return Md5(rawPwd) == dbPwd
}
hex := []byte(dbPwd)
return CompareHash(hex, rawPwd)
}
// 加密
func GenPwd(rawPwd string) string {
digest, err := GenerateHash(rawPwd)
if err != nil {
return ""
}
return string(digest)
}

View File

@ -2,36 +2,30 @@ package service
import ( import (
"gopkg.in/mgo.v2/bson" "gopkg.in/mgo.v2/bson"
// "github.com/leanote/leanote/app/db" // "github.com/leanote/leanote/app/db"
"github.com/leanote/leanote/app/info" "github.com/leanote/leanote/app/info"
// "github.com/revel/revel" // "github.com/revel/revel"
"strings"
. "github.com/leanote/leanote/app/lea"
"fmt"
"strconv"
"errors" "errors"
"fmt"
. "github.com/leanote/leanote/app/lea"
"strconv"
"strings"
) )
// 登录与权限 // 登录与权限 Login & Register
type AuthService struct { type AuthService struct {
} }
// 使用bcrypt认证或者Md5认证 // 使用bcrypt认证或者Md5认证
// Use bcrypt (Md5 depreciated)
func (this *AuthService) Login(emailOrUsername, pwd string) (info.User, error) { func (this *AuthService) Login(emailOrUsername, pwd string) (info.User, error) {
emailOrUsername = strings.Trim(emailOrUsername, " ") emailOrUsername = strings.Trim(emailOrUsername, " ")
// pwd = strings.Trim(pwd, " ") // pwd = strings.Trim(pwd, " ")
userInfo := userService.GetUserInfoByName(emailOrUsername) userInfo := userService.GetUserInfoByName(emailOrUsername)
passwd := userInfo.Pwd if userInfo.UserId == "" || !ComparePwd(pwd, userInfo.Pwd) {
if len(passwd) == 32 && Md5(pwd) != passwd { return userInfo, errors.New("wrong username or password")
return userInfo, errors.New("wrong username or password") }
}
if len(passwd) > 32 {
hex := []byte(passwd)
if !CompareHash(hex, pwd) {
return userInfo, errors.New("wrong username or password")
}
}
return userInfo, nil return userInfo, nil
} }
@ -51,37 +45,35 @@ func (this *AuthService) Register(email, pwd, fromUserId string) (bool, string)
if userService.IsExistsUser(email) { if userService.IsExistsUser(email) {
return false, "userHasBeenRegistered-" + email return false, "userHasBeenRegistered-" + email
} }
digest, err := GenerateHash(pwd) passwd := GenPwd(pwd)
if err != nil { if passwd == "" {
return false,"GenerateHash error" return false, "GenerateHash error"
} }
passwd := string(digest)
user := info.User{UserId: bson.NewObjectId(), Email: email, Username: email, Pwd: passwd} user := info.User{UserId: bson.NewObjectId(), Email: email, Username: email, Pwd: passwd}
if fromUserId != "" && IsObjectId(fromUserId) { if fromUserId != "" && IsObjectId(fromUserId) {
user.FromUserId = bson.ObjectIdHex(fromUserId) user.FromUserId = bson.ObjectIdHex(fromUserId)
} }
LogJ(user)
return this.register(user) return this.register(user)
} }
func (this *AuthService) register(user info.User) (bool, string) { func (this *AuthService) register(user info.User) (bool, string) {
if userService.AddUser(user) { if userService.AddUser(user) {
// 添加笔记本, 生活, 学习, 工作 // 添加笔记本, 生活, 学习, 工作
userId := user.UserId.Hex(); userId := user.UserId.Hex()
notebook := info.Notebook{ notebook := info.Notebook{
Seq: -1, Seq: -1,
UserId: user.UserId} UserId: user.UserId}
title2Id := map[string]bson.ObjectId{"life": bson.NewObjectId(), "study": bson.NewObjectId(), "work": bson.NewObjectId()} title2Id := map[string]bson.ObjectId{"life": bson.NewObjectId(), "study": bson.NewObjectId(), "work": bson.NewObjectId()}
for title, objectId := range title2Id { for title, objectId := range title2Id {
notebook.Title = title notebook.Title = title
notebook.NotebookId = objectId notebook.NotebookId = objectId
notebook.UserId = user.UserId notebook.UserId = user.UserId
notebookService.AddNotebook(notebook); notebookService.AddNotebook(notebook)
} }
// 添加leanote -> 该用户的共享 // 添加leanote -> 该用户的共享
registerSharedUserId := configService.GetGlobalStringConfig("registerSharedUserId") registerSharedUserId := configService.GetGlobalStringConfig("registerSharedUserId")
if(registerSharedUserId != "") { if registerSharedUserId != "" {
registerSharedNotebooks := configService.GetGlobalArrMapConfig("registerSharedNotebooks") registerSharedNotebooks := configService.GetGlobalArrMapConfig("registerSharedNotebooks")
registerSharedNotes := configService.GetGlobalArrMapConfig("registerSharedNotes") registerSharedNotes := configService.GetGlobalArrMapConfig("registerSharedNotes")
registerCopyNoteIds := configService.GetGlobalArrayConfig("registerCopyNoteIds") registerCopyNoteIds := configService.GetGlobalArrayConfig("registerCopyNoteIds")
@ -89,21 +81,21 @@ func (this *AuthService) register(user info.User) (bool, string) {
// 添加共享笔记本 // 添加共享笔记本
for _, notebook := range registerSharedNotebooks { for _, notebook := range registerSharedNotebooks {
perm, _ := strconv.Atoi(notebook["perm"]) perm, _ := strconv.Atoi(notebook["perm"])
shareService.AddShareNotebookToUserId(notebook["notebookId"], perm, registerSharedUserId, userId); shareService.AddShareNotebookToUserId(notebook["notebookId"], perm, registerSharedUserId, userId)
} }
// 添加共享笔记 // 添加共享笔记
for _, note := range registerSharedNotes { for _, note := range registerSharedNotes {
perm, _ := strconv.Atoi(note["perm"]) perm, _ := strconv.Atoi(note["perm"])
shareService.AddShareNoteToUserId(note["noteId"], perm, registerSharedUserId, userId); shareService.AddShareNoteToUserId(note["noteId"], perm, registerSharedUserId, userId)
} }
// 复制笔记 // 复制笔记
for _, noteId := range registerCopyNoteIds { for _, noteId := range registerCopyNoteIds {
note := noteService.CopySharedNote(noteId, title2Id["life"].Hex(), registerSharedUserId, user.UserId.Hex()); note := noteService.CopySharedNote(noteId, title2Id["life"].Hex(), registerSharedUserId, user.UserId.Hex())
// Log(noteId) // Log(noteId)
// Log("Copy") // Log("Copy")
// LogJ(note) // LogJ(note)
noteUpdate := bson.M{"IsBlog": false} // 不要是博客 noteUpdate := bson.M{"IsBlog": false} // 不要是博客
noteService.UpdateNote(user.UserId.Hex(), note.NoteId.Hex(), noteUpdate, -1) noteService.UpdateNote(user.UserId.Hex(), note.NoteId.Hex(), noteUpdate, -1)
} }
@ -112,11 +104,11 @@ func (this *AuthService) register(user info.User) (bool, string) {
//--------------- //---------------
// 添加一条userBlog // 添加一条userBlog
blogService.UpdateUserBlog(info.UserBlog{UserId: user.UserId, blogService.UpdateUserBlog(info.UserBlog{UserId: user.UserId,
Title: user.Username + " 's Blog", Title: user.Username + " 's Blog",
SubTitle: "Love Leanote!", SubTitle: "Love Leanote!",
AboutMe: "Hello, I am (^_^)", AboutMe: "Hello, I am (^_^)",
CanComment: true, CanComment: true,
}) })
// 添加一个单页面 // 添加一个单页面
blogService.AddOrUpdateSingle(user.UserId.Hex(), "", "About Me", "Hello, I am (^_^)") blogService.AddOrUpdateSingle(user.UserId.Hex(), "", "About Me", "Hello, I am (^_^)")
} }
@ -131,7 +123,7 @@ func (this *AuthService) register(user info.User) (bool, string) {
func (this *AuthService) getUsername(thirdType, thirdUsername string) (username string) { func (this *AuthService) getUsername(thirdType, thirdUsername string) (username string) {
username = thirdType + "-" + thirdUsername username = thirdType + "-" + thirdUsername
i := 1 i := 1
for ;; { for {
if !userService.IsExistsUserByUsername(username) { if !userService.IsExistsUserByUsername(username) {
return return
} }
@ -148,10 +140,10 @@ func (this *AuthService) ThirdRegister(thirdType, thirdUserId, thirdUsername str
username := this.getUsername(thirdType, thirdUsername) username := this.getUsername(thirdType, thirdUsername)
userInfo = info.User{UserId: bson.NewObjectId(), userInfo = info.User{UserId: bson.NewObjectId(),
Username: username, Username: username,
ThirdUserId: thirdUserId, ThirdUserId: thirdUserId,
ThirdUsername: thirdUsername, ThirdUsername: thirdUsername,
} }
_, _ = this.register(userInfo) _, _ = this.register(userInfo)
return return
} }

View File

@ -295,10 +295,16 @@ func (this *UserService) UpdateAvatar(userId, avatarPath string) (bool) {
// 已经登录了的用户修改密码 // 已经登录了的用户修改密码
func (this *UserService) UpdatePwd(userId, oldPwd, pwd string) (bool, string) { func (this *UserService) UpdatePwd(userId, oldPwd, pwd string) (bool, string) {
userInfo := this.GetUserInfo(userId) userInfo := this.GetUserInfo(userId)
if userInfo.Pwd != Md5(oldPwd) { if !ComparePwd(oldPwd, userInfo.Pwd) {
return false, "oldPasswordError" return false, "oldPasswordError"
} }
ok := db.UpdateByQField(db.Users, bson.M{"_id": bson.ObjectIdHex(userId)}, "Pwd", Md5(pwd))
passwd := GenPwd(pwd)
if passwd == "" {
return false, "GenerateHash error"
}
ok := db.UpdateByQField(db.Users, bson.M{"_id": bson.ObjectIdHex(userId)}, "Pwd", passwd)
return ok, "" return ok, ""
} }
@ -307,7 +313,12 @@ func (this *UserService) ResetPwd(adminUserId, userId, pwd string) (ok bool, msg
if configService.GetAdminUserId() != adminUserId { if configService.GetAdminUserId() != adminUserId {
return return
} }
ok = db.UpdateByQField(db.Users, bson.M{"_id": bson.ObjectIdHex(userId)}, "Pwd", Md5(pwd))
passwd := GenPwd(pwd)
if passwd == "" {
return false, "GenerateHash error"
}
ok = db.UpdateByQField(db.Users, bson.M{"_id": bson.ObjectIdHex(userId)}, "Pwd", passwd)
return return
} }