Skip to content

Commit 9db5be8

Browse files
committed
show QR code for device auth
1 parent ea99a5e commit 9db5be8

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

main.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ var configByHost = map[string]oauth2.Config{
105105
var (
106106
verbose bool
107107
// populated by GoReleaser https://goreleaser.com/cookbooks/using-main.version
108-
version = "dev"
108+
version = "dev"
109+
qrencodeType string
109110
)
110111

111112
func getVersion() string {
@@ -139,6 +140,7 @@ func main() {
139140
flag.BoolVar(&verbose, "verbose", false, "log debug information to stderr")
140141
var device bool
141142
flag.BoolVar(&device, "device", false, "instead of opening a web browser locally, print a code to enter on another device")
143+
flag.StringVar(&qrencodeType, "qr", "", "qrencode type")
142144
flag.Usage = func() {
143145
printVersion()
144146
fmt.Fprintln(os.Stderr, "usage: git credential-oauth [<options>] <action>")
@@ -437,6 +439,19 @@ func getDeviceToken(ctx context.Context, c oauth2.Config) (*oauth2.Token, error)
437439
if verbose {
438440
fmt.Fprintf(os.Stderr, "%+v\n", deviceAuth)
439441
}
442+
if deviceAuth.VerificationURIComplete != "" && qrencodeType != "" {
443+
qrencodePath, err := exec.LookPath("qrencode")
444+
if err == nil {
445+
cmd := exec.Command(qrencodePath, "-t", qrencodeType, deviceAuth.VerificationURIComplete)
446+
bytes, err := cmd.CombinedOutput()
447+
if err != nil {
448+
log.Fatalln(err)
449+
}
450+
if err == nil {
451+
fmt.Fprintf(os.Stderr, "%s\n", bytes)
452+
}
453+
}
454+
}
440455
fmt.Fprintf(os.Stderr, "Please enter code %s at %s\n", deviceAuth.UserCode, deviceAuth.VerificationURI)
441456
return c.DeviceAccessToken(ctx, deviceAuth)
442457
}

0 commit comments

Comments
 (0)