作者 yangfu

auth

@@ -18,3 +18,5 @@ @@ -18,3 +18,5 @@
18 *.idea 18 *.idea
19 19
20 *.tmp 20 *.tmp
  21 +
  22 +*.sum
@@ -9,6 +9,6 @@ ADD go.mod . @@ -9,6 +9,6 @@ ADD go.mod .
9 ADD go.sum . 9 ADD go.sum .
10 RUN go mod download 10 RUN go mod download
11 11
12 -RUN GOOS=linux CGO_ENABLED=0 go build -ldflags="-s -w" -installsuffix cgo -o examples_gocomm main.go 12 +RUN GOOS=linux CGO_ENABLED=0 go build -ldflags="-s -w" -installsuffix cgo -o ability main.go
13 13
14 CMD ["/ability"] 14 CMD ["/ability"]
@@ -13,6 +13,7 @@ type AuthController struct { @@ -13,6 +13,7 @@ type AuthController struct {
13 controllers.BaseController 13 controllers.BaseController
14 } 14 }
15 15
  16 +//Login
16 func(this *AuthController)Login(){ 17 func(this *AuthController)Login(){
17 var msg *mybeego.Message 18 var msg *mybeego.Message
18 defer func(){ 19 defer func(){
@@ -30,3 +31,79 @@ func(this *AuthController)Login(){ @@ -30,3 +31,79 @@ func(this *AuthController)Login(){
30 } 31 }
31 msg = this.GenMessage(auth.Login(request)) 32 msg = this.GenMessage(auth.Login(request))
32 } 33 }
  34 +
  35 +//SmsCode
  36 +func(this *AuthController)SmsCode(){
  37 + var msg *mybeego.Message
  38 + defer func(){
  39 + this.Resp(msg)
  40 + }()
  41 + var request *protocol.SmsCodeRequest
  42 + if err:=json.Unmarshal(this.ByteBody,&request);err!=nil{
  43 + log.Error(err)
  44 + msg = mybeego.NewMessage(1)
  45 + return
  46 + }
  47 + if b,m :=this.Valid(request);!b{
  48 + msg = m
  49 + return
  50 + }
  51 + msg = this.GenMessage(auth.SmsCode(request))
  52 +}
  53 +
  54 +//UpdateDevice
  55 +func(this *AuthController)UpdateDevice(){
  56 + var msg *mybeego.Message
  57 + defer func(){
  58 + this.Resp(msg)
  59 + }()
  60 + var request *protocol.UpdateDeviceRequest
  61 + if err:=json.Unmarshal(this.ByteBody,&request);err!=nil{
  62 + log.Error(err)
  63 + msg = mybeego.NewMessage(1)
  64 + return
  65 + }
  66 + if b,m :=this.Valid(request);!b{
  67 + msg = m
  68 + return
  69 + }
  70 + msg = this.GenMessage(auth.UpdateDevice(request))
  71 +}
  72 +
  73 +//AccessToken
  74 +func(this *AuthController)AccessToken(){
  75 + var msg *mybeego.Message
  76 + defer func(){
  77 + this.Resp(msg)
  78 + }()
  79 + var request *protocol.AccessTokenRequest
  80 + if err:=json.Unmarshal(this.ByteBody,&request);err!=nil{
  81 + log.Error(err)
  82 + msg = mybeego.NewMessage(1)
  83 + return
  84 + }
  85 + if b,m :=this.Valid(request);!b{
  86 + msg = m
  87 + return
  88 + }
  89 + msg = this.GenMessage(auth.AccessToken(request))
  90 +}
  91 +
  92 +//RefreshToken
  93 +func(this *AuthController)RefreshToken(){
  94 + var msg *mybeego.Message
  95 + defer func(){
  96 + this.Resp(msg)
  97 + }()
  98 + var request *protocol.RefreshTokenRequest
  99 + if err:=json.Unmarshal(this.ByteBody,&request);err!=nil{
  100 + log.Error(err)
  101 + msg = mybeego.NewMessage(1)
  102 + return
  103 + }
  104 + if b,m :=this.Valid(request);!b{
  105 + msg = m
  106 + return
  107 + }
  108 + msg = this.GenMessage(auth.RefreshToken(request))
  109 +}
@@ -11,7 +11,7 @@ import ( @@ -11,7 +11,7 @@ import (
11 ) 11 )
12 12
13 type UserInfo struct { 13 type UserInfo struct {
14 - Id int `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:"宽度"`
@@ -73,7 +73,7 @@ func AddUserInfo(m *UserInfo) (id int64, err error) { @@ -73,7 +73,7 @@ func AddUserInfo(m *UserInfo) (id int64, err error) {
73 73
74 // GetUserInfoById retrieves UserInfo by Id. Returns error if 74 // GetUserInfoById retrieves UserInfo by Id. Returns error if
75 // Id doesn't exist 75 // Id doesn't exist
76 -func GetUserInfoById(id int) (v *UserInfo, err error) { 76 +func GetUserInfoById(id int64) (v *UserInfo, err error) {
77 o := orm.NewOrm() 77 o := orm.NewOrm()
78 v = &UserInfo{Id: id} 78 v = &UserInfo{Id: id}
79 if err = o.Read(v); err == nil { 79 if err = o.Read(v); err == nil {
@@ -177,7 +177,7 @@ func UpdateUserInfoById(m *UserInfo) (err error) { @@ -177,7 +177,7 @@ func UpdateUserInfoById(m *UserInfo) (err error) {
177 177
178 // DeleteUserInfo deletes UserInfo by Id and returns error if 178 // DeleteUserInfo deletes UserInfo by Id and returns error if
179 // the record to be deleted doesn't exist 179 // the record to be deleted doesn't exist
180 -func DeleteUserInfo(id int) (err error) { 180 +func DeleteUserInfo(id int64) (err error) {
181 o := orm.NewOrm() 181 o := orm.NewOrm()
182 v := UserInfo{Id: id} 182 v := UserInfo{Id: id}
183 // ascertain id exists in the database 183 // ascertain id exists in the database
@@ -198,3 +198,13 @@ func GetUserInfoByMobile(mobile string)(v *UserInfo, err error) { @@ -198,3 +198,13 @@ func GetUserInfoByMobile(mobile string)(v *UserInfo, err error) {
198 } 198 }
199 return nil, err 199 return nil, err
200 } 200 }
  201 +
  202 +func GetUserInfoByClientId(clintId string)(v *UserInfo, err error) {
  203 + o := orm.NewOrm()
  204 + sql :="select * from user_info where clientId=?"
  205 + if err = o.Raw(sql,clintId).QueryRow(&v); err == nil {
  206 + return v, nil
  207 + }
  208 + return nil, err
  209 +}
  210 +
@@ -5,6 +5,7 @@ const ( @@ -5,6 +5,7 @@ const (
5 LoginSmdcode ="signInCaptcha" 5 LoginSmdcode ="signInCaptcha"
6 ) 6 )
7 7
  8 +/*Login */
8 type LoginRequest struct { 9 type LoginRequest struct {
9 Phone string `json:"phone" valid:"Required;Mobile"` 10 Phone string `json:"phone" valid:"Required;Mobile"`
10 Code string `json:"code"` 11 Code string `json:"code"`
@@ -12,8 +13,53 @@ type LoginRequest struct { @@ -12,8 +13,53 @@ type LoginRequest struct {
12 PassWord string `json:"password"` 13 PassWord string `json:"password"`
13 ClientId string `json:"clientId" valid:"Required"` 14 ClientId string `json:"clientId" valid:"Required"`
14 } 15 }
15 -  
16 type LoginResponse struct { 16 type LoginResponse struct {
17 AuthCode string `json:"authCode"` 17 AuthCode string `json:"authCode"`
18 } 18 }
19 19
  20 +/*SmsCode*/
  21 +type SmsCodeRequest struct {
  22 + Phone string `json:"phone" valid:"Required;Mobile"`
  23 +}
  24 +type SmsCodeResponse struct {
  25 +}
  26 +
  27 +/*UpdateDevice*/
  28 +type UpdateDeviceRequest struct {
  29 + ClientId string `json:"clientId" valid:"Required"`
  30 + DeviceToken string `json:"deviceToken"`
  31 +}
  32 +type UpdateDeviceResponse struct {
  33 +}
  34 +
  35 +/*AccessToken */
  36 +type AccessTokenRequest struct {
  37 + ClientId string `json:"clientId" valid:"Required"`
  38 + ClientSecret string `json:"clientSecret" valid:"Required"`
  39 + AuthCode string `json:"authCode" valid:"Required"`
  40 +}
  41 +type AccessTokenResponse struct {
  42 + RefreshToken string `json:"refreshToken"`
  43 + AccessToken string `json:"accessToken"`
  44 + ExpiresIn int `json:"expiresIn"`
  45 +}
  46 +
  47 +/*RefreshToken */
  48 +type RefreshTokenRequest struct {
  49 + ClientId string `json:"clientId" valid:"Required"`
  50 + ClientSecret string `json:"clientSecret" valid:"Required"`
  51 + RefreshToken string `json:"refreshToken" valid:"Required"`
  52 +}
  53 +type RefreshTokenResponse struct {
  54 + RefreshToken string `json:"refreshToken"`
  55 + AccessToken string `json:"accessToken"`
  56 + ExpiresIn int `json:"expiresIn"`
  57 +}
  58 +
  59 +type Access struct {
  60 + Uid int64
  61 + Type string
  62 + AccessToken string
  63 + RefreshToken string
  64 +}
  65 +
@@ -19,6 +19,8 @@ func init() { @@ -19,6 +19,8 @@ func init() {
19 { 19 {
20 auth :=&v1.AuthController{} 20 auth :=&v1.AuthController{}
21 nsV1.Router("/auth/login",auth,"post:Login") 21 nsV1.Router("/auth/login",auth,"post:Login")
  22 + nsV1.Router("/auth/accessToken",auth,"post:AccessToken")
  23 + nsV1.Router("/auth/refreshToken",auth,"post:RefreshToken")
22 } 24 }
23 beego.AddNamespace(nsV1) 25 beego.AddNamespace(nsV1)
24 } 26 }
@@ -4,8 +4,8 @@ import ( @@ -4,8 +4,8 @@ import (
4 "fmt" 4 "fmt"
5 "gitlab.fjmaimaimai.com/mmm-go/ability/models" 5 "gitlab.fjmaimaimai.com/mmm-go/ability/models"
6 "gitlab.fjmaimaimai.com/mmm-go/ability/protocol" 6 "gitlab.fjmaimaimai.com/mmm-go/ability/protocol"
  7 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
7 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" 8 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
8 - "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"  
9 "strings" 9 "strings"
10 ) 10 )
11 11
@@ -39,9 +39,66 @@ func Login(request *protocol.LoginRequest)(rsp *protocol.LoginResponse,err error @@ -39,9 +39,66 @@ func Login(request *protocol.LoginRequest)(rsp *protocol.LoginResponse,err error
39 return 39 return
40 } 40 }
41 rsp =&protocol.LoginResponse{AuthCode:userInfo.Auth} 41 rsp =&protocol.LoginResponse{AuthCode:userInfo.Auth}
42 - msg :=mybeego.NewMessage(0)  
43 - msg.Data =rsp  
44 return 42 return
45 } 43 }
46 return 44 return
47 } 45 }
  46 +
  47 +func SmsCode(request *protocol.SmsCodeRequest)(rsp *protocol.SmsCodeResponse,err error){
  48 + return nil,nil
  49 +}
  50 +
  51 +func UpdateDevice(request *protocol.UpdateDeviceRequest)(rsp *protocol.UpdateDeviceResponse,err error){
  52 + return nil,nil
  53 +}
  54 +
  55 +
  56 +func AccessToken(request *protocol.AccessTokenRequest)(rsp *protocol.AccessTokenResponse,err error){
  57 + var (
  58 + userInfo *models.UserInfo
  59 + )
  60 + userInfo,err = models.GetUserInfoByClientId(request.ClientId)
  61 + if err!=nil{
  62 + return
  63 + }
  64 + if strings.Compare(userInfo.Auth,request.AuthCode)!=0{
  65 + err = common.NewErrorWithMsg(2,"auth code error.")
  66 + return
  67 + }
  68 + //valid token
  69 + rsp = &protocol.AccessTokenResponse{
  70 + RefreshToken:userInfo.RefreshToken,
  71 + AccessToken:userInfo.AccessToken,
  72 + ExpiresIn:3600,
  73 + }
  74 + return
  75 +}
  76 +
  77 +func RefreshToken(request *protocol.RefreshTokenRequest)(rsp *protocol.RefreshTokenResponse,err error){
  78 + var (
  79 + userInfo *models.UserInfo
  80 + newAccess *protocol.Access
  81 + )
  82 + userInfo,err = models.GetUserInfoByClientId(request.ClientId)
  83 + if err!=nil{
  84 + return
  85 + }
  86 + if strings.Compare(userInfo.RefreshToken,request.RefreshToken)!=0{
  87 + err = common.NewErrorWithMsg(2,"refresh token error.")
  88 + return
  89 + }
  90 + if newAccess,err =refreshToken(userInfo.Id,"");err!=nil{
  91 + return
  92 + }
  93 + rsp = &protocol.RefreshTokenResponse{
  94 + AccessToken:newAccess.AccessToken,
  95 + RefreshToken:newAccess.RefreshToken,
  96 + ExpiresIn:3600,
  97 + }
  98 + return
  99 +}
  100 +
  101 +//loginType mobile im
  102 +func refreshToken(uid int64,loginType string)(rsp *protocol.Access,err error){
  103 + return nil,nil
  104 +}