作者 唐旭辉

登录认证

@@ -4,16 +4,17 @@ import ( @@ -4,16 +4,17 @@ import (
4 "bytes" 4 "bytes"
5 "encoding/json" 5 "encoding/json"
6 "fmt" 6 "fmt"
7 - "gitlab.fjmaimaimai.com/mmm-go/gocomm/identity/uid"  
8 "html/template" 7 "html/template"
9 "strings" 8 "strings"
10 "time" 9 "time"
11 10
  11 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/identity/uid"
  12 +
12 "ability/internal/repository" 13 "ability/internal/repository"
13 "ability/models" 14 "ability/models"
14 "ability/protocol" 15 "ability/protocol"
15 - s_sms "ability/services/sms"  
16 s_im "ability/services/im" 16 s_im "ability/services/im"
  17 + s_sms "ability/services/sms"
17 18
18 "github.com/astaxie/beego" 19 "github.com/astaxie/beego"
19 "gitlab.fjmaimaimai.com/mmm-go/gocomm/common" 20 "gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
@@ -40,7 +41,7 @@ func assertImplement() { @@ -40,7 +41,7 @@ func assertImplement() {
40 41
41 var ( 42 var (
42 //服务 43 //服务
43 - sms s_sms.ISmsService = &s_sms.YunPianSmsService{} 44 + sms s_sms.ISmsService = &s_sms.YunPianSmsService{}
44 ConfigRepository repository.IConfigRepository = &repository.ConfigRepository{} 45 ConfigRepository repository.IConfigRepository = &repository.ConfigRepository{}
45 //仓储 46 //仓储
46 UserRepository repository.IUserRepository = &repository.UserRepository{} 47 UserRepository repository.IUserRepository = &repository.UserRepository{}
@@ -49,9 +50,9 @@ var ( @@ -49,9 +50,9 @@ var (
49 //登录 50 //登录
50 func (s *AuthService) Login(request *protocol.LoginRequest) (rsp *protocol.LoginResponse, err error) { 51 func (s *AuthService) Login(request *protocol.LoginRequest) (rsp *protocol.LoginResponse, err error) {
51 var ( 52 var (
52 - user *models.Users  
53 - userInfo *models.UserInfo  
54 - result bool 53 + user *models.Users
  54 + userInfo *models.UserInfo
  55 + result bool
55 checkImResponse *protocol.CheckImResponse 56 checkImResponse *protocol.CheckImResponse
56 ) 57 )
57 user, err = UserRepository.GetUsersByMobile(request.Phone) 58 user, err = UserRepository.GetUsersByMobile(request.Phone)
@@ -89,19 +90,19 @@ Success: @@ -89,19 +90,19 @@ Success:
89 if userInfo.Auth == "" { 90 if userInfo.Auth == "" {
90 userInfo.Auth = uid.NewV1().StringNoDash() 91 userInfo.Auth = uid.NewV1().StringNoDash()
91 } 92 }
92 - if checkImResponse,err=CheckIm(&protocol.CheckImRequest{  
93 - Uid:fmt.Sprintf("%v",userInfo.Id),  
94 - Uname:userInfo.Uname,  
95 - Icon:userInfo.Icon,  
96 - IsCreated:userInfo.ImToken=="",  
97 - });err!=nil{ 93 + if checkImResponse, err = CheckIm(&protocol.CheckImRequest{
  94 + Uid: fmt.Sprintf("%v", userInfo.Id),
  95 + Uname: userInfo.Uname,
  96 + Icon: userInfo.Icon,
  97 + IsCreated: userInfo.ImToken == "",
  98 + }); err != nil {
98 return 99 return
99 } 100 }
100 userInfo.ImToken = checkImResponse.ImToken 101 userInfo.ImToken = checkImResponse.ImToken
101 - if userInfo.CsAccount==0{ 102 + if userInfo.CsAccount == 0 {
102 userInfo.CsAccount = checkImResponse.CsAccount 103 userInfo.CsAccount = checkImResponse.CsAccount
103 } 104 }
104 - userInfo.AuthExp = time.Now().Add(time.Second*protocol.TokenExpire) 105 + userInfo.AuthExp = time.Now().Add(time.Second * protocol.TokenExpire)
105 if err = UserRepository.UpdateUserInfoById(userInfo); err != nil { 106 if err = UserRepository.UpdateUserInfoById(userInfo); err != nil {
106 return 107 return
107 } 108 }
@@ -149,7 +150,7 @@ func (s *AuthService) AccessToken(request *protocol.AccessTokenRequest) (rsp *pr @@ -149,7 +150,7 @@ func (s *AuthService) AccessToken(request *protocol.AccessTokenRequest) (rsp *pr
149 //刷新token 150 //刷新token
150 func (s *AuthService) RefreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.RefreshTokenResponse, err error) { 151 func (s *AuthService) RefreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.RefreshTokenResponse, err error) {
151 var ( 152 var (
152 - userInfo *models.UserInfo 153 + userInfo *models.UserInfo
153 ) 154 )
154 _, err = ConfigRepository.GetCfgClient(request.ClientId, request.ClientSecret) 155 _, err = ConfigRepository.GetCfgClient(request.ClientId, request.ClientSecret)
155 if err != nil { 156 if err != nil {
@@ -326,93 +327,92 @@ Fail: @@ -326,93 +327,92 @@ Fail:
326 return 327 return
327 } 328 }
328 329
329 -func CheckIm(request *protocol.CheckImRequest)(rsp *protocol.CheckImResponse,err error){  
330 - var (  
331 -  
332 - ) 330 +func CheckIm(request *protocol.CheckImRequest) (rsp *protocol.CheckImResponse, err error) {
  331 + var ()
333 if beego.BConfig.RunMode != "prod" { 332 if beego.BConfig.RunMode != "prod" {
334 return 333 return
335 } 334 }
336 - rsp =&protocol.CheckImResponse{}  
337 - if !request.IsCreated{  
338 - if err = imUserCreate(request,rsp);err!=nil{ 335 + rsp = &protocol.CheckImResponse{}
  336 + if !request.IsCreated {
  337 + if err = imUserCreate(request, rsp); err != nil {
339 return 338 return
340 } 339 }
341 - }else{  
342 - if err = imUserInfoUpdate(request,rsp);err!=nil{ 340 + } else {
  341 + if err = imUserInfoUpdate(request, rsp); err != nil {
343 return 342 return
344 } 343 }
345 } 344 }
346 - if err = imUserRefreshToken(request,rsp);err!=nil{ 345 + if err = imUserRefreshToken(request, rsp); err != nil {
347 return 346 return
348 } 347 }
349 return 348 return
350 } 349 }
  350 +
351 //create 351 //create
352 -func imUserCreate(request *protocol.CheckImRequest,rsp *protocol.CheckImResponse)(err error){  
353 - var(  
354 - param s_im.UserCreate=s_im.UserCreate{  
355 - Accid:request.Uid,  
356 - Name:request.Uname,  
357 - Icon:request.Icon, 352 +func imUserCreate(request *protocol.CheckImRequest, rsp *protocol.CheckImResponse) (err error) {
  353 + var (
  354 + param s_im.UserCreate = s_im.UserCreate{
  355 + Accid: request.Uid,
  356 + Name: request.Uname,
  357 + Icon: request.Icon,
358 } 358 }
359 resp []byte 359 resp []byte
360 - out s_im.UserCreateResult 360 + out s_im.UserCreateResult
361 ) 361 )
362 - if resp,err=s_im.DefaultImClient.Call(param);err!=nil{ 362 + if resp, err = s_im.DefaultImClient.Call(param); err != nil {
363 return 363 return
364 } 364 }
365 - if err = json.Unmarshal(resp,&out);err!=nil{ 365 + if err = json.Unmarshal(resp, &out); err != nil {
366 return 366 return
367 } 367 }
368 - if out.Code!=200 || (out.Info.Accid!=request.Uid){ 368 + if out.Code != 200 || (out.Info.Accid != request.Uid) {
369 return s_im.ErrorFailCall 369 return s_im.ErrorFailCall
370 } 370 }
371 rsp.ImToken = out.Info.Token 371 rsp.ImToken = out.Info.Token
372 return 372 return
373 } 373 }
  374 +
374 //update user info 375 //update user info
375 -func imUserInfoUpdate(request *protocol.CheckImRequest,rsp *protocol.CheckImResponse)(err error){  
376 - var(  
377 - param s_im.UserUpdateUinfo=s_im.UserUpdateUinfo{  
378 - Accid:request.Uid,  
379 - Name:request.Uname,  
380 - Icon:request.Icon, 376 +func imUserInfoUpdate(request *protocol.CheckImRequest, rsp *protocol.CheckImResponse) (err error) {
  377 + var (
  378 + param s_im.UserUpdateUinfo = s_im.UserUpdateUinfo{
  379 + Accid: request.Uid,
  380 + Name: request.Uname,
  381 + Icon: request.Icon,
381 } 382 }
382 resp []byte 383 resp []byte
383 - out s_im.BaseResp 384 + out s_im.BaseResp
384 ) 385 )
385 - if resp,err=s_im.DefaultImClient.Call(param);err!=nil{ 386 + if resp, err = s_im.DefaultImClient.Call(param); err != nil {
386 return 387 return
387 } 388 }
388 - if err = json.Unmarshal(resp,&out);err!=nil{ 389 + if err = json.Unmarshal(resp, &out); err != nil {
389 return 390 return
390 } 391 }
391 - if out.Code!=200{ 392 + if out.Code != 200 {
392 return s_im.ErrorFailCall 393 return s_im.ErrorFailCall
393 } 394 }
394 return 395 return
395 } 396 }
  397 +
396 //refresh token 398 //refresh token
397 -func imUserRefreshToken(request *protocol.CheckImRequest,rsp *protocol.CheckImResponse)(err error){  
398 - var(  
399 - param s_im.UserRefreshToken=s_im.UserRefreshToken{  
400 - Accid:request.Uid, 399 +func imUserRefreshToken(request *protocol.CheckImRequest, rsp *protocol.CheckImResponse) (err error) {
  400 + var (
  401 + param s_im.UserRefreshToken = s_im.UserRefreshToken{
  402 + Accid: request.Uid,
401 } 403 }
402 resp []byte 404 resp []byte
403 - out s_im.UserCreateResult 405 + out s_im.UserCreateResult
404 ) 406 )
405 - if resp,err=s_im.DefaultImClient.Call(param);err!=nil{ 407 + if resp, err = s_im.DefaultImClient.Call(param); err != nil {
406 return 408 return
407 } 409 }
408 - if err = json.Unmarshal(resp,&out);err!=nil{ 410 + if err = json.Unmarshal(resp, &out); err != nil {
409 return 411 return
410 } 412 }
411 - if out.Code!=200 || (out.Info.Accid!=request.Uid){ 413 + if out.Code != 200 || (out.Info.Accid != request.Uid) {
412 return s_im.ErrorFailCall 414 return s_im.ErrorFailCall
413 } 415 }
414 rsp.ImToken = out.Info.Token 416 rsp.ImToken = out.Info.Token
415 return 417 return
416 } 418 }
417 -  
418 -  
1 package auth 1 package auth
2 2
  3 +import (
  4 + "ability/protocol"
  5 + "math/rand"
  6 + "strings"
  7 + "time"
  8 +
  9 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
  10 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
  11 +)
  12 +
3 type ILoginAuth interface { 13 type ILoginAuth interface {
4 - LoginAuth() error  
5 - TODO(v interface{}) error 14 + LoginAuth() (*UserIdentity, error) //登录认证操作,返回身份ID和错误
  15 + TODO(v interface{}) error //预留,不知道干嘛用,也许可以用来获取取认证结果,或进行认证后附带的操作
  16 +}
  17 +
  18 +type UserIdentity struct {
  19 + Auth string `json:"auth`
  20 + Uid int64 `json:"uid"`
  21 + Imtoken string `json:"imtoken"`
  22 + Accid int64 `json:"accid"`
  23 + Icon string `json:"icon"`
  24 + Uname string `json:"uname"`
6 } 25 }
7 26
8 //TODO 27 //TODO
@@ -10,12 +29,28 @@ type ILoginAuth interface { @@ -10,12 +29,28 @@ type ILoginAuth interface {
10 type LoginByPassword struct { 29 type LoginByPassword struct {
11 Username string 30 Username string
12 Password string 31 Password string
  32 + err error
  33 +}
  34 +
  35 +func NewLoginByPassword(username string, password string) *LoginByPassword {
  36 + return &LoginByPassword{
  37 + Username: username,
  38 + Password: password,
  39 + }
13 } 40 }
14 41
15 //手机短信登录 42 //手机短信登录
16 type LoginBySms struct { 43 type LoginBySms struct {
17 Phone string 44 Phone string
18 Code string 45 Code string
  46 + err error
  47 +}
  48 +
  49 +func NewLoginBySms(phone string, code string) *LoginBySms {
  50 + return &LoginBySms{
  51 + Phone: phone,
  52 + Code: code,
  53 + }
19 } 54 }
20 55
21 //其他第三方登录 56 //其他第三方登录
@@ -30,19 +65,66 @@ var ( @@ -30,19 +65,66 @@ var (
30 _ ILoginAuth = LoginByXxxx{} 65 _ ILoginAuth = LoginByXxxx{}
31 ) 66 )
32 67
33 -func (o LoginByPassword) LoginAuth() error {  
34 - return nil 68 +func (o LoginByPassword) LoginAuth() (*UserIdentity, error) {
  69 + user, err := UserRepository.GetUserInfoByMobile(o.Username)
  70 + if err != nil {
  71 + log.Error(err)
  72 + o.err = common.NewError(2020, err) //账号不存在
  73 + return nil, o.err
  74 + }
  75 + if strings.Compare(user.Passwd, o.Password) != 0 {
  76 + o.err = common.NewError(2021, err) //登录密码错误
  77 + return nil, o.err
  78 + }
  79 + identity := &UserIdentity{
  80 + Uid: user.Id,
  81 + Imtoken: user.ImToken,
  82 + Accid: user.CsAccount,
  83 + Icon: user.Icon,
  84 + Uname: user.Uname,
  85 + }
  86 + return identity, nil
35 } 87 }
36 88
37 -func (o LoginBySms) LoginAuth() error {  
38 - return nil 89 +func (o LoginBySms) LoginAuth() (*UserIdentity, error) {
  90 + user, err := UserRepository.GetUserInfoByMobile(o.Phone)
  91 + if err != nil {
  92 + log.Error(err)
  93 + o.err = common.NewError(2020, err) //账号不存在
  94 + return nil, o.err
  95 + }
  96 + result, err := CheckSmsCode(o.Phone, o.Code, protocol.SmsLoginCode)
  97 + if err != nil && !result {
  98 + return nil, err
  99 + }
  100 + identity := &UserIdentity{
  101 + Uid: user.Id,
  102 + Imtoken: user.ImToken,
  103 + Accid: user.CsAccount,
  104 + Icon: user.Icon,
  105 + Uname: user.Uname,
  106 + }
  107 + return identity, nil
39 } 108 }
40 109
41 -func (o LoginByXxxx) LoginAuth() error {  
42 - return nil 110 +func (o LoginByXxxx) LoginAuth() (*UserIdentity, error) {
  111 + //TODO
  112 + return nil, nil
43 } 113 }
44 114
45 func (o LoginByPassword) TODO(v interface{}) error { 115 func (o LoginByPassword) TODO(v interface{}) error {
  116 + // rv := reflect.ValueOf(v)
  117 + // if rv.Kind() != reflect.Ptr {
  118 + // return errors.New("v must be point")
  119 + // }
  120 + // rv = rv.Elem()
  121 + // if ok := rv.CanSet(); !ok {
  122 + // panic("v can not set")
  123 + // }
  124 + // a := "random string"
  125 + // ra := reflect.ValueOf(a)
  126 + // rv.Set(ra)
  127 + //生成并更新authcode
46 return nil 128 return nil
47 } 129 }
48 130
@@ -53,3 +135,15 @@ func (o LoginBySms) TODO(v interface{}) error { @@ -53,3 +135,15 @@ func (o LoginBySms) TODO(v interface{}) error {
53 func (o LoginByXxxx) TODO(v interface{}) error { 135 func (o LoginByXxxx) TODO(v interface{}) error {
54 return nil 136 return nil
55 } 137 }
  138 +
  139 +func randomString(l int) string {
  140 + str := "0123456789abcdefghijklmnopqrstuvwxyz"
  141 + bytes := []byte(str)
  142 + result := []byte{}
  143 + r := rand.New(rand.NewSource(time.Now().UnixNano()))
  144 + for i := 0; i < l; i++ {
  145 + result = append(result, bytes[r.Intn(len(bytes))])
  146 + }
  147 + return string(result)
  148 +
  149 +}