diff --git a/go.mod b/go.mod index dc5f282..a65a255 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( github.com/crewjam/saml v0.4.14 github.com/go-ini/ini v1.67.0 github.com/icza/gog v0.0.0-20230509085756-00e776132a34 - github.com/karalabe/hid v1.0.0 + github.com/karalabe/hid v1.0.1-0.20240919124526-821c38d2678e github.com/mattn/go-colorable v0.1.13 github.com/mitchellh/go-homedir v1.1.0 github.com/nightlyone/lockfile v1.0.0 diff --git a/go.sum b/go.sum index a6ab4a7..cb6419d 100644 --- a/go.sum +++ b/go.sum @@ -60,6 +60,8 @@ github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc= github.com/karalabe/hid v1.0.0 h1:+/CIMNXhSU/zIJgnIvBD2nKHxS/bnRHhhs9xBryLpPo= github.com/karalabe/hid v1.0.0/go.mod h1:Vr51f8rUOLYrfrWDFlV12GGQgM5AT8sVh+2fY4MPeu8= +github.com/karalabe/hid v1.0.1-0.20240919124526-821c38d2678e h1:ryNJIEs1fyZNVwJ/Dsz7+EFZTow0ggBdnAIVo8SAs+A= +github.com/karalabe/hid v1.0.1-0.20240919124526-821c38d2678e/go.mod h1:qk1sX/IBgppQNcGCRoj90u6EGC056EBoIc1oEjCWla8= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= diff --git a/onelogin/get.go b/onelogin/get.go index ed26e60..292e42f 100644 --- a/onelogin/get.go +++ b/onelogin/get.go @@ -46,7 +46,7 @@ var ( type DeviceOptions struct { // Detect if a YubiKey is inserted and automatically select the device - AutodetectYubiKey bool + IsYubiKeyAutoDetected bool // Override all other choices and select this device name if available MfaDevice string @@ -55,25 +55,26 @@ type DeviceOptions struct { // NewDeviceOptions returns a configured pointer to a DeviceOptions type func NewDeviceOptions() *DeviceOptions { d := new(DeviceOptions) - d.setAutodetectYubiKey() + d.detectYubiKey() d.setMfaDevice() log.WithFields(log.Fields{ - "autodetectYubiKey": d.AutodetectYubiKey, - "MfaDevice": d.MfaDevice, + "IsYubiKeyAutoDetected": d.IsYubiKeyAutoDetected, + "MfaDevice": d.MfaDevice, }).Debug("created device options configuration") return d } -// setAutodetectYubiKey sets the AutodetectYubiKey parameter -func (d *DeviceOptions) setAutodetectYubiKey() { +// detectYubiKey sets the IsYubiKeyAutoDetected parameter +func (d *DeviceOptions) detectYubiKey() { var a bool if viper.IsSet("global.autodetect-yubikey") { a = viper.GetBool("global.autodetect-yubikey") + log.WithField("autodetect-yubikey", a).Trace("global.autodetect-yubikey is set in config") } if a && yubikey.IsAttached() { - d.AutodetectYubiKey = true + d.IsYubiKeyAutoDetected = true return } } @@ -323,7 +324,7 @@ func getDevice(devices []Device, opts *DeviceOptions) (device *Device, err error fmt.Printf("MFA device %s not found.\n", opts.MfaDevice) } - if opts.AutodetectYubiKey { + if opts.IsYubiKeyAutoDetected { for _, d := range devices { if d.DeviceType == MFADeviceYubicoYubiKey { device = &d diff --git a/onelogin/get_test.go b/onelogin/get_test.go index 11860cd..add9f47 100644 --- a/onelogin/get_test.go +++ b/onelogin/get_test.go @@ -46,7 +46,7 @@ func TestGetDevice(t *testing.T) { { Name: "AutodetectYubiKey", Devices: deviceList, - Opts: &DeviceOptions{AutodetectYubiKey: true}, + Opts: &DeviceOptions{IsYubiKeyAutoDetected: true}, ExpectedDevice: &Device{DeviceID: 01, DeviceType: "Yubico YubiKey"}, ExpectedError: nil, }, @@ -60,7 +60,7 @@ func TestGetDevice(t *testing.T) { { Name: "SelectedMfaDeviceOverride", Devices: deviceList, - Opts: &DeviceOptions{AutodetectYubiKey: true, MfaDevice: "Google Authenticator"}, + Opts: &DeviceOptions{IsYubiKeyAutoDetected: true, MfaDevice: "Google Authenticator"}, ExpectedDevice: &Device{DeviceID: 03, DeviceType: "Google Authenticator"}, ExpectedError: nil, }, diff --git a/yubikey/yubikey.go b/yubikey/yubikey.go index 7e7845b..56bd8bc 100644 --- a/yubikey/yubikey.go +++ b/yubikey/yubikey.go @@ -12,7 +12,11 @@ const yubiKeyVendorID uint16 = 0x1050 func IsAttached() bool { // List all USB devices matching the YubiKey vendor ID - devices := hid.Enumerate(yubiKeyVendorID, 0) + devices, err := hid.Enumerate(yubiKeyVendorID, 0) + if err != nil { + log.WithError(err).Error("Failed to enumerate USB devices") + return false + } if len(devices) == 0 { log.Debug("No YubiKey device detected")