generated from fluffy-bunny/fluffycore-grpc-starterkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_password_reset_finish.go
201 lines (179 loc) · 7.39 KB
/
api_password_reset_finish.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package api_password_reset_finish
import (
"net/http"
di "github.com/fluffy-bunny/fluffy-dozm-di"
contracts_config "github.com/fluffy-bunny/fluffycore-rage-identity/pkg/contracts/config"
contracts_cookies "github.com/fluffy-bunny/fluffycore-rage-identity/pkg/contracts/cookies"
contracts_email "github.com/fluffy-bunny/fluffycore-rage-identity/pkg/contracts/email"
contracts_identity "github.com/fluffy-bunny/fluffycore-rage-identity/pkg/contracts/identity"
contracts_oidc_session "github.com/fluffy-bunny/fluffycore-rage-identity/pkg/contracts/oidc_session"
"github.com/fluffy-bunny/fluffycore-rage-identity/pkg/models/api/login_models"
services_echo_handlers_base "github.com/fluffy-bunny/fluffycore-rage-identity/pkg/services/echo/handlers/base"
wellknown_echo "github.com/fluffy-bunny/fluffycore-rage-identity/pkg/wellknown/echo"
proto_oidc_models "github.com/fluffy-bunny/fluffycore-rage-identity/proto/oidc/models"
proto_oidc_user "github.com/fluffy-bunny/fluffycore-rage-identity/proto/oidc/user"
contracts_handler "github.com/fluffy-bunny/fluffycore/echo/contracts/handler"
fluffycore_utils "github.com/fluffy-bunny/fluffycore/utils"
status "github.com/gogo/status"
echo "github.com/labstack/echo/v4"
zerolog "github.com/rs/zerolog"
codes "google.golang.org/grpc/codes"
wrapperspb "google.golang.org/protobuf/types/known/wrapperspb"
)
type (
service struct {
*services_echo_handlers_base.BaseHandler
config *contracts_config.Config
wellknownCookies contracts_cookies.IWellknownCookies
passwordHasher contracts_identity.IPasswordHasher
oidcSession contracts_oidc_session.IOIDCSession
}
)
var stemService = (*service)(nil)
func init() {
var _ contracts_handler.IHandler = stemService
}
func (s *service) Ctor(
container di.Container,
config *contracts_config.Config,
wellknownCookies contracts_cookies.IWellknownCookies,
passwordHasher contracts_identity.IPasswordHasher,
oidcSession contracts_oidc_session.IOIDCSession,
) (*service, error) {
return &service{
BaseHandler: services_echo_handlers_base.NewBaseHandler(container),
config: config,
wellknownCookies: wellknownCookies,
passwordHasher: passwordHasher,
oidcSession: oidcSession,
}, nil
}
// API_PasswordResetFinish = "/api/password-reset-finish"
// AddScopedIHandler registers the *service as a singleton.
func AddScopedIHandler(builder di.ContainerBuilder) {
contracts_handler.AddScopedIHandleWithMetadata[*service](builder,
stemService.Ctor,
[]contracts_handler.HTTPVERB{
contracts_handler.POST,
},
wellknown_echo.API_PasswordResetFinish,
)
}
func (s *service) GetMiddleware() []echo.MiddlewareFunc {
return []echo.MiddlewareFunc{}
}
func (s *service) validatePasswordResetFinishRequest(model *login_models.PasswordResetFinishRequest) error {
if fluffycore_utils.IsNil(model) {
return status.Error(codes.InvalidArgument, "model is nil")
}
if fluffycore_utils.IsEmptyOrNil(model.Password) {
return status.Error(codes.InvalidArgument, "model.Password is nil")
}
if fluffycore_utils.IsEmptyOrNil(model.PasswordConfirm) {
return status.Error(codes.InvalidArgument, "model.PasswordConfirm is nil")
}
if model.Password != model.PasswordConfirm {
return status.Error(codes.InvalidArgument, "model.Password and model.PasswordConfirm do not match")
}
return nil
}
// API Manifest godoc
// @Summary get the login manifest.
// @Description This is the configuration of the server..
// @Tags root
// @Accept */*
// @Produce json
// @Param request body login_models.PasswordResetFinishRequest true "PasswordResetStartRequest"
// @Success 200 {object} login_models.PasswordResetFinishResponse
// @Failure 400 {string} login_models.PasswordResetFinishResponse
// @Router /api/password-reset-finish [post]
func (s *service) Do(c echo.Context) error {
ctx := c.Request().Context()
log := zerolog.Ctx(ctx).With().Logger()
model := &login_models.PasswordResetFinishRequest{}
if err := c.Bind(model); err != nil {
log.Error().Err(err).Msg("Bind")
return c.JSONPretty(http.StatusInternalServerError, err.Error(), " ")
}
if err := s.validatePasswordResetFinishRequest(model); err != nil {
log.Error().Err(err).Msg("validatePasswordResetFinishRequest")
return c.JSONPretty(http.StatusBadRequest, err.Error(), " ")
}
response := &login_models.PasswordResetFinishResponse{}
getPasswordResetCookieResponse, err := s.wellknownCookies.GetPasswordResetCookie(c)
if err != nil {
log.Error().Err(err).Msg("GetPasswordResetCookie")
return c.JSONPretty(http.StatusInternalServerError, err.Error(), " ")
}
if getPasswordResetCookieResponse == nil {
response.Directive = login_models.DIRECTIVE_LoginPhaseOne_DisplayPhaseOnePage
return c.JSONPretty(http.StatusOK, response, " ")
}
if getPasswordResetCookieResponse.PasswordReset == nil {
response.Directive = login_models.DIRECTIVE_LoginPhaseOne_DisplayPhaseOnePage
}
if fluffycore_utils.IsEmptyOrNil(getPasswordResetCookieResponse.PasswordReset.Subject) {
s.wellknownCookies.DeletePasswordResetCookie(c)
response.Directive = login_models.DIRECTIVE_LoginPhaseOne_DisplayPhaseOnePage
}
getUserResponse, err := s.RageUserService().GetRageUser(ctx,
&proto_oidc_user.GetRageUserRequest{
By: &proto_oidc_user.GetRageUserRequest_Subject{
Subject: getPasswordResetCookieResponse.PasswordReset.Subject,
},
})
if err != nil {
log.Error().Err(err).Msg("ListUser")
return c.JSONPretty(http.StatusInternalServerError, err.Error(), " ")
}
// do password acceptablity check
err = s.passwordHasher.IsAcceptablePassword(&contracts_identity.IsAcceptablePasswordRequest{
Password: model.Password,
})
if err != nil {
response.Directive = login_models.DIRECTIVE_PasswordReset_DisplayPasswordResetPage
response.ErrorReason = login_models.PasswordResetErrorReason_InvalidPassword
return c.JSONPretty(http.StatusBadRequest, response, " ")
}
// hash the password
hashPasswordResponse, err := s.passwordHasher.HashPassword(ctx,
&contracts_identity.HashPasswordRequest{
Password: model.Password,
})
if err != nil {
log.Error().Err(err).Msg("GeneratePasswordHash")
return c.JSONPretty(http.StatusInternalServerError, err.Error(), " ")
}
_, err = s.RageUserService().UpdateRageUser(ctx, &proto_oidc_user.UpdateRageUserRequest{
User: &proto_oidc_models.RageUserUpdate{
RootIdentity: &proto_oidc_models.IdentityUpdate{
Subject: getPasswordResetCookieResponse.PasswordReset.Subject,
},
Password: &proto_oidc_models.PasswordUpdate{
Hash: &wrapperspb.StringValue{
Value: hashPasswordResponse.HashedPassword,
},
},
},
})
if err != nil {
log.Error().Err(err).Msg("UpdateUser")
return c.JSONPretty(http.StatusInternalServerError, err.Error(), " ")
}
// send the email
sendSimpleEmailRequest := &contracts_email.SendSimpleEmailRequest{
ToEmail: getUserResponse.User.RootIdentity.Email,
SubjectId: "password.reset.changed.subject",
BodyId: "password.reset.changed.message",
}
log.Info().Interface("sendSimpleEmailRequest", sendSimpleEmailRequest).Err(err).Msg("SendEmail Reset Password")
_, err = s.EmailService().SendSimpleEmail(ctx, sendSimpleEmailRequest)
if err != nil {
log.Error().Interface("sendSimpleEmailRequest", sendSimpleEmailRequest).Err(err).Msg("SendEmail")
// eat it since we have already updated the password
err = nil
}
s.wellknownCookies.DeletePasswordResetCookie(c)
response.Directive = login_models.DIRECTIVE_LoginPhaseOne_DisplayPhaseOnePage
return c.JSONPretty(http.StatusOK, response, " ")
}