...
|
...
|
@@ -8,281 +8,289 @@ import ( |
|
|
"strings"
|
|
|
"time"
|
|
|
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/ability/internal/repository"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/ability/models"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/ability/protocol"
|
|
|
s_sms "gitlab.fjmaimaimai.com/mmm-go/ability/services/sms"
|
|
|
"ability/internal/repository"
|
|
|
"ability/models"
|
|
|
"ability/protocol"
|
|
|
s_sms "ability/services/sms"
|
|
|
|
|
|
"github.com/astaxie/beego"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/redis"
|
|
|
comm_time "gitlab.fjmaimaimai.com/mmm-go/gocomm/time"
|
|
|
"github.com/astaxie/beego"
|
|
|
)
|
|
|
|
|
|
type IAuthService interface {
|
|
|
Login(request *protocol.LoginRequest)(rsp *protocol.LoginResponse,err error)
|
|
|
AccessToken(request *protocol.AccessTokenRequest)(rsp *protocol.AccessTokenResponse,err error)
|
|
|
RefreshToken(request *protocol.RefreshTokenRequest)(rsp *protocol.RefreshTokenResponse,err error)
|
|
|
UpdateDevice(request *protocol.UpdateDeviceRequest)(rsp *protocol.UpdateDeviceResponse,err error)
|
|
|
CheckToken(request *protocol.CheckTokenRequest)(rsp *protocol.CheckTokenResponse,err error)
|
|
|
CheckUuid(request *protocol.CheckUuidRequest)(rsp *protocol.CheckUuidResponse,err error)
|
|
|
SmsCode(request *protocol.SmsCodeRequest)(rsp *protocol.SmsCodeResponse,err error)
|
|
|
Login(request *protocol.LoginRequest) (rsp *protocol.LoginResponse, err error)
|
|
|
AccessToken(request *protocol.AccessTokenRequest) (rsp *protocol.AccessTokenResponse, err error)
|
|
|
RefreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.RefreshTokenResponse, err error)
|
|
|
UpdateDevice(request *protocol.UpdateDeviceRequest) (rsp *protocol.UpdateDeviceResponse, err error)
|
|
|
CheckToken(request *protocol.CheckTokenRequest) (rsp *protocol.CheckTokenResponse, err error)
|
|
|
CheckUuid(request *protocol.CheckUuidRequest) (rsp *protocol.CheckUuidResponse, err error)
|
|
|
SmsCode(request *protocol.SmsCodeRequest) (rsp *protocol.SmsCodeResponse, err error)
|
|
|
}
|
|
|
|
|
|
type AuthService struct {}
|
|
|
type AuthService struct{}
|
|
|
|
|
|
func assertImplement(){
|
|
|
func assertImplement() {
|
|
|
var _ IAuthService = (*AuthService)(nil)
|
|
|
}
|
|
|
|
|
|
var(
|
|
|
var (
|
|
|
//服务
|
|
|
sms s_sms.ISmsService = &s_sms.YunPianSmsService{}
|
|
|
sms s_sms.ISmsService = &s_sms.YunPianSmsService{}
|
|
|
|
|
|
//仓储
|
|
|
UserRepository repository.IUserRepository =&repository.UserRepository{}
|
|
|
UserRepository repository.IUserRepository = &repository.UserRepository{}
|
|
|
)
|
|
|
|
|
|
//登录
|
|
|
func (s *AuthService)Login(request *protocol.LoginRequest)(rsp *protocol.LoginResponse,err error){
|
|
|
func (s *AuthService) Login(request *protocol.LoginRequest) (rsp *protocol.LoginResponse, err error) {
|
|
|
var (
|
|
|
user *models.Users
|
|
|
user *models.Users
|
|
|
userInfo *models.UserInfo
|
|
|
result bool
|
|
|
result bool
|
|
|
)
|
|
|
user,err =UserRepository.GetUsersByMobile(request.Phone)
|
|
|
if err!=nil{
|
|
|
user, err = UserRepository.GetUsersByMobile(request.Phone)
|
|
|
if err != nil {
|
|
|
log.Error(err)
|
|
|
err =common.NewError(2020,err)//账号不存在
|
|
|
err = common.NewError(2020, err) //账号不存在
|
|
|
return
|
|
|
}
|
|
|
switch request.GrantType {
|
|
|
case protocol.LoginTypePassPord:
|
|
|
if strings.Compare(user.Password,request.PassWord)==0{
|
|
|
if strings.Compare(user.Password, request.PassWord) == 0 {
|
|
|
goto Success
|
|
|
}else{
|
|
|
err =common.NewError(2021,err)//登录密码错误
|
|
|
} else {
|
|
|
err = common.NewError(2021, err) //登录密码错误
|
|
|
return
|
|
|
}
|
|
|
break
|
|
|
case protocol.LoginTypeSmdcode:
|
|
|
if result,err=CheckSmsCode(request.Phone,request.Code,protocol.SmsLoginCode);result && err==nil{
|
|
|
if result, err = CheckSmsCode(request.Phone, request.Code, protocol.SmsLoginCode); result && err == nil {
|
|
|
goto Success
|
|
|
}else{
|
|
|
} else {
|
|
|
return
|
|
|
}
|
|
|
default:
|
|
|
err =fmt.Errorf("grantType error")
|
|
|
err = fmt.Errorf("grantType error")
|
|
|
return
|
|
|
}
|
|
|
Success:
|
|
|
{
|
|
|
userInfo,err =UserRepository.GetUserInfoByMobile(request.Phone)
|
|
|
if err!=nil{
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
rsp =&protocol.LoginResponse{AuthCode:userInfo.Auth}
|
|
|
Success:
|
|
|
{
|
|
|
userInfo, err = UserRepository.GetUserInfoByMobile(request.Phone)
|
|
|
if err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
rsp = &protocol.LoginResponse{AuthCode: userInfo.Auth}
|
|
|
return
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//更新设备信息
|
|
|
func (s *AuthService)UpdateDevice(request *protocol.UpdateDeviceRequest)(rsp *protocol.UpdateDeviceResponse,err error){
|
|
|
return nil,nil
|
|
|
func (s *AuthService) UpdateDevice(request *protocol.UpdateDeviceRequest) (rsp *protocol.UpdateDeviceResponse, err error) {
|
|
|
return nil, nil
|
|
|
}
|
|
|
|
|
|
//获取accessToken
|
|
|
func (s *AuthService)AccessToken(request *protocol.AccessTokenRequest)(rsp *protocol.AccessTokenResponse,err error){
|
|
|
func (s *AuthService) AccessToken(request *protocol.AccessTokenRequest) (rsp *protocol.AccessTokenResponse, err error) {
|
|
|
var (
|
|
|
userInfo *models.UserInfo
|
|
|
)
|
|
|
userInfo,err = models.GetUserInfoByClientId(request.ClientId)
|
|
|
if err!=nil{
|
|
|
userInfo, err = models.GetUserInfoByClientId(request.ClientId)
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
|
if strings.Compare(userInfo.Auth,request.AuthCode)!=0{
|
|
|
err = common.NewErrorWithMsg(2,"auth code error.")
|
|
|
if strings.Compare(userInfo.Auth, request.AuthCode) != 0 {
|
|
|
err = common.NewErrorWithMsg(2, "auth code error.")
|
|
|
return
|
|
|
}
|
|
|
//valid token
|
|
|
rsp = &protocol.AccessTokenResponse{
|
|
|
RefreshToken:userInfo.RefreshToken,
|
|
|
AccessToken:userInfo.AccessToken,
|
|
|
ExpiresIn:3600,
|
|
|
RefreshToken: userInfo.RefreshToken,
|
|
|
AccessToken: userInfo.AccessToken,
|
|
|
ExpiresIn: 3600,
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//刷新token
|
|
|
func (s *AuthService)RefreshToken(request *protocol.RefreshTokenRequest)(rsp *protocol.RefreshTokenResponse,err error){
|
|
|
func (s *AuthService) RefreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.RefreshTokenResponse, err error) {
|
|
|
var (
|
|
|
userInfo *models.UserInfo
|
|
|
userInfo *models.UserInfo
|
|
|
newAccess *protocol.Access
|
|
|
)
|
|
|
userInfo,err = models.GetUserInfoByClientId(request.ClientId)
|
|
|
if err!=nil{
|
|
|
userInfo, err = models.GetUserInfoByClientId(request.ClientId)
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
|
if strings.Compare(userInfo.RefreshToken,request.RefreshToken)!=0{
|
|
|
err = common.NewErrorWithMsg(2,"refresh token error.")
|
|
|
if strings.Compare(userInfo.RefreshToken, request.RefreshToken) != 0 {
|
|
|
err = common.NewErrorWithMsg(2, "refresh token error.")
|
|
|
return
|
|
|
}
|
|
|
request.Uid,request.LoginType = userInfo.Id,"mobile"
|
|
|
if newAccess,err =refreshToken(request);err!=nil{
|
|
|
request.Uid, request.LoginType = userInfo.Id, "mobile"
|
|
|
if newAccess, err = refreshToken(request); err != nil {
|
|
|
return
|
|
|
}
|
|
|
rsp = &protocol.RefreshTokenResponse{
|
|
|
AccessToken:newAccess.AccessToken,
|
|
|
RefreshToken:newAccess.RefreshToken,
|
|
|
ExpiresIn:3600,
|
|
|
AccessToken: newAccess.AccessToken,
|
|
|
RefreshToken: newAccess.RefreshToken,
|
|
|
ExpiresIn: 3600,
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//刷新token loginType mobile im web
|
|
|
func refreshToken(request *protocol.RefreshTokenRequest)(rsp *protocol.Access,err error){
|
|
|
if request.Uid==0{
|
|
|
func refreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.Access, err error) {
|
|
|
if request.Uid == 0 {
|
|
|
return
|
|
|
}
|
|
|
return nil,nil
|
|
|
return nil, nil
|
|
|
}
|
|
|
|
|
|
//检查token有效性
|
|
|
func (s *AuthService)CheckToken(request *protocol.CheckTokenRequest)(rsp *protocol.CheckTokenResponse,err error){
|
|
|
func (s *AuthService) CheckToken(request *protocol.CheckTokenRequest) (rsp *protocol.CheckTokenResponse, err error) {
|
|
|
var (
|
|
|
userInfo *models.UserInfo
|
|
|
)
|
|
|
if len(request.Token)==0{
|
|
|
err = common.NewErrorWithMsg(4141,"token not empty")
|
|
|
if len(request.Token) == 0 {
|
|
|
err = common.NewErrorWithMsg(4141, "token not empty")
|
|
|
return
|
|
|
}
|
|
|
userInfo,err =models.GetUserInfoByToken(request.Token)
|
|
|
if err!=nil{
|
|
|
err =common.NewError(4141,err)
|
|
|
userInfo, err = models.GetUserInfoByToken(request.Token)
|
|
|
if err != nil {
|
|
|
err = common.NewError(4141, err)
|
|
|
return
|
|
|
}
|
|
|
rsp =&protocol.CheckTokenResponse{
|
|
|
UserInfo:userInfo,
|
|
|
IsValid:true,
|
|
|
rsp = &protocol.CheckTokenResponse{
|
|
|
UserInfo: userInfo,
|
|
|
IsValid: true,
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//检查uuid 是否重复
|
|
|
func (s *AuthService)CheckUuid(request *protocol.CheckUuidRequest)(rsp *protocol.CheckUuidResponse,err error){
|
|
|
func (s *AuthService) CheckUuid(request *protocol.CheckUuidRequest) (rsp *protocol.CheckUuidResponse, err error) {
|
|
|
var (
|
|
|
logUuid *models.LogUuid
|
|
|
)
|
|
|
if len(request.Uuid)==0{
|
|
|
err = common.NewErrorWithMsg(4142,"uuid not empty")
|
|
|
if len(request.Uuid) == 0 {
|
|
|
err = common.NewErrorWithMsg(4142, "uuid not empty")
|
|
|
}
|
|
|
logUuid,err=models.GetLogUuidByUuid(request.Uuid)
|
|
|
if err==nil && logUuid!=nil{
|
|
|
err = common.NewErrorWithMsg(4142,"uuid not valid")
|
|
|
logUuid, err = models.GetLogUuidByUuid(request.Uuid)
|
|
|
if err == nil && logUuid != nil {
|
|
|
err = common.NewErrorWithMsg(4142, "uuid not valid")
|
|
|
}
|
|
|
models.AddLogUuid(&models.LogUuid{Uuid:request.Uuid})
|
|
|
rsp =&protocol.CheckUuidResponse{}
|
|
|
models.AddLogUuid(&models.LogUuid{Uuid: request.Uuid})
|
|
|
rsp = &protocol.CheckUuidResponse{}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//短信验证码 T
|
|
|
func (s *AuthService)SmsCode(request *protocol.SmsCodeRequest)(rsp *protocol.SmsCodeResponse,err error){
|
|
|
var(
|
|
|
value,key,msgContent string
|
|
|
smsInfo *protocol.SmsInfo
|
|
|
func (s *AuthService) SmsCode(request *protocol.SmsCodeRequest) (rsp *protocol.SmsCodeResponse, err error) {
|
|
|
var (
|
|
|
value, key, msgContent string
|
|
|
smsInfo *protocol.SmsInfo
|
|
|
)
|
|
|
msgContent = `【买买买信息科技】{{.Code}}({{.AppName}}手机验证码,请完成验证),如非本人操作,请忽略本短信`
|
|
|
switch request.SendType {
|
|
|
case protocol.SmsLoginCode:
|
|
|
case protocol.SmsChangeMobile:
|
|
|
default:
|
|
|
err = common.NewErrorWithMsg(2,"send_type error.")
|
|
|
err = common.NewErrorWithMsg(2, "send_type error.")
|
|
|
return
|
|
|
}
|
|
|
key = request.SendType
|
|
|
//check user phone exists
|
|
|
if !redis.Hexists(key,request.Phone){
|
|
|
if !redis.Hexists(key, request.Phone) {
|
|
|
smsInfo = &protocol.SmsInfo{
|
|
|
CreateTime:time.Now().Unix(),
|
|
|
CreateTime: time.Now().Unix(),
|
|
|
}
|
|
|
goto Send
|
|
|
}else{
|
|
|
if value,err =redis.Hget(key,request.Phone);err!=nil{
|
|
|
} else {
|
|
|
if value, err = redis.Hget(key, request.Phone); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
if err=json.Unmarshal([]byte(value),&smsInfo);err!=nil{
|
|
|
if err = json.Unmarshal([]byte(value), &smsInfo); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
//第二天重置
|
|
|
if smsInfo.LastTime<comm_time.GetUnixTimeByYyyymmdd(){
|
|
|
smsInfo.Count=0
|
|
|
if smsInfo.LastTime < comm_time.GetUnixTimeByYyyymmdd() {
|
|
|
smsInfo.Count = 0
|
|
|
smsInfo.CreateTime = time.Now().Unix()
|
|
|
}
|
|
|
if smsInfo.Count>100{//TODO:limit send time
|
|
|
if smsInfo.Count > 100 { //TODO:limit send time
|
|
|
return
|
|
|
}
|
|
|
goto Send
|
|
|
}
|
|
|
Send:
|
|
|
{
|
|
|
smsInfo.Code = common.RandomStringWithChars(6,string(protocol.Nums))
|
|
|
smsInfo.LastTime=time.Now().Unix()
|
|
|
smsInfo.ErrorCount =0
|
|
|
//Todo Lock
|
|
|
smsInfo.Count +=1
|
|
|
if err=redis.Hset(key,request.Phone,common.AssertJson(smsInfo),-1);err!=nil{
|
|
|
return
|
|
|
}
|
|
|
tp :=template.New("sms_code")
|
|
|
tp.Parse(msgContent)
|
|
|
buf :=bytes.NewBuffer(nil)
|
|
|
tp.Execute(
|
|
|
buf,
|
|
|
map[string]string{
|
|
|
"Code":smsInfo.Code,
|
|
|
"AppName":beego.BConfig.AppName,
|
|
|
},)
|
|
|
request.Content = buf.String()
|
|
|
err = sms.Send(request)
|
|
|
rsp = &protocol.SmsCodeResponse{
|
|
|
Code:smsInfo.Code,
|
|
|
}
|
|
|
Send:
|
|
|
{
|
|
|
smsInfo.Code = common.RandomStringWithChars(6, string(protocol.Nums))
|
|
|
smsInfo.LastTime = time.Now().Unix()
|
|
|
smsInfo.ErrorCount = 0
|
|
|
//Todo Lock
|
|
|
smsInfo.Count += 1
|
|
|
if err = redis.Hset(key, request.Phone, common.AssertJson(smsInfo), -1); err != nil {
|
|
|
return
|
|
|
}
|
|
|
tp := template.New("sms_code")
|
|
|
tp.Parse(msgContent)
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
tp.Execute(
|
|
|
buf,
|
|
|
map[string]string{
|
|
|
"Code": smsInfo.Code,
|
|
|
"AppName": beego.BConfig.AppName,
|
|
|
})
|
|
|
request.Content = buf.String()
|
|
|
err = sms.Send(request)
|
|
|
rsp = &protocol.SmsCodeResponse{
|
|
|
Code: smsInfo.Code,
|
|
|
}
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//验证短信验证码 T
|
|
|
func CheckSmsCode(phone ,code ,sendType string)(result bool,err error){
|
|
|
var(
|
|
|
value string
|
|
|
func CheckSmsCode(phone, code, sendType string) (result bool, err error) {
|
|
|
var (
|
|
|
value string
|
|
|
smsInfo *protocol.SmsInfo
|
|
|
)
|
|
|
result =false
|
|
|
if value,err =redis.Hget(sendType,phone);err!=nil{//protocol.SmsLoginCode
|
|
|
err = common.NewErrorWithMsg(1009,"smscode expire")
|
|
|
result = false
|
|
|
if value, err = redis.Hget(sendType, phone); err != nil { //protocol.SmsLoginCode
|
|
|
err = common.NewErrorWithMsg(1009, "smscode expire")
|
|
|
return
|
|
|
}
|
|
|
if err=json.Unmarshal([]byte(value),&smsInfo);err!=nil{
|
|
|
if err = json.Unmarshal([]byte(value), &smsInfo); err != nil {
|
|
|
return
|
|
|
}
|
|
|
if smsInfo.ErrorCount>=5{
|
|
|
err = common.NewErrorWithMsg(1011,"smscode over error times")
|
|
|
if smsInfo.ErrorCount >= 5 {
|
|
|
err = common.NewErrorWithMsg(1011, "smscode over error times")
|
|
|
return
|
|
|
}
|
|
|
if (smsInfo.LastTime+60*5)<time.Now().Unix(){
|
|
|
err = common.NewErrorWithMsg(1009,fmt.Sprintf("smscode expire %v < %v",(smsInfo.LastTime+60*5),time.Now().Unix()))
|
|
|
if (smsInfo.LastTime + 60*5) < time.Now().Unix() {
|
|
|
err = common.NewErrorWithMsg(1009, fmt.Sprintf("smscode expire %v < %v", (smsInfo.LastTime+60*5), time.Now().Unix()))
|
|
|
goto Fail
|
|
|
}
|
|
|
if smsInfo.Code == code{
|
|
|
if smsInfo.Code == code {
|
|
|
result = true
|
|
|
return
|
|
|
}else{
|
|
|
err = common.NewErrorWithMsg(1012,"smscode error")
|
|
|
} else {
|
|
|
err = common.NewErrorWithMsg(1012, "smscode error")
|
|
|
goto Fail
|
|
|
}
|
|
|
Fail:
|
|
|
{
|
|
|
smsInfo.ErrorCount +=1
|
|
|
if err=redis.Hset(sendType,phone,common.AssertJson(smsInfo),-1);err!=nil{
|
|
|
return
|
|
|
}
|
|
|
Fail:
|
|
|
{
|
|
|
smsInfo.ErrorCount += 1
|
|
|
if err = redis.Hset(sendType, phone, common.AssertJson(smsInfo), -1); err != nil {
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
return
|
|
|
} |
|
|
\ No newline at end of file |
|
|
} |
...
|
...
|
|