1 console.log("Loading navigator.gssweb.js - #2");
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();
20 this.xhr.open("POST", this.serverPath, true);
21 this.xhr.setRequestHeader(
23 'application/x-www-form-urlencoded'
25 this.xhr.onreadystatechange = this.recvTokenFromServer.bind(this);
27 this.gss = new navigator.gss_eap({
29 error: this.handleGSSError.bind(this)
32 /* What to invoke when the underlying GSS library
35 GSSWeb.prototype.handleGSSError =
36 function (major, minor, errMsg, appTag) {
37 this.error(errMsg, appTag);
40 /* The basic authenticate function.
41 * Takes no arguments, as all should be supplied
44 GSSWeb.prototype.authenticate = function () {
46 // Ensure that the callbacks exist well
47 if ( "function" != typeof(this.error) )
49 // I can't even call my error function! All that can be done
51 throw("Error function not supplied to navigator.gssweb object!");
53 if ( "function" != typeof(this.success) )
55 // OK, so we have an error function; use it.
57 "Success function not supplied to navigator.gssweb object!"
60 if ( ! this.presentString(this.serverPath) )
62 this.error("Server path not supplied to navigator.gssweb object!");
67 this.nonce = navigator.generateNonce();
69 // Start off the cascade by getting the
70 // GSS name of the server
71 this.authGetServerName();
74 GSSWeb.prototype.authGetServerName = function () {
75 this.gss.import_name({
76 name: "HTTP@" + window.location.host,
77 success: this.authReceiveServerName.bind(this)
80 GSSWeb.prototype.authReceiveServerName = function (data, appTag) {
81 this.serverName = data.gss_name;
83 /* Either move on to acquire_cred because we have been
84 * supplied a credential, or move on to init_sec_context
87 if ( this.presentString(this.credential) )
88 this.authGetClientName();
90 this.authInitSecContext();
93 GSSWeb.prototype.authGetClientName = function () {
94 this.gss.import_name({
95 name: this.credential,
96 success: this.authReceiveClientName.bind(this)
99 GSSWeb.prototype.authReceiveClientName = function (data, appTag) {
100 this.clientName = data.gss_name;
102 // Next up: Get the local credential
103 this.authAcquireCred();
106 GSSWeb.prototype.authAcquireCred = function () {
107 this.gss.acquire_cred({
108 desired_name: this.clientName,
110 success: this.authReceiveClientCred.bind(this)
113 GSSWeb.prototype.authReceiveClientCred =
114 function (cred, actual_mechs, lifetime_rec) {
115 this.clientCred = cred;
117 this.authInitSecContext();
120 GSSWeb.prototype.authInitSecContext = function () {
122 target_name: this.serverName,
123 success: this.sendTokenToServer.bind(this)
126 if ("" != this.clientCred) {
127 params.claimant_cred_handle = this.clientCred;
129 if ("" != this.serverToken) {
130 params.input_token = this.serverToken;
132 if ("" != this.context) {
133 params.context_handle = this.context;
136 this.gss.init_sec_context(params);
138 GSSWeb.prototype.sendTokenToServer =
145 this.clientToken = output_token;
148 var data = "nonce=" + this.nonce +
149 "&token=" + encodeURIComponent(this.clientToken);
152 GSSWeb.prototype.recvTokenFromServer = function () {
153 // Only care when we're ready
154 if (this.xhr.readyState != 4) {
158 var serverResponse = JSON.parse(this.xhr.responseText);
159 this.serverToken = serverResponse.gssweb.token;
160 switch (this.xhr.status) {
163 var decoded = window.atob(serverResponse.application.data);
164 this.authenticationState = true;
167 serverResponse.application["content-type"],
173 this.authInitSecContext();
176 // We have some server-reported error
178 window.location.host +
179 " reported an error; aborting",
183 // Destroy the GSS context.
184 this.context = undefined;
189 /*************************************
191 *************************************/
192 // return true if the variable is a non-empty string
193 GSSWeb.prototype.presentString = function(str)
196 "string" == typeof(str) &&
204 navigator.gssweb = GSSWeb;