summaryrefslogtreecommitdiff
path: root/school/node_modules/node-forge/tests/loginDemo.js
blob: 859e1f0cdee535caea123e818b810acefd76b630 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
 * Forge Web ID Tests
 *
 * @author Dave Longley
 *
 * Copyright (c) 2010 Digital Bazaar, Inc. All rights reserved.
 */
(function($)
{
   // load flash socket pool
   window.forge.socketPool = {};
   window.forge.socketPool.ready = function()
   {
      // init page
      init($);
   };
   swfobject.embedSWF(
      'forge/SocketPool.swf', 'socketPool', '0', '0', '9.0.0',
      false, {}, {allowscriptaccess: 'always'}, {});
})(jQuery);

var init = function($)
{
   // logging category
   var cat = 'forge.tests.loginDemo';
   
   // local alias
   var forge = window.forge;
   try
   {
      // get query variables
      var query = forge.util.getQueryVariables();
      var domain = query.domain || '';
      var auth = query.auth || '';
      var redirect = query.redirect || '';
      var pport = query.pport || 843;
      redirect = 'https://' + domain + '/' + redirect;
      if(domain)
      {
         $('#domain').html('`' + domain + '`');
      } 
      
      // for chosen webid
      var chosen = null;
      
      // init forge xhr
      forge.xhr.init({
         flashId: 'socketPool',
         msie: $.browser.msie,
         url: 'https://' + domain,
         policyPort: pport,
         connections: 1,
         caCerts: [],
         verify: function(c, verified, depth, certs)
         {
            // don't care about cert verification for test
            return true;
         },
         getCertificate: function(c)
         {
            forge.log.debug(cat, 'using cert', chosen.certificate);
            return chosen.certificate;
         },
         getPrivateKey: function(c)
         {
            //forge.log.debug(cat, 'using private key', chosen.privateKey);
            return chosen.privateKey;
         }
      });
      
      // get flash API
      var flashApi = document.getElementById('socketPool');
      
      // get web ids collection
      var webids = forge.util.getItem(
         flashApi, 'forge.test.webid', 'webids');
      webids = webids || {};
      
      var id = 0;
      var list = $('<ul/>');
      for(var key in webids)
      {
         (function(webid)
         {
            var cert = forge.pki.certificateFromPem(webid.certificate);
            var item = $('<li/>');
            var button = $('<button>');
            button.attr('id', '' + (webid + id++));
            button.html('Choose');
            button.click(function()
            {
               button.attr('disabled', 'disabled');
               
               // set chosen webid
               chosen = webid;
               
               // do webid call
               $.ajax(
               {
                  type: 'GET',
                  url: '/' + auth,
                  success: function(data, textStatus, xhr)
                  {
                     if(data !== '')
                     {
                        forge.log.debug(cat, 'authentication completed');
                        forge.log.debug(cat, data);
                        window.name = data;
                     }
                     else
                     {
                        forge.log.debug(cat, 'authentication failed');
                        window.name = '';
                     }
                     window.location = redirect;
                  },
                  error: function(xhr, textStatus, errorThrown)
                  {
                     forge.log.error(cat, 'authentication failed');
                  },
                  xhr: forge.xhr.create
               });
            });
            item.append(button);
            item.append(' ' + key + '<br/>');
            
            // display certificate attributes
            var attr;
            for(var n = 0; n < cert.subject.attributes.length; ++n)
            {
               attr = cert.subject.attributes[n];
               item.append(attr.name + ': ' + attr.value + '<br/>');
            }
            
            list.append(item);
         })(webids[key]);
      }
      if(list.html() === '<ul/>')
      {
         list.append('None');
      }
      
      $('#webids').append(list);
   }
   catch(ex)
   {
      forge.log.error(cat, ex);
   }
};