Skip to content

Commit f84e951

Browse files
authored
Merge pull request #613 from rusq/i511-2
adjust max QR code size
2 parents ac411ac + ff6f33d commit f84e951

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

auth/auth_ui/huh.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,21 @@ import (
2626

2727
"github.com/charmbracelet/bubbles/key"
2828
"github.com/charmbracelet/huh"
29+
"github.com/rusq/osenv/v2"
2930
"github.com/rusq/slackauth"
3031

3132
"github.com/rusq/slackdump/v4/internal/structures"
3233
)
3334

35+
const (
36+
defQRCodeSz = 9000 // default limit for encoded image size, seen values 6174, 8462
37+
maxQRCodeSz = 1<<16 - 1 // maximum allowed QR code image size.
38+
imgPrefix = "data:image/png;base64,"
39+
)
40+
41+
// limQRCodeSz is the size of the input field for QR Code image.
42+
var limQRCodeSz = osenv.Value("QR_CODE_SIZE", defQRCodeSz)
43+
3444
// Huh is the Auth UI that uses the huh library to provide a terminal UI.
3545
type Huh struct{}
3646

@@ -250,21 +260,17 @@ func valSixDigits(s string) error {
250260
return nil
251261
}
252262

253-
const (
254-
maxEncImgSz = 7000
255-
imgPrefix = "data:image/png;base64,"
256-
)
257-
258263
func (*Huh) RequestQR(ctx context.Context, _ io.Writer) (string, error) {
259264
const description = `In logged in Slack Client or Web:
260265
1. click the username in the upper left corner;
261266
2. choose 'Sign in on mobile';
262267
3. right-click the QR code image;
263268
4. choose Copy Image.`
269+
264270
var imageData string
265271
q := huh.NewForm(huh.NewGroup(
266272
huh.NewText().
267-
CharLimit(maxEncImgSz).
273+
CharLimit(qrCharLimit()).
268274
Value(&imageData).
269275
Validate(func(s string) error {
270276
if !strings.HasPrefix(s, imgPrefix) {
@@ -281,3 +287,12 @@ func (*Huh) RequestQR(ctx context.Context, _ io.Writer) (string, error) {
281287
}
282288
return imageData, nil
283289
}
290+
291+
// qrCharLimit returns the effective character limit for the QR code input
292+
// field.
293+
func qrCharLimit() int {
294+
if defQRCodeSz <= limQRCodeSz && limQRCodeSz <= maxQRCodeSz {
295+
return limQRCodeSz
296+
}
297+
return defQRCodeSz
298+
}

auth/auth_ui/huh_test.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515

1616
package auth_ui
1717

18-
import "testing"
18+
import (
19+
"testing"
20+
)
1921

2022
func Test_valSixDigits(t *testing.T) {
2123
type args struct {
@@ -60,3 +62,27 @@ func Test_valSixDigits(t *testing.T) {
6062
})
6163
}
6264
}
65+
66+
func Test_qrCharLimit(t *testing.T) {
67+
tests := []struct {
68+
name string
69+
limQRsz int
70+
want int
71+
}{
72+
{"zero", 0, defQRCodeSz},
73+
{"default", defQRCodeSz, defQRCodeSz},
74+
{"between allowed values", (maxQRCodeSz - defQRCodeSz) / 2, (maxQRCodeSz - defQRCodeSz) / 2},
75+
{"over maximum", maxQRCodeSz + 1, defQRCodeSz},
76+
}
77+
for _, tt := range tests {
78+
t.Run(tt.name, func(t *testing.T) {
79+
var old = limQRCodeSz
80+
limQRCodeSz = tt.limQRsz
81+
t.Cleanup(func() { limQRCodeSz = old })
82+
83+
if got := qrCharLimit(); got != tt.want {
84+
t.Errorf("qrCharLimit() = %v, want %v", got, tt.want)
85+
}
86+
})
87+
}
88+
}

slackdump.1

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,13 @@ for more details.
394394
.It Ev NOCOLOR
395395
.BOOL
396396
disables colorful log messages.
397+
.It Ev QR_CODE_SIZE
398+
Sets the maximum length, in characters, of the pasted QR Code image data
399+
(the
400+
.Ql data:image/png;base64,...
401+
string) used for "Sign in on Mobile". The default limit is 9000 characters;
402+
values outside the supported range (up to a maximum of 65535 characters) are
403+
ignored and the default is used.
397404
.It Ev SLACK_COOKIE
398405
Contains Slack cookie (for token+cookie-based authentication). See
399406
.Sx Authentication

0 commit comments

Comments
 (0)