Skip to content

Commit

Permalink
upgrade go hid (#468)
Browse files Browse the repository at this point in the history
* upgrade to master as a new version has not been released since 2019

karalabe/hid#54 highlights that a new release
is due but not present. Workaround is to select the master branch.

* new library returns error

* refactor AutodetectYubiKey to IsYubiKeyAutoDetected to reflect the nature of the symbol
  • Loading branch information
bitte-ein-bit authored Dec 17, 2024
1 parent b192c68 commit 9d051e1
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
17 changes: 9 additions & 8 deletions onelogin/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions onelogin/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand All @@ -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,
},
Expand Down
6 changes: 5 additions & 1 deletion yubikey/yubikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 9d051e1

Please sign in to comment.