Skip to content

Commit d0a06a0

Browse files
committed
add qr code to oidc flow; add application name
1 parent 19cf1ef commit d0a06a0

File tree

6 files changed

+58
-8
lines changed

6 files changed

+58
-8
lines changed

go.mod

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ require (
66
github.com/Songmu/prompter v0.5.1
77
github.com/evanphx/json-patch/v5 v5.6.0
88
github.com/gliderlabs/ssh v0.3.5
9+
github.com/mdp/qrterminal/v3 v3.0.0
910
github.com/oidc-mytoken/api v0.8.0
10-
github.com/oidc-mytoken/lib v0.5.0
11+
github.com/oidc-mytoken/lib v0.6.0
1112
github.com/oidc-mytoken/server v0.6.0
1213
github.com/olekukonko/tablewriter v0.0.5
1314
github.com/pkg/errors v0.9.1

go.sum

+9-1
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
316316
github.com/lyft/protoc-gen-star v0.5.3/go.mod h1:V0xaHgaf5oCCqmcxYcWiDfTiKsZsRc87/1qhoTACD8w=
317317
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
318318
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
319+
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
319320
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
320321
github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
321322
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
@@ -332,6 +333,10 @@ github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m
332333
github.com/mattn/go-slim v0.0.0-20200618151855-bde33eecb5ee/go.mod h1:ma9TUJeni8LGZMJvOwbAv/FOwiwqIMQN570LnpqCBSM=
333334
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
334335
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
336+
github.com/mdp/qrterminal v1.0.1 h1:07+fzVDlPuBlXS8tB0ktTAyf+Lp1j2+2zK3fBOL5b7c=
337+
github.com/mdp/qrterminal v1.0.1/go.mod h1:Z33WhxQe9B6CdW37HaVqcRKzP+kByF3q/qLxOGe12xQ=
338+
github.com/mdp/qrterminal/v3 v3.0.0 h1:ywQqLRBXWTktytQNDKFjhAvoGkLVN3J2tAFZ0kMd9xQ=
339+
github.com/mdp/qrterminal/v3 v3.0.0/go.mod h1:NJpfAs7OAm77Dy8EkWrtE4aq+cE6McoLXlBqXQEwvE0=
335340
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
336341
github.com/miekg/dns v1.1.26/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso=
337342
github.com/miekg/dns v1.1.41/go.mod h1:p6aan82bvRIyn+zDIv9xYNUpwa73JcSh9BKwknJysuI=
@@ -351,8 +356,9 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWb
351356
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
352357
github.com/oidc-mytoken/api v0.8.0 h1:V/8LyLcVtYX1xxj+r6KyShDYUhl1giHLPVB6dTZyQtk=
353358
github.com/oidc-mytoken/api v0.8.0/go.mod h1:DBIlUbaIgGlf607VZx8zFC97VR3WNN0kaMVO1AqyTdE=
354-
github.com/oidc-mytoken/lib v0.5.0 h1:/DHEjHCSVDFE5DXyXUp3dO7NWZfSFN2nYVaigYdmv5w=
355359
github.com/oidc-mytoken/lib v0.5.0/go.mod h1:r6shfKy8/15vqGJvciCPphRkKffvIegSqj0uA6kzuns=
360+
github.com/oidc-mytoken/lib v0.6.0 h1:hsydbF2QPrL40kWny2mWciW055TpuPPkcTMMqzHl/6s=
361+
github.com/oidc-mytoken/lib v0.6.0/go.mod h1:r6shfKy8/15vqGJvciCPphRkKffvIegSqj0uA6kzuns=
356362
github.com/oidc-mytoken/server v0.6.0 h1:c3OEJZpshK8sMdrCIHSqLrf6TU7cHIir6I/kHqdovqA=
357363
github.com/oidc-mytoken/server v0.6.0/go.mod h1:UuhznDeB+pgB+b/fNVwn95ui+3tCW0eg2pJOA2lhYyI=
358364
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
@@ -955,5 +961,7 @@ honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt
955961
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
956962
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
957963
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
964+
rsc.io/qr v0.2.0 h1:6vBLea5/NRMVTz8V66gipeLycZMl/+UlFmk8DvqQ6WY=
965+
rsc.io/qr v0.2.0/go.mod h1:IF+uZjkb9fqyeF/4tlBoynqmQxUoPfWEKh921coOuXs=
958966
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
959967
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=

internal/commands/mt.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/oidc-mytoken/client/internal/config"
1414
cutils "github.com/oidc-mytoken/client/internal/utils"
1515
"github.com/oidc-mytoken/client/internal/utils/profile"
16+
"github.com/oidc-mytoken/client/internal/utils/qr"
1617
)
1718

