aboutsummaryrefslogtreecommitdiffstats
path: root/etherpad/src/etherpad/pro/pro_account_auto_signin.js
blob: ebcd227cd1b4c62a21283ba47826889748528710 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
 * Copyright 2009 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import("sqlbase.sqlobj");
import("stringutils");

import("etherpad.pro.pro_accounts");
import("etherpad.pro.pro_accounts.getSessionProAccount");

jimport("java.lang.System.out.println");

var _COOKIE_NAME = "PUAS";

function dmesg(m) {
  if (false) {
    println("[pro-account-auto-sign-in]: "+m);
  }
}

function checkAutoSignin() {
  dmesg("checking auto sign-in...");
  if (pro_accounts.isAccountSignedIn()) {
    dmesg("account already signed in...");
    // don't mess with already signed-in account 
    return;
  }
  var cookie = request.cookies[_COOKIE_NAME];
  if (!cookie) {
    dmesg("no auto-sign-in cookie found...");
    return;
  }
  var record = sqlobj.selectSingle('pro_accounts_auto_signin', {cookie: cookie}, {});
  if (!record) {
    return;
  }

  var now = +(new Date);
  if (+record.expires < now) {
    sqlobj.deleteRows('pro_accounts_auto_signin', {id: record.id});
    response.deleteCookie(_COOKIE_NAME);
    dmesg("deleted expired record...");
    return;
  }
  // do auto-signin (bypasses normal security)
  dmesg("Doing auto sign in...");
  var account = pro_accounts.getAccountById(record.accountId);
  pro_accounts.signInSession(account);
  response.redirect('/ep/account/sign-in?cont='+encodeURIComponent(request.url));
}

function setAutoSigninCookie(rememberMe) {
  if (!pro_accounts.isAccountSignedIn()) {
    return; // only call this function after account is already signed in.
  }

  var accountId = getSessionProAccount().id;
  // delete any existing auto-signins for this account.
  sqlobj.deleteRows('pro_accounts_auto_signin', {accountId: accountId});

  // set this insecure cookie just to indicate that account is auto-sign-in-able
  response.setCookie({
    name: "ASIE",
    value: (rememberMe ? "T" : "F"),
    path: "/",
    domain: request.domain,
    expires: new Date(32503708800000), // year 3000
  });

  if (!rememberMe) {
    return;
  }

  var cookie = stringutils.randomHash(16);
  var now = +(new Date);
  var expires = new Date(now + 1000*60*60*24*30); // 30 days
  //var expires = new Date(now + 1000 * 60 * 5); // 2 minutes

  sqlobj.insert('pro_accounts_auto_signin', {cookie: cookie, accountId: accountId, expires: expires});
  response.setCookie({
    name: _COOKIE_NAME,
    value: cookie,
    path: "/ep/account/",
    domain: request.domain,
    expires: new Date(32503708800000), // year 3000
    secure: true
  });
}