f66f8bcf91cf16ed1adbe1493b142d9eb6b15080
[citadel.git] / webcit / static / authmethods.js
1 /*
2  * Copyright 2010-2011, the Citadel Team
3  * Licensed under the GPL V3
4  *
5  * JavaScript functions which handle various authentication methods.
6  */
7
8
9
10 /****************** COMMON CODE ***********************/
11
12
13 /*
14  * Are we logged in right now?
15  */
16 function IsLoggedIn() {
17         if ($('is_logged_in').innerHTML == "yes") {
18                 return 1;
19         }
20         else {
21                 return 0;
22         }
23 }
24
25
26 /*
27  * Wrapper script to require logging in before completing an action
28  */
29 function GetLoggedInFirst(destination_url) {
30
31         /* If logged in already, go directly to the destination. */
32         if (IsLoggedIn()) {
33                 window.location = decodeURIComponent(destination_url);
34                 return;
35         }
36
37         p = 'push?url=' + destination_url;
38         new Ajax.Request(p, { method: 'get' } );
39
40         /* If not logged in, go modal and ask the user to log in first. */
41         new Ajax.Updater(
42                 'md-content',
43                 'do_template?template=get_logged_in',
44                 {
45                         method: 'get',
46                         onSuccess: function() {
47                                 toggleModal(1);
48                         }
49                 }
50         );
51 }
52
53
54 /*
55  * tab handler for the login box
56  */
57 function authtoggle(show_which_div) {
58         $('authbox_userpass').style.display = 'none';
59         $('authbox_newuser').style.display = 'none';
60         $('authbox_openid').style.display = 'none';
61         $('authbox_google').style.display = 'none';
62         $('authbox_yahoo').style.display = 'none';
63         $('authbox_aol').style.display = 'none';
64         $('authbox_success').style.display = 'none';
65         $(show_which_div).style.display = 'block';
66 }
67
68
69 /*
70  * Pop out a window for external auth methods
71  * (most of them don't handle inline auth very well)
72  */
73 function do_auth_popout(popout_url) {
74         window.open(popout_url, "authpopout", "status=1,toolbar=0,width=600,height=400");
75 }
76
77
78
79
80 /****************** USERNAME AND PASSWORD ***********************/
81
82 /*
83  * Attempt login with username/password, called from modal dialog
84  */
85 function ajax_try_username_and_password() {
86
87         $('login_errmsg').innerHTML = "";
88         authtoggle('authbox_success');
89         $('ajax_username_password_form').request({
90                 onSuccess: function(ctdlresult) {
91                         if (ctdlresult.responseText.substr(0,1) == '2') {
92                                 window.location = 'pop';
93                         }
94                         else {
95                                 $('login_errmsg').innerHTML = ctdlresult.responseText.substr(4) ;
96                         }
97                 }
98         });
99 }
100
101
102 /*
103  * The user pressed a key while in the username or password box.
104  * Is it the enter/return key?  Submit the form.
105  */
106 function username_and_password_onkeypress(e) {
107         if (window.event) {             /* IE */
108                 keynum = e.keyCode
109         }
110         else if (e.which) {             /* real browsers */
111                 keynum = e.which
112         }
113         if (keynum == 13) {             /* enter/return key */
114                 ajax_try_username_and_password();
115         }
116 }
117
118
119 /****************** REGISTER NEW USER ***********************/
120
121 /*
122  * Attempt to create a new local username/password, called from modal dialog
123  */
124 function ajax_try_newuser() {
125
126         $('login_errmsg').innerHTML = "";
127         $('ajax_newuser_form').request({
128                 onSuccess: function(ctdlresult) {
129                         if (ctdlresult.responseText.substr(0,1) == '2') {
130                                 authtoggle('authbox_success');
131                                 window.location = 'pop';
132                         }
133                         else {
134                                 $('login_errmsg').innerHTML = ctdlresult.responseText.substr(4) ;
135                         }
136                 }
137         });
138 }
139
140
141 /*
142  * The user pressed a key while in the newuser or newpassword box.
143  * Is it the enter/return key?  Submit the form.
144  */
145 function newuser_onkeypress(e) {
146         if (window.event) {             /* IE */
147                 keynum = e.keyCode;
148         }
149         else if (e.which) {             /* real browsers */
150                 keynum = e.which;
151         }
152         if (keynum == 13) {             /* enter/return key */
153                 ajax_try_newuser();
154         }
155 }
156
157
158
159
160 /****************** OPENID ***********************/
161
162 /*
163  * Attempt login with OpenID, called from modal dialog
164  */
165 function ajax_try_openid() {
166         $('login_errmsg').innerHTML = "";
167         openid_url = encodeURI($('ajax_openid_form').elements["openid_url"].value);
168         do_auth_popout("openid_login?openid_url=" + openid_url);
169 }
170
171
172 /*
173  * The user pressed a key while in the openid login box.
174  * Is it the enter/return key?  Submit the form.
175  */
176 function openid_onkeypress(e) {
177         if (window.event) {             /* IE */
178                 keynum = e.keyCode;
179         }
180         else if (e.which) {             /* real browsers */
181                 keynum = e.which;
182         }
183         if (keynum == 13) {             /* enter/return key */
184                 ajax_try_openid();
185                 return false;
186         }
187         return true;
188 }
189
190
191 /****************** GOOGLE ***********************/
192
193 /*
194  * Attempt login with Google, called from modal dialog
195  */
196 function ajax_try_google() {
197         $('login_errmsg').innerHTML = "";
198         openid_url = encodeURI("https://www.google.com/accounts/o8/id");
199         do_auth_popout("openid_login?openid_url=" + openid_url);
200 }
201
202
203 /****************** GOOGLE ***********************/
204
205 /*
206  * Attempt login with Yahoo, called from modal dialog
207  */
208 function ajax_try_yahoo() {
209         $('login_errmsg').innerHTML = "";
210         openid_url = encodeURI("http://yahoo.com");
211         do_auth_popout("openid_login?openid_url=" + openid_url);
212 }
213
214
215 /****************** AOL ***********************/
216
217 /*
218  * Attempt login with AOL, called from modal dialog
219  */
220 function ajax_try_aol() {
221         $('login_errmsg').innerHTML = "";
222         openid_url = encodeURI($('ajax_aol_form').elements["aol_screenname"].value);
223         do_auth_popout("openid_login?openid_url=http://openid.aol.com/" + openid_url);
224 }
225
226
227 /*
228  * The user pressed a key while in the AOL login box.
229  * Is it the enter/return key?  Submit the form.
230  */
231 function aol_onkeypress(e) {
232         if (window.event) {             /* IE */
233                 keynum = e.keyCode;
234         }
235         else if (e.which) {             /* real browsers */
236                 keynum = e.which;
237         }
238         if (keynum == 13) {             /* enter/return key */
239                 ajax_try_aol();
240                 return false;
241         }
242         return true;
243 }
244
245