-
Notifications
You must be signed in to change notification settings - Fork 280
/
options.go
306 lines (274 loc) · 9.69 KB
/
options.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package webauthnutil
import (
"encoding/base64"
"fmt"
"net/http"
"time"
"github.com/pomerium/pomerium/pkg/cryptutil"
"github.com/pomerium/pomerium/pkg/grpc/device"
"github.com/pomerium/pomerium/pkg/grpc/user"
"github.com/pomerium/pomerium/pkg/slices"
"github.com/pomerium/webauthn"
"github.com/pomerium/webauthn/cose"
)
const (
ceremonyTimeout = time.Minute * 15
rpName = "Pomerium"
)
// GenerateChallenge generates a new Challenge.
func GenerateChallenge(key []byte, expiry time.Time) cryptutil.SecureToken {
return cryptutil.GenerateSecureToken(key, expiry, cryptutil.NewRandomToken())
}
// GenerateCreationOptions generates creation options for WebAuthn.
func GenerateCreationOptions(
r *http.Request,
key []byte,
deviceType *device.Type,
user *user.User,
) *webauthn.PublicKeyCredentialCreationOptions {
expiry := time.Now().Add(ceremonyTimeout)
return newCreationOptions(
r,
GenerateChallenge(key, expiry).Bytes(),
deviceType,
user,
)
}
// GenerateRequestOptions generates request options for WebAuthn.
func GenerateRequestOptions(
r *http.Request,
key []byte,
deviceType *device.Type,
knownDeviceCredentials []*device.Credential,
) *webauthn.PublicKeyCredentialRequestOptions {
expiry := time.Now().Add(ceremonyTimeout)
return newRequestOptions(
r,
GenerateChallenge(key, expiry).Bytes(),
deviceType,
knownDeviceCredentials,
)
}
// GetCreationOptionsForCredential gets the creation options for the public key creation credential. An error may be
// returned if the challenge used to generate the credential is invalid.
func GetCreationOptionsForCredential(
r *http.Request,
key []byte,
deviceType *device.Type,
user *user.User,
credential *webauthn.PublicKeyCreationCredential,
) (*webauthn.PublicKeyCredentialCreationOptions, error) {
clientData, err := credential.Response.UnmarshalClientData()
if err != nil {
return nil, fmt.Errorf("invalid client data: %w", err)
}
rawChallenge, err := base64.RawURLEncoding.DecodeString(clientData.Challenge)
if err != nil {
return nil, fmt.Errorf("invalid challenge: %w", err)
}
var challenge cryptutil.SecureToken
copy(challenge[:], rawChallenge)
err = challenge.Verify(key, time.Now())
if err != nil {
return nil, err
}
return newCreationOptions(r, challenge.Bytes(), deviceType, user), nil
}
// GetRequestOptionsForCredential gets the request options for the public key request credential. An error may be
// returned if the challenge used to generate the credential is invalid.
func GetRequestOptionsForCredential(
r *http.Request,
key []byte,
deviceType *device.Type,
knownDeviceCredentials []*device.Credential,
credential *webauthn.PublicKeyAssertionCredential,
) (*webauthn.PublicKeyCredentialRequestOptions, error) {
clientData, err := credential.Response.UnmarshalClientData()
if err != nil {
return nil, fmt.Errorf("invalid client data: %w", err)
}
rawChallenge, err := base64.RawURLEncoding.DecodeString(clientData.Challenge)
if err != nil {
return nil, fmt.Errorf("invalid challenge: %w", err)
}
var challenge cryptutil.SecureToken
copy(challenge[:], rawChallenge)
err = challenge.Verify(key, time.Now())
if err != nil {
return nil, err
}
return newRequestOptions(r, challenge.Bytes(), deviceType, knownDeviceCredentials), nil
}
// newCreationOptions gets the creation options for WebAuthn with the provided challenge.
func newCreationOptions(
r *http.Request,
challenge []byte,
deviceType *device.Type,
user *user.User,
) *webauthn.PublicKeyCredentialCreationOptions {
options := &webauthn.PublicKeyCredentialCreationOptions{
RP: webauthn.PublicKeyCredentialRPEntity{
Name: rpName,
ID: GetEffectiveDomain(r),
},
User: GetUserEntity(user),
Challenge: challenge,
Timeout: ceremonyTimeout,
}
if deviceOptions := deviceType.GetWebauthn().GetOptions(); deviceOptions != nil {
fillAllPublicKeyCredentialParameters(options, deviceOptions.GetPubKeyCredParams())
fillAuthenticatorSelection(options, deviceOptions.GetAuthenticatorSelection())
fillAttestationConveyance(options, deviceOptions.Attestation)
}
return options
}
// newRequestOptions gets the request options for WebAuthn with the provided challenge.
func newRequestOptions(
r *http.Request,
challenge []byte,
deviceType *device.Type,
knownDeviceCredentials []*device.Credential,
) *webauthn.PublicKeyCredentialRequestOptions {
options := &webauthn.PublicKeyCredentialRequestOptions{
Challenge: challenge,
Timeout: ceremonyTimeout,
RPID: GetEffectiveDomain(r),
}
fillRequestUserVerificationRequirement(
options,
deviceType.GetWebauthn().GetOptions().GetAuthenticatorSelection().UserVerification,
)
knownDeviceCredentialsForType := slices.Filter(knownDeviceCredentials, func(c *device.Credential) bool {
return c.GetTypeId() == deviceType.GetId()
})
for _, knownDeviceCredential := range knownDeviceCredentialsForType {
if publicKey := knownDeviceCredential.GetWebauthn(); publicKey != nil {
options.AllowCredentials = append(options.AllowCredentials, webauthn.PublicKeyCredentialDescriptor{
Type: webauthn.PublicKeyCredentialTypePublicKey,
ID: publicKey.GetId(),
})
}
}
return options
}
func fillAllPublicKeyCredentialParameters(
options *webauthn.PublicKeyCredentialCreationOptions,
allDeviceParams []*device.WebAuthnOptions_PublicKeyCredentialParameters,
) {
options.PubKeyCredParams = nil
for _, deviceParams := range allDeviceParams {
p := webauthn.PublicKeyCredentialParameters{}
fillPublicKeyCredentialParameters(&p, deviceParams)
options.PubKeyCredParams = append(options.PubKeyCredParams, p)
}
}
func fillAttestationConveyance(
options *webauthn.PublicKeyCredentialCreationOptions,
attestationConveyance *device.WebAuthnOptions_AttestationConveyancePreference,
) {
options.Attestation = ""
if attestationConveyance == nil {
return
}
switch *attestationConveyance {
case device.WebAuthnOptions_NONE:
options.Attestation = webauthn.AttestationConveyanceNone
case device.WebAuthnOptions_INDIRECT:
options.Attestation = webauthn.AttestationConveyanceIndirect
case device.WebAuthnOptions_DIRECT:
options.Attestation = webauthn.AttestationConveyanceDirect
case device.WebAuthnOptions_ENTERPRISE:
options.Attestation = webauthn.AttestationConveyanceEnterprise
}
}
func fillAuthenticatorAttachment(
criteria *webauthn.AuthenticatorSelectionCriteria,
authenticatorAttachment *device.WebAuthnOptions_AuthenticatorAttachment,
) {
criteria.AuthenticatorAttachment = ""
if authenticatorAttachment == nil {
return
}
switch *authenticatorAttachment {
case device.WebAuthnOptions_CROSS_PLATFORM:
criteria.AuthenticatorAttachment = webauthn.AuthenticatorAttachmentCrossPlatform
case device.WebAuthnOptions_PLATFORM:
criteria.AuthenticatorAttachment = webauthn.AuthenticatorAttachmentPlatform
}
}
func fillAuthenticatorSelection(
options *webauthn.PublicKeyCredentialCreationOptions,
deviceCriteria *device.WebAuthnOptions_AuthenticatorSelectionCriteria,
) {
options.AuthenticatorSelection = new(webauthn.AuthenticatorSelectionCriteria)
fillAuthenticatorAttachment(options.AuthenticatorSelection, deviceCriteria.AuthenticatorAttachment)
fillResidentKeyRequirement(options.AuthenticatorSelection, deviceCriteria.ResidentKeyRequirement)
options.AuthenticatorSelection.RequireResidentKey = deviceCriteria.GetRequireResidentKey()
fillUserVerificationRequirement(options.AuthenticatorSelection, deviceCriteria.UserVerification)
}
func fillPublicKeyCredentialParameters(
params *webauthn.PublicKeyCredentialParameters,
deviceParams *device.WebAuthnOptions_PublicKeyCredentialParameters,
) {
params.Type = ""
params.COSEAlgorithmIdentifier = 0
if deviceParams == nil {
return
}
switch deviceParams.Type {
case device.WebAuthnOptions_PUBLIC_KEY:
params.Type = webauthn.PublicKeyCredentialTypePublicKey
}
params.COSEAlgorithmIdentifier = cose.Algorithm(deviceParams.GetAlg())
}
func fillRequestUserVerificationRequirement(
options *webauthn.PublicKeyCredentialRequestOptions,
userVerificationRequirement *device.WebAuthnOptions_UserVerificationRequirement,
) {
options.UserVerification = ""
if userVerificationRequirement == nil {
return
}
switch *userVerificationRequirement {
case device.WebAuthnOptions_USER_VERIFICATION_DISCOURAGED:
options.UserVerification = webauthn.UserVerificationDiscouraged
case device.WebAuthnOptions_USER_VERIFICATION_PREFERRED:
options.UserVerification = webauthn.UserVerificationPreferred
case device.WebAuthnOptions_USER_VERIFICATION_REQUIRED:
options.UserVerification = webauthn.UserVerificationRequired
}
}
func fillResidentKeyRequirement(
criteria *webauthn.AuthenticatorSelectionCriteria,
residentKeyRequirement *device.WebAuthnOptions_ResidentKeyRequirement,
) {
criteria.ResidentKey = ""
if residentKeyRequirement == nil {
return
}
switch *residentKeyRequirement {
case device.WebAuthnOptions_RESIDENT_KEY_DISCOURAGED:
criteria.ResidentKey = webauthn.ResidentKeyDiscouraged
case device.WebAuthnOptions_RESIDENT_KEY_PREFERRED:
criteria.ResidentKey = webauthn.ResidentKeyPreferred
case device.WebAuthnOptions_RESIDENT_KEY_REQUIRED:
criteria.ResidentKey = webauthn.ResidentKeyRequired
}
}
func fillUserVerificationRequirement(
criteria *webauthn.AuthenticatorSelectionCriteria,
userVerificationRequirement *device.WebAuthnOptions_UserVerificationRequirement,
) {
criteria.UserVerification = ""
if userVerificationRequirement == nil {
return
}
switch *userVerificationRequirement {
case device.WebAuthnOptions_USER_VERIFICATION_DISCOURAGED:
criteria.UserVerification = webauthn.UserVerificationDiscouraged
case device.WebAuthnOptions_USER_VERIFICATION_PREFERRED:
criteria.UserVerification = webauthn.UserVerificationPreferred
case device.WebAuthnOptions_USER_VERIFICATION_REQUIRED:
criteria.UserVerification = webauthn.UserVerificationRequired
}
}