Source file utility.ml

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
(* let print_bytes id bytes = *)
(*   let _ = print_endline (id ^ " length: " ^ (string_of_int (Bytes.length bytes))) in *)
(*   let print_n n = print_endline (id ^ string_of_int n ^ ": " ^ (string_of_int (Char.code (Bytes.get bytes n)))) in *)
(*   let _ = print_n 0 in *)
(*   let _ = print_n 1 in *)
(*   let _ = print_n 2 in *)
(*   let _ = print_n 3 in *)
(*   let _ = print_n 4 in *)
(*   () *)

let string_replace old_value new_value string =
  let regexp = Str.regexp_string old_value in
  let result = Str.global_replace regexp new_value string in
  result

let string_of_bool = function true -> "true" | false -> "false"

let adjust_host endpoint =
  let azure_end = ".documents.azure.com" in
  if String.length endpoint > String.length azure_end then
    let start =
      String.sub endpoint
        (String.length endpoint - String.length azure_end)
        (String.length azure_end)
    in
    if String.equal start azure_end then endpoint else endpoint ^ azure_end
  else endpoint ^ azure_end

let authorization_token_using_master_key verb resource_type resource_id date
    master_key =
  let key = Base64.decode_exn master_key in
  let text =
    String.lowercase_ascii verb
    ^ "\n"
    ^ String.lowercase_ascii resource_type
    ^ "\n" ^ resource_id ^ "\n"
    ^ String.lowercase_ascii date
    ^ "\n" ^ "" ^ "\n"
  in
  let body = Bytes.of_string text in
  let signature =
    let hash = Cryptokit.MAC.hmac_sha256 key in
    hash#add_substring body 0 (Bytes.length body);
    Base64.encode_exn hash#result
  in
  let master_token = "master" in
  let token_version = "1.0" in
  let result =
    Uri.pct_encode ~component:`Userinfo
      ("type=" ^ master_token ^ "&ver=" ^ token_version ^ "&sig=" ^ signature)
  in
  let result2 = string_replace "%3D" "%3d" result in
  let result3 = string_replace "%2B" "%2b" result2 in
  let result4 = string_replace "%2F" "%2f" result3 in
  (* let regexp_3d = Str.regexp_string "%3D" in *)
  (* let result2 = Str.global_replace regexp_3d "%3d" result in *)
  (* let regexp_2b = Str.regexp_string "%2B" in *)
  (* let result3 = Str.global_replace regexp_2b "%2b" result2 in *)
  result4
(*
C#:

string GenerateAuthToken(string verb, string resourceType, string resourceId, string date, string key, string keyType, string tokenVersion)
{
    var hmacSha256 = new System.Security.Cryptography.HMACSHA256 { Key = Convert.FromBase64String(key) };

    verb = verb ?? "";
    resourceType = resourceType ?? "";
    resourceId = resourceId ?? "";

    string payLoad = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}\n{1}\n{2}\n{3}\n{4}\n",
            verb.ToLowerInvariant(),
            resourceType.ToLowerInvariant(),
            resourceId,
            date.ToLowerInvariant(),
            ""
    );

    byte[] hashPayLoad = hmacSha256.ComputeHash(System.Text.Encoding.UTF8.GetBytes(payLoad));
    string signature = Convert.ToBase64String(hashPayLoad);

    return System.Web.HttpUtility.UrlEncode(String.Format(System.Globalization.CultureInfo.InvariantCulture, "type={0}&ver={1}&sig={2}",
        keyType,
        tokenVersion,
        signature));
}


payLoadBytes;
{byte[52]}
        [0]: 103
        [1]: 101
        [2]: 116
        [3]: 10
        [4]: 100
        [5]: 98
        [6]: 115
        [7]: 10
        [8]: 100
        [9]: 98
        [10]: 115
        [11]: 47
        [12]: 84
        [13]: 111
        [14]: 68
        [15]: 111
        [16]: 76
        [17]: 105
        [18]: 115
        [19]: 116
        [20]: 10
        [21]: 116
        [22]: 104
        [23]: 117
        [24]: 44
        [25]: 32
        [26]: 50
        [27]: 55
        [28]: 32
        [29]: 97
        [30]: 112
        [31]: 114
        [32]: 32
        [33]: 50
        [34]: 48
        [35]: 49
        [36]: 55
        [37]: 32
        [38]: 48
        [39]: 48
        [40]: 58
        [41]: 53
        [42]: 49
        [43]: 58
        [44]: 49
        [45]: 50
        [46]: 32
        [47]: 103
        [48]: 109
        [49]: 116
        [50]: 10
        [51]: 10

> payLoad;
"get\ndbs\ndbs/ToDoList\nthu, 27 apr 2017 00:51:12 gmt\n\n"
> hashPayLoad;
{byte[32]}
        [0]: 115
        [1]: 79
        [2]: 79
        [3]: 17
        [4]: 82
        [5]: 107
        [6]: 130
        [7]: 157
        [8]: 174
        [9]: 65
        [10]: 25
        [11]: 43
        [12]: 247
        [13]: 126
        [14]: 36
        [15]: 21
        [16]: 180
        [17]: 234
        [18]: 132
        [19]: 28
        [20]: 156
        [21]: 237
        [22]: 53
        [23]: 107
        [24]: 220
        [25]: 225
        [26]: 242
        [27]: 170
        [28]: 91
        [29]: 190
        [30]: 115
        [31]: 231
> signature;
"c09PEVJrgp2uQRkr934kFbTqhByc7TVr3OHyqlu+c+c="
> String.Format (System.Globalization.CultureInfo.InvariantCulture, "type={0}&ver={1}&sig={2}",
            keyType,
            tokenVersion,
            signature);
Identifier expected, `string' is a keyword
> s;
"type=master&ver=1.0&sig=c09PEVJrgp2uQRkr934kFbTqhByc7TVr3OHyqlu+c+c="
>

Node.js:

var crypto = require("crypto");

function getAuthorizationTokenUsingMasterKey(verb, resourceType, resourceId, date, masterKey) {
    var key = new Buffer(masterKey, "base64");

    var text = (verb || "").toLowerCase() + "\n" +
               (resourceType || "").toLowerCase() + "\n" +
               (resourceId || "") + "\n" +
               date.toLowerCase() + "\n" +
               "" + "\n";

    var body = new Buffer(text, "utf8");
    var signature = crypto.createHmac("sha256", key).update(body).digest("base64");

    var MasterToken = "master";

    var TokenVersion = "1.0";

    return encodeURIComponent("type=" + MasterToken + "&ver=" + TokenVersion + "&sig=" + signature);
}
*)