-
Notifications
You must be signed in to change notification settings - Fork 5
/
Types.fs
437 lines (361 loc) · 12.7 KB
/
Types.fs
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
module Logibit.Hawk.Types
open System
open System.Security.Cryptography
open NodaTime
open Logibit.Hawk.Logging
type Lens<'a,'b> = ('a -> 'b) * ('b -> 'a -> 'a)
type HttpMethod =
| GET
| HEAD
| PUT
| POST
| TRACE
| DELETE
| PATCH
| CONNECT
| OPTIONS
override x.ToString () =
match x with
| GET -> "GET"
| HEAD -> "HEAD"
| PUT -> "PUT"
| POST -> "POST"
| TRACE -> "TRACE"
| DELETE -> "DELETE"
| PATCH -> "PATCH"
| CONNECT -> "CONNECT"
| OPTIONS -> "OPTIONS"
type Algo =
| SHA1
| SHA256
| SHA384
| SHA512
/// Create a new HashAlgorithm
member x.create () =
match x with
| SHA1 -> SHA1.Create() :> HashAlgorithm
| SHA256 -> SHA256.Create() :> _
| SHA384 -> SHA384.Create() :> _
| SHA512 -> SHA512.Create() :> _
/// Create a new HMAC
member x.createHMAC () =
match x with
| SHA1 -> new HMACSHA1() :> HMAC
| SHA256 -> new HMACSHA256() :> HMAC
| SHA384 -> new HMACSHA384() :> HMAC
| SHA512 -> new HMACSHA512() :> HMAC
/// A credential structure which has all fields required - this contains the private key too.
type Credentials =
{ id : string
key : string
algorithm : Algo }
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module Credentials =
let id_ =
(fun x -> x.id),
fun v x -> { x with id = v }
let key_ =
(fun x -> x.key),
fun v x -> { x with key = v }
let algorithm_ =
(fun x -> x.id),
fun v x -> { x with algorithm = v }
type Port = uint16
type HawkAttributes =
{ ``method`` : HttpMethod
uri : Uri // host, port, resource
id : string
ts : Instant
nonce : string
mac : string
hash : string option
ext : string option
app : string option
dlg : string option }
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module HawkAttributes =
let empty =
{ ``method`` = GET
uri = Uri("http://example.com/abc")
id = "empty"
ts = Instant.MinValue
nonce = "empty"
mac = "empty"
hash = None
ext = None
app = None
dlg = None }
let create meth uri =
{ empty with ``method`` = meth; uri = uri }
let method_ =
(fun x -> x.``method``),
fun v (x : HawkAttributes) -> { x with ``method`` = v }
let uri_ =
(fun x -> x.uri),
fun v (x : HawkAttributes) -> { x with uri = v }
let id_ =
(fun x -> x.id),
fun v (x : HawkAttributes) -> { x with id = v }
let ts_ =
(fun x -> x.ts),
fun v x -> { x with ts = v }
let nonce_ =
(fun x -> x.nonce),
fun v (x : HawkAttributes) -> { x with nonce = v }
let mac_ =
(fun x -> x.mac),
fun v (x : HawkAttributes) -> { x with mac = v }
let hash_ =
(fun x -> x.hash),
fun v (x : HawkAttributes) -> { x with hash = v }
let ext_ =
(fun x -> x.ext),
fun v (x : HawkAttributes) -> { x with ext = v }
let app_ =
(fun x -> x.app),
fun v (x : HawkAttributes) -> { x with app = v }
let dlg_ =
(fun x -> x.dlg),
fun v (x : HawkAttributes) -> { x with dlg = v }
type BewitAttributes =
{ ``method`` : HttpMethod
uri : Uri // host, port, resource
id : string
expiry : Instant
nonce : string
mac : string
ext : string option }
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module BewitAttributes =
let empty =
{ ``method`` = GET
uri = Uri("http://example.com/abc")
id = ""
expiry = Instant.MinValue
nonce = ""
mac = ""
ext = None }
let create meth uri =
{ empty with ``method`` = meth; uri = uri }
let id_ =
(fun x -> x.id),
fun v (x : BewitAttributes) -> { x with id = v }
let nonce_ =
(fun x -> x.nonce),
fun v (x : BewitAttributes) -> { x with nonce = v }
let mac_ =
(fun x -> x.mac),
fun v (x : BewitAttributes) -> { x with mac = v }
let expiry_ =
(fun x -> x.expiry),
fun v (x : BewitAttributes) -> { x with expiry = v }
let ext_ =
(fun x -> x.ext),
fun v (x : BewitAttributes) -> { x with ext = v }
/// A structure that represents the fully calculated hawk request data structure
type FullAuth =
{ credentials : Credentials
/// The # seconds since unix epoch
timestamp : Instant
nonce : string
``method`` : HttpMethod
resource : string
host : string
port : Port
/// The hash is optional in the method that only calculate MACs, but when it
/// is used from the Client#header function, it's a required field.
hash : string option
ext : string option
app : string option
dlg : string option }
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module FullAuth =
let credentials_ =
(fun x -> x.credentials),
fun v (x : FullAuth) -> { x with credentials = v }
let timestamp_ =
(fun x -> x.timestamp),
fun v (x : FullAuth) -> { x with timestamp = v }
let nonce_ =
(fun x -> x.nonce),
fun v (x : FullAuth) -> { x with nonce = v }
let method_ =
(fun x -> x.``method``),
fun v (x : FullAuth) -> { x with ``method`` = v }
let resource_ =
(fun x -> x.resource),
fun v (x : FullAuth) -> { x with resource = v }
let host_ =
(fun x -> x.host),
fun v (x : FullAuth) -> { x with host = v }
let port_ =
(fun x -> x.port),
fun v (x : FullAuth) -> { x with port = v }
let hash_ =
(fun x -> x.hash),
fun v (x : FullAuth) -> { x with hash = v }
let ext_ =
(fun x -> x.ext),
fun v (x : FullAuth) -> { x with ext = v }
let app_ =
(fun x -> x.app),
fun v (x : FullAuth) -> { x with app = v }
let dlg_ =
(fun x -> x.dlg),
fun v (x : FullAuth) -> { x with dlg = v }
let ofHawkAttrs creds (host : string option) (port : Port option) (a : HawkAttributes) =
{ credentials = creds
timestamp = a.ts
nonce = a.nonce
``method`` = a.``method``
resource = a.uri.PathAndQuery
host = host |> Option.defaultValue a.uri.Host
port = port |> Option.orDefault (fun () -> uint16 a.uri.Port)
hash = a.hash
ext = a.ext
app = a.app
dlg = a.dlg }
let ofBewitAttrs creds (host : string option) (port : Port option) (a : BewitAttributes) =
{ credentials = creds
timestamp = a.expiry
nonce = a.nonce
``method`` = a.``method``
resource = a.uri.PathAndQuery
host = host |> Option.defaultValue a.uri.Host
port = port |> Option.orDefault (fun () -> uint16 a.uri.Port)
hash = None
ext = a.ext
app = None
dlg = None }
type UserId = string
/// The errors that may arise from trying to fetch credentials.
type CredsError =
| CredentialsNotFound
| UnknownAlgo of algo:Algo
| Other of string
/// A credential repository maps a UserId to a
/// `Choice<Credentials * 'a, CredsError>`. The rest of the library
/// takes care of validating these returned credentials, or yielding
/// the correct error in response.
type UserRepo<'a> = UserId -> Async<Choice<Credentials * 'a, CredsError>>
type NonceError =
| AlreadySeen
| Other of string
/// Authentication settings
type Settings<'user> =
{ /// The clock to use for getting the time.
clock: IClock
/// A logger - useful to use for finding input for the authentication
/// verification
logger: Logger
/// Number of seconds of permitted clock skew for incoming
/// timestamps. Defaults to 60 seconds. Provides a +/- skew which
/// means actual allowed window is double the number of seconds.
allowedClockSkew: Duration
/// Local clock time offset which can be both +/-. Defaults to 0 s.
localClockOffset: Duration
/// An extra nonce validator - allows you to keep track of the last,
/// say, 1000 nonces, to be safe against replay attacks. By default
/// saves in memory, so if you want to run across load balancers, then
/// replace this validator with something that stores data shared
/// between the nodes.
nonceValidator: string * Instant -> Choice<unit, NonceError>
/// Credentials repository to fetch credentials based on UserId
/// from the Hawk authorisation header.
userRepo: UserRepo<'user>
/// Enable this flag if your proxy sets the headers "x-forwarded-host". If
/// your proxy doesn't, a malicious client can spoof the headers for any
/// domain. It is up implementations like Logibit.Hawk.Suave to read and
/// use this setting.
useProxyHost: bool
/// Enable this flag if your proxy sets the headers "x-forwarded-port". If
/// your proxy doesn't, a malicious client can spoof the headers for any
/// domain. It is up implementations like Logibit.Hawk.Suave to read and
/// use this setting.
useProxyPort: bool }
module Settings =
open System.Collections.Generic
open System.Collections.Concurrent
/// This nonce validator lets all nonces through, boo yah!
let nonceValidatorNoop = fun _ -> Choice1Of2 ()
let private tsem = obj ()
let private runTimeouts (timeouts: Queue<_>) (cache: ConcurrentDictionary<string, Instant>) (now: Instant) =
/// Iterate until the head is due later than now.
let rec iter () =
if timeouts.Count = 0 then () else
let struct (nonce, timeout) = timeouts.Peek()
if timeout > now then () else
ignore (timeouts.Dequeue())
ignore (cache.TryRemove nonce)
iter ()
lock tsem iter
let nonceValidatorMem (clock: IClock) (keepFor: Duration) =
// We will use a queue, because it's O(1) on dequeue/peek and since the duration is a constant for the
// life time of this validator, we know the head is due closest in time.
let timeouts = Queue<_>()
// The cache stores the nonces and when they were added
let cache = ConcurrentDictionary<_, _>()
fun (nonce: string, ts: Instant) ->
let now = clock.GetCurrentInstant()
do runTimeouts timeouts cache now
// https://docs.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentdictionary-2.tryadd?view=netframework-4.7.1
// returns true if the nonce was successfully added
if cache.TryAdd (nonce, now) then
do timeouts.Enqueue (struct (nonce, now + keepFor))
Choice1Of2 ()
else
Choice2Of2 AlreadySeen
/// Create a new empty settings; beware that it will always return that
/// the credentials for the id given were not found.
let empty<'a>(): Settings<'a> =
let clock = SystemClock.Instance
{ clock = clock
logger = Targets.create Warn [| "Logibit"; "Hawk" |]
allowedClockSkew = Duration.FromSeconds 60L
localClockOffset = Duration.Zero
nonceValidator = nonceValidatorMem clock (Duration.FromMinutes 20.)
userRepo = fun _ -> async.Return (Choice2Of2 CredentialsNotFound)
useProxyHost = false
useProxyPort = false }
/// The pieces of the request that the `authenticateBewit` method cares about.
type QueryRequest =
{ /// Required method for the request
``method``: HttpMethod
/// Required uri for the request
uri: Uri
/// Optional host name override (from uri) - useful if your web server
/// is behind a proxy and you can't easily feed a 'public' URI to the
/// `authenticate` function.
host: string option
/// Optional port number override (from uri) - useful if your web
/// server is behind a proxy and you can't easily feed the 'public'
/// URI to the `authenticate` function.
port: Port option }
type Bewit = string
/// Errors that can come from a validation pass of the Hawk Bewit header.
type BewitAuthError =
/// There was a problem when validating the credentials of the principal
| CredsError of error:CredsError
| Other of error:string
override x.ToString() =
match x with
| Other s -> s
| x -> sprintf "%A" x
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module BewitAuthError =
/// Use constructor as function
let ofCredsError = CredsError
/// A structure that represents the fully calculated hawk request data structure
type BewitFullAuth =
{ credentials : Credentials
``method`` : HttpMethod }
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module BewitFullAuth =
let credentials_ =
(fun x -> x.credentials),
fun v (x : BewitFullAuth) -> { x with credentials = v }
let method_ =
(fun x -> x.``method``),
fun v (x : BewitFullAuth) -> { x with ``method`` = v }
let ofAttributes (attributes : BewitAttributes) =
attributes