1 console.log("Loading navigator.gssweb.js - #4");
4 var GSSWeb = (function () {
5 function GSSWeb(config) {
6 this.serverPath = config.serverPath;
7 this.credential = config.credential;
8 this.appTag = config.appTag || "GSSWeb-" + navigator.generateNonce();
9 this.error = config.error || function (err) {
12 this.success = config.success;
14 this.version = "0.0.1";
15 this.authenticationState = false;
17 this.serverToken = "";
19 this.xhr = new XMLHttpRequest();
21 this.gss = new navigator.gssEap({
23 error: this.handleGSSError.bind(this)
26 /* What to invoke when the underlying GSS library
29 GSSWeb.prototype.handleGSSError =
30 function (major, minor, errMsg, appTag) {
31 this.error(errMsg, appTag);
34 /* The basic authenticate function.
35 * Takes no arguments, as all should be supplied
38 GSSWeb.prototype.authenticate = function () {
40 // Ensure that the callbacks exist well
41 if ( "function" != typeof(this.error) )
43 // I can't even call my error function! All that can be done
45 throw("Error function not supplied to navigator.gssweb object!");
47 if ( "function" != typeof(this.success) )
49 // OK, so we have an error function; use it.
51 "Success function not supplied to navigator.gssweb object!"
54 if ( ! this.presentString(this.serverPath) )
56 this.error("Server path not supplied to navigator.gssweb object!");
61 this.nonce = navigator.generateNonce();
63 // Start off the cascade by getting the
64 // GSS name of the server
65 this.authGetServerName();
68 GSSWeb.prototype.authGetServerName = function () {
69 this.gss.import_name({
70 name: "HTTP@" + window.location.hostname,
71 success: this.authReceiveServerName.bind(this)
74 GSSWeb.prototype.authReceiveServerName = function (data, appTag) {
75 this.serverName = data.gss_name;
77 /* Either move on to acquire_cred because we have been
78 * supplied a credential, or move on to init_sec_context
81 if ( this.presentString(this.credential) )
82 this.authGetClientName();
84 this.authInitSecContext();
87 GSSWeb.prototype.authGetClientName = function () {
88 this.gss.import_name({
89 name: this.credential,
90 success: this.authReceiveClientName.bind(this)
93 GSSWeb.prototype.authReceiveClientName = function (data, appTag) {
94 this.clientName = data.gss_name;
96 // Next up: Get the local credential
97 this.authAcquireCred();
100 GSSWeb.prototype.authAcquireCred = function () {
101 this.gss.acquire_cred({
102 desired_name: this.clientName,
104 success: this.authReceiveClientCred.bind(this)
107 GSSWeb.prototype.authReceiveClientCred =
108 function (cred, actual_mechs, lifetime_rec) {
109 this.clientCred = cred;
111 this.authInitSecContext();
114 GSSWeb.prototype.authInitSecContext = function () {
116 target_name: this.serverName,
117 success: this.sendTokenToServer.bind(this)
120 if ("" != this.clientCred) {
121 params.cred_handle = this.clientCred;
123 if ("" != this.serverToken) {
124 params.input_token = this.serverToken;
126 if ("" != this.context) {
127 params.context_handle = this.context;
130 this.gss.init_sec_context(params);
133 GSSWeb.prototype.sendTokenToServer =
136 this.clientToken = data.output_token;
137 this.context = data.context_handle;
139 var msg = "nonce=" + this.nonce +
140 "&token=" + encodeURIComponent(this.clientToken);
142 this.xhr.open("POST", this.serverPath, true);
144 this.xhr.setRequestHeader(
146 'application/x-www-form-urlencoded'
148 this.xhr.onreadystatechange = this.recvTokenFromServer.bind(this);
153 GSSWeb.prototype.recvTokenFromServer = function () {
154 // Only care when we're ready
155 if (this.xhr.readyState != 4) {
159 switch (this.xhr.status) {
162 var serverResponse = JSON.parse(this.xhr.responseText);
163 var decoded = window.atob(serverResponse.application.data);
164 this.authenticationState = true;
167 serverResponse.application["content-type"],
173 var serverResponse = JSON.parse(this.xhr.responseText);
174 this.serverToken = serverResponse.gssweb.token;
175 this.authInitSecContext();
178 // We have some server-reported error
180 window.location.hostname +
181 " reported an error; aborting",
185 // Destroy the GSS context.
186 this.context = undefined;
191 /*************************************
193 *************************************/
194 // return true if the variable is a non-empty string
195 GSSWeb.prototype.presentString = function(str)
198 "string" == typeof(str) &&
206 navigator.gssweb = GSSWeb;