diff --git a/challenge/entities/challenge.go b/challenge/entities/challenge.go index eb11fc6..c378f39 100644 --- a/challenge/entities/challenge.go +++ b/challenge/entities/challenge.go @@ -3,3 +3,7 @@ package entities type Challenge struct { Name string `json:"name"` } + +func (c Challenge) GetName() string { + return c.Name +} diff --git a/challenge/interface.go b/challenge/interface.go index 3fc1bb0..8626f25 100644 --- a/challenge/interface.go +++ b/challenge/interface.go @@ -3,6 +3,7 @@ package challenge import "context" type IChallenge interface { + GetName() string Solve(ctx context.Context, body map[string]interface{}) (*map[string]interface{}, error) Request(ctx context.Context, body map[string]interface{}) (*map[string]interface{}, error) // Request a challenge, ex: for OTP you have to request an OTP before you can solve it } diff --git a/examples/single_flow_multiple_challenges/challenges/dummy2.go b/examples/single_flow_multiple_challenges/challenges/dummy2.go index ef8ac38..165e797 100644 --- a/examples/single_flow_multiple_challenges/challenges/dummy2.go +++ b/examples/single_flow_multiple_challenges/challenges/dummy2.go @@ -39,7 +39,7 @@ func (c *DummyTwoChallenge) Request(ctx context.Context, body map[string]interfa func NewDummyTwoChallenge() challenge.IChallenge { dummyChallenge := entities.Challenge{ - Name: "dummy", + Name: "dummy2", } return &DummyTwoChallenge{ Challenge: dummyChallenge, diff --git a/examples/single_flow_multiple_challenges/flows/single_flow.go b/examples/single_flow_multiple_challenges/flows/single_flow.go index c89ad63..fc2f749 100644 --- a/examples/single_flow_multiple_challenges/flows/single_flow.go +++ b/examples/single_flow_multiple_challenges/flows/single_flow.go @@ -40,9 +40,9 @@ func (f SingleFlow) Initialize(ctx context.Context) (*JWTEntities.JWTAdditions, func NewSingleFlow() flow.IFlow { flow := entities.Flow{ Name: "single_flow_single_challenge", - Challenges: map[string]challenge.IChallenge{ - "dummy": challenges.NewDummyChallenge(), - "dummy2": challenges.NewDummyTwoChallenge(), + Challenges: []challenge.IChallenge{ + challenges.NewDummyChallenge(), + challenges.NewDummyTwoChallenge(), }, } return &SingleFlow{ diff --git a/examples/single_flow_single_challenge/flows/single_flow.go b/examples/single_flow_single_challenge/flows/single_flow.go index 0120f46..2d9a5f3 100644 --- a/examples/single_flow_single_challenge/flows/single_flow.go +++ b/examples/single_flow_single_challenge/flows/single_flow.go @@ -40,8 +40,8 @@ func (f SingleFlow) Resolve(jwtData mfaEntities.JWTData) (*map[string]interface{ func NewSingleFlow() flow.IFlow { flow := entities.Flow{ Name: "single_flow_single_challenge", - Challenges: map[string]challenge.IChallenge{ - "dummy": challenges.NewDummyChallenge(), + Challenges: []challenge.IChallenge{ + challenges.NewDummyChallenge(), }, } return &SingleFlow{ diff --git a/flow/entities/flow.go b/flow/entities/flow.go index 3a3d81c..6c09b06 100644 --- a/flow/entities/flow.go +++ b/flow/entities/flow.go @@ -10,8 +10,8 @@ import ( ) type Flow struct { - Name string `json:"name"` - Challenges map[string]challengeEntity.IChallenge `json:"challenges"` + Name string `json:"name"` + Challenges []challengeEntity.IChallenge `json:"challenges"` } func (f Flow) GetName() string { @@ -27,21 +27,38 @@ func (f Flow) Solve(ctx context.Context, challenge string, input string, JWTData if err != nil { return nil, err } - if challenge, ok := f.Challenges[challenge]; ok { - return challenge.Solve(ctx, marshaledInput) + if ichallenge, ok := f.GetChallenge(challenge); ok == nil { + return ichallenge.Solve(ctx, marshaledInput) } return nil, errors.New("Challenge not found") } +func (f Flow) GetChallenge(name string) (challengeEntity.IChallenge, error) { + var challenge challengeEntity.IChallenge + for _, c := range f.Challenges { + if c.GetName() == name { + challenge = c + + break + } + } + if challenge == nil { + return nil, errors.New("Challenge not found") + } + + return challenge, nil +} + func (f Flow) Request(ctx context.Context, challenge string, input string, JWTData mfaEntities.JWTData) (*map[string]interface{}, error) { var marshaledInput map[string]interface{} err := json.Unmarshal([]byte(input), &marshaledInput) if err != nil { return nil, err } - if challenge, ok := f.Challenges[challenge]; ok { - return challenge.Request(ctx, marshaledInput) + + if ichallenge, ok := f.GetChallenge(challenge); ok == nil { + return ichallenge.Request(ctx, marshaledInput) } return nil, errors.New("Challenge not found") @@ -77,8 +94,8 @@ func (f Flow) GetJWT(ctx context.Context) *string { func (f Flow) GetChallenges(challengesStatus *map[string]mfaEntities.Challenge, challenge *string, getAll bool) []string { var challenges []string - for k := range f.Challenges { - challenges = append(challenges, k) + for _, k := range f.Challenges { + challenges = append(challenges, k.GetName()) } return challenges diff --git a/flow/entities/flow_test.go b/flow/entities/flow_test.go index faab74d..5c18573 100644 --- a/flow/entities/flow_test.go +++ b/flow/entities/flow_test.go @@ -20,11 +20,12 @@ func TestFlow_GetChallenges(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() dummyChallenge := mocks.NewMockIChallenge(ctrl) + dummyChallenge.EXPECT().GetName().Return("dummy") flow := entities.Flow{ Name: "test", - Challenges: map[string]challenge.IChallenge{ - "dummy": dummyChallenge, + Challenges: []challenge.IChallenge{ + dummyChallenge, }, } challenges := flow.GetChallenges(nil, nil, true) @@ -37,11 +38,12 @@ func TestFlow_GetChallenges(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() dummyChallenge := mocks.NewMockIChallenge(ctrl) + dummyChallenge.EXPECT().GetName().Return("dummy") flow := entities.Flow{ Name: "test", - Challenges: map[string]challenge.IChallenge{ - "dummy": dummyChallenge, + Challenges: []challenge.IChallenge{ + dummyChallenge, }, } @@ -64,11 +66,12 @@ func TestFlow_GetChallenges(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() dummyChallenge := mocks.NewMockIChallenge(ctrl) + dummyChallenge.EXPECT().GetName().Return("dummy") flow := entities.Flow{ Name: "test", - Challenges: map[string]challenge.IChallenge{ - "dummy": dummyChallenge, + Challenges: []challenge.IChallenge{ + dummyChallenge, }, } @@ -94,8 +97,8 @@ func TestFlow_GetChallenges(t *testing.T) { flow := entities.Flow{ Name: "test", - Challenges: map[string]challenge.IChallenge{ - "dummy": dummyChallenge, + Challenges: []challenge.IChallenge{ + dummyChallenge, }, } @@ -116,11 +119,12 @@ func TestFlow_GetChallenges(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() dummyChallenge := mocks.NewMockIChallenge(ctrl) + dummyChallenge.EXPECT().GetName().Return("dummy") flow := entities.Flow{ Name: "test", - Challenges: map[string]challenge.IChallenge{ - "dummy": dummyChallenge, + Challenges: []challenge.IChallenge{ + dummyChallenge, }, } @@ -141,11 +145,12 @@ func TestFlow_GetChallenges(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() dummyChallenge := mocks.NewMockIChallenge(ctrl) + dummyChallenge.EXPECT().GetName().Return("dummy") flow := entities.Flow{ Name: "test", - Challenges: map[string]challenge.IChallenge{ - "dummy": dummyChallenge, + Challenges: []challenge.IChallenge{ + dummyChallenge, }, } @@ -169,11 +174,12 @@ func TestFlow_GetChallenges(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() dummyChallenge := mocks.NewMockIChallenge(ctrl) + dummyChallenge.EXPECT().GetName().Return("dummy") flow := entities.Flow{ Name: "test", - Challenges: map[string]challenge.IChallenge{ - "dummy": dummyChallenge, + Challenges: []challenge.IChallenge{ + dummyChallenge, }, } @@ -199,8 +205,8 @@ func TestFlow_GetChallenges(t *testing.T) { flow := entities.Flow{ Name: "test", - Challenges: map[string]challenge.IChallenge{ - "dummy": dummyChallenge, + Challenges: []challenge.IChallenge{ + dummyChallenge, }, } @@ -226,8 +232,8 @@ func TestFlow_GetChallenges(t *testing.T) { flow := entities.Flow{ Name: "test", - Challenges: map[string]challenge.IChallenge{ - "dummy": dummyChallenge, + Challenges: []challenge.IChallenge{ + dummyChallenge, }, } @@ -246,8 +252,8 @@ func TestFlow_GetChallenges(t *testing.T) { flow := entities.Flow{ Name: "test", - Challenges: map[string]challenge.IChallenge{ - "dummy": dummyChallenge, + Challenges: []challenge.IChallenge{ + dummyChallenge, }, } @@ -265,8 +271,8 @@ func TestFlow_GetChallenges(t *testing.T) { flow := entities.Flow{ Name: "test", - Challenges: map[string]challenge.IChallenge{ - "dummy": dummyChallenge, + Challenges: []challenge.IChallenge{ + dummyChallenge, }, } @@ -285,8 +291,8 @@ func TestFlow_GetChallenges(t *testing.T) { flow := entities.Flow{ Name: "test", - Challenges: map[string]challenge.IChallenge{ - "dummy": dummyChallenge, + Challenges: []challenge.IChallenge{ + dummyChallenge, }, } @@ -304,8 +310,8 @@ func TestFlow_GetChallenges(t *testing.T) { flow := entities.Flow{ Name: "test", - Challenges: map[string]challenge.IChallenge{ - "dummy": dummyChallenge, + Challenges: []challenge.IChallenge{ + dummyChallenge, }, } diff --git a/mocks/mock_challenge.go b/mocks/mock_challenge.go index e55c8a3..28608a7 100644 --- a/mocks/mock_challenge.go +++ b/mocks/mock_challenge.go @@ -34,6 +34,20 @@ func (m *MockIChallenge) EXPECT() *MockIChallengeMockRecorder { return m.recorder } +// GetName mocks base method. +func (m *MockIChallenge) GetName() string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetName") + ret0, _ := ret[0].(string) + return ret0 +} + +// GetName indicates an expected call of GetName. +func (mr *MockIChallengeMockRecorder) GetName() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetName", reflect.TypeOf((*MockIChallenge)(nil).GetName)) +} + // Request mocks base method. func (m *MockIChallenge) Request(arg0 context.Context, arg1 map[string]interface{}) (*map[string]interface{}, error) { m.ctrl.T.Helper()