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
|
Define_Tag('Include_URL_Digest',
-Required = 'url', -Type = 'string',
-Required = 'Username', -Type = 'string',
-Required = 'Password', -Type = 'string',
-Optional = 'POSTParams',
-Optional = 'SendMIMEHeaders', -Type = 'array'
);
Local('includeParams' = Params, 'splitURL', 'path', 'authRequestHeaders', 'wwwAuthenticate', 'responseHeaders' = Array, 'realm', 'nonce', 'algorithm', 'authResponse', 'response');
#includeParams->RemoveAll(-Username);
#includeParams->RemoveAll(-Password);
// Retrieve path portion of URL ( proto://example.com(/path) )
#splitURL = String_FindRegExp(#url, -Find = '^[a-z]+:\/\/[^:\/\\s]+(?::[^\/]*)?(.*)$', -ignoreCase);
Fail_If((#splitURL->Size !== 2), -1, #url+" is not a valid address");
#path = #splitURL->Get(2);
Include_URL(#url, -RetrieveMIMEHeaders='authRequestHeaders', -NoData);
#wwwAuthenticate = #authRequestHeaders->find('WWW-Authenticate');
Local('currentHeader', 'realmMatch', 'nonceMatch', 'algorithmMatch');
Loop(#wwwAuthenticate->Size);
#currentHeader = #wwwAuthenticate->Get(Loop_Count)->Value;
#realmMatch = String_FindRegExp(#currentHeader, -Find = 'realm="(.+?)"');
#nonceMatch = String_FindRegExp(#currentHeader, -Find = 'nonce="(.+?)"');
#algorithmMatch = String_FindRegExp(#currentHeader, -Find = 'algorithm="(.+?)"');
If (#realmMatch->Size == 2 && #nonceMatch->Size == 2);
#realm = #realmMatch->Get(2);
#nonce = #nonceMatch->Get(2);
If (#algorithmMatch-> Size == 2);
#algorithm = #algorithmMatch->Get(2);
/If;
Loop_Abort;
/If;
/Loop;
If (!(#realm && #nonce));
Return;
/If;
Fail_If(#algorithm && ((String_UpperCase: #algorithm) !== 'MD5'), -1, #algorithm+' is not a supported digest authentication algorithm ');
If(Local_Defined('SendMIMEHeaders'));
#responseHeaders->Merge(#SendMIMEHeaders);
/If;
#authResponse = Encrypt_MD5(Encrypt_MD5(#Username + ':' + #realm + ':' + #Password) + ':' + #nonce + ':' + Encrypt_MD5((Local_Defined('POSTParams') ? 'POST' | 'GET') + ':' + #path));
#responseHeaders->Insert('Authorization'='Digest username="'+#Username+'", realm="'+#realm+'", nonce="'+#nonce+'", uri="'+#path+'", response="'+#authResponse+(#algorithm ? '", algorithm="'+#algorithm+'"' | ''));
#includeParams->RemoveAll(-SendMIMEHeaders);
#includeParams->Insert(-SendMIMEHeaders = #responseHeaders);
return(\Include_URL->Run(-Params = #includeParams));
/Define_Tag;
|