Skip to content

Commit

Permalink
fix: change challenge order, maps are not ordered (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
bludot authored Apr 29, 2022
1 parent dcfbe86 commit c71c8fe
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 40 deletions.
4 changes: 4 additions & 0 deletions challenge/entities/challenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ package entities
type Challenge struct {
Name string `json:"name"`
}

func (c Challenge) GetName() string {
return c.Name
}
1 change: 1 addition & 0 deletions challenge/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions examples/single_flow_multiple_challenges/flows/single_flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions examples/single_flow_single_challenge/flows/single_flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
33 changes: 25 additions & 8 deletions flow/entities/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand Down
58 changes: 32 additions & 26 deletions flow/entities/flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
},
}

Expand All @@ -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,
},
}

Expand All @@ -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,
},
}

Expand All @@ -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,
},
}

Expand All @@ -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,
},
}

Expand All @@ -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,
},
}

Expand All @@ -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,
},
}

Expand All @@ -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,
},
}

Expand All @@ -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,
},
}

Expand All @@ -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,
},
}

Expand All @@ -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,
},
}

Expand All @@ -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,
},
}

Expand Down
14 changes: 14 additions & 0 deletions mocks/mock_challenge.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c71c8fe

Please sign in to comment.