正在显示
5 个修改的文件
包含
89 行增加
和
44 行删除
@@ -152,3 +152,12 @@ func DeleteCfgClient(id int) (err error) { | @@ -152,3 +152,12 @@ func DeleteCfgClient(id int) (err error) { | ||
152 | } | 152 | } |
153 | return | 153 | return |
154 | } | 154 | } |
155 | + | ||
156 | +func GetCfgClient(clintId, clientSecret string) (v *CfgClient, err error) { | ||
157 | + o := orm.NewOrm() | ||
158 | + sql := "select * from cfg_client where enabled=1 and client_id=? and client_secret=?" | ||
159 | + if err = o.Raw(sql, clintId, clientSecret).QueryRow(&v); err == nil { | ||
160 | + return v, nil | ||
161 | + } | ||
162 | + return nil, err | ||
163 | +} |
@@ -11,7 +11,7 @@ import ( | @@ -11,7 +11,7 @@ import ( | ||
11 | ) | 11 | ) |
12 | 12 | ||
13 | type UserInfo struct { | 13 | type UserInfo struct { |
14 | - Id int64 `orm:"column(uid);pk" description:"用户ID"` | 14 | + Id int64 `orm:"column(uid);pk" description:"用户ID"` |
15 | Uname string `orm:"column(uname);size(100)" description:"名称"` | 15 | Uname string `orm:"column(uname);size(100)" description:"名称"` |
16 | Icon string `orm:"column(icon);size(128)" description:"头像"` | 16 | Icon string `orm:"column(icon);size(128)" description:"头像"` |
17 | Width int `orm:"column(width)" description:"宽度"` | 17 | Width int `orm:"column(width)" description:"宽度"` |
@@ -190,30 +190,47 @@ func DeleteUserInfo(id int64) (err error) { | @@ -190,30 +190,47 @@ func DeleteUserInfo(id int64) (err error) { | ||
190 | return | 190 | return |
191 | } | 191 | } |
192 | 192 | ||
193 | -func GetUserInfoByMobile(mobile string)(v *UserInfo, err error) { | 193 | +func GetUserInfoByMobile(mobile string) (v *UserInfo, err error) { |
194 | o := orm.NewOrm() | 194 | o := orm.NewOrm() |
195 | - sql :="select * from user_info where phone=?" | ||
196 | - if err = o.Raw(sql,mobile).QueryRow(&v); err == nil { | 195 | + sql := "select * from user_info where phone=?" |
196 | + if err = o.Raw(sql, mobile).QueryRow(&v); err == nil { | ||
197 | return v, nil | 197 | return v, nil |
198 | } | 198 | } |
199 | return nil, err | 199 | return nil, err |
200 | } | 200 | } |
201 | 201 | ||
202 | -func GetUserInfoByClientId(clintId string)(v *UserInfo, err error) { | 202 | +func GetUserInfoByClientId(clintId string) (v *UserInfo, err error) { |
203 | o := orm.NewOrm() | 203 | o := orm.NewOrm() |
204 | - sql :="select * from user_info where clientId=?" | ||
205 | - if err = o.Raw(sql,clintId).QueryRow(&v); err == nil { | 204 | + sql := "select * from user_info where clientId=?" |
205 | + if err = o.Raw(sql, clintId).QueryRow(&v); err == nil { | ||
206 | return v, nil | 206 | return v, nil |
207 | } | 207 | } |
208 | return nil, err | 208 | return nil, err |
209 | } | 209 | } |
210 | 210 | ||
211 | -func GetUserInfoByToken(token string)(v *UserInfo, err error) { | 211 | +func GetUserInfoByToken(token string) (v *UserInfo, err error) { |
212 | o := orm.NewOrm() | 212 | o := orm.NewOrm() |
213 | - sql :="select * from user_info where access_token=? and access_exp >= Now() and enabled = 1" | ||
214 | - if err = o.Raw(sql,token).QueryRow(&v); err == nil { | 213 | + sql := "select * from user_info where access_token=? and access_exp >= Now() and enabled = 1" |
214 | + if err = o.Raw(sql, token).QueryRow(&v); err == nil { | ||
215 | return v, nil | 215 | return v, nil |
216 | } | 216 | } |
217 | return nil, err | 217 | return nil, err |
218 | } | 218 | } |
219 | 219 | ||
220 | +func GetUserInfoByAuthCode(authCode string) (v *UserInfo, err error) { | ||
221 | + o := orm.NewOrm() | ||
222 | + sql := "select * from user_info where auth=? and auth_exp >= Now() and enabled = 1" | ||
223 | + if err = o.Raw(sql, authCode).QueryRow(&v); err == nil { | ||
224 | + return v, nil | ||
225 | + } | ||
226 | + return nil, err | ||
227 | +} | ||
228 | + | ||
229 | +func GetUserInfoByRefreshToken(refreshToken string) (v *UserInfo, err error) { | ||
230 | + o := orm.NewOrm() | ||
231 | + sql := "select * from user_info where refresh_token=? and auth_exp >= Now() and enabled = 1" | ||
232 | + if err = o.Raw(sql, refreshToken).QueryRow(&v); err == nil { | ||
233 | + return v, nil | ||
234 | + } | ||
235 | + return nil, err | ||
236 | +} |
@@ -7,6 +7,8 @@ const ( | @@ -7,6 +7,8 @@ const ( | ||
7 | LoginTypeSmdcode = "signInCaptcha" | 7 | LoginTypeSmdcode = "signInCaptcha" |
8 | ) | 8 | ) |
9 | 9 | ||
10 | +const TokenExpire = 3600 | ||
11 | + | ||
10 | var Nums = []byte("0123456789") | 12 | var Nums = []byte("0123456789") |
11 | 13 | ||
12 | type RequestHeader struct { | 14 | type RequestHeader struct { |
@@ -2,30 +2,31 @@ package protocol | @@ -2,30 +2,31 @@ package protocol | ||
2 | 2 | ||
3 | import "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego" | 3 | import "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego" |
4 | 4 | ||
5 | -func InitMessageCode(){ | 5 | +func InitMessageCode() { |
6 | messages := []struct { | 6 | messages := []struct { |
7 | Code int | 7 | Code int |
8 | Msg string | 8 | Msg string |
9 | }{ | 9 | }{ |
10 | - {113,"签名验证失败"}, | ||
11 | - {1009,"验证码已超时,登录失败"}, | ||
12 | - {1011,"短信验证码次数超过限制,请稍后重试"}, | ||
13 | - {1012,"验证码错误"}, | 10 | + {101, "clientId或clientSecret无效"}, |
11 | + {113, "签名验证失败"}, | ||
12 | + {1009, "验证码已超时,登录失败"}, | ||
13 | + {1011, "短信验证码次数超过限制,请稍后重试"}, | ||
14 | + {1012, "验证码错误"}, | ||
14 | 15 | ||
15 | - {2001,"请输入正确的手机号码"}, | ||
16 | - {2002,"后台未配置账号信息,请联系管理员配置"}, | ||
17 | - {2009,"上传的文件流为空"}, | ||
18 | - {2020,"帐号不存在,请联系管理员"}, | ||
19 | - {2021,"登录失败,手机号或密码错误"}, | ||
20 | - {2025,"短信验证码验证失败"}, | ||
21 | - {2026,"两次输入的密码不一致"}, | 16 | + {2001, "请输入正确的手机号码"}, |
17 | + {2002, "后台未配置账号信息,请联系管理员配置"}, | ||
18 | + {2009, "上传的文件流为空"}, | ||
19 | + {2020, "帐号不存在,请联系管理员"}, | ||
20 | + {2021, "登录失败,手机号或密码错误"}, | ||
21 | + {2025, "短信验证码验证失败"}, | ||
22 | + {2026, "两次输入的密码不一致"}, | ||
22 | 23 | ||
23 | - {4139,"authCode无效或过期"}, | ||
24 | - {4140,"refreshToken过期,需要重新登录授权"}, | ||
25 | - {4141,"accessToken过期或无效,需要进行重新获取令牌"}, | ||
26 | - {4142,"Uuid已存在,请求失败"}, | 24 | + {4139, "authCode无效或过期"}, |
25 | + {4140, "refreshToken过期,需要重新登录授权"}, | ||
26 | + {4141, "accessToken过期或无效,需要进行重新获取令牌"}, | ||
27 | + {4142, "Uuid已存在,请求失败"}, | ||
27 | } | 28 | } |
28 | - for i:=range messages{ | ||
29 | - mybeego.SetMessage(messages[i].Code,messages[i].Msg) | 29 | + for i := range messages { |
30 | + mybeego.SetMessage(messages[i].Code, messages[i].Msg) | ||
30 | } | 31 | } |
31 | } | 32 | } |
@@ -4,6 +4,7 @@ import ( | @@ -4,6 +4,7 @@ import ( | ||
4 | "bytes" | 4 | "bytes" |
5 | "encoding/json" | 5 | "encoding/json" |
6 | "fmt" | 6 | "fmt" |
7 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/identity/uid" | ||
7 | "html/template" | 8 | "html/template" |
8 | "strings" | 9 | "strings" |
9 | "time" | 10 | "time" |
@@ -83,6 +84,12 @@ Success: | @@ -83,6 +84,12 @@ Success: | ||
83 | log.Error(err) | 84 | log.Error(err) |
84 | return | 85 | return |
85 | } | 86 | } |
87 | + if userInfo.Auth == "" { | ||
88 | + userInfo.Auth = uid.NewV1().StringNoDash() | ||
89 | + } | ||
90 | + if err = models.UpdateUserInfoById(userInfo); err != nil { | ||
91 | + return | ||
92 | + } | ||
86 | rsp = &protocol.LoginResponse{AuthCode: userInfo.Auth} | 93 | rsp = &protocol.LoginResponse{AuthCode: userInfo.Auth} |
87 | return | 94 | return |
88 | } | 95 | } |
@@ -99,19 +106,26 @@ func (s *AuthService) AccessToken(request *protocol.AccessTokenRequest) (rsp *pr | @@ -99,19 +106,26 @@ func (s *AuthService) AccessToken(request *protocol.AccessTokenRequest) (rsp *pr | ||
99 | var ( | 106 | var ( |
100 | userInfo *models.UserInfo | 107 | userInfo *models.UserInfo |
101 | ) | 108 | ) |
102 | - userInfo, err = models.GetUserInfoByClientId(request.ClientId) | 109 | + _, err = models.GetCfgClient(request.ClientId, request.ClientSecret) |
110 | + if err != nil { | ||
111 | + common.NewError(101, err) | ||
112 | + return | ||
113 | + } | ||
114 | + userInfo, err = models.GetUserInfoByAuthCode(request.AuthCode) | ||
103 | if err != nil { | 115 | if err != nil { |
116 | + common.NewError(4139, err) | ||
104 | return | 117 | return |
105 | } | 118 | } |
106 | - if strings.Compare(userInfo.Auth, request.AuthCode) != 0 { | ||
107 | - err = common.NewErrorWithMsg(2, "auth code error.") | 119 | + userInfo.AccessToken = uid.NewV1().StringNoDash() |
120 | + userInfo.RefreshToken = uid.NewV1().StringNoDash() | ||
121 | + if err = models.UpdateUserInfoById(userInfo); err != nil { | ||
108 | return | 122 | return |
109 | } | 123 | } |
110 | //valid token | 124 | //valid token |
111 | rsp = &protocol.AccessTokenResponse{ | 125 | rsp = &protocol.AccessTokenResponse{ |
112 | RefreshToken: userInfo.RefreshToken, | 126 | RefreshToken: userInfo.RefreshToken, |
113 | AccessToken: userInfo.AccessToken, | 127 | AccessToken: userInfo.AccessToken, |
114 | - ExpiresIn: 3600, | 128 | + ExpiresIn: protocol.TokenExpire, |
115 | } | 129 | } |
116 | return | 130 | return |
117 | } | 131 | } |
@@ -122,33 +136,35 @@ func (s *AuthService) RefreshToken(request *protocol.RefreshTokenRequest) (rsp * | @@ -122,33 +136,35 @@ func (s *AuthService) RefreshToken(request *protocol.RefreshTokenRequest) (rsp * | ||
122 | userInfo *models.UserInfo | 136 | userInfo *models.UserInfo |
123 | newAccess *protocol.Access | 137 | newAccess *protocol.Access |
124 | ) | 138 | ) |
125 | - userInfo, err = models.GetUserInfoByClientId(request.ClientId) | 139 | + _, err = models.GetCfgClient(request.ClientId, request.ClientSecret) |
126 | if err != nil { | 140 | if err != nil { |
141 | + common.NewError(101, err) | ||
127 | return | 142 | return |
128 | } | 143 | } |
129 | - if strings.Compare(userInfo.RefreshToken, request.RefreshToken) != 0 { | ||
130 | - err = common.NewErrorWithMsg(2, "refresh token error.") | 144 | + userInfo, err = models.GetUserInfoByAuthCode(request.RefreshToken) |
145 | + if err != nil { | ||
146 | + common.NewError(4139, err) | ||
131 | return | 147 | return |
132 | } | 148 | } |
133 | - request.Uid, request.LoginType = userInfo.Id, "mobile" | ||
134 | - if newAccess, err = refreshToken(request); err != nil { | 149 | + userInfo.AccessToken = uid.NewV1().StringNoDash() |
150 | + if err = models.UpdateUserInfoById(userInfo); err != nil { | ||
135 | return | 151 | return |
136 | } | 152 | } |
137 | rsp = &protocol.RefreshTokenResponse{ | 153 | rsp = &protocol.RefreshTokenResponse{ |
138 | AccessToken: newAccess.AccessToken, | 154 | AccessToken: newAccess.AccessToken, |
139 | RefreshToken: newAccess.RefreshToken, | 155 | RefreshToken: newAccess.RefreshToken, |
140 | - ExpiresIn: 3600, | 156 | + ExpiresIn: protocol.TokenExpire, |
141 | } | 157 | } |
142 | return | 158 | return |
143 | } | 159 | } |
144 | 160 | ||
145 | //刷新token loginType mobile im web | 161 | //刷新token loginType mobile im web |
146 | -func refreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.Access, err error) { | ||
147 | - if request.Uid == 0 { | ||
148 | - return | ||
149 | - } | ||
150 | - return nil, nil | ||
151 | -} | 162 | +//func refreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.Access, err error) { |
163 | +// if request.Uid == 0 { | ||
164 | +// return | ||
165 | +// } | ||
166 | +// return nil, nil | ||
167 | +//} | ||
152 | 168 | ||
153 | //检查token有效性 | 169 | //检查token有效性 |
154 | func (s *AuthService) CheckToken(request *protocol.CheckTokenRequest) (rsp *protocol.CheckTokenResponse, err error) { | 170 | func (s *AuthService) CheckToken(request *protocol.CheckTokenRequest) (rsp *protocol.CheckTokenResponse, err error) { |
-
请 注册 或 登录 后发表评论