-
Notifications
You must be signed in to change notification settings - Fork 0
/
confirm_handler.go
103 lines (81 loc) · 2.95 KB
/
confirm_handler.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
// Copyright 2024 Bill Nixon. All rights reserved.
// Use of this source code is governed by the license found in the LICENSE file.
package webauth
import (
"log/slog"
"net/http"
"strings"
"github.com/bnixon67/webapp/webhandler"
"github.com/bnixon67/webapp/webutil"
)
const ConfirmTmpl = "confirm.html"
// ConfirmData contains data to render the confirm template.
type ConfirmData struct {
CommonData
Message string
ConfirmToken string
}
// ConfirmHandlerGet processes GET requests for the email confirmation page.
//
// This function retrieves the 'ctoken' (confirmation token) from the query
// parameters of the request URL. If a 'ctoken' is present, it is displayed to
// the user on the confirmation page. Users may also manually enter a 'ctoken'.
//
// Submission of the token is via a POST request, which is handled by
// ConfirmHandlerPost, which completes the email confirmation process.
func (app *AuthApp) ConfirmHandlerGet(w http.ResponseWriter, r *http.Request) {
logger := webhandler.NewRequestLoggerWithFuncName(r)
if !webutil.IsMethodOrError(w, r, http.MethodGet) {
logger.Error("invalid method")
return
}
ctoken := r.URL.Query().Get("ctoken")
data := ConfirmData{ConfirmToken: ctoken}
app.RenderPage(w, logger, ConfirmTmpl, &data)
logger.Info("done")
}
const (
MsgMissingConfirmToken = "Please provide a token."
MsgInvalidConfirmToken = "Token is invalid. Request a new token."
MsgExpiredConfirmToken = "Token is expired. Request a new token."
)
var tokenErrToMsg = map[error]string{
ErrMissingConfirmToken: MsgMissingConfirmToken,
ErrTokenNotFound: MsgInvalidConfirmToken,
ErrConfirmTokenExpired: MsgExpiredConfirmToken,
}
func (app *AuthApp) respondWithError(w http.ResponseWriter, logger *slog.Logger, err error, ctoken string) {
logger.Error("failed to confirm user", "err", err)
msg, ok := tokenErrToMsg[err]
if !ok {
webutil.RespondWithError(w, http.StatusInternalServerError)
return
}
app.RenderPage(w, logger, ConfirmTmpl, &ConfirmData{Message: msg})
}
// ConfirmHandlerPost processes POST requests for user email confirmation.
//
// It extracts the 'ctoken' (confirmation token) from the form data to verify
// and confirm the associated user. If the token is valid, the user's status
// is updated to confirmed.
func (app *AuthApp) ConfirmHandlerPost(w http.ResponseWriter, r *http.Request) {
logger := webhandler.NewRequestLoggerWithFuncName(r)
if !webutil.IsMethodOrError(w, r, http.MethodPost) {
logger.Error("invalid method")
return
}
ctoken := strings.TrimSpace(r.PostFormValue("ctoken"))
username, err := app.DB.UsernameForConfirmToken(ctoken)
if err != nil {
app.respondWithError(w, logger, err, ctoken)
return
}
err = app.DB.ConfirmUser(username, ctoken)
if err != nil {
app.respondWithError(w, logger, err, ctoken)
return
}
logger.Info("user confirmed", "username", username)
app.DB.WriteEvent(EventConfirmed, true, username, "success")
http.Redirect(w, r, "/confirmed", http.StatusSeeOther)
}