CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/hub/user-remember-me.coffee
Views: 687
1
#########################################################################
2
# This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
# License: MS-RSL – see LICENSE.md for details
4
#########################################################################
5
6
###
7
A cached function from remember_me cookie to account_id.
8
Cache expires after 60s, to stop accepting requests from user
9
in case they invalidate their cookie.
10
###
11
12
Cache = require('lru-cache')
13
14
auth = require('./auth')
15
generateHash =require("@cocalc/server/auth/hash").default;
16
17
# Do NOT change this - this exact string is assumed in @cocalc/util/client
18
{NOT_SIGNED_IN} = require("@cocalc/util/consts")
19
20
remember_me_cache = new Cache(max:5000, ttl:60000)
21
22
exports.get_account_id = (database, remember_me, cb) ->
23
if not remember_me?
24
cb(NOT_SIGNED_IN)
25
return
26
account_id = remember_me_cache.get(remember_me)
27
if account_id
28
cb(undefined, account_id)
29
return
30
x = remember_me.split('$')
31
try
32
hash = generateHash(x[0], x[1], x[2], x[3])
33
catch err
34
cb(NOT_SIGNED_IN)
35
return
36
database.get_remember_me
37
hash : hash
38
cb : (err, signed_in_mesg) ->
39
if err
40
cb(err)
41
return
42
remember_me_cache.set(remember_me, signed_in_mesg?.account_id)
43
if not signed_in_mesg?
44
cb(NOT_SIGNED_IN)
45
else
46
cb(undefined, signed_in_mesg.account_id)
47