|
1
|
-package serveauth
|
|
|
|
2
|
-
|
|
|
|
3
|
-import (
|
|
|
|
4
|
- "crypto/sha1"
|
|
|
|
5
|
- "encoding/hex"
|
|
|
|
6
|
- "fmt"
|
|
|
|
7
|
- "io"
|
|
|
|
8
|
- "oppmg/common/config"
|
|
|
|
9
|
- "oppmg/common/log"
|
|
|
|
10
|
- "oppmg/models"
|
|
|
|
11
|
- "oppmg/protocol"
|
|
|
|
12
|
- "oppmg/utils"
|
|
|
|
13
|
- "strings"
|
|
|
|
14
|
- "time"
|
|
|
|
15
|
-
|
|
|
|
16
|
- "github.com/astaxie/beego/orm"
|
|
|
|
17
|
-)
|
|
|
|
18
|
-
|
|
|
|
19
|
-//GetAccessToken 获取accessToken
|
|
|
|
20
|
-func GetAccessToken(param protocol.RequestCheckSmsCode) (*protocol.DataUserInfo, error) {
|
|
|
|
21
|
- data := &protocol.DataUserInfo{}
|
|
|
|
22
|
- err := protocol.NewErrWithMessage("00000")
|
|
|
|
23
|
- log.Info("log 打印")
|
|
|
|
24
|
- log.Info("%+v", config.MConfig)
|
|
|
|
25
|
- return data, err
|
|
|
|
26
|
-}
|
|
|
|
27
|
-
|
|
|
|
28
|
-//ValidatePassword ...
|
|
|
|
29
|
-//from:待校验的密码;to:比对用的密文
|
|
|
|
30
|
-func validatePassword(from, to string) bool {
|
|
|
|
31
|
- //密码加密方式sha1
|
|
|
|
32
|
- h := sha1.New()
|
|
|
|
33
|
- io.WriteString(h, from)
|
|
|
|
34
|
- str := hex.EncodeToString(h.Sum(nil))
|
|
|
|
35
|
- if strings.Compare(str, to) == 0 {
|
|
|
|
36
|
- return true
|
|
|
|
37
|
- }
|
|
|
|
38
|
- return false
|
|
|
|
39
|
-}
|
|
|
|
40
|
-
|
|
|
|
41
|
-//LoginAuth 登录认证
|
|
|
|
42
|
-func LoginAuthByPassword(account, password string) error {
|
|
|
|
43
|
- var (
|
|
|
|
44
|
- user *models.User
|
|
|
|
45
|
- uAuth *models.UserAuth
|
|
|
|
46
|
- err error
|
|
|
|
47
|
- )
|
|
|
|
48
|
- user, err = models.GetUserByPhone(account)
|
|
|
|
49
|
- if err != nil {
|
|
|
|
50
|
- log.Error(err.Error())
|
|
|
|
51
|
- return protocol.NewErrWithMessage("1", err)
|
|
|
|
52
|
- }
|
|
|
|
53
|
- if ok := validatePassword(password, user.Passwd); !ok {
|
|
|
|
54
|
- return protocol.NewErrWithMessage("1", err)
|
|
|
|
55
|
- }
|
|
|
|
56
|
-
|
|
|
|
57
|
- uAuth, err = models.ReadUserAuthByDevice(user.Id, models.DEVICE_TYPE_WEB)
|
|
|
|
58
|
- if err != nil && err != orm.ErrNoRows {
|
|
|
|
59
|
- e := fmt.Errorf("ReadUserAuthByDevice(%d,%d) err:%s", user.Id, models.DEVICE_TYPE_WEB, err)
|
|
|
|
60
|
- log.Error(e.Error())
|
|
|
|
61
|
- return protocol.NewErrWithMessage("1", e)
|
|
|
|
62
|
- }
|
|
|
|
63
|
- var (
|
|
|
|
64
|
- authcode string
|
|
|
|
65
|
- authcodeExp time.Time
|
|
|
|
66
|
- )
|
|
|
|
67
|
- authcode = utils.GenerateIDByUUID()
|
|
|
|
68
|
- authcodeExp = time.Now().Add(time.Duration(models.AUTHCODE_TIME) * time.Second)
|
|
|
|
69
|
- if err == orm.ErrNoRows {
|
|
|
|
70
|
- uAuth := &models.UserAuth{
|
|
|
|
71
|
- UserId: user.Id,
|
|
|
|
72
|
- AuthCode: authcode,
|
|
|
|
73
|
- AuthCodeExp: authcodeExp,
|
|
|
|
74
|
- CreateAt: time.Now(),
|
|
|
|
75
|
- }
|
|
|
|
76
|
- _, err = models.AddUserAuth(uAuth)
|
|
|
|
77
|
- if err != nil {
|
|
|
|
78
|
- e := fmt.Errorf("AddUserAuth err:%s", err)
|
|
|
|
79
|
- log.Error(e.Error())
|
|
|
|
80
|
- return protocol.NewErrWithMessage("1", e)
|
|
|
|
81
|
- }
|
|
|
|
82
|
- }
|
|
|
|
83
|
- if err == nil {
|
|
|
|
84
|
- uAuth.AuthCode = authcode
|
|
|
|
85
|
- uAuth.AuthCodeExp = authcodeExp
|
|
|
|
86
|
- uAuth.UpdateAt = time.Now()
|
|
|
|
87
|
- err = models.UpdateUserAuthById(uAuth)
|
|
|
|
88
|
- if err != nil {
|
|
|
|
89
|
- e := fmt.Errorf("UpdateUserAuthById err:%s", err)
|
|
|
|
90
|
- log.Error(e.Error())
|
|
|
|
91
|
- return protocol.NewErrWithMessage("1", e)
|
|
|
|
92
|
- }
|
|
|
|
93
|
- }
|
|
|
|
94
|
-
|
|
|
|
95
|
- return nil
|
|
|
|
96
|
-}
|
|
|
|
97
|
-
|
|
|
|
98
|
-//RefreshAccessToken 刷新token
|
|
|
|
99
|
-func RefreshAccessToken(account string, token string) error {
|
|
|
|
100
|
- return nil
|
|
|
|
101
|
-}
|
|
|
|
102
|
-
|
|
|
|
103
|
-// func buildNewUserAuth(uid int64,) *models.UserAuth {
|
|
|
|
104
|
-// m:=&models.UserAuth{
|
|
|
|
105
|
-// User
|
|
|
|
106
|
-// }
|
|
|
|
107
|
-// return nil
|
|
|
|
108
|
-// } |
|
|