Now we can log in with AOL too
[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         $(show_which_div).style.display = 'block';
65 }
66
67
68 /*
69  * Pop out a window for external auth methods
70  * (most of them don't handle inline auth very well)
71  */
72 function do_auth_popout(popout_url) {
73         window.open(popout_url, "authpopout", "status=1,toolbar=0,width=600,height=400");
74 }
75
76
77
78
79 /****************** USERNAME AND PASSWORD ***********************/
80
81 /*
82  * Attempt login with username/password, called from modal dialog
83  */
84 function ajax_try_username_and_password() {
85
86         $('login_errmsg').innerHTML = "";
87         $('ajax_username_password_form').request({
88                 onSuccess: function(ctdlresult) {
89                         if (ctdlresult.responseText.substr(0,1) == '2') {
90                                 window.location = 'pop';
91                         }
92                         else {
93                                 $('login_errmsg').innerHTML = ctdlresult.responseText.substr(4) ;
94                         }
95                 }
96         });
97 }
98
99
100 /*
101  * The user pressed a key while in the username or password box.
102  * Is it the enter/return key?  Submit the form.
103  */
104 function username_and_password_onkeypress(e) {
105         if (window.event) {             /* IE */
106                 keynum = e.keyCode
107         }
108         else if (e.which) {             /* real browsers */
109                 keynum = e.which
110         }
111         if (keynum == 13) {             /* enter/return key */
112                 ajax_try_username_and_password();
113         }
114 }
115
116
117 /****************** REGISTER NEW USER ***********************/
118
119 /*
120  * Attempt to create a new local username/password, called from modal dialog
121  */
122 function ajax_try_newuser() {
123
124         $('login_errmsg').innerHTML = "";
125         $('ajax_newuser_form').request({
126                 onSuccess: function(ctdlresult) {
127                         if (ctdlresult.responseText.substr(0,1) == '2') {
128                                 window.location = 'pop';
129                         }
130                         else {
131                                 $('login_errmsg').innerHTML = ctdlresult.responseText.substr(4) ;
132                         }
133                 }
134         });
135 }
136
137
138 /*
139  * The user pressed a key while in the newuser or newpassword box.
140  * Is it the enter/return key?  Submit the form.
141  */
142 function newuser_onkeypress(e) {
143         if (window.event) {             /* IE */
144                 keynum = e.keyCode;
145         }
146         else if (e.which) {             /* real browsers */
147                 keynum = e.which;
148         }
149         if (keynum == 13) {             /* enter/return key */
150                 ajax_try_newuser();
151         }
152 }
153
154
155
156
157 /****************** OPENID ***********************/
158
159 /*
160  * Attempt login with OpenID, called from modal dialog
161  */
162 function ajax_try_openid() {
163         $('login_errmsg').innerHTML = "";
164         openid_url = encodeURI($('ajax_openid_form').elements["openid_url"].value);
165         do_auth_popout("openid_login?openid_url=" + openid_url);
166 }
167
168
169 /*
170  * The user pressed a key while in the openid login box.
171  * Is it the enter/return key?  Submit the form.
172  */
173 function openid_onkeypress(e) {
174         if (window.event) {             /* IE */
175                 keynum = e.keyCode;
176         }
177         else if (e.which) {             /* real browsers */
178                 keynum = e.which;
179         }
180         if (keynum == 13) {             /* enter/return key */
181                 ajax_try_openid();
182                 return false;
183         }
184         return true;
185 }
186
187
188 /****************** GOOGLE ***********************/
189
190 /*
191  * Attempt login with Google, called from modal dialog
192  */
193 function ajax_try_google() {
194         $('login_errmsg').innerHTML = "";
195         openid_url = encodeURI("https://www.google.com/accounts/o8/id");
196         do_auth_popout("openid_login?openid_url=" + openid_url);
197 }
198
199
200 /****************** GOOGLE ***********************/
201
202 /*
203  * Attempt login with Yahoo, called from modal dialog
204  */
205 function ajax_try_yahoo() {
206         $('login_errmsg').innerHTML = "";
207         openid_url = encodeURI("http://yahoo.com");
208         do_auth_popout("openid_login?openid_url=" + openid_url);
209 }
210
211
212 /****************** AOL ***********************/
213
214 /*
215  * Attempt login with AOL, called from modal dialog
216  */
217 function ajax_try_aol() {
218         $('login_errmsg').innerHTML = "";
219         openid_url = encodeURI($('ajax_aol_form').elements["aol_screenname"].value);
220         do_auth_popout("openid_login?openid_url=http://openid.aol.com/" + openid_url);
221 }
222
223
224 /*
225  * The user pressed a key while in the AOL login box.
226  * Is it the enter/return key?  Submit the form.
227  */
228 function aol_onkeypress(e) {
229         if (window.event) {             /* IE */
230                 keynum = e.keyCode;
231         }
232         else if (e.which) {             /* real browsers */
233                 keynum = e.which;
234         }
235         if (keynum == 13) {             /* enter/return key */
236                 ajax_try_aol();
237                 return false;
238         }
239         return true;
240 }
241
242