-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathattempt_change_email.go
93 lines (72 loc) · 2.41 KB
/
attempt_change_email.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
package api
import (
pttbbsapi "github.com/Ptt-official-app/go-pttbbs/api"
"github.com/Ptt-official-app/go-pttbbs/bbs"
"github.com/Ptt-official-app/go-pttbbsweb/types"
"github.com/Ptt-official-app/go-pttbbsweb/utils"
"github.com/gin-gonic/gin"
)
const ATTEMPT_CHANGE_EMAIL_R = "/user/:user_id/attemptchangeemail"
type AttemptChangeEmailParams struct {
ClientID string `json:"client_id" form:"client_id"`
ClientSecret string `json:"client_secret" form:"client_secret"`
Password string `json:"password" form:"password"`
Email string `json:"email" form:"email"`
}
type AttemptChangeEmailPath struct {
UserID bbs.UUserID `uri:"user_id"`
}
type AttemptChangeEmailResult struct {
UserID bbs.UUserID `json:"user_id"`
Email string `json:"email"`
TokenUser bbs.UUserID `json:"tokenuser"`
}
func AttemptChangeEmailWrapper(c *gin.Context) {
params := &AttemptChangeEmailParams{}
path := &AttemptChangeEmailPath{}
LoginRequiredPathJSON(AttemptChangeEmail, params, path, c)
}
func AttemptChangeEmail(remoteAddr string, userID bbs.UUserID, params interface{}, path interface{}, c *gin.Context) (result interface{}, statusCode int, err error) {
theParams, ok := params.(*AttemptChangeEmailParams)
if !ok {
return nil, 400, ErrInvalidParams
}
thePath, ok := path.(*AttemptChangeEmailPath)
if !ok {
return nil, 400, ErrInvalidPath
}
err = checkUniqueEmail(theParams.Email)
if err != nil {
return nil, 403, err
}
isValidClient, client := checkClient(theParams.ClientID, theParams.ClientSecret)
if !isValidClient {
return nil, 400, ErrInvalidParams
}
clientInfo := getClientInfo(client)
// get backend data
theParams_b := &pttbbsapi.AttemptChangeEmailParams{
ClientInfo: clientInfo,
Passwd: theParams.Password,
Email: theParams.Email,
}
var result_b *pttbbsapi.AttemptChangeEmailResult
urlMap := map[string]string{
"uid": string(thePath.UserID),
}
url := utils.MergeURL(urlMap, pttbbsapi.ATTEMPT_CHANGE_EMAIL_R)
statusCode, err = utils.BackendPost(c, url, theParams_b, nil, &result_b)
if err != nil {
return nil, statusCode, err
}
err = deserializeEmailTokenAndEmail(theParams.Email, types.EMAILTOKEN_TITLE, result_b.UserID, result_b.Jwt, USER_CHANGE_EMAIL_HTML_R, types.EMAILTOKEN_TEMPLATE_CONTENT)
if err != nil {
return nil, 500, err
}
result = &AttemptChangeEmailResult{
UserID: thePath.UserID,
Email: theParams.Email,
TokenUser: userID,
}
return result, 200, nil
}