-
Notifications
You must be signed in to change notification settings - Fork 16
/
login_handler.go
175 lines (146 loc) · 5.52 KB
/
login_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package httphandler
import (
"errors"
"fmt"
"net/http"
"github.com/stellar/stellar-disbursement-platform-backend/internal/data"
"github.com/stellar/stellar-disbursement-platform-backend/internal/htmltemplate"
"github.com/stellar/stellar-disbursement-platform-backend/internal/message"
"github.com/stellar/stellar-disbursement-platform-backend/internal/utils"
"github.com/stellar/go/support/http/httpdecode"
"github.com/stellar/go/support/log"
"github.com/stellar/go/support/render/httpjson"
"github.com/stellar/stellar-disbursement-platform-backend/internal/serve/httperror"
"github.com/stellar/stellar-disbursement-platform-backend/internal/serve/validators"
"github.com/stellar/stellar-disbursement-platform-backend/stellar-auth/pkg/auth"
)
const mfaMessageTitle = "Verification code to access your account"
type LoginRequest struct {
Email string `json:"email"`
Password string `json:"password"`
ReCAPTCHAToken string `json:"recaptcha_token"`
}
func (r LoginRequest) validate() *httperror.HTTPError {
validator := validators.NewValidator()
validator.Check(r.Email != "", "email", "email is required")
validator.Check(r.Password != "", "password", "password is required")
if validator.HasErrors() {
return httperror.BadRequest("Request invalid", nil, validator.Errors)
}
return nil
}
type LoginResponse struct {
Token string `json:"token"`
}
type LoginHandler struct {
AuthManager auth.AuthManager
ReCAPTCHAValidator validators.ReCAPTCHAValidator
MessengerClient message.MessengerClient
Models *data.Models
ReCAPTCHADisabled bool
MFADisabled bool
}
func (h LoginHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
ctx := req.Context()
var reqBody LoginRequest
if err := httpdecode.DecodeJSON(req, &reqBody); err != nil {
err = fmt.Errorf("decoding the request body: %w", err)
log.Ctx(ctx).Error(err)
httperror.BadRequest("", err, nil).Render(rw)
return
}
if err := reqBody.validate(); err != nil {
err.Render(rw)
return
}
if !h.ReCAPTCHADisabled {
// validating reCAPTCHA Token
isValid, err := h.ReCAPTCHAValidator.IsTokenValid(ctx, reqBody.ReCAPTCHAToken)
if err != nil {
httperror.Unauthorized("Cannot validate reCAPTCHA token", err, nil).Render(rw)
return
}
if !isValid {
log.Ctx(ctx).Errorf("reCAPTCHA token is invalid for request with email %s", utils.TruncateString(reqBody.Email, 3))
httperror.BadRequest("reCAPTCHA token invalid", nil, nil).Render(rw)
return
}
}
token, err := h.AuthManager.Authenticate(ctx, reqBody.Email, reqBody.Password)
if errors.Is(err, auth.ErrInvalidCredentials) {
httperror.Unauthorized("", err, map[string]interface{}{"details": "Incorrect email or password"}).Render(rw)
return
}
if err != nil {
log.Ctx(ctx).Errorf("error authenticating user with email %s: %s", utils.TruncateString(reqBody.Email, 3), err)
httperror.InternalError(ctx, "Cannot authenticate user credentials", err, nil).Render(rw)
return
}
user, err := h.AuthManager.GetUser(ctx, token)
if err != nil {
log.Ctx(ctx).Errorf("error getting user with email %s: %s", utils.TruncateString(reqBody.Email, 3), err)
httperror.InternalError(ctx, "", err, nil).Render(rw)
return
}
if h.MFADisabled {
log.Ctx(ctx).Infof("[UserLogin] - Logged in user with account ID %s", user.ID)
httpjson.RenderStatus(rw, http.StatusOK, LoginResponse{Token: token}, httpjson.JSON)
return
}
// 🔒 Handling MFA
deviceID := req.Header.Get(DeviceIDHeader)
if deviceID == "" {
httperror.BadRequest("Device-ID header is required", nil, nil).Render(rw)
return
}
isRemembered, err := h.AuthManager.MFADeviceRemembered(ctx, deviceID, user.ID)
if err != nil {
log.Ctx(ctx).Errorf("error checking if device is remembered for user with email %s: %s", utils.TruncateString(reqBody.Email, 3), err.Error())
httperror.InternalError(ctx, "", err, nil).Render(rw)
return
}
if isRemembered {
log.Ctx(ctx).Infof("[UserLogin] - Logged in user with account ID %s", user.ID)
httpjson.RenderStatus(rw, http.StatusOK, LoginResponse{Token: token}, httpjson.JSON)
return
}
// Get the MFA code for the user
code, err := h.AuthManager.GetMFACode(ctx, deviceID, user.ID)
if err != nil {
log.Ctx(ctx).Errorf("error getting MFA code for user with email %s: %s", utils.TruncateString(reqBody.Email, 3), err.Error())
httperror.InternalError(ctx, "Cannot get MFA code", err, nil).Render(rw)
return
}
if code == "" {
log.Ctx(ctx).Errorf("MFA code for user with email %s is empty", utils.TruncateString(reqBody.Email, 3))
httperror.InternalError(ctx, "Cannot get MFA code", err, nil).Render(rw)
return
}
organization, err := h.Models.Organizations.Get(ctx)
if err != nil {
httperror.InternalError(ctx, "Cannot fetch organization", err, nil).Render(rw)
return
}
msgTemplate := htmltemplate.MFAMessageTemplate{
MFACode: code,
OrganizationName: organization.Name,
}
msgContent, err := htmltemplate.ExecuteHTMLTemplateForMFAMessage(msgTemplate)
if err != nil {
httperror.InternalError(ctx, "Cannot execute mfa message template", err, nil).Render(rw)
return
}
msg := message.Message{
ToEmail: user.Email,
Title: mfaMessageTitle,
Message: msgContent,
}
err = h.MessengerClient.SendMessage(msg)
if err != nil {
err = fmt.Errorf("error sending mfa code for email %s: %w", user.Email, err)
log.Ctx(ctx).Error(err)
httperror.InternalError(ctx, "", err, nil).Render(rw)
return
}
httpjson.RenderStatus(rw, http.StatusOK, map[string]string{"message": "MFA code sent to email. Check your inbox and spam folders."}, httpjson.JSON)
}