-
Notifications
You must be signed in to change notification settings - Fork 562
/
okta_webauthn.go
156 lines (132 loc) · 3.73 KB
/
okta_webauthn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package okta
import (
"errors"
"fmt"
"log"
"time"
"github.com/marshallbrekka/go-u2fhost"
)
const (
MaxOpenRetries = 10
RetryDelayMS = 200 * time.Millisecond
)
var (
errNoDeviceFound = fmt.Errorf("no U2F devices found. device might not be plugged in")
)
// FidoClient represents a challenge and the device used to respond
type FidoClient struct {
ChallengeNonce string
AppID string
Version string
Device u2fhost.Device
KeyHandle string
StateToken string
}
// SignedAssertion is passed back to Okta as response
type SignedAssertion struct {
StateToken string `json:"stateToken"`
ClientData string `json:"clientData"`
SignatureData string `json:"signatureData"`
AuthenticatorData string `json:"authenticatorData"`
}
// DeviceFinder is used to mock out finding devices
type DeviceFinder interface {
findDevice() (u2fhost.Device, error)
}
// U2FDevice is used to support mocking this device with mockery https://github.com/vektra/mockery/issues/210#issuecomment-485026348
type U2FDevice interface {
u2fhost.Device
}
// NewFidoClient returns a new initialized FIDO1-based WebAuthnClient, representing a single device
func NewFidoClient(challengeNonce, appID, version, keyHandle, stateToken string, deviceFinder DeviceFinder) (FidoClient, error) {
var device u2fhost.Device
var err error
retryCount := 0
for retryCount < MaxOpenRetries {
device, err = deviceFinder.findDevice()
if err != nil {
if err == errNoDeviceFound {
return FidoClient{}, err
}
retryCount++
time.Sleep(RetryDelayMS)
continue
}
return FidoClient{
Device: device,
ChallengeNonce: challengeNonce,
AppID: appID,
Version: version,
KeyHandle: keyHandle,
StateToken: stateToken,
}, nil
}
return FidoClient{}, fmt.Errorf("failed to create client: %s. exceeded max retries of %d", err, MaxOpenRetries)
}
// ChallengeU2F takes a FidoClient and returns a signed assertion to send to Okta
func (d *FidoClient) ChallengeU2F() (*SignedAssertion, error) {
if d.Device == nil {
return nil, errors.New("No Device Found")
}
request := &u2fhost.AuthenticateRequest{
Challenge: d.ChallengeNonce,
Facet: "https://" + d.AppID,
AppId: d.AppID,
KeyHandle: d.KeyHandle,
WebAuthn: true,
}
// do the change
prompted := false
timeout := time.After(time.Second * 25)
interval := time.NewTicker(time.Millisecond * 250)
var responsePayload *SignedAssertion
defer func() {
d.Device.Close()
}()
defer interval.Stop()
for {
select {
case <-timeout:
return nil, errors.New("Failed to get authentication response after 25 seconds")
case <-interval.C:
response, err := d.Device.Authenticate(request)
if err == nil {
responsePayload = &SignedAssertion{
StateToken: d.StateToken,
ClientData: response.ClientData,
SignatureData: response.SignatureData,
AuthenticatorData: response.AuthenticatorData,
}
log.Println(" ==> Touch accepted. Proceeding with authentication")
return responsePayload, nil
}
switch err.(type) {
case *u2fhost.TestOfUserPresenceRequiredError:
if !prompted {
log.Println("Touch the flashing U2F device to authenticate...")
prompted = true
}
default:
return responsePayload, err
}
}
}
}
// U2FDeviceFinder returns a U2F device
type U2FDeviceFinder struct{}
func (*U2FDeviceFinder) findDevice() (u2fhost.Device, error) {
var err error
allDevices := u2fhost.Devices()
if len(allDevices) == 0 {
return nil, errNoDeviceFound
}
for i, device := range allDevices {
err = device.Open()
if err != nil {
device.Close()
continue
}
return allDevices[i], nil
}
return nil, fmt.Errorf("failed to open fido U2F device: %s", err)
}