1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 window.SessionDB = function() {
9 loadFromLocalStorage: function() {
11 return JSON.parse(window.localStorage.getItem('sessions')) || {}
16 var all = db.loadFromLocalStorage()
17 if (window.defaultSession) {
18 window.defaultSession.isFromRails = true
19 all[window.defaultSession.user.uuid.slice(0, 5)] = window.defaultSession
23 loadActive: function() {
24 var sessions = db.loadAll()
25 Object.keys(sessions).forEach(function(key) {
26 if (!sessions[key].token)
31 save: function(k, v) {
32 var sessions = db.loadAll()
34 Object.keys(sessions).forEach(function(key) {
35 if (sessions[key].isFromRails)
38 window.localStorage.setItem('sessions', JSON.stringify(sessions))
41 var sessions = db.loadAll()
43 window.localStorage.setItem('sessions', JSON.stringify(sessions))
45 findAPI: function(url) {
46 // Given a Workbench or API host or URL, return a promise
47 // for the corresponding API server's base URL. Typical
49 // sessionDB.findAPI('https://workbench.example/foo').then(sessionDB.login)
50 if (url.indexOf('://') < 0)
51 url = 'https://' + url
53 return m.request(url.origin + '/discovery/v1/apis/arvados/v1/rest').then(function() {
54 return url.origin + '/'
55 }).catch(function(err) {
56 // If url is a Workbench site (and isn't too old),
57 // /status.json will tell us its API host.
58 return m.request(url.origin + '/status.json').then(function(resp) {
60 throw 'no apiBaseURL in status response'
61 return resp.apiBaseURL
65 login: function(baseURL) {
66 // Initiate login procedure with given API base URL (e.g.,
67 // "http://api.example/").
69 // Any page that has a button that invokes login() must
70 // also call checkForNewToken() on (at least) its first
71 // render. Otherwise, the login procedure can't be
73 document.location = baseURL + 'login?return_to=' + encodeURIComponent(document.location.href.replace(/\?.*/, '')+'?baseURL='+encodeURIComponent(baseURL))
77 // Forget the token, but leave the other info in the db so
78 // the user can log in again without providing the login
80 var sessions = db.loadAll()
81 delete sessions[k].token
82 db.save(k, sessions[k])
84 checkForNewToken: function() {
85 // If there's a token and baseURL in the location bar (i.e.,
86 // we just landed here after a successful login), save it and
87 // scrub the location bar.
88 if (document.location.search[0] != '?')
91 document.location.search.slice(1).split('&').map(function(kv) {
92 var e = kv.indexOf('=')
95 params[decodeURIComponent(kv.slice(0, e))] = decodeURIComponent(kv.slice(e+1))
97 if (!params.baseURL || !params.api_token)
98 // Have a query string, but it's not a login callback.
100 params.token = params.api_token
101 delete params.api_token
102 db.save(params.baseURL, params)
103 history.replaceState({}, '', document.location.origin + document.location.pathname)
105 fillMissingUUIDs: function() {
106 var sessions = db.loadAll()
107 Object.keys(sessions).map(function(key) {
108 if (key.indexOf('://') < 0)
110 // key is the baseURL placeholder. We need to get our user
111 // record to find out the cluster's real uuid prefix.
112 var session = sessions[key]
113 m.request(session.baseURL+'arvados/v1/users/current', {
115 authorization: 'OAuth2 '+session.token,
117 }).then(function(user) {
119 db.save(user.uuid.slice(0, 5), session)
124 // Return the Workbench base URL advertised by the session's
125 // API server, or a reasonable guess, or (if neither strategy
127 workbenchBaseURL: function(session) {
128 var dd = db.discoveryDoc(session)()
130 // Don't fall back to guessing until we receive the discovery doc
133 return dd.workbenchUrl
134 // Guess workbench.{apihostport} is a Workbench... unless
135 // the host part of apihostport is an IPv4 or [IPv6]
137 if (!session.baseURL.match('://(\\[|\\d+\\.\\d+\\.\\d+\\.\\d+[:/])'))
138 var wbUrl = session.baseURL.replace('://', '://workbench.')
139 // Remove the trailing slash, if it's there.
140 return wbUrl.slice(-1) == '/' ? wbUrl.slice(0, -1) : wbUrl
143 // Return a m.stream that will get fulfilled with the
144 // discovery doc from a session's API server.
145 discoveryDoc: function(session) {
146 var cache = db.discoveryCache[session.baseURL]
148 db.discoveryCache[session.baseURL] = cache = m.stream()
149 m.request(session.baseURL+'discovery/v1/apis/arvados/v1/rest').then(cache)
153 request: function(session, path, opts) {
155 opts.headers = opts.headers || {}
156 opts.headers.authorization = 'OAuth2 '+ session.token
157 return m.request(session.baseURL + path, opts)