summaryrefslogtreecommitdiff
path: root/school/node_modules/node-forge/tests/ws-webid.js
blob: 2ce58167638a1c4e882b2b36e192ae2e01a095a1 (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
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
var websocket_webid = function(host, port)
{
   var cat = 'ws';
   
   // TODO: get private key and certificate from local storage
   var privateKey =
   '-----BEGIN RSA PRIVATE KEY-----\r\n' +
'MIICXAIBAAKBgQCTmE8QLARsC57Z1OrOaLM6AS3fn70N7BvlU7z7yw8UpcJA/jOl\r\n' +
'NWu7eS9uzuckdVZ9FE0+x3DRvhtDI6K+18dcrUWtl5ADWXcs1QS3/7bGh7IybFyY\r\n' +
'0xT4VzLHcx6K4PNmfkjAQdyOz/EsuRqZ/ngIQ2tdHdkkzdQPECbTvFeG2wIDAQAB\r\n' +
'AoGAds3l7l2QHaxo7GzfqNBMXEdwto2tLxS8C6eQ+pkkBXm72HcF+Vj75AcTMD2p\r\n' +
'fwZYXQxHdV4yqRI+fZeku7uTA/3yBAAvNobbEN5jtHnq0ZTO/HO8HuHkKrCvD8c3\r\n' +
'0rJV6lNIuaARI9jZFf6HVchW3PMjKUpYhTs/sFhRxmsMpTkCQQDu8TPzXRmN1aw8\r\n' +
'tSI2Nyn8QUy9bw/12tlVaZIhrcVCiJl7JHGqSCowTqZlwmJIjd4W0zWjTvS7tEeO\r\n' +
'FaZHtP8lAkEAniGvm8S9zyzmhWRRIuU6EE2dtTbeAa5aSOK3nBaaNu2cHUxWle+J\r\n' +
'8lE4uequ9wqDG1AfOLobPmHReccmOI6N/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ci5jb20v\r\n' +
'aWRzLzE1MzQ1NzI2NDcjbWUwDQYJKoZIhvcNAQEFBQADgYEAPNm8albI4w6anynw\r\n' +
'XE/+00sCVks9BbgTcIpRqZPGqSuTRwoYW35isNLDqFqIUdVREMvFrEn3nOlOyKi0\r\n' +
'29G8JtLHFSXZsqf38Zou/bGAhtEH1AVEbM2bRtEnG8IW24jL8hiciz4htxmjnkHN\r\n' +
'JnQ8SQtUSWplGnz0vMFEOv6JbnI=\r\n' +
'-----END CERTIFICATE-----';

   // create websocket
   var ws = new WebSocket('ws://' + host + ':' + port);
   forge.log.debug(cat, 'Created WebSocket', ws);

   // create TLS client
   var success = false;
   var tls = forge.tls.createConnection(
   {
      server: false,
      caStore: [],
      sessionCache: {},
      // supported cipher suites in order of preference
      cipherSuites: [
         forge.tls.CipherSuites.TLS_RSA_WITH_AES_128_CBC_SHA,
         forge.tls.CipherSuites.TLS_RSA_WITH_AES_256_CBC_SHA],
      virtualHost: host,
      verify: function(c, verified, depth, certs)
      {
         forge.log.debug(cat, 
            'TLS Client verifying certificate w/CN: \"' +
            certs[0].subject.getField('CN').value + '\"');
         // accept any certificate from the server for this test
         return true;
      },
      connected: function(c)
      {
         forge.log.debug(cat, 'Client connected');
      },
      getCertificate: function(c, hint)
      {
         forge.log.debug(cat, 'Client using client-certificate');
         return certificate;
      },
      getPrivateKey: function(c, cert)
      {
         return privateKey;
      },
      tlsDataReady: function(c)
      {
         // send base64-encoded TLS data to server
         ws.send(forge.util.encode64(c.tlsData.getBytes()));
      },
      dataReady: function(c)
      {
         var response = c.data.getBytes();
         forge.log.debug(cat, 'Client received \"' + response + '\"');
         try
         {
            response = JSON.parse(response);
            success = response.success;
            
            // TODO: call window.authenticate on response json, just like
            // w/flash version
         }
         catch(ex) {}
         c.close();
      },
      closed: function(c)
      {
         forge.log.debug(cat, 'Client disconnected');
         if(success)
         {
            forge.log.debug(cat, 'PASS');
         }
         else
         {
            forge.log.debug(cat, 'FAIL');
         }
      },
      error: function(c, error)
      {
         forge.log.debug(cat, 'Client error: ' + error.message);
      }
   });

   ws.onopen = function(evt)
   {
      forge.log.debug(cat, 'WebSocket connected');

      // do TLS handshake
      tls.handshake();
   };
   ws.onmessage = function(evt)
   {
      // base64-decode data and process it
      tls.process(forge.util.decode64(evt.data));
   };
   ws.onclose = function(evt)
   {
      forge.log.debug(cat, 'WebSocket closed');
   };
};