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