forked from Azure-Samples/edgecast-cdn-token-fsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edgecast.fsx
63 lines (55 loc) · 1.94 KB
/
edgecast.fsx
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
#r "nuget: BouncyCastle.NetCore, 1.8.8"
#r "nuget: CommandLineParser.FSharp, 2.8.0"
#load "edgecastlib/EdgecastCrypto.fs"
open System
open EdgecastCrypto
open CommandLine
// --direction Encrypt --key yyy --ipaddress 128.0.0.1 --urls http://fee http://fee2
type MyCommandLineOptions = {
[<Option(HelpText = "The key.", Required = true)>] Key : string;
[<Option(HelpText = "'Encrypt' or 'Decrypt'.", Required = true)>] Direction : Direction;
[<Option(HelpText = "Client IP address.")>] IPAddress : string option;
[<Option(HelpText = "Token to decrypt.")>] Token : string;
[<Option("urls", HelpText = "Allowed URLs.")>] AllowedUrls : seq<string>;
}
and Direction =
| Encrypt = 1
| Decrypt = 2
let inspect msg a =
printfn "%s: %A" msg a
a
let maybe func optionalArg =
match optionalArg with
| Some(arg) -> func arg
| None -> id
let forall (func : 'a -> 't -> 't) (l : 'a seq) (t: 't) : 't =
match (Seq.isEmpty l) with
| true -> t
| false -> Seq.fold (fun t a -> func a t) t l
let argv = fsi.CommandLineArgs |> Array.tail
let args = match Parser.Default.ParseArguments<MyCommandLineOptions>( argv ) with
| :? Parsed<MyCommandLineOptions> as parsed -> parsed.Value
| :? NotParsed<MyCommandLineOptions> as notParsed -> failwith "Could not parse"
| _ -> failwith "Could not parse"
match args.Direction with
| Direction.Encrypt ->
createTokenValidFor (TimeSpan.FromDays(365.0))
|> maybe withClientIPAddress args.IPAddress
|> forall addAllowedUrl args.AllowedUrls
|> encrypt args.Key
|> printfn "%s"
|> ignore
| Direction.Decrypt ->
args.Token
|> inspect "the token"
|> decrypt args.Key
|> printfn "%A"
| _ -> failwith "Unknown operation"
// createTokenValidFor (TimeSpan.FromDays(365.0))
// |> withClientIPAddress address
// |> inspect "token"
// |> encrypt key
// |> inspect "encrypted"
// |> decrypt key
// |> inspect "decrypted again"
// |> ignore