12033: Add session-manager page.
[arvados.git] / apps / workbench / app / assets / javascripts / models / session_db.js
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 window.models = window.models || {}
6 window.models.SessionDB = function() {
7     var db = this
8     Object.assign(db, {
9         loadAll: function() {
10             try {
11                 return JSON.parse(window.localStorage.getItem('sessions')) || {}
12             } catch(e) {}
13             return {}
14         },
15         save: function(k, v) {
16             var sessions = db.loadAll()
17             sessions[k] = v
18             window.localStorage.setItem('sessions', JSON.stringify(sessions))
19         },
20         trash: function(k) {
21             var sessions = db.loadAll()
22             delete sessions[k]
23             window.localStorage.setItem('sessions', JSON.stringify(sessions))
24         },
25         login: function(host) {
26             // Initiate login procedure with given API host (which can
27             // optionally include scheme://).
28             //
29             // Any page that has a button that invokes login() must
30             // also call checkForNewToken() on (at least) its first
31             // render. Otherwise, the login procedure can't be
32             // completed.
33             var baseURL = host
34             if (baseURL.indexOf('://') < 0)
35                 baseURL = 'https://' + baseURL
36             if (!baseURL.endsWith('/'))
37                 baseURL = baseURL + '/'
38             document.location = baseURL + 'login?return_to=' + encodeURIComponent(document.location.href+'?baseURL='+encodeURIComponent(baseURL))
39             return false
40         },
41         checkForNewToken: function() {
42             // If there's a token and baseURL in the location bar (i.e.,
43             // we just landed here after a successful login), save it and
44             // scrub the location bar.
45             if (!document.location.search.startsWith('?'))
46                 return
47             var params = {}
48             document.location.search.slice(1).split('&').map(function(kv) {
49                 var e = kv.indexOf('=')
50                 if (e < 0)
51                     return
52                 params[decodeURIComponent(kv.slice(0, e))] = decodeURIComponent(kv.slice(e+1))
53             })
54             if (!params.baseURL || !params.api_token)
55                 // Have a query string, but it's not a login callback.
56                 return
57             params.token = params.api_token
58             delete params.api_token
59             db.save(params.baseURL, params)
60             history.replaceState({}, '', document.location.origin + document.location.pathname)
61         },
62         fillMissingUUIDs: function() {
63             var sessions = db.loadAll()
64             Object.keys(sessions).map(function(key) {
65                 if (key.indexOf('://') < 0)
66                     return
67                 // key is the baseURL placeholder. We need to get our user
68                 // record to find out the cluster's real uuid prefix.
69                 var session = sessions[key]
70                 m.request(session.baseURL+'arvados/v1/users/current', {
71                     headers: {
72                         authorization: 'OAuth2 '+session.token,
73                     },
74                 }).then(function(user) {
75                     session.user = user
76                     db.save(user.uuid.slice(0, 5), session)
77                     db.trash(key)
78                 })
79             })
80             // m.request(session.baseURL + 'discovery/v1/apis/arvados/v1/rest').then(function(dd) {})
81         },
82     })
83 }