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.onreadystatechange = this.recvTokenFromServer.bind(this);
22 this.gss = new navigator.gss_eap({
24 error: this.handleGSSError.bind(this)
27 /* What to invoke when the underlying GSS library
30 GSSWeb.prototype.handleGSSError =
31 function (major, minor, errMsg, appTag) {
32 this.error(errMsg, appTag);
35 /* The basic authenticate function.
36 * Takes no arguments, as all should be supplied
39 GSSWeb.prototype.authenticate = function () {
41 // Ensure that the callbacks exist well
42 if ( "function" != typeof(this.error) )
44 // I can't even call my error function! All that can be done
46 throw("Error function not supplied to navigator.gssweb object!");
48 if ( "function" != typeof(this.success) )
50 // OK, so we have an error function; use it.
52 "Success function not supplied to navigator.gssweb object!"
55 if ( ! this.presentString(this.serverPath) )
57 this.error("Server path not supplied to navigator.gssweb object!");
62 this.nonce = navigator.generateNonce();
64 // Start off the cascade by getting the
65 // GSS name of the server
66 this.authGetServerName();
69 GSSWeb.prototype.authGetServerName = function () {
70 this.gss.import_name({
71 name: "HTTP@" + window.location.host,
72 success: this.authReceiveServerName.bind(this)
75 GSSWeb.prototype.authReceiveServerName = function (data, appTag) {
76 this.serverName = data.gss_name;
78 /* Either move on to acquire_cred because we have been
79 * supplied a credential, or move on to init_sec_context
82 if ( this.presentString(this.credential) )
83 this.authGetClientName();
85 this.authInitSecContext();
88 GSSWeb.prototype.authGetClientName = function () {
89 this.gss.import_name({
90 name: this.credential,
91 success: this.authReceiveClientName.bind(this)
94 GSSWeb.prototype.authReceiveClientName = function (data, appTag) {
95 this.clientName = data.gss_name;
97 // Next up: Get the local credential
98 this.authAcquireCred();
101 GSSWeb.prototype.authAcquireCred = function () {
102 this.gss.acquire_cred({
103 desired_name: this.clientName,
105 success: this.authReceiveClientCred.bind(this)
108 GSSWeb.prototype.authReceiveClientCred =
109 function (cred, actual_mechs, lifetime_rec) {
110 this.clientCred = cred;
112 this.authInitSecContext();
115 GSSWeb.prototype.authInitSecContext = function () {
117 target_name: this.serverName,
118 success: this.sendTokenToServer.bind(this)
121 if ("" != this.clientCred) {
122 params.cred_handle = this.clientCred;
124 if ("" != this.serverToken) {
125 params.input_token = this.serverToken;
127 if ("" != this.context) {
128 params.context_handle = this.context;
131 this.gss.init_sec_context(params);
134 GSSWeb.prototype.sendTokenToServer =
137 this.clientToken = data.output_token;
138 this.context = data.context_handle;
140 var msg = "nonce=" + this.nonce +
141 "&token=" + encodeURIComponent(this.clientToken);
142 this.xhr.open("POST", this.serverPath, true);
143 this.xhr.setRequestHeader(
145 'application/x-www-form-urlencoded'
150 GSSWeb.prototype.recvTokenFromServer = function () {
151 // Only care when we're ready
152 if (this.xhr.readyState != 4) {
156 var serverResponse = JSON.parse(this.xhr.responseText);
157 this.serverToken = serverResponse.gssweb.token;
158 switch (this.xhr.status) {
161 var decoded = window.atob(serverResponse.application.data);
162 this.authenticationState = true;
165 serverResponse.application["content-type"],
171 this.authInitSecContext();
174 // We have some server-reported error
176 window.location.host +
177 " reported an error; aborting",
181 // Destroy the GSS context.
182 this.context = undefined;
187 /*************************************
189 *************************************/
190 // return true if the variable is a non-empty string
191 GSSWeb.prototype.presentString = function(str)
194 "string" == typeof(str) &&
202 navigator.gssweb = GSSWeb;