Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examplebroker/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,10 @@ func qrcodeData(sessionInfo *sessionInfo) (content string, code string) {
"https://www.ubuntu-it.org/",
}

if strings.HasPrefix(sessionInfo.username, UserIntegrationQRcodeWithoutCodePrefix) {
return qrcodeURIs[0], ""
}

if strings.HasPrefix(sessionInfo.username, UserIntegrationQRcodeStaticPrefix) {
return qrcodeURIs[0], fmt.Sprint(baseCode)
}
Expand Down
2 changes: 2 additions & 0 deletions examplebroker/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ const (
UserIntegrationCanResetPrefix = "user-can-reset-integration-"
// UserIntegrationLocalGroupsPrefix is the prefix for a local-groups user for integration tests.
UserIntegrationLocalGroupsPrefix = "user-local-groups-integration-"
// UserIntegrationQRcodeWithoutCodePrefix is the prefix for a qrcode user returning an URI without a code for integration tests.
UserIntegrationQRcodeWithoutCodePrefix = "user-integration-qrcode-without-code-"
// UserIntegrationQRcodeStaticPrefix is the prefix for a static qrcode user for integration tests.
UserIntegrationQRcodeStaticPrefix = "user-integration-qrcode-static-"
// UserIntegrationPreCheckValue is the value for a pre-check user for integration tests.
Expand Down
11 changes: 11 additions & 0 deletions pam/integration-tests/native_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,17 @@ func TestNativeAuthenticate(t *testing.T) {
"AUTHD_QRCODE_TAPE_ITEM_NAME": "QR code",
},
},
"Authenticate_user_with_qr_code_without_code": {
tape: "qr_code",
tapeSettings: []tapeSetting{{vhsHeight, 3000}},
tapeVariables: map[string]string{
"AUTHD_QRCODE_TAPE_ITEM": "7",
"AUTHD_QRCODE_TAPE_ITEM_NAME": "QR code",
},
clientOptions: clientOptions{
PamUser: examplebroker.UserIntegrationQRcodeWithoutCodePrefix + "native@example.com",
},
},
"Authenticate_user_with_qr_code_in_a_TTY": {
tape: "qr_code",
tapeSettings: []tapeSetting{{vhsHeight, 4000}},
Expand Down

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions pam/internal/adapter/nativemodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,11 +729,11 @@ func (m nativeModel) handleQrCode() tea.Cmd {
qrcodeView = append(qrcodeView, qrcode)
}

qrcodeView = append(qrcodeView, fmt.Sprintf("URL: %s", m.uiLayout.GetContent()))

labeledFields := []labeledField{{"URL", m.uiLayout.GetContent()}}
if code := m.uiLayout.GetCode(); code != "" {
qrcodeView = append(qrcodeView, fmt.Sprintf("Code: %s", code))
labeledFields = append(labeledFields, labeledField{"Code", code})
}
qrcodeView = append(qrcodeView, formatAlignedFields(labeledFields)...)

// Add some extra vertical space to improve readability
qrcodeView = append(qrcodeView, " ")
Expand Down
6 changes: 3 additions & 3 deletions pam/internal/adapter/qrcodemodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ func (m qrcodeModel) View() string {
// Add some extra vertical space to improve readability
fields = append(fields, "")

fields = append(fields, fmt.Sprintf("URL: %s", m.content))

labeledFields := []labeledField{{"URL", m.content}}
if m.code != "" {
fields = append(fields, fmt.Sprintf("Code: %s", m.code))
labeledFields = append(labeledFields, labeledField{"Code", m.code})
}
fields = append(fields, formatAlignedFields(labeledFields)...)

if m.buttonModel != nil {
fields = append(fields, style.Render(m.buttonModel.View()))
Expand Down
24 changes: 24 additions & 0 deletions pam/internal/adapter/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"strings"
"sync"
"unicode/utf8"

"github.com/canonical/authd/internal/proto/authd"
"github.com/canonical/authd/log"
Expand Down Expand Up @@ -252,3 +253,26 @@ func goBackLabel(previousStage proto.Stage) string {
return ""
}
}

// labeledField is a label-value pair used by [formatAlignedFields].
type labeledField struct{ label, value string }

// formatAlignedFields pads labels so that all values start at the same column.
//
// NOTE: This is not RTL-friendly and should be adjusted when adding RTL
// language support.
func formatAlignedFields(fields []labeledField) []string {
maxLen := 0
for _, f := range fields {
if n := utf8.RuneCountInString(f.label); n > maxLen {
maxLen = n
}
}

out := make([]string, 0, len(fields))
for _, f := range fields {
padding := strings.Repeat(" ", maxLen-utf8.RuneCountInString(f.label)+1)
out = append(out, f.label+":"+padding+f.value)
}
Comment thread
nooreldeenmansour marked this conversation as resolved.
return out
}
39 changes: 39 additions & 0 deletions pam/internal/adapter/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,42 @@ func TestSafeMessageDebug(t *testing.T) {
})
}
}

func TestFormatAlignedFields(t *testing.T) {
t.Parallel()

tests := map[string]struct {
fields []labeledField
want []string
}{
"Single field has no extra padding": {
fields: []labeledField{{"URL", "https://example.com"}},
want: []string{"URL: https://example.com"},
},
"Two fields are padded to equal width": {
fields: []labeledField{{"URL", "https://example.com"}, {"Code", "1337"}},
want: []string{"URL: https://example.com", "Code: 1337"},
},
"Longer first label pads the second": {
fields: []labeledField{{"Verification URL", "https://example.com"}, {"Code", "1337"}},
want: []string{"Verification URL: https://example.com", "Code: 1337"},
},
"Equal length labels": {
fields: []labeledField{{"Name", "Alice"}, {"Role", "Admin"}},
want: []string{"Name: Alice", "Role: Admin"},
},
"Multibyte label is measured by rune count": {
// "URLé" is 5 bytes but 4 runes — padding must use rune count.
fields: []labeledField{{"URLé", "https://example.com"}, {"Code", "1337"}},
want: []string{"URLé: https://example.com", "Code: 1337"},
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()
got := formatAlignedFields(tc.fields)
require.Equal(t, tc.want, got)
})
}
}
Loading