作者 yangfu

accesstoken refreshtoken login

... ... @@ -152,3 +152,12 @@ func DeleteCfgClient(id int) (err error) {
}
return
}
func GetCfgClient(clintId, clientSecret string) (v *CfgClient, err error) {
o := orm.NewOrm()
sql := "select * from cfg_client where enabled=1 and client_id=? and client_secret=?"
if err = o.Raw(sql, clintId, clientSecret).QueryRow(&v); err == nil {
return v, nil
}
return nil, err
}
... ...
... ... @@ -11,7 +11,7 @@ import (
)
type UserInfo struct {
Id int64 `orm:"column(uid);pk" description:"用户ID"`
Id int64 `orm:"column(uid);pk" description:"用户ID"`
Uname string `orm:"column(uname);size(100)" description:"名称"`
Icon string `orm:"column(icon);size(128)" description:"头像"`
Width int `orm:"column(width)" description:"宽度"`
... ... @@ -190,30 +190,47 @@ func DeleteUserInfo(id int64) (err error) {
return
}
func GetUserInfoByMobile(mobile string)(v *UserInfo, err error) {
func GetUserInfoByMobile(mobile string) (v *UserInfo, err error) {
o := orm.NewOrm()
sql :="select * from user_info where phone=?"
if err = o.Raw(sql,mobile).QueryRow(&v); err == nil {
sql := "select * from user_info where phone=?"
if err = o.Raw(sql, mobile).QueryRow(&v); err == nil {
return v, nil
}
return nil, err
}
func GetUserInfoByClientId(clintId string)(v *UserInfo, err error) {
func GetUserInfoByClientId(clintId string) (v *UserInfo, err error) {
o := orm.NewOrm()
sql :="select * from user_info where clientId=?"
if err = o.Raw(sql,clintId).QueryRow(&v); err == nil {
sql := "select * from user_info where clientId=?"
if err = o.Raw(sql, clintId).QueryRow(&v); err == nil {
return v, nil
}
return nil, err
}
func GetUserInfoByToken(token string)(v *UserInfo, err error) {
func GetUserInfoByToken(token string) (v *UserInfo, err error) {
o := orm.NewOrm()
sql :="select * from user_info where access_token=? and access_exp >= Now() and enabled = 1"
if err = o.Raw(sql,token).QueryRow(&v); err == nil {
sql := "select * from user_info where access_token=? and access_exp >= Now() and enabled = 1"
if err = o.Raw(sql, token).QueryRow(&v); err == nil {
return v, nil
}
return nil, err
}
func GetUserInfoByAuthCode(authCode string) (v *UserInfo, err error) {
o := orm.NewOrm()
sql := "select * from user_info where auth=? and auth_exp >= Now() and enabled = 1"
if err = o.Raw(sql, authCode).QueryRow(&v); err == nil {
return v, nil
}
return nil, err
}
func GetUserInfoByRefreshToken(refreshToken string) (v *UserInfo, err error) {
o := orm.NewOrm()
sql := "select * from user_info where refresh_token=? and auth_exp >= Now() and enabled = 1"
if err = o.Raw(sql, refreshToken).QueryRow(&v); err == nil {
return v, nil
}
return nil, err
}
... ...
... ... @@ -7,6 +7,8 @@ const (
LoginTypeSmdcode = "signInCaptcha"
)
const TokenExpire = 3600
var Nums = []byte("0123456789")
type RequestHeader struct {
... ...
... ... @@ -2,30 +2,31 @@ package protocol
import "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
func InitMessageCode(){
func InitMessageCode() {
messages := []struct {
Code int
Msg string
}{
{113,"签名验证失败"},
{1009,"验证码已超时,登录失败"},
{1011,"短信验证码次数超过限制,请稍后重试"},
{1012,"验证码错误"},
{101, "clientId或clientSecret无效"},
{113, "签名验证失败"},
{1009, "验证码已超时,登录失败"},
{1011, "短信验证码次数超过限制,请稍后重试"},
{1012, "验证码错误"},
{2001,"请输入正确的手机号码"},
{2002,"后台未配置账号信息,请联系管理员配置"},
{2009,"上传的文件流为空"},
{2020,"帐号不存在,请联系管理员"},
{2021,"登录失败,手机号或密码错误"},
{2025,"短信验证码验证失败"},
{2026,"两次输入的密码不一致"},
{2001, "请输入正确的手机号码"},
{2002, "后台未配置账号信息,请联系管理员配置"},
{2009, "上传的文件流为空"},
{2020, "帐号不存在,请联系管理员"},
{2021, "登录失败,手机号或密码错误"},
{2025, "短信验证码验证失败"},
{2026, "两次输入的密码不一致"},
{4139,"authCode无效或过期"},
{4140,"refreshToken过期,需要重新登录授权"},
{4141,"accessToken过期或无效,需要进行重新获取令牌"},
{4142,"Uuid已存在,请求失败"},
{4139, "authCode无效或过期"},
{4140, "refreshToken过期,需要重新登录授权"},
{4141, "accessToken过期或无效,需要进行重新获取令牌"},
{4142, "Uuid已存在,请求失败"},
}
for i:=range messages{
mybeego.SetMessage(messages[i].Code,messages[i].Msg)
for i := range messages {
mybeego.SetMessage(messages[i].Code, messages[i].Msg)
}
}
... ...
... ... @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/identity/uid"
"html/template"
"strings"
"time"
... ... @@ -83,6 +84,12 @@ Success:
log.Error(err)
return
}
if userInfo.Auth == "" {
userInfo.Auth = uid.NewV1().StringNoDash()
}
if err = models.UpdateUserInfoById(userInfo); err != nil {
return
}
rsp = &protocol.LoginResponse{AuthCode: userInfo.Auth}
return
}
... ... @@ -99,19 +106,26 @@ func (s *AuthService) AccessToken(request *protocol.AccessTokenRequest) (rsp *pr
var (
userInfo *models.UserInfo
)
userInfo, err = models.GetUserInfoByClientId(request.ClientId)
_, err = models.GetCfgClient(request.ClientId, request.ClientSecret)
if err != nil {
common.NewError(101, err)
return
}
userInfo, err = models.GetUserInfoByAuthCode(request.AuthCode)
if err != nil {
common.NewError(4139, err)
return
}
if strings.Compare(userInfo.Auth, request.AuthCode) != 0 {
err = common.NewErrorWithMsg(2, "auth code error.")
userInfo.AccessToken = uid.NewV1().StringNoDash()
userInfo.RefreshToken = uid.NewV1().StringNoDash()
if err = models.UpdateUserInfoById(userInfo); err != nil {
return
}
//valid token
rsp = &protocol.AccessTokenResponse{
RefreshToken: userInfo.RefreshToken,
AccessToken: userInfo.AccessToken,
ExpiresIn: 3600,
ExpiresIn: protocol.TokenExpire,
}
return
}
... ... @@ -122,33 +136,35 @@ func (s *AuthService) RefreshToken(request *protocol.RefreshTokenRequest) (rsp *
userInfo *models.UserInfo
newAccess *protocol.Access
)
userInfo, err = models.GetUserInfoByClientId(request.ClientId)
_, err = models.GetCfgClient(request.ClientId, request.ClientSecret)
if err != nil {
common.NewError(101, err)
return
}
if strings.Compare(userInfo.RefreshToken, request.RefreshToken) != 0 {
err = common.NewErrorWithMsg(2, "refresh token error.")
userInfo, err = models.GetUserInfoByAuthCode(request.RefreshToken)
if err != nil {
common.NewError(4139, err)
return
}
request.Uid, request.LoginType = userInfo.Id, "mobile"
if newAccess, err = refreshToken(request); err != nil {
userInfo.AccessToken = uid.NewV1().StringNoDash()
if err = models.UpdateUserInfoById(userInfo); err != nil {
return
}
rsp = &protocol.RefreshTokenResponse{
AccessToken: newAccess.AccessToken,
RefreshToken: newAccess.RefreshToken,
ExpiresIn: 3600,
ExpiresIn: protocol.TokenExpire,
}
return
}
//刷新token loginType mobile im web
func refreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.Access, err error) {
if request.Uid == 0 {
return
}
return nil, nil
}
//func refreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.Access, err error) {
// if request.Uid == 0 {
// return
// }
// return nil, nil
//}
//检查token有效性
func (s *AuthService) CheckToken(request *protocol.CheckTokenRequest) (rsp *protocol.CheckTokenResponse, err error) {
... ...