1819
type restrictionOpts struct {
@@ -474,9 +475,9 @@ func obtainMT(context *cli.Context) (string, error) {
474475
if req.Name != "" && prefix != "" {
475476
req.Name = fmt.Sprintf("%s:%s", prefix, req.Name)
476477
}
478+
req.ApplicationName = fmt.Sprintf("mytoken client on %s", config.Get().Hostname)
477479
if ssh := mtCommand.SSH(); ssh != "" {
478480
req.GrantType = api.GrantTypeSSH
479-
req.ApplicationName = "mytoken client"
480481
mt, err := doSSHReturnOutput(ssh, api.SSHRequestMytoken, req)
481482
if mt != "" && mt[len(mt)-1] == '\n' {
482483
mt = mt[:len(mt)-1]
@@ -506,10 +507,15 @@ func obtainMT(context *cli.Context) (string, error) {
506507
}
507508
callbacks := mytokenlib.PollingCallbacks{
508509
Init: func(authorizationURL string) error {
509-
_, _ = fmt.Fprintln(os.Stderr, "Using any device please visit the following url to continue:")
510+
_, _ = fmt.Fprintln(
511+
os.Stderr,
512+
"Using any device please visit the following url to continue or use the qr code:",
513+
)
510514
_, _ = fmt.Fprintln(os.Stderr)
511515
_, _ = fmt.Fprintln(os.Stderr, authorizationURL)
512516
_, _ = fmt.Fprintln(os.Stderr)
517+
qr.FPrintQR(os.Stderr, authorizationURL)
518+
_, _ = fmt.Fprintln(os.Stderr)
513519
return nil
514520
},
515521
Callback: func(interval int64, iteration int) {
@@ -526,7 +532,6 @@ func obtainMT(context *cli.Context) (string, error) {
526532
_, _ = fmt.Fprintln(os.Stderr, "success")
527533
},
528534
}
529-
return mytoken.Mytoken.FromAuthorizationFlow(
530-
req.Issuer, req.Restrictions, req.Capabilities, req.Rotation, req.ResponseType, req.Name, callbacks,
531-
)
535+
resp, err := mytoken.Mytoken.APIFromAuthorizationFlowReq(*req, callbacks)
536+
return resp.Mytoken, err
532537
}

internal/config/config.go

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Config struct {
2424
Providers map[string]string `yaml:"providers"`
2525

2626
usedConfigDir string
27+
Hostname string
2728
}
2829

2930
var defaultConfig = Config{
@@ -61,6 +62,7 @@ func load(name string, locations []string) {
6162
conf.Mytoken = mytoken
6263

6364
hostname, _ := os.Hostname()
65+
conf.Hostname = hostname
6466
conf.TokenNamePrefix = strings.ReplaceAll(conf.TokenNamePrefix, "<hostname>", hostname)
6567
}
6668

internal/model/version/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
const (
99
MAJOR = 0
1010
MINOR = 5
11-
FIX = 0
11+
FIX = 1
1212
DEV = false
1313
)
1414

internal/utils/qr/qr.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package qr
2+
3+
import (
4+
"io"
5+
"os"
6+
7+
"github.com/mdp/qrterminal/v3"
8+
)
9+
10+
var halfConfig = qrterminal.Config{
11+
Level: qrterminal.M,
12+
HalfBlocks: true,
13+
BlackChar: qrterminal.BLACK_BLACK,
14+
BlackWhiteChar: qrterminal.BLACK_WHITE,
15+
WhiteChar: qrterminal.WHITE_WHITE,
16+
WhiteBlackChar: qrterminal.WHITE_BLACK,
17+
QuietZone: 1,
18+
}
19+
20+
// PrintQR prints a qr code for the passed data to os.Stdout
21+
func PrintQR(data string, border ...int) {
22+
FPrintQR(os.Stdout, data, border...)
23+
}
24+
25+
// FPrintQR writes a qr code for the passed data to an os.Writer
26+
func FPrintQR(writer io.Writer, data string, border ...int) {
27+
quietZone := 1
28+
if len(border) > 0 {
29+
quietZone = border[0]
30+
}
31+
halfConfig.QuietZone = quietZone
32+
halfConfig.Writer = writer
33+
qrterminal.GenerateWithConfig(data, halfConfig)
34+
}

0 commit comments

Comments
 (0)