/
confirm.go
82 lines (62 loc) · 1.95 KB
/
confirm.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
package auth
import (
"io"
"net/http"
"github.com/artem-malko/auth-and-go/managers/user"
"github.com/artem-malko/auth-and-go/api/middleware"
"github.com/artem-malko/auth-and-go/api/response"
"github.com/go-chi/render"
"github.com/google/uuid"
"github.com/pkg/errors"
)
type confirmParams struct {
RawConfirmationToken string `json:"confirmation_token"`
ParsedConfirmationToken uuid.UUID
}
func (p *confirmParams) Bind(r *http.Request) error {
if p.RawConfirmationToken == "" {
return errors.New("confirmation_token is required")
}
parsedConfirmationToken, err := uuid.Parse(p.RawConfirmationToken)
if err != nil {
return errors.New("confirmation_token is invalid")
}
p.ParsedConfirmationToken = parsedConfirmationToken
return nil
}
func (h *handlers) ConfirmRegistration(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// Requests from trusted clients can be proceeded only
if !middleware.IsTrustedClient(ctx) {
response.Error(w, http.StatusForbidden, "Unknown client")
return
}
reqParams := &confirmParams{}
if err := render.Bind(r, reqParams); err != nil {
switch errors.Cause(err) {
case io.EOF:
response.Error(w, http.StatusBadRequest, "Request body is required. Pass correct confirmation_token.")
default:
response.Error(w, http.StatusBadRequest, err.Error())
}
return
}
u, sessionTokens, err := h.userManager.ConfirmRegistration(reqParams.ParsedConfirmationToken)
if err != nil {
switch errors.Cause(err) {
case user.ErrUserIncorrectTokenToUse:
response.Error(w, http.StatusForbidden, "Token can not be used")
default:
h.logger(r).
WithField("method", "ConfirmRegistration").
WithField("code", http.StatusInternalServerError).
Error(err.Error())
response.InternalServerError(w)
}
return
}
sessionCookies := h.createSessionCookies(*sessionTokens)
http.SetCookie(w, &sessionCookies.accessTokenCookie)
http.SetCookie(w, &sessionCookies.refreshTokenCookie)
response.OK(w, u)
}