Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50640 views
1
###
2
User management of their API key.
3
4
(c) SageMath, Inc. LGPLv3
5
###
6
7
async = require('async')
8
9
misc = require('smc-util/misc')
10
message = require('smc-util/message') # salvus message protocol
11
{defaults, required} = misc
12
13
{is_password_correct} = require('../auth')
14
15
exports.api_key_action = (opts) ->
16
opts = defaults opts,
17
database : required
18
account_id : undefined
19
password : undefined
20
action : undefined # 'get', 'delete', 'regenerate'
21
cb : required
22
if not opts.password?
23
opts.cb("password must be given")
24
return
25
if not opts.action?
26
opts.cb("action must be given")
27
return
28
if not opts.account_id?
29
opts.cb("account_id must be signed in")
30
return
31
32
api_key = undefined
33
async.series([
34
(cb) ->
35
is_password_correct
36
database : opts.database
37
password : opts.password
38
account_id : opts.account_id
39
allow_empty_password : false
40
cb : (err, is_correct) ->
41
if err? # auth failed
42
cb(err)
43
else if not is_correct
44
cb("password is invalid")
45
else
46
cb()
47
(cb) ->
48
# do the action
49
switch opts.action
50
when 'get'
51
opts.database.get_api_key
52
account_id : opts.account_id
53
cb : (err, x) ->
54
api_key = x; cb(err)
55
when 'delete'
56
opts.database.delete_api_key
57
account_id : opts.account_id
58
cb : cb
59
when 'regenerate'
60
opts.database.regenerate_api_key
61
account_id : opts.account_id
62
cb : (err, x) ->
63
api_key = x; cb(err)
64
else
65
cb("unknown action '#{opts.action}'")
66
], (err) ->
67
opts.cb(err, api_key)
68
)
69
70