From 7c811247ecf143e08c576986a884bedadc57dd66 Mon Sep 17 00:00:00 2001 From: liuxiaolong <liuxiaolong@aiotlink.com> Date: 星期五, 05 六月 2020 18:29:41 +0800 Subject: [PATCH] add refresh token to resp --- controllers/user.go | 94 ++++++++++++++++++++++++++++++++++++----------- 1 files changed, 72 insertions(+), 22 deletions(-) diff --git a/controllers/user.go b/controllers/user.go index a78d271..9dbdb68 100644 --- a/controllers/user.go +++ b/controllers/user.go @@ -2,7 +2,6 @@ import ( "basic.com/dbapi.git" - "basic.com/valib/logger.git" "github.com/gin-gonic/gin" "net/http" "time" @@ -22,7 +21,7 @@ // @Summary 鐢ㄦ埛鐧诲綍 // @Description 鐢ㄦ埛鐧诲綍 -// @Accept json +// @Accept x-www-form-urlencoded // @Produce json // @Tags 鐢ㄦ埛 // @Param username formData string true "鐢ㄦ埛鍚�" @@ -46,22 +45,53 @@ tokenM["id"] = loginedM["id"] tokenM["username"] = loginedM["username"] tokenM["permissions"] = loginedM["permissions"] - tokenStr := (*authDriver).Login(c.Request, c.Writer, tokenM) + b,tokenStr,refreshTokenStr := (*authDriver).Login(c.Request, c.Writer, tokenM) + if b { + userId := loginedM["id"].(string) + auth.RemoveOutUser(userId) - - userId := loginedM["id"].(string) - auth.RemoveOutUser(userId) - - c.JSON(200,map[string]interface{}{ - "userInfo":loginedM, - "access_token":tokenStr, - "refresh_token":tokenStr, - "scope":"app", - "token_type":"Bearer", - "expires_in":time.Now().Add(time.Hour * 8).Unix(), - }) + c.JSON(200,map[string]interface{}{ + "userInfo":loginedM, + "access_token": tokenStr, + "refresh_token": refreshTokenStr, + "scope":"app", + "token_type":"Bearer", + "expires_in":time.Now().Add(time.Hour * 8).Unix(), + }) + } else { + c.JSON(500,"鐢ㄦ埛鍚嶆垨瀵嗙爜閿欒") + } } else { c.JSON(500,"鐢ㄦ埛鍚嶆垨瀵嗙爜閿欒") + } +} + +// @Security ApiKeyAuth +// @Summary 淇敼褰撳墠鐧诲綍鐢ㄦ埛鐨勫瘑鐮� +// @Description 淇敼褰撳墠鐧诲綍鐢ㄦ埛鐨勫瘑鐮� +// @Accept x-www-form-urlencoded +// @Produce json +// @Tags 鐢ㄦ埛 +// @Param oldPwd formData string true "鏃у瘑鐮�" +// @Param newPwd formData string true "鏂板瘑鐮�" +// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}" +// @Failure 500 {string} json "{"code":500, success:false, msg:"", data:""}" +// @Router /data/api-u/users/updatePwd [post] +func (uc UserController) UpdatePwd(c *gin.Context) { + oldPwd := c.PostForm("oldPwd") + newPwd := c.PostForm("newPwd") + if oldPwd == "" || newPwd == "" { + util.ResponseFormat(c,code.RequestParamError,"鍙傛暟鏈夎") + return + } + authDriver := auth.GenerateAuthDriver() + userM := (*authDriver).User(c) + userId := userM["id"].(string) + var userApi dbapi.UserApi + if b,_ :=userApi.UpdatePwd(userId, oldPwd, newPwd);b { + util.ResponseFormat(c,code.UpdateSuccess,"鏇存柊鎴愬姛") + } else { + util.ResponseFormat(c,code.AccountPassUnmatch,"瀵嗙爜鏈夎") } } @@ -83,9 +113,31 @@ c.JSON(http.StatusUnauthorized,"") } } + +// @Summary token杩囨湡鍚庡埛鏂皌oken +// @Description token杩囨湡鍚庡埛鏂皌oken +// @Accept x-www-form-urlencoded +// @Produce json +// @Tags 鐢ㄦ埛 +// @Param refresh_token formData string true "涓婃鑾峰彇token鏃惰繑鍥炵殑refresh_token鍊�" +// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}" +// @Failure 500 {string} json "{"code":500, success:false, msg:"", data:""}" // @Router /data/api-u/sys/refresh_token [post] func (uc UserController) RefreshToken(c *gin.Context){ - + refreshToken := c.PostForm("refresh_token") + if refreshToken == "" { + util.ResponseFormat(c,code.TokenNotFound,"鏈壘鍒皉efresh_token") + return + } + authDriver := auth.GenerateAuthDriver() + if b,newToken,newRefreshToken := (*authDriver).RefreshToken(refreshToken);b { + util.ResponseFormat(c,code.Success,map[string]string{ + "token": newToken, + "refresh_token": newRefreshToken, + }) + } else { + util.ResponseFormat(c,code.NotLogin,"璇烽噸鏂扮櫥褰�") + } } // @Router /data/api-u/sys/logout [get] @@ -104,10 +156,8 @@ // @Router /data/api-u/users/findAllUser [get] func (uc UserController) FindAllUser(c *gin.Context) { authDriver := auth.GenerateAuthDriver() - user := (*authDriver).User(c) - logger.Debug("cur userInfo:",user) - loginM := user.(map[string]interface{}) - userId := loginM["id"].(string) + userM := (*authDriver).User(c) + userId := userM["id"].(string) var api dbapi.UserApi b,d := api.FindAllUser(userId) if b { @@ -127,7 +177,7 @@ // @Security ApiKeyAuth // @Summary 缂栬緫姝ょ敤鎴凤紝杩斿洖姝ょ敤鎴风殑鏉冮檺鑿滃崟 // @Description 缂栬緫姝ょ敤鎴凤紝杩斿洖姝ょ敤鎴风殑鏉冮檺鑿滃崟 -// @Accept json +// @Accept x-www-form-urlencoded // @Produce json // @Tags 鐢ㄦ埛 // @Param userId formData string true "鐢ㄦ埛id" @@ -158,7 +208,7 @@ // @Param userVo body controllers.UserEditVo true "鐢ㄦ埛鍙婃潈闄愪俊鎭�" // @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}" // @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}" -// @Router /data/api-v/user/saveAuth [post] +// @Router /data/api-v/users/saveAuth [post] func (uc UserController) SaveAuth(c *gin.Context) { var userEditVo UserEditVo err := c.BindJSON(&userEditVo) -- Gitblit v1.8.0