Skip to content

Commit

Permalink
Addressing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
denisonbarbosa committed Aug 28, 2023
1 parent edb9493 commit c4d3e68
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
17 changes: 12 additions & 5 deletions internal/brokers/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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, ",")
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions internal/brokers/broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit c4d3e68

Please sign in to comment.