forked from Versent/saml2aws
/
jumpcloud_webauthn.go
193 lines (164 loc) · 4.56 KB
/
jumpcloud_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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package jumpcloud
import (
"encoding/base64"
"errors"
"fmt"
"log"
"time"
"github.com/marshallbrekka/go-u2fhost"
)
const (
MaxOpenRetries = 10
RetryDelayMS = 200 * time.Millisecond
jumpCloudOrigin = "https://console.jumpcloud.com"
)
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 {
challenge string
rpId string
keyHandle string
token string
Device u2fhost.Device
}
type JumpCloudResponse struct {
PublicKeyCredential PublicKey `json:"publicKeyCredential"`
Token string `json:"token"`
}
type PublicKey struct {
Id string `json:"id"`
RawId string `json:"rawId"`
Type string `json:"type"`
Response PublicKeyResponse `json:"response"`
}
type PublicKeyResponse struct {
ClientData string `json:"clientDataJSON"`
AuthenticatorData string `json:"authenticatorData"`
SignatureData string `json:"signature"`
UserHandle *string `json:"userHandle"`
}
// 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(challenge, rpId, keyHandle, token 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,
challenge: challenge,
rpId: rpId,
keyHandle: keyHandle,
token: token,
}, 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() (*JumpCloudResponse, error) {
if d.Device == nil {
return nil, errors.New("No Device Found")
}
request := &u2fhost.AuthenticateRequest{
Challenge: d.challenge,
Facet: jumpCloudOrigin,
AppId: d.rpId,
KeyHandle: d.keyHandle,
WebAuthn: true,
}
// do the change
prompted := false
timeout := time.After(time.Second * 25)
interval := time.NewTicker(time.Millisecond * 250)
var responsePayload *JumpCloudResponse
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 {
authenticatorData, err := urlEncode(response.AuthenticatorData)
if err != nil {
return nil, err
}
signatureData, err := urlEncode(response.SignatureData)
if err != nil {
return nil, err
}
responsePayload = &JumpCloudResponse{
PublicKeyCredential: PublicKey{
Id: response.KeyHandle,
RawId: response.KeyHandle,
Type: "public-key",
Response: PublicKeyResponse{
ClientData: response.ClientData,
AuthenticatorData: authenticatorData,
SignatureData: signatureData,
UserHandle: nil,
},
},
Token: d.token,
}
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
}
}
}
}
func urlEncode(stdEncodedStr string) (string, error) {
decodedStr, err := base64.StdEncoding.DecodeString(stdEncodedStr)
if err != nil {
return "", err
}
return base64.RawURLEncoding.EncodeToString(decodedStr), nil
}
// 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)
}