Skip to content
Open
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/int128/oauth2cli v1.15.1
github.com/int128/oauth2dev v1.0.1
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
github.com/spf13/cobra v1.9.1
github.com/spf13/pflag v1.0.6
github.com/stretchr/testify v1.10.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,8 @@ github.com/sivchari/containedctx v1.0.3 h1:x+etemjbsh2fB5ewm5FeLNi5bUjK0V8n0RB+W
github.com/sivchari/containedctx v1.0.3/go.mod h1:c1RDvCbnJLtH4lLcYD/GqwiBSSf4F5Qk0xld2rBqzJ4=
github.com/sivchari/tenv v1.12.1 h1:+E0QzjktdnExv/wwsnnyk4oqZBUfuh89YMQT1cyuvSY=
github.com/sivchari/tenv v1.12.1/go.mod h1:1LjSOUCc25snIr5n3DtGGrENhX3LuWefcplwVGC24mw=
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M=
github.com/sonatard/noctx v0.1.0 h1:JjqOc2WN16ISWAjAk8M5ej0RfExEXtkEyExl2hLW+OM=
github.com/sonatard/noctx v0.1.0/go.mod h1:0RvBxqY8D4j9cTTTWE8ylt2vqj2EPI8fHmrxHdsaZ2c=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
Expand Down
3 changes: 3 additions & 0 deletions pkg/cmd/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type authenticationOptions struct {
AuthRequestExtraParams map[string]string
Username string
Password string
GenerateQRCode bool
}

var allGrantType = strings.Join([]string{
Expand All @@ -52,6 +53,7 @@ func (o *authenticationOptions) addFlags(f *pflag.FlagSet) {
f.StringToStringVar(&o.AuthRequestExtraParams, "oidc-auth-request-extra-params", nil, "[authcode, authcode-keyboard] Extra query parameters to send with an authentication request")
f.StringVar(&o.Username, "username", "", "[password] Username for resource owner password credentials grant")
f.StringVar(&o.Password, "password", "", "[password] Password for resource owner password credentials grant")
f.BoolVar(&o.GenerateQRCode, "generate-qrcode", false, "[device-code] Generate a QR code for authentication")
}

func (o *authenticationOptions) expandHomedir() {
Expand Down Expand Up @@ -87,6 +89,7 @@ func (o *authenticationOptions) grantOptionSet() (s authentication.GrantOptionSe
s.DeviceCodeOption = &devicecode.Option{
SkipOpenBrowser: o.SkipOpenBrowser,
BrowserCommand: o.BrowserCommand,
GenerateQRCode: o.GenerateQRCode,
}
default:
err = fmt.Errorf("grant-type must be one of (%s)", allGrantType)
Expand Down
28 changes: 24 additions & 4 deletions pkg/usecases/authentication/devicecode/devicecode.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package devicecode
import (
"context"
"fmt"

"github.com/int128/kubelogin/pkg/infrastructure/browser"
"github.com/int128/kubelogin/pkg/infrastructure/logger"
"github.com/int128/kubelogin/pkg/oidc"
"github.com/int128/kubelogin/pkg/oidc/client"
"github.com/skip2/go-qrcode"
)

type Option struct {
SkipOpenBrowser bool
BrowserCommand string
GenerateQRCode bool
}

// DeviceCode provides the oauth2 device code flow.
Expand Down Expand Up @@ -49,9 +50,26 @@ func (u *DeviceCode) Do(ctx context.Context, in *Option, oidcClient client.Inter
return tokenSet, nil
}

func (u *DeviceCode) getQRCode(o *Option, url string) (string, error) {
if !o.GenerateQRCode {
return "", nil
}
var q *qrcode.QRCode
q, err := qrcode.New(url, qrcode.Low)
if err != nil {
return "", fmt.Errorf("unable to generate QRCode: %w", err)
}
return q.ToString(true), nil
}

func (u *DeviceCode) openURL(ctx context.Context, o *Option, url string) {
qr, err := u.getQRCode(o, url)

if err != nil {
u.Logger.Printf("error: could not generate QR code: %s", err)
}
if o != nil && o.SkipOpenBrowser {
u.Logger.Printf("Please visit the following URL in your browser: %s", url)
u.Logger.Printf("Please visit the following URL in your browser: %s\n%s", url, qr)
return
}

Expand All @@ -60,13 +78,15 @@ func (u *DeviceCode) openURL(ctx context.Context, o *Option, url string) {
if err := u.Browser.OpenCommand(ctx, url, o.BrowserCommand); err != nil {
u.Logger.Printf(`error: could not open the browser: %s

Please visit the following URL in your browser manually: %s`, err, url)
Please visit the following URL in your browser manually: %s
%s`, err, url, qr)
}
return
}
if err := u.Browser.Open(url); err != nil {
u.Logger.Printf(`error: could not open the browser: %s

Please visit the following URL in your browser manually: %s`, err, url)
Please visit the following URL in your browser manually: %s
%s`, err, url, qr)
}
}