Fix race condition in magic link token verification and typo in auth error message
- Hold SELECT FOR UPDATE lock within transaction by using tx directly instead of separate WithConn, ensuring mutual exclusion when multiple requests race to verify the same token - Fix "resouce" → "resource" typo in authentication error messages Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
@@ -628,21 +628,12 @@ func (s AuthService) OpenSessionWithMagicLink(ctx context.Context, tokenString s
|
|||||||
hashedValue := HashToken(tokenString)
|
hashedValue := HashToken(tokenString)
|
||||||
token := &coredata.Token{}
|
token := &coredata.Token{}
|
||||||
|
|
||||||
if err := s.pg.WithConn(
|
if err := token.LoadByHashedValueForUpdate(ctx, tx, hashedValue); err != nil {
|
||||||
ctx,
|
if errors.Is(err, coredata.ErrResourceNotFound) {
|
||||||
func(conn pg.Conn) error {
|
return NewInvalidTokenError()
|
||||||
if err := token.LoadByHashedValueForUpdate(ctx, conn, hashedValue); err != nil {
|
}
|
||||||
if errors.Is(err, coredata.ErrResourceNotFound) {
|
|
||||||
return NewInvalidTokenError()
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Errorf("cannot load token by hashed value: %w", err)
|
return fmt.Errorf("cannot load token by hashed value: %w", err)
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
); err != nil {
|
|
||||||
return fmt.Errorf("cannot load token: %w", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err := identity.LoadByEmail(ctx, tx, payload.Data.Email)
|
err := identity.LoadByEmail(ctx, tx, payload.Data.Email)
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ func NewIdentityPresenceMiddleware() func(next http.Handler) http.Handler {
|
|||||||
Errors: gqlerror.List{
|
Errors: gqlerror.List{
|
||||||
gqlutils.Unauthenticatedf(
|
gqlutils.Unauthenticatedf(
|
||||||
r.Context(),
|
r.Context(),
|
||||||
"authentication is required to access this resouce",
|
"authentication is required to access this resource",
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -92,7 +92,7 @@ func Directive(ctx context.Context, obj any, next graphql.Resolver, required Ses
|
|||||||
if identity == nil {
|
if identity == nil {
|
||||||
return nil, gqlutils.Unauthenticatedf(
|
return nil, gqlutils.Unauthenticatedf(
|
||||||
ctx,
|
ctx,
|
||||||
"authentication is required to access this resouce",
|
"authentication is required to access this resource",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
case SessionRequirementNone:
|
case SessionRequirementNone:
|
||||||
|
|||||||
Reference in New Issue
Block a user