Skip to content

Commit 0042f62

Browse files
Fix session expire error when opening settings
ref DEV-2501
2 parents 36f5721 + 9eab39b commit 0042f62

26 files changed

+94
-54
lines changed

.vettedpositions

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@
4040
/pkg/auth/handler/webapp/auth_entry_point_middleware.go:31:31: requestcontext
4141
/pkg/auth/handler/webapp/auth_entry_point_middleware.go:32:35: requestcontext
4242
/pkg/auth/handler/webapp/authflow_change_password.go:96:26: requestcontext
43-
/pkg/auth/handler/webapp/authflow_controller.go:989:30: requestcontext
44-
/pkg/auth/handler/webapp/authflow_controller.go:994:24: requestcontext
45-
/pkg/auth/handler/webapp/authflow_controller.go:1002:19: requestcontext
46-
/pkg/auth/handler/webapp/authflow_controller.go:1011:19: requestcontext
43+
/pkg/auth/handler/webapp/authflow_controller.go:990:30: requestcontext
44+
/pkg/auth/handler/webapp/authflow_controller.go:995:24: requestcontext
45+
/pkg/auth/handler/webapp/authflow_controller.go:1003:19: requestcontext
46+
/pkg/auth/handler/webapp/authflow_controller.go:1012:19: requestcontext
4747
/pkg/auth/handler/webapp/authflow_create_password.go:132:26: requestcontext
4848
/pkg/auth/handler/webapp/authflow_enter_oob_otp.go:156:26: requestcontext
4949
/pkg/auth/handler/webapp/authflow_enter_password.go:138:26: requestcontext

pkg/auth/handler/webapp/alternatives.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type CreateAuthenticatorPhoneOTPNode interface {
2828
// nolint: gocognit
2929
func handleAlternativeSteps(ctrl *Controller) {
3030
ctrl.PostAction("choose_step", func(ctx context.Context) (err error) {
31-
session, err := ctrl.GetWebappSession(ctx)
31+
session, err := ctrl.InteractionSession(ctx)
3232
if err != nil {
3333
return err
3434
}

pkg/auth/handler/webapp/authflow_controller.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func (c *AuthflowController) HandleStartOfFlow(
202202
handleWithScreen(screen)
203203
}
204204

205-
func (c *AuthflowController) isExpectedWebSessionError(err error) bool {
205+
func (c *AuthflowController) isWebSessionNotFoundOrCompletedError(err error) bool {
206206
return apierrors.IsKind(err, webapp.WebUIInvalidSession) || apierrors.IsKind(err, webapp.WebUISessionCompleted)
207207
}
208208

@@ -211,7 +211,7 @@ func (c *AuthflowController) HandleOAuthCallback(ctx context.Context, w http.Res
211211

212212
s, err := c.Sessions.Get(ctx, state.WebSessionID)
213213
if err != nil {
214-
if !c.isExpectedWebSessionError(err) {
214+
if !c.isWebSessionNotFoundOrCompletedError(err) {
215215
c.Logger.WithError(err).Errorf("failed to get web session")
216216
}
217217
c.renderError(ctx, w, r, err)
@@ -313,7 +313,7 @@ func (c *AuthflowController) HandleStep(ctx context.Context, w http.ResponseWrit
313313

314314
s, err := c.getWebSession(ctx)
315315
if err != nil {
316-
if !c.isExpectedWebSessionError(err) {
316+
if !c.isWebSessionNotFoundOrCompletedError(err) {
317317
c.Logger.WithError(err).Errorf("failed to get web session")
318318
}
319319
c.renderError(ctx, w, r, err)
@@ -345,7 +345,7 @@ func (c *AuthflowController) HandleWithoutFlow(ctx context.Context, w http.Respo
345345
var session *webapp.Session
346346
s, err := c.getWebSession(ctx)
347347
if err != nil {
348-
if !c.isExpectedWebSessionError(err) {
348+
if !c.isWebSessionNotFoundOrCompletedError(err) {
349349
c.Logger.WithError(err).Errorf("failed to get web session")
350350
}
351351
} else {
@@ -384,7 +384,8 @@ func (c *AuthflowController) getOrCreateWebSession(ctx context.Context, w http.R
384384
if err == nil && s != nil {
385385
return s, nil
386386
}
387-
if !errors.Is(err, webapp.ErrSessionNotFound) {
387+
388+
if !c.isWebSessionNotFoundOrCompletedError(err) {
388389
return nil, err
389390
}
390391

pkg/auth/handler/webapp/authflowv2/select_account.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func (h *AuthflowV2SelectAccountHandler) ServeHTTP(w http.ResponseWriter, r *htt
123123
ctrl.BeforeHandle(func(ctx context.Context) error {
124124

125125
// Ensure webapp session exist
126-
ws, err := ctrl.GetWebappSession(ctx)
126+
ws, err := ctrl.InteractionSession(ctx)
127127
if err != nil {
128128
return err
129129
}

pkg/auth/handler/webapp/confirm_terminate_other_sessions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (h *ConfirmTerminateOtherSessionsHandler) ServeHTTP(w http.ResponseWriter,
6666
defer ctrl.ServeWithDBTx(r.Context())
6767

6868
ctrl.Get(func(ctx context.Context) error {
69-
session, err := ctrl.GetWebappSession(ctx)
69+
session, err := ctrl.InteractionSession(ctx)
7070
if err != nil {
7171
return err
7272
}
@@ -91,7 +91,7 @@ func (h *ConfirmTerminateOtherSessionsHandler) ServeHTTP(w http.ResponseWriter,
9191
return err
9292
}
9393

94-
session, err := ctrl.GetWebappSession(ctx)
94+
session, err := ctrl.InteractionSession(ctx)
9595
if err != nil {
9696
return err
9797
}

pkg/auth/handler/webapp/controller.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package webapp
22

33
import (
44
"context"
5-
"errors"
65
"fmt"
76
"net/http"
87

@@ -315,19 +314,16 @@ func (c *Controller) rewindSessionHistory(session *webapp.Session) error {
315314
return nil
316315
}
317316

318-
func (c *Controller) GetWebappSession(ctx context.Context) (*webapp.Session, error) {
317+
func (c *Controller) InteractionSession(ctx context.Context) (*webapp.Session, error) {
319318
s := webapp.GetSession(ctx)
320-
if s == nil {
319+
if s == nil || s.IsCompleted {
321320
return nil, webapp.ErrSessionNotFound
322321
}
323-
if s.IsCompleted {
324-
return nil, webapp.ErrSessionCompleted
325-
}
326322
return s, nil
327323
}
328324

329325
func (c *Controller) InteractionGet(ctx context.Context) (*interaction.Graph, error) {
330-
s, err := c.GetWebappSession(ctx)
326+
s, err := c.InteractionSession(ctx)
331327
if err != nil {
332328
return nil, err
333329
}
@@ -348,7 +344,7 @@ func (c *Controller) InteractionGetWithSession(ctx context.Context, s *webapp.Se
348344
}
349345

350346
func (c *Controller) InteractionPost(ctx context.Context, inputFn func() (interface{}, error)) (*webapp.Result, error) {
351-
s, err := c.GetWebappSession(ctx)
347+
s, err := c.InteractionSession(ctx)
352348
if err != nil {
353349
return nil, err
354350
}
@@ -381,13 +377,9 @@ func (c *Controller) InteractionOAuthCallback(ctx context.Context, oauthInput In
381377
}
382378

383379
func (c *Controller) getSettingsActionWebSession(ctx context.Context, r *http.Request) (*webapp.Session, error) {
384-
webappSession, err := c.GetWebappSession(ctx)
385-
if err != nil {
386-
// No session means it is not in settings action
387-
if errors.Is(err, webapp.ErrSessionNotFound) {
388-
return nil, nil
389-
}
390-
return nil, err
380+
webappSession := webapp.GetSession(ctx)
381+
if webappSession == nil {
382+
return nil, nil
391383
}
392384
if webappSession.SettingsActionID == "" {
393385
// This session is not for a settings action, ignore it
@@ -401,6 +393,9 @@ func (c *Controller) getSettingsActionWebSession(ctx context.Context, r *http.Re
401393
// This session is not for the current settings action, ignore it
402394
return nil, nil
403395
}
396+
if webappSession.IsCompleted {
397+
return nil, webapp.ErrSessionCompleted
398+
}
404399
return webappSession, nil
405400
}
406401

pkg/auth/handler/webapp/create_passkey.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (h *CreatePasskeyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
5353
defer ctrl.ServeWithDBTx(r.Context())
5454

5555
ctrl.Get(func(ctx context.Context) error {
56-
session, err := ctrl.GetWebappSession(ctx)
56+
session, err := ctrl.InteractionSession(ctx)
5757
if err != nil {
5858
return err
5959
}

pkg/auth/handler/webapp/create_password.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func (h *CreatePasswordHandler) ServeHTTP(w http.ResponseWriter, r *http.Request
107107
defer ctrl.ServeWithDBTx(r.Context())
108108

109109
ctrl.Get(func(ctx context.Context) error {
110-
session, err := ctrl.GetWebappSession(ctx)
110+
session, err := ctrl.InteractionSession(ctx)
111111
if err != nil {
112112
return err
113113
}

pkg/auth/handler/webapp/enter_oob_otp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func (h *EnterOOBOTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
161161
defer ctrl.ServeWithDBTx(r.Context())
162162

163163
ctrl.Get(func(ctx context.Context) error {
164-
session, err := ctrl.GetWebappSession(ctx)
164+
session, err := ctrl.InteractionSession(ctx)
165165
if err != nil {
166166
return err
167167
}

pkg/auth/handler/webapp/enter_password.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (h *EnterPasswordHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
118118
defer ctrl.ServeWithDBTx(r.Context())
119119

120120
ctrl.Get(func(ctx context.Context) error {
121-
session, err := ctrl.GetWebappSession(ctx)
121+
session, err := ctrl.InteractionSession(ctx)
122122
if err != nil {
123123
return err
124124
}

0 commit comments

Comments
 (0)