/
passwdController.go
114 lines (101 loc) · 3.04 KB
/
passwdController.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package controllers
import (
"net/http"
"strings"
"github.com/astaxie/beego/context"
"github.com/hzwy23/hauth/core/models"
"github.com/hzwy23/hauth/utils/crypto/haes"
"github.com/hzwy23/hauth/utils/hret"
"github.com/hzwy23/hauth/utils/i18n"
"github.com/hzwy23/hauth/utils/jwt"
"github.com/hzwy23/hauth/utils/logs"
)
type passwdController struct {
p *models.PasswdModels
}
var PasswdController = &passwdController{
p: &models.PasswdModels{},
}
// swagger:operation POST /v1/auth/passwd/update passwdController passwdController
//
// 修改用户自己的密码信息
//
// API提供了修改用户自己密码的服务,这个服务,不能删除其他用户的密码
//
// ---
// produces:
// - application/json
// - application/xml
// - text/xml
// - text/html
// parameters:
// - name: orapasswd
// in: query
// description: domain code number
// required: true
// type: string
// format:
// - name: newpasswd
// in: query
// description: domain code number
// required: true
// type: string
// format:
// - name: surepasswd
// in: query
// description: domain code number
// required: true
// type: string
// format:
// responses:
// '200':
// description: all domain information
func (this passwdController) PostModifyPasswd(ctx *context.Context) {
ctx.Request.ParseForm()
oriPasswd := ctx.Request.FormValue("orapasswd")
newPasswd := ctx.Request.FormValue("newpasswd")
surePasswd := ctx.Request.FormValue("surepasswd")
if oriPasswd == newPasswd {
hret.Error(ctx.ResponseWriter, 421, i18n.Get(ctx.Request, "error_passwd_same"))
return
}
if newPasswd != surePasswd {
logs.Error("new passwd confirm failed. please check your new password and confirm password")
hret.Error(ctx.ResponseWriter, 421, i18n.Get(ctx.Request, "error_passwd_confirm_failed"))
return
}
if len(strings.TrimSpace(newPasswd)) != len(newPasswd) {
hret.Error(ctx.ResponseWriter, 421, i18n.Get(ctx.Request, "error_passwd_blank"))
return
}
if len(strings.TrimSpace(newPasswd)) < 6 || len(strings.TrimSpace(newPasswd)) > 30 {
logs.Error("新密码长度不能小于6位,且不能大于30位")
hret.Error(ctx.ResponseWriter, 421, i18n.Get(ctx.Request, "error_passwd_short"))
return
}
oriEn, err := haes.Encrypt(oriPasswd)
if err != nil {
hret.Error(ctx.ResponseWriter, 421, i18n.Get(ctx.Request, "error_password_encrpty"))
return
}
newPd, err := haes.Encrypt(newPasswd)
if err != nil {
hret.Error(ctx.ResponseWriter, 421, i18n.Get(ctx.Request, "error_password_encrpty"))
return
}
cookie, _ := ctx.Request.Cookie("Authorization")
jclaim, err := jwt.ParseJwt(cookie.Value)
if err != nil {
logs.Error(err)
hret.Error(ctx.ResponseWriter, 403, i18n.Disconnect(ctx.Request))
return
}
err_msg, err := this.p.UpdateMyPasswd(newPd, jclaim.UserId, oriEn)
if err != nil {
logs.Error(err)
hret.Error(ctx.ResponseWriter, 421, i18n.Get(ctx.Request, err_msg), err)
return
}
http.SetCookie(ctx.ResponseWriter, &http.Cookie{Name: "Authorization", Value: "", Path: "/", MaxAge: -1})
hret.Success(ctx.ResponseWriter, i18n.Success(ctx.Request))
}