@@ -3,6 +3,7 @@ package authenticate_test
33import (
44 "context"
55 "encoding/base64"
6+ "encoding/json"
67 "errors"
78 "io"
89 "log/slog"
@@ -575,7 +576,7 @@ func TestService_FinishFlow(t *testing.T) {
575576 },
576577 },
577578 {
578- name : "destroy the flow if the code is wrong past the attempt cap" ,
579+ name : "destroy the flow if the wrong code reaches the attempt cap" ,
579580 args : args {
580581 ctx : context .Background (),
581582 request : authenticate.RegistrationFinishRequest {
@@ -590,7 +591,7 @@ func TestService_FinishFlow(t *testing.T) {
590591 mockFlowRepo , _ , _ , _ , _ := createMocks (t )
591592 ctx := context .Background ()
592593 mockFlowRepo .EXPECT ().Get (ctx , flowID ).
593- Return (mailOTPFlow (flowID , timeNow , string (otpHash ), pkgMetadata.Metadata {"callback_url" : "" , "attempt" : 3 }), nil )
594+ Return (mailOTPFlow (flowID , timeNow , string (otpHash ), pkgMetadata.Metadata {"callback_url" : "" , "attempt" : 2 }), nil )
594595 mockFlowRepo .EXPECT ().Delete (ctx , flowID ).Return (nil )
595596 srv := authenticate .NewService (nil , authenticate.Config {}, mockFlowRepo , nil ,
596597 nil , nil , nil , nil , nil , nil )
@@ -657,6 +658,91 @@ func TestService_FinishFlow_WrongThenRightOTP(t *testing.T) {
657658 assert .Equal (t , sampleUser , got .User )
658659}
659660
661+ // jsonFlowRepository keeps flow metadata as JSON bytes, the same way the
662+ // Postgres repository stores it. Numbers in metadata come back from Get as
663+ // float64, matching a real database round trip.
664+ type jsonFlowRepository struct {
665+ flows map [uuid.UUID ]jsonStoredFlow
666+ }
667+
668+ type jsonStoredFlow struct {
669+ flow authenticate.Flow
670+ rawMetadata []byte
671+ }
672+
673+ func (r * jsonFlowRepository ) Set (_ context.Context , flow * authenticate.Flow ) error {
674+ raw , err := json .Marshal (flow .Metadata )
675+ if err != nil {
676+ return err
677+ }
678+ r .flows [flow .ID ] = jsonStoredFlow {flow : * flow , rawMetadata : raw }
679+ return nil
680+ }
681+
682+ func (r * jsonFlowRepository ) Get (_ context.Context , id uuid.UUID ) (* authenticate.Flow , error ) {
683+ stored , ok := r .flows [id ]
684+ if ! ok {
685+ return nil , authenticate .ErrFlowInvalid
686+ }
687+ flow := stored .flow
688+ var md pkgMetadata.Metadata
689+ if err := json .Unmarshal (stored .rawMetadata , & md ); err != nil {
690+ return nil , err
691+ }
692+ flow .Metadata = md
693+ return & flow , nil
694+ }
695+
696+ func (r * jsonFlowRepository ) Delete (_ context.Context , id uuid.UUID ) error {
697+ delete (r .flows , id )
698+ return nil
699+ }
700+
701+ func (r * jsonFlowRepository ) DeleteExpiredFlows (_ context.Context ) error {
702+ return nil
703+ }
704+
705+ func TestService_FinishFlow_OTPAttemptCapAfterJSONRoundTrip (t * testing.T ) {
706+ flowID := uuid .New ()
707+ timeNow := time .Now ()
708+ otpHash , err := bcrypt .GenerateFromPassword ([]byte ("111111" ), bcrypt .MinCost )
709+ require .NoError (t , err )
710+
711+ ctx := context .Background ()
712+ flowRepo := & jsonFlowRepository {flows : map [uuid.UUID ]jsonStoredFlow {}}
713+ require .NoError (t , flowRepo .Set (ctx , mailOTPFlow (flowID , timeNow , string (otpHash ), pkgMetadata.Metadata {"callback_url" : "" })))
714+
715+ srv := authenticate .NewService (nil , authenticate.Config {}, flowRepo , nil ,
716+ nil , nil , nil , nil , nil , nil )
717+ srv .Now = func () time.Time {
718+ return timeNow
719+ }
720+
721+ wrongCode := authenticate.RegistrationFinishRequest {
722+ Method : authenticate .MailOTPAuthMethod .String (),
723+ State : flowID .String (),
724+ Code : "222222" ,
725+ }
726+
727+ for attempt := 1 ; attempt <= 2 ; attempt ++ {
728+ got , err := srv .FinishFlow (ctx , wrongCode )
729+ assert .ErrorIs (t , err , authenticate .ErrInvalidMailOTP )
730+ assert .Nil (t , got )
731+
732+ flow , err := flowRepo .Get (ctx , flowID )
733+ require .NoError (t , err )
734+ assert .Equal (t , float64 (attempt ), flow .Metadata ["attempt" ])
735+ }
736+
737+ // the third wrong code reaches the cap and destroys the flow
738+ got , err := srv .FinishFlow (ctx , wrongCode )
739+ assert .ErrorIs (t , err , authenticate .ErrInvalidMailOTP )
740+ assert .Nil (t , got )
741+
742+ _ , err = flowRepo .Get (ctx , flowID )
743+ assert .ErrorIs (t , err , authenticate .ErrFlowInvalid )
744+ }
745+
660746func TestService_GetPrincipal_JWTGrantSkipsNonGrantToken (t * testing.T ) {
661747 userID := uuid .New ()
662748 patValue := "fpt_opaque-not-a-jwt"
0 commit comments