Skip to content

Commit d0be96d

Browse files
committed
[Server] logging on failure
1 parent bff163f commit d0be96d

File tree

6 files changed

+30
-7
lines changed

6 files changed

+30
-7
lines changed

.semver

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
:major: 0
33
:minor: 5
4-
:patch: 0
4+
:patch: 1
55
:special: ''
66
:metadata: ''

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@ This module adds some functions for composing Choice-s:
247247
- `(>>@) : m:Choice<'a, 'b> -> f:('b -> 'c) -> Choice<'a, 'c>` - map the
248248
second/error choice value to another one (and another type, possibly).
249249
- `map_2` - same as above
250+
- `(>>*) : m:Choice<'a, 'b> -> f:('b -> unit) -> Choice<'a, 'b>` - inject
251+
a side-effect if the choice is in the error case.
252+
- `inject_2` - same as above
250253

251254
#### Example
252255

src/logibit.hawk.sln

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,4 @@ Global
4040
EndGlobalSection
4141
GlobalSection(NestedProjects) = preSolution
4242
EndGlobalSection
43-
GlobalSection(MonoDevelopProperties) = preSolution
44-
StartupItem = logibit.hawk.tests\logibit.hawk.tests.fsproj
45-
EndGlobalSection
4643
EndGlobal

src/logibit.hawk.suave.tests/Hawk.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ let making_request =
4747
Hawk.authenticate
4848
settings
4949
Hawk.bind_req
50-
(fun (attr, creds, user) -> OK (sprintf "authenticated user '%s'" user.real_name))
5150
(fun err -> UNAUTHORIZED (err.ToString()))
51+
(fun (attr, creds, user) -> OK (sprintf "authenticated user '%s'" user.real_name))
5252

5353
let req m data f_req f_resp =
5454
req_resp m "/" "" data None System.Net.DecompressionMethods.None f_req f_resp

src/logibit.hawk/Choice.fs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ let (>>=) m f =
1313
/// bind the successful value to f
1414
let bind f m = (m >>= f)
1515

16-
/// bind f to the successful value
16+
/// bind f to the error value
1717
let (>>!) m f =
1818
m
1919
|> function
@@ -42,6 +42,7 @@ let (>>-) m f =
4242
/// map success
4343
let map f o = (o >>- f)
4444

45+
/// map error
4546
let (>>@) m f =
4647
m
4748
|> function
@@ -50,3 +51,16 @@ let (>>@) m f =
5051

5152
/// map error
5253
let map_2 f o = o >>@ f
54+
55+
/// inject a side-effect beside the error
56+
let (>>*) m f =
57+
m
58+
|> function
59+
| Choice1Of2 x -> Choice1Of2 x
60+
| Choice2Of2 err ->
61+
f err
62+
Choice2Of2 err
63+
64+
/// inject a side-effect beside the error
65+
let inject_2 f o =
66+
o >>* f

src/logibit.hawk/Server.fs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,14 @@ module internal Impl =
278278
else
279279
Choice2Of2 (StaleTimestamp (given_ts, now, local_offset))
280280

281+
let log_failure (logger : Logger) timestamp (err : AuthError) =
282+
{ message = "authenticate failure"
283+
level = Info
284+
path = "logibit.hawk.Server.authenticate"
285+
data = [ "error", box err ] |> Map.ofList
286+
timestamp = timestamp }
287+
|> logger.Log
288+
281289
open Impl
282290

283291
/// Parse the header into key-value pairs in the form
@@ -305,7 +313,7 @@ let authenticate (s : Settings<'a>)
305313
let map_result (a, (b, c)) = a, b, c
306314

307315
(fun _ ->
308-
{ message = "authenticate"
316+
{ message = "authenticate start"
309317
level = Debug
310318
path = "logibit.hawk.Server.authenticate"
311319
data =
@@ -345,6 +353,7 @@ let authenticate (s : Settings<'a>)
345353
>>= validate_nonce s.nonce_validator
346354
>>= validate_timestamp now_with_offset s.allowed_clock_skew s.local_clock_offset
347355
>>- map_result
356+
>>* log_failure s.logger now
348357

349358
/// Authenticate payload hash - used when payload cannot be provided
350359
/// during authenticate()

0 commit comments

Comments
 (0)