summaryrefslogtreecommitdiff
path: root/src/FundLab.Api/Authentication.fs
blob: e98d71e1c54c1fdde26270ef6d9fed3aafcf6efa (plain)
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
namespace FundLab.Api

open System
open System.Security.Cryptography
open System.Text

type AuthDecision =
    | Authorized
    | Unauthorized

module Authentication =
    let authorize (expectedToken: string) (authorizationHeader: string) : AuthDecision =
        if String.IsNullOrWhiteSpace expectedToken
           || String.IsNullOrWhiteSpace authorizationHeader
           || not (authorizationHeader.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase)) then
            AuthDecision.Unauthorized
        else
            let presentedToken = authorizationHeader.Substring("Bearer ".Length)
            let expectedBytes = Encoding.UTF8.GetBytes expectedToken
            let presentedBytes = Encoding.UTF8.GetBytes presentedToken

            if CryptographicOperations.FixedTimeEquals(expectedBytes, presentedBytes) then
                AuthDecision.Authorized
            else
                AuthDecision.Unauthorized