2 * Copyright (c) 2014, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
35 console.log('Loading navigator.gss.js - #1');
37 navigator.generateNonce = function() {
38 // TODO: Make sure that we don't have a collision!
39 // Random integer in the range [0..(2^32-1)]
40 return Math.floor(Math.random() * ( 4294967295 )) ;
45 /* This file gets injected into the web page verbatim */
47 var GSS = (function ()
52 this.version = "0.0.1";
53 this.implemented_methods = ["gss_import_name", "gss_display_name", "gss_init_sec_context", "gss_acquire_cred"];
54 // MRW -- combine success/error callback hashes?
57 this.appTag = config.appTag || "";
58 this.default_error = config.error ||
59 function (major, minor, error, appTag) {
62 window.addEventListener(
64 this.dispatch_responses.bind(this)
67 GSS.prototype.dispatch_responses = function (event)
75 /* This message is destined for us only if all the following apply:
76 * - The data.method_name is one of the methods implemented by this
78 * - data.return_values exists or data.error_mssage exists
79 * - data.cookies exists
80 * - One of my callbacks matches the nonce in
81 * data.cookies.navigator_gss_tag
82 * - One of my methods matches the nonce in
83 * data.cookies.navigator_gss_tag and that method matches
86 method = event.data.method;
88 ( -1 == this.implemented_methods.indexOf(method) ) ||
89 ( ("undefined" == typeof (event.data.return_values) ) &&
90 ("undefined" == typeof (event.data.error_message) ) ) ||
91 ("undefined" == typeof (event.data.cookies)))
96 nonce = event.data.cookies.navigator_gss_tag;
97 event.data.cookies.navigator_gss_tag = undefined;
98 callback = this.callbacks[nonce];
99 if ("undefined" == typeof (callback)) {
103 // We now know that this message is for us!
104 this.callbacks[nonce] = undefined;
105 app_tag = event.data.cookies.app_tag;
106 error = this.errors[nonce] || this.default_error;
108 if ("undefined" != typeof(event.data.error_message) )
110 error(-1, -1, "Error parsing message: " + event.data.error_message, app_tag);
112 else if (this.gss_error(event.data.return_values.major_status))
114 var errMsg = "Error during " + method + ": " +
115 "Major status message: " +
116 event.data.return_values.errors.major_status_message +
117 "; Minor status message: " +
118 event.data.return_values.errors.minor_status_message;
120 event.data.return_values.major_status,
121 event.data.return_values.minor_status,
125 callback(event.data.return_values, app_tag);
130 GSS.prototype.init_sec_context = function (params)
133 // internal variables
136 // Required parameters
137 var target_name = params.target_name;
138 var callback = params.success || this.success;
140 // Optional & defaulted parameters (some are defaulted at lower layer)
141 var context_handle = params.context_handle;
142 var cred_handle = params.cred_handle;
143 var mech_type = params.mech_type;
144 var req_flags = params.req_flags;
145 var time_req = params.time_req;
146 var input_token = params.input_token;
148 var error = params.error || this.default_error;
149 var app_tag = params.app_tag || this.appTag;
152 // Call an error if we don't have the required parameters.
155 if ( "undefined" == typeof(target_name) ||
156 "undefined" == typeof(callback) )
159 "init_sec_context called missing either target_name or success callback"
165 nonce = navigator.generateNonce();
167 /* Main processing */
168 // Save our callback, method name, and error function
169 this.callbacks[nonce] = callback;
170 this.errors[nonce] = error;
172 // Now pass the request on to the C code
174 "method":"gss_init_sec_context",
177 "target_name": target_name,
178 "context_handle": context_handle,
179 "cred_handle": cred_handle,
180 "mech_type": mech_type,
181 "req_flags": req_flags,
182 "time_req": time_req,
183 "input_token": input_token
188 "navigator_gss_tag": nonce,
195 GSS.prototype.display_name = function(params)
198 // required parameters
199 var input_name = params.input_name;
200 var callback = params.success;
202 if ( "undefined" == typeof(name) ||
203 "undefined" == typeof(callback) )
206 "import_name called missing either name or success callback"
211 var error = params.error || this.default_error;
212 var app_tag = params.app_tag || this.appTag;
215 nonce = navigator.generateNonce();
218 /* Main processing */
219 // Save our callback, method name, and error function
220 this.callbacks[nonce] = callback;
221 this.errors[nonce] = error;
223 // Now pass the request on to the C code
225 "method":"gss_display_name",
228 "input_name": input_name,
232 "navigator_gss_tag": nonce,
239 GSS.prototype.import_name = function (params)
242 // internal variables
245 // Required parameters
246 var name = params.name;
247 var callback = params.success;
249 // Optional & defaulted parameters
250 var name_type = params.name_type || "{1 2 840 113554 1 2 1 4 }";
251 var error = params.error || this.default_error;
252 var app_tag = params.app_tag || this.appTag;
256 // Call an error if we don't have the required parameters.
259 if ( "undefined" == typeof(name) ||
260 "undefined" == typeof(callback) )
263 "import_name called missing either name or success callback"
270 nonce = navigator.generateNonce();
273 /* Main processing */
274 // Save our callback, method name, and error function
275 this.callbacks[nonce] = callback;
276 this.errors[nonce] = error;
278 // Now pass the request on to the C code
280 "method":"gss_import_name",
284 "input_name_type": name_type
288 "navigator_gss_tag": nonce,
295 GSS.prototype.gss_error = function (major)
301 callingMask = 255 << 24;
302 routineMask = 255 << 16;
303 mask = callingMask | routineMask;
305 return (0 != (major & mask));