From c4d3e6864475abbeb380874a4e1cd4f9ef6402fe Mon Sep 17 00:00:00 2001 From: denisonbarbosa Date: Mon, 28 Aug 2023 12:23:25 -0400 Subject: [PATCH] Addressing comments --- internal/brokers/broker.go | 17 ++++++++++++----- internal/brokers/broker_test.go | 2 ++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/internal/brokers/broker.go b/internal/brokers/broker.go index 454b4847e..29e45874f 100644 --- a/internal/brokers/broker.go +++ b/internal/brokers/broker.go @@ -96,7 +96,10 @@ func (b Broker) newSession(ctx context.Context, username, lang string) (sessionI func (b Broker) GetAuthenticationModes(ctx context.Context, sessionID string, supportedUILayouts []map[string]string) (authenticationModes []map[string]string, err error) { sessionID = b.parseSessionID(sessionID) - b.generateValidators(supportedUILayouts) + b.layoutValidators, err = generateValidators(supportedUILayouts) + if err != nil { + return nil, fmt.Errorf("could not generate layout validators: %w", err) + } authenticationModes, err = b.brokerer.GetAuthenticationModes(ctx, sessionID, supportedUILayouts) if err != nil { @@ -188,19 +191,23 @@ func (b Broker) cancelIsAuthorized(ctx context.Context, sessionID string) { // } // } // } -func (b *Broker) generateValidators(supportedUILayouts []map[string]string) { +func generateValidators(supportedUILayouts []map[string]string) (map[string]map[string]fieldValidator, error) { validators := make(map[string]map[string]fieldValidator) for _, layout := range supportedUILayouts { + if _, exists := layout["type"]; !exists { + return nil, fmt.Errorf("Supported UI layouts are invalid") + } + layoutValidator := make(map[string]fieldValidator) for key, value := range layout { if key == "type" { continue } - modifier, supportedValues, _ := strings.Cut(value, ":") + required, supportedValues, _ := strings.Cut(value, ":") validator := fieldValidator{ supportedValues: nil, - required: (modifier == "required"), + required: (required == "required"), } if supportedValues != "" { validator.supportedValues = strings.Split(supportedValues, ",") @@ -209,7 +216,7 @@ func (b *Broker) generateValidators(supportedUILayouts []map[string]string) { } validators[layout["type"]] = layoutValidator } - b.layoutValidators = validators + return validators, nil } // validateUILayout validates the layout fields and content according to the broker validators and returns the layout diff --git a/internal/brokers/broker_test.go b/internal/brokers/broker_test.go index e0c75410e..37e38e342 100644 --- a/internal/brokers/broker_test.go +++ b/internal/brokers/broker_test.go @@ -165,6 +165,8 @@ func TestSelectAuthenticationMode(t *testing.T) { t.Parallel() b, _ := newBrokerForTests(t) + + // This is normally done in the broker's GetAuthenticationModes method, but we need to do it here to test the SelectAuthenticationMode method. b.GenerateLayoutValidators(supportedLayouts) gotUI, err := b.SelectAuthenticationMode(context.Background(), tc.sessionID, "mode1")