From c87c3010fa470968c5b4c72c7eb4b1063abfe458 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Wed, 26 Jun 2024 15:38:30 +0200 Subject: [PATCH 1/2] pam/qrcodemodel: Do not add an empty line if code is not available --- pam/internal/adapter/qrcodemodel.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pam/internal/adapter/qrcodemodel.go b/pam/internal/adapter/qrcodemodel.go index cd38e1ad4..620528786 100644 --- a/pam/internal/adapter/qrcodemodel.go +++ b/pam/internal/adapter/qrcodemodel.go @@ -111,7 +111,9 @@ func (m qrcodeModel) View() string { qrcodeWidth := lipgloss.Width(qr) style := centeredStyle.Width(qrcodeWidth) - fields = append(fields, style.Render(m.code)) + if m.code != "" { + fields = append(fields, style.Render(m.code)) + } if m.buttonModel != nil { fields = append(fields, style.Render(m.buttonModel.View())) From 94abab80f03caa2510b3dce483352f80deef3f39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Tue, 18 Jun 2024 02:15:04 +0200 Subject: [PATCH 2/2] qrcodemodel: Always include the content URI to the output In some cases (e.g. small terminals, tty's) the qrcode is not properly rendered or is not fully visible, so make the URI to be always written so that it can be used also without a qrcode scanner --- .../golden/authenticate_user_with_qr_code | 10 +++++----- .../golden/authenticate_user_with_qr_code_in_a_tty | 10 +++++----- .../authenticate_user_with_qr_code_in_a_tty_session | 10 +++++----- .../golden/authenticate_user_with_qr_code_in_screen | 10 +++++----- pam/internal/adapter/qrcodemodel.go | 6 ++++++ 5 files changed, 26 insertions(+), 20 deletions(-) diff --git a/pam/integration-tests/testdata/TestCLIAuthenticate/golden/authenticate_user_with_qr_code b/pam/integration-tests/testdata/TestCLIAuthenticate/golden/authenticate_user_with_qr_code index d82e68e06..b7f8c05d1 100644 --- a/pam/integration-tests/testdata/TestCLIAuthenticate/golden/authenticate_user_with_qr_code +++ b/pam/integration-tests/testdata/TestCLIAuthenticate/golden/authenticate_user_with_qr_code @@ -147,6 +147,7 @@ Scan the qrcode or enter the code in the login page ████▄▄▄▄▄▄▄█▄█▄█▄█▄████▄▄▄▄▄▄████ █████████████████████████████████ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ + https://ubuntu.com 1337 [ Regenerate code ] @@ -167,7 +168,6 @@ Scan the qrcode or enter the code in the login page - ──────────────────────────────────────────────────────────────────────────────── > if [ -v AUTHD_PAM_CLI_TERM ]; then export TERM=${AUTHD_PAM_CLI_TERM}; fi > ./pam_authd login socket=${AUTHD_TESTS_CLI_AUTHENTICATE_TESTS_SOCK} @@ -190,6 +190,7 @@ Scan the qrcode or enter the code in the login page ████▄▄▄▄▄▄▄█▄█▄█▄█▄████▄▄▄▄▄▄████ █████████████████████████████████ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ + https://ubuntu.com 1337 [ Regenerate code ] @@ -210,7 +211,6 @@ Scan the qrcode or enter the code in the login page - ──────────────────────────────────────────────────────────────────────────────── > if [ -v AUTHD_PAM_CLI_TERM ]; then export TERM=${AUTHD_PAM_CLI_TERM}; fi > ./pam_authd login socket=${AUTHD_TESTS_CLI_AUTHENTICATE_TESTS_SOCK} @@ -233,6 +233,7 @@ Scan the qrcode or enter the code in the login page ████▄▄▄▄▄▄▄█▄█▄█▄█▄████▄▄▄▄▄▄████ █████████████████████████████████ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ + https://ubuntu.com 1337 [ Regenerate code ] @@ -253,7 +254,6 @@ Scan the qrcode or enter the code in the login page - ──────────────────────────────────────────────────────────────────────────────── > if [ -v AUTHD_PAM_CLI_TERM ]; then export TERM=${AUTHD_PAM_CLI_TERM}; fi > ./pam_authd login socket=${AUTHD_TESTS_CLI_AUTHENTICATE_TESTS_SOCK} @@ -276,6 +276,7 @@ Scan the qrcode or enter the code in the login page ████▄▄▄▄▄▄▄█▄█▄█▄█▄████▄▄▄▄▄▄████ █████████████████████████████████ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ + https://ubuntu.com 1337 PAM Authenticate() for user "user-integration-qr-code" exited with success @@ -296,7 +297,6 @@ PAM AcctMgmt() exited with success - ──────────────────────────────────────────────────────────────────────────────── > if [ -v AUTHD_PAM_CLI_TERM ]; then export TERM=${AUTHD_PAM_CLI_TERM}; fi > ./pam_authd login socket=${AUTHD_TESTS_CLI_AUTHENTICATE_TESTS_SOCK} @@ -319,6 +319,7 @@ Scan the qrcode or enter the code in the login page ████▄▄▄▄▄▄▄█▄█▄█▄█▄████▄▄▄▄▄▄████ █████████████████████████████████ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ + https://ubuntu.com 1337 PAM Authenticate() for user "user-integration-qr-code" exited with success @@ -339,5 +340,4 @@ PAM AcctMgmt() exited with success - ──────────────────────────────────────────────────────────────────────────────── diff --git a/pam/integration-tests/testdata/TestCLIAuthenticate/golden/authenticate_user_with_qr_code_in_a_tty b/pam/integration-tests/testdata/TestCLIAuthenticate/golden/authenticate_user_with_qr_code_in_a_tty index c8f9d72bb..97ac922cd 100644 --- a/pam/integration-tests/testdata/TestCLIAuthenticate/golden/authenticate_user_with_qr_code_in_a_tty +++ b/pam/integration-tests/testdata/TestCLIAuthenticate/golden/authenticate_user_with_qr_code_in_a_tty @@ -163,11 +163,11 @@ Scan the qrcode or enter the code in the login page ██████████████████████████████████████████████████████████████████ ██████████████████████████████████████████████████████████████████ ██████████████████████████████████████████████████████████████████ + https://ubuntu.com 1337 [ Regenerate code ] - ──────────────────────────────────────────────────────────────────────────────── > if [ -v AUTHD_PAM_CLI_TERM ]; then export TERM=${AUTHD_PAM_CLI_TERM}; fi > ./pam_authd login socket=${AUTHD_TESTS_CLI_AUTHENTICATE_TESTS_SOCK} @@ -206,11 +206,11 @@ Scan the qrcode or enter the code in the login page ██████████████████████████████████████████████████████████████████ ██████████████████████████████████████████████████████████████████ ██████████████████████████████████████████████████████████████████ + https://ubuntu.com 1337 [ Regenerate code ] - ──────────────────────────────────────────────────────────────────────────────── > if [ -v AUTHD_PAM_CLI_TERM ]; then export TERM=${AUTHD_PAM_CLI_TERM}; fi > ./pam_authd login socket=${AUTHD_TESTS_CLI_AUTHENTICATE_TESTS_SOCK} @@ -249,11 +249,11 @@ Scan the qrcode or enter the code in the login page ██████████████████████████████████████████████████████████████████ ██████████████████████████████████████████████████████████████████ ██████████████████████████████████████████████████████████████████ + https://ubuntu.com 1337 [ Regenerate code ] - ──────────────────────────────────────────────────────────────────────────────── > if [ -v AUTHD_PAM_CLI_TERM ]; then export TERM=${AUTHD_PAM_CLI_TERM}; fi > ./pam_authd login socket=${AUTHD_TESTS_CLI_AUTHENTICATE_TESTS_SOCK} @@ -292,11 +292,11 @@ Scan the qrcode or enter the code in the login page ██████████████████████████████████████████████████████████████████ ██████████████████████████████████████████████████████████████████ ██████████████████████████████████████████████████████████████████ + https://ubuntu.com 1337 PAM Authenticate() for user "user-integration-qr-code-tty" exited with success PAM AcctMgmt() exited with success -> ──────────────────────────────────────────────────────────────────────────────── > if [ -v AUTHD_PAM_CLI_TERM ]; then export TERM=${AUTHD_PAM_CLI_TERM}; fi > ./pam_authd login socket=${AUTHD_TESTS_CLI_AUTHENTICATE_TESTS_SOCK} @@ -335,9 +335,9 @@ Scan the qrcode or enter the code in the login page ██████████████████████████████████████████████████████████████████ ██████████████████████████████████████████████████████████████████ ██████████████████████████████████████████████████████████████████ + https://ubuntu.com 1337 PAM Authenticate() for user "user-integration-qr-code-tty" exited with success PAM AcctMgmt() exited with success -> ──────────────────────────────────────────────────────────────────────────────── diff --git a/pam/integration-tests/testdata/TestCLIAuthenticate/golden/authenticate_user_with_qr_code_in_a_tty_session b/pam/integration-tests/testdata/TestCLIAuthenticate/golden/authenticate_user_with_qr_code_in_a_tty_session index 14d16e725..b6364dd19 100644 --- a/pam/integration-tests/testdata/TestCLIAuthenticate/golden/authenticate_user_with_qr_code_in_a_tty_session +++ b/pam/integration-tests/testdata/TestCLIAuthenticate/golden/authenticate_user_with_qr_code_in_a_tty_session @@ -163,11 +163,11 @@ Scan the qrcode or enter the code in the login page ██████████████████████████████████████████████████████████████████ ██████████████████████████████████████████████████████████████████ ██████████████████████████████████████████████████████████████████ + https://ubuntu.com 1337 [ Regenerate code ] - ──────────────────────────────────────────────────────────────────────────────── > if [ -v AUTHD_PAM_CLI_TERM ]; then export TERM=${AUTHD_PAM_CLI_TERM}; fi > ./pam_authd login socket=${AUTHD_TESTS_CLI_AUTHENTICATE_TESTS_SOCK} @@ -206,11 +206,11 @@ Scan the qrcode or enter the code in the login page ██████████████████████████████████████████████████████████████████ ██████████████████████████████████████████████████████████████████ ██████████████████████████████████████████████████████████████████ + https://ubuntu.com 1337 [ Regenerate code ] - ──────────────────────────────────────────────────────────────────────────────── > if [ -v AUTHD_PAM_CLI_TERM ]; then export TERM=${AUTHD_PAM_CLI_TERM}; fi > ./pam_authd login socket=${AUTHD_TESTS_CLI_AUTHENTICATE_TESTS_SOCK} @@ -249,11 +249,11 @@ Scan the qrcode or enter the code in the login page ██████████████████████████████████████████████████████████████████ ██████████████████████████████████████████████████████████████████ ██████████████████████████████████████████████████████████████████ + https://ubuntu.com 1337 [ Regenerate code ] - ──────────────────────────────────────────────────────────────────────────────── > if [ -v AUTHD_PAM_CLI_TERM ]; then export TERM=${AUTHD_PAM_CLI_TERM}; fi > ./pam_authd login socket=${AUTHD_TESTS_CLI_AUTHENTICATE_TESTS_SOCK} @@ -292,11 +292,11 @@ Scan the qrcode or enter the code in the login page ██████████████████████████████████████████████████████████████████ ██████████████████████████████████████████████████████████████████ ██████████████████████████████████████████████████████████████████ + https://ubuntu.com 1337 PAM Authenticate() for user "user-integration-qr-code-tty-session" exited with success PAM AcctMgmt() exited with success -> ──────────────────────────────────────────────────────────────────────────────── > if [ -v AUTHD_PAM_CLI_TERM ]; then export TERM=${AUTHD_PAM_CLI_TERM}; fi > ./pam_authd login socket=${AUTHD_TESTS_CLI_AUTHENTICATE_TESTS_SOCK} @@ -335,9 +335,9 @@ Scan the qrcode or enter the code in the login page ██████████████████████████████████████████████████████████████████ ██████████████████████████████████████████████████████████████████ ██████████████████████████████████████████████████████████████████ + https://ubuntu.com 1337 PAM Authenticate() for user "user-integration-qr-code-tty-session" exited with success PAM AcctMgmt() exited with success -> ──────────────────────────────────────────────────────────────────────────────── diff --git a/pam/integration-tests/testdata/TestCLIAuthenticate/golden/authenticate_user_with_qr_code_in_screen b/pam/integration-tests/testdata/TestCLIAuthenticate/golden/authenticate_user_with_qr_code_in_screen index b4e5df5b3..13047ee12 100644 --- a/pam/integration-tests/testdata/TestCLIAuthenticate/golden/authenticate_user_with_qr_code_in_screen +++ b/pam/integration-tests/testdata/TestCLIAuthenticate/golden/authenticate_user_with_qr_code_in_screen @@ -163,11 +163,11 @@ Scan the qrcode or enter the code in the login page ██████████████████████████████████████████████████████████████████ ██████████████████████████████████████████████████████████████████ ██████████████████████████████████████████████████████████████████ + https://ubuntu.com 1337 [ Regenerate code ] - ──────────────────────────────────────────────────────────────────────────────── > if [ -v AUTHD_PAM_CLI_TERM ]; then export TERM=${AUTHD_PAM_CLI_TERM}; fi > ./pam_authd login socket=${AUTHD_TESTS_CLI_AUTHENTICATE_TESTS_SOCK} @@ -206,11 +206,11 @@ Scan the qrcode or enter the code in the login page ██████████████████████████████████████████████████████████████████ ██████████████████████████████████████████████████████████████████ ██████████████████████████████████████████████████████████████████ + https://ubuntu.com 1337 [ Regenerate code ] - ──────────────────────────────────────────────────────────────────────────────── > if [ -v AUTHD_PAM_CLI_TERM ]; then export TERM=${AUTHD_PAM_CLI_TERM}; fi > ./pam_authd login socket=${AUTHD_TESTS_CLI_AUTHENTICATE_TESTS_SOCK} @@ -249,11 +249,11 @@ Scan the qrcode or enter the code in the login page ██████████████████████████████████████████████████████████████████ ██████████████████████████████████████████████████████████████████ ██████████████████████████████████████████████████████████████████ + https://ubuntu.com 1337 [ Regenerate code ] - ──────────────────────────────────────────────────────────────────────────────── > if [ -v AUTHD_PAM_CLI_TERM ]; then export TERM=${AUTHD_PAM_CLI_TERM}; fi > ./pam_authd login socket=${AUTHD_TESTS_CLI_AUTHENTICATE_TESTS_SOCK} @@ -292,11 +292,11 @@ Scan the qrcode or enter the code in the login page ██████████████████████████████████████████████████████████████████ ██████████████████████████████████████████████████████████████████ ██████████████████████████████████████████████████████████████████ + https://ubuntu.com 1337 PAM Authenticate() for user "user-integration-qr-code-screen" exited with success PAM AcctMgmt() exited with success -> ──────────────────────────────────────────────────────────────────────────────── > if [ -v AUTHD_PAM_CLI_TERM ]; then export TERM=${AUTHD_PAM_CLI_TERM}; fi > ./pam_authd login socket=${AUTHD_TESTS_CLI_AUTHENTICATE_TESTS_SOCK} @@ -335,9 +335,9 @@ Scan the qrcode or enter the code in the login page ██████████████████████████████████████████████████████████████████ ██████████████████████████████████████████████████████████████████ ██████████████████████████████████████████████████████████████████ + https://ubuntu.com 1337 PAM Authenticate() for user "user-integration-qr-code-screen" exited with success PAM AcctMgmt() exited with success -> ──────────────────────────────────────────────────────────────────────────────── diff --git a/pam/internal/adapter/qrcodemodel.go b/pam/internal/adapter/qrcodemodel.go index 620528786..3162d2496 100644 --- a/pam/internal/adapter/qrcodemodel.go +++ b/pam/internal/adapter/qrcodemodel.go @@ -111,6 +111,12 @@ func (m qrcodeModel) View() string { qrcodeWidth := lipgloss.Width(qr) style := centeredStyle.Width(qrcodeWidth) + renderedContent := m.content + if lipgloss.Width(m.content) < qrcodeWidth { + renderedContent = style.Render(m.content) + } + fields = append(fields, renderedContent) + if m.code != "" { fields = append(fields, style.Render(m.code)) }