Remove redundant definition of gss_imp_name_type
[gssweb.git] / navigator.gssweb.js
1 console.log("Loading navigator.gssweb.js - #2");
2
3
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) {
10       console.warn(err);
11     };
12     this.success = config.success;
13
14     this.version = "0.0.1";
15     this.authenticationState = false;
16     this.context = "";
17     this.serverToken = "";
18     this.clientCred = "";
19     this.xhr = new XMLHttpRequest();
20     this.xhr.onreadystatechange = this.recvTokenFromServer.bind(this);
21
22     this.gss = new navigator.gss_eap({
23       appTag: this.appTag,
24       error: this.handleGSSError.bind(this)
25     });
26   }
27   /* What to invoke when the underlying GSS library 
28    * has a problem.
29    */
30   GSSWeb.prototype.handleGSSError = 
31     function (major, minor, errMsg, appTag) {
32       this.error(errMsg, appTag);
33     };
34
35   /* The basic authenticate function.
36    * Takes no arguments, as all should be supplied
37    * on the base object.
38    */
39   GSSWeb.prototype.authenticate = function () {
40     /* Error checking */
41     // Ensure that the callbacks exist well
42     if ( "function" != typeof(this.error) )
43     {
44       // I can't even call my error function!  All that can be done
45       // is throw an error.
46       throw("Error function not supplied to navigator.gssweb object!");
47     }
48     if ( "function" != typeof(this.success) )
49     {
50       // OK, so we have an error function; use it.
51       this.error(
52         "Success function not supplied to navigator.gssweb object!"
53       );
54     }
55     if ( ! this.presentString(this.serverPath) )
56     {
57       this.error("Server path not supplied to navigator.gssweb object!");
58     }
59
60     
61     /* Setup */
62     this.nonce = navigator.generateNonce();
63
64     // Start off the cascade by getting the
65     // GSS name of the server
66     this.authGetServerName();
67   };
68
69   GSSWeb.prototype.authGetServerName = function () {
70     this.gss.import_name({
71       name: "HTTP@" + window.location.host,
72       success: this.authReceiveServerName.bind(this)
73     });
74   };
75   GSSWeb.prototype.authReceiveServerName = function (data, appTag) {
76     this.serverName = data.gss_name;
77
78     /* Either move on to acquire_cred because we have been
79     * supplied a credential, or move on to init_sec_context
80     * when we have not.
81     */
82     if ( this.presentString(this.credential) )
83       this.authGetClientName();
84     else
85       this.authInitSecContext();
86   };
87
88   GSSWeb.prototype.authGetClientName = function () {
89     this.gss.import_name({
90       name: this.credential,
91       success: this.authReceiveClientName.bind(this)
92     });
93   };
94   GSSWeb.prototype.authReceiveClientName = function (data, appTag) {
95     this.clientName = data.gss_name;
96
97     // Next up: Get the local credential
98     this.authAcquireCred();
99   };
100
101   GSSWeb.prototype.authAcquireCred = function () {
102     this.gss.acquire_cred({
103       desired_name: this.clientName,
104       cred_usage: 1,
105       success: this.authReceiveClientCred.bind(this)
106     });
107   };
108   GSSWeb.prototype.authReceiveClientCred =
109     function (cred, actual_mechs, lifetime_rec) {
110       this.clientCred = cred;
111
112       this.authInitSecContext();
113     };
114
115   GSSWeb.prototype.authInitSecContext = function () {
116     var params = {
117       target_name: this.serverName,
118       success: this.sendTokenToServer.bind(this)
119     };
120
121     if ("" != this.clientCred) {
122       params.cred_handle = this.clientCred;
123     }
124     if ("" != this.serverToken) {
125       params.input_token = this.serverToken;
126     }
127     if ("" != this.context) {
128       params.context_handle = this.context;
129     }
130
131     this.gss.init_sec_context(params);
132   };
133
134   GSSWeb.prototype.sendTokenToServer = 
135     function (data, 
136               app_tag) {
137     this.clientToken = data.output_token;
138     this.context = data.context_handle;
139
140     var msg = "nonce=" + this.nonce +
141                "&token=" + encodeURIComponent(this.clientToken);
142     this.xhr.open("POST", this.serverPath, true);
143     this.xhr.setRequestHeader(
144       'Content-Type', 
145       'application/x-www-form-urlencoded'
146     );
147     this.xhr.send(msg);
148   };
149
150   GSSWeb.prototype.recvTokenFromServer = function () {
151     // Only care when we're ready
152     if (this.xhr.readyState != 4) {
153       return;
154     }
155
156     var serverResponse = JSON.parse(this.xhr.responseText);
157     this.serverToken = serverResponse.gssweb.token;
158     switch (this.xhr.status) {
159       case 200:
160         // Finished!
161         var decoded = window.atob(serverResponse.application.data);
162         this.authenticationState = true;
163         this.success(
164           decoded,
165           serverResponse.application["content-type"],
166           this.appTag
167         );
168         break;
169       case 401:
170         // Continue needed
171         this.authInitSecContext();
172         break;
173       default:
174         // We have some server-reported error
175         this.error(
176           window.location.host + 
177           " reported an error; aborting",
178           this.appTag
179         );
180
181         // Destroy the GSS context.
182         this.context = undefined;
183         return;
184     }
185   };
186   
187   /*************************************
188    * Utility methods
189    *************************************/
190   // return true if the variable is a non-empty string
191   GSSWeb.prototype.presentString = function(str)
192   {
193     return(
194       "string" == typeof(str) &&
195       "" != str
196     );
197   }
198   
199   return GSSWeb;
200 })();
201
202 navigator.gssweb = GSSWeb;