Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch pcsclite dependency from cgo to native go #9

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 1 addition & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: go
sudo: required

go:
- "1.11.x"
- "1.12"

env:
global:
Expand All @@ -12,9 +12,5 @@ os:
- linux
- osx

before_install:
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get install -qy libpcsclite-dev; fi
- go get -t -v ./...

script:
- go test -coverprofile=coverage.txt -covermode=atomic -parallel 1 -race -tags ci
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Changed

- Switch pcsclite dependency from cgo to native go

## 1.0.2

### Changed
Expand Down
5 changes: 1 addition & 4 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ environment:
GOPATH: c:\gopath
GO111MODULE: on

stack: go 1.11

before_test:
- go get -t -v ./...
stack: go 1.12

test_script:
- go test -coverprofile=coverage.txt -covermode=atomic -parallel 1 -race -tags ci
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/yawn/ykoath

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/ebfe/scard v0.0.0-20190212122703-c3d1b1916a95
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff
github.com/pkg/errors v0.8.1
github.com/stretchr/objx v0.1.1 // indirect
github.com/stretchr/testify v1.3.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/ebfe/scard v0.0.0-20190212122703-c3d1b1916a95 h1:OM0MnUcXBysj7ZtXvThVWHMoahuKQ8FuwIdeSLcNdP4=
github.com/ebfe/scard v0.0.0-20190212122703-c3d1b1916a95/go.mod h1:8hHvF8DlEq5kE3KWOsZQezdWq1OTOVxZArZMscS954E=
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff h1:tY80oXqGNY4FhTFhk+o9oFHGINQ/+vhlm8HFzi6znCI=
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff/go.mod h1:x7DCsMOv1taUwEWCzT4cmDeAkigA5/QCwUodaVOe8Ww=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
36 changes: 18 additions & 18 deletions ykoath.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ import (
"strings"
"time"

"github.com/ebfe/scard"
pcsc "github.com/gballet/go-libpcsclite"
"github.com/pkg/errors"
)

type card interface {
Disconnect(scard.Disposition) error
Transmit([]byte) ([]byte, error)
Disconnect(uint32) error
Transmit([]byte) ([]byte, *pcsc.SCardIoRequest, error)
}

type context interface {
Release() error
type client interface {
ReleaseContext() error
}

type debugger func(string, ...interface{})

// OATH implements most parts of the TOTP portion of the YKOATH specification
// https://developers.yubico.com/OATH/YKOATH_Protocol.html
type OATH struct {
card card
Clock func() time.Time
context context
Debug debugger
card card
Clock func() time.Time
client client
Debug debugger
}

const (
Expand All @@ -43,13 +43,13 @@ const (
// New initializes a new OATH session
func New() (*OATH, error) {

context, err := scard.EstablishContext()
client, err := pcsc.EstablishContext("", pcsc.ScopeSystem)

if err != nil {
return nil, errors.Wrapf(err, errFailedToEstablishContext)
}

readers, err := context.ListReaders()
readers, err := client.ListReaders()

if err != nil {
return nil, errors.Wrapf(err, errFailedToListReaders)
Expand All @@ -59,16 +59,16 @@ func New() (*OATH, error) {

if strings.Contains(reader, "Yubikey") {

card, err := context.Connect(reader, scard.ShareShared, scard.ProtocolAny)
card, err := client.Connect(reader, pcsc.ShareShared, pcsc.ProtocolAny)

if err != nil {
return nil, errors.Wrapf(err, errFailedToConnect)
}

return &OATH{
card: card,
Clock: time.Now,
context: context,
card: card,
Clock: time.Now,
client: client,
}, nil

}
Expand All @@ -82,11 +82,11 @@ func New() (*OATH, error) {
// Close terminates an OATH session
func (o *OATH) Close() error {

if err := o.card.Disconnect(scard.LeaveCard); err != nil {
if err := o.card.Disconnect(pcsc.LeaveCard); err != nil {
return errors.Wrapf(err, errFailedToDisconnect)
}

if err := o.context.Release(); err != nil {
if err := o.client.ReleaseContext(); err != nil {
return errors.Wrapf(err, errFailedToReleaseContext)
}

Expand All @@ -109,7 +109,7 @@ func (o *OATH) send(cla, ins, p1, p2 byte, data ...[]byte) (*tvs, error) {
o.Debug("SEND % x (%d)", send, len(send))
}

res, err := o.card.Transmit(send)
res, _, err := o.card.Transmit(send)

if err != nil {
return nil, errors.Wrapf(err, errFailedToTransmit)
Expand Down
13 changes: 9 additions & 4 deletions ykoath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"
"time"

"github.com/ebfe/scard"
pcsc "github.com/gballet/go-libpcsclite"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
Expand All @@ -30,14 +30,14 @@ type testCard struct {
mock.Mock
}

func (t *testCard) Disconnect(d scard.Disposition) error {
func (t *testCard) Disconnect(d uint32) error {
args := t.Called(d)
return args.Error(0)
}

func (t *testCard) Transmit(b []byte) ([]byte, error) {
func (t *testCard) Transmit(b []byte) ([]byte, *pcsc.SCardIoRequest, error) {
args := t.Called(b)
return args.Get(0).([]byte), args.Error(1)
return args.Get(0).([]byte), nil, args.Error(1)
}

func TestCalculate6DigitTouch(t *testing.T) {
Expand Down Expand Up @@ -80,6 +80,7 @@ func TestCalculate6DigitTouch(t *testing.T) {
0x39, 0x39, 0x61, 0xff,
},
nil,
nil,
).Once().
On(
"Transmit",
Expand Down Expand Up @@ -112,6 +113,7 @@ func TestCalculate6DigitTouch(t *testing.T) {
0x73, 0x74, 0x61, 0xff,
},
nil,
nil,
).Once().
On(
"Transmit",
Expand Down Expand Up @@ -144,6 +146,7 @@ func TestCalculate6DigitTouch(t *testing.T) {
0x38, 0x32, 0x61, 0x5a,
},
nil,
nil,
).Once().
On(
"Transmit",
Expand All @@ -162,6 +165,7 @@ func TestCalculate6DigitTouch(t *testing.T) {
0x05, 0x08, 0x04, 0xaf, 0x17, 0x64, 0x90, 0x00,
},
nil,
nil,
).Once().
On(
"Transmit",
Expand All @@ -179,6 +183,7 @@ func TestCalculate6DigitTouch(t *testing.T) {
0x76, 0x05, 0x06, 0x00, 0x0c, 0xc7, 0xe4, 0x90, 0x00,
},
nil,
nil,
).Once()

client := new(OATH)
Expand Down