2018-08-08 23:10:22 +00:00
|
|
|
import Service, { inject as service } from '@ember/service';
|
2017-12-15 21:39:18 +00:00
|
|
|
import { computed } from '@ember/object';
|
2021-07-13 00:42:08 +00:00
|
|
|
import { alias, reads } from '@ember/object/computed';
|
2020-01-20 20:57:01 +00:00
|
|
|
import { getOwner } from '@ember/application';
|
2017-12-15 21:39:18 +00:00
|
|
|
import { assign } from '@ember/polyfills';
|
2022-08-17 15:12:43 +00:00
|
|
|
import Ember from 'ember';
|
2020-01-20 20:57:01 +00:00
|
|
|
import { task } from 'ember-concurrency';
|
2018-08-08 23:10:22 +00:00
|
|
|
import queryString from 'query-string';
|
2017-11-14 18:50:29 +00:00
|
|
|
import fetch from 'nomad-ui/utils/fetch';
|
2020-06-10 13:49:16 +00:00
|
|
|
import classic from 'ember-classic-decorator';
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@classic
|
|
|
|
export default class TokenService extends Service {
|
|
|
|
@service store;
|
|
|
|
@service system;
|
2018-08-08 23:10:22 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
aclEnabled = true;
|
2020-01-31 00:07:43 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed
|
|
|
|
get secret() {
|
|
|
|
return window.localStorage.nomadTokenSecret;
|
|
|
|
}
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
set secret(value) {
|
|
|
|
if (value == null) {
|
|
|
|
window.localStorage.removeItem('nomadTokenSecret');
|
|
|
|
} else {
|
|
|
|
window.localStorage.nomadTokenSecret = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
@task(function* () {
|
2020-01-20 20:57:01 +00:00
|
|
|
const TokenAdapter = getOwner(this).lookup('adapter:token');
|
|
|
|
try {
|
2022-08-17 15:12:43 +00:00
|
|
|
if (this.secret || Ember.testing) {
|
|
|
|
var token = yield TokenAdapter.findSelf();
|
|
|
|
this.secret = token.secret;
|
|
|
|
return token;
|
|
|
|
}
|
2020-01-20 20:57:01 +00:00
|
|
|
} catch (e) {
|
2020-01-31 00:07:43 +00:00
|
|
|
const errors = e.errors ? e.errors.mapBy('detail') : [];
|
2021-12-28 14:45:20 +00:00
|
|
|
if (errors.find((error) => error === 'ACL support disabled')) {
|
2020-01-31 00:07:43 +00:00
|
|
|
this.set('aclEnabled', false);
|
|
|
|
}
|
2020-01-20 20:57:01 +00:00
|
|
|
return null;
|
|
|
|
}
|
2020-06-10 13:49:16 +00:00
|
|
|
})
|
|
|
|
fetchSelfToken;
|
2020-01-20 20:57:01 +00:00
|
|
|
|
2021-07-13 00:42:08 +00:00
|
|
|
@reads('fetchSelfToken.lastSuccessful.value') selfToken;
|
|
|
|
|
2021-04-01 18:21:30 +00:00
|
|
|
async exchangeOneTimeToken(oneTimeToken) {
|
|
|
|
const TokenAdapter = getOwner(this).lookup('adapter:token');
|
|
|
|
|
|
|
|
const token = await TokenAdapter.exchangeOneTimeToken(oneTimeToken);
|
|
|
|
this.secret = token.secret;
|
|
|
|
}
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
@task(function* () {
|
2020-01-20 20:57:01 +00:00
|
|
|
try {
|
|
|
|
if (this.selfToken) {
|
|
|
|
return yield this.selfToken.get('policies');
|
|
|
|
} else {
|
|
|
|
let policy = yield this.store.findRecord('policy', 'anonymous');
|
|
|
|
return [policy];
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return [];
|
|
|
|
}
|
2020-06-10 13:49:16 +00:00
|
|
|
})
|
|
|
|
fetchSelfTokenPolicies;
|
2020-01-20 20:57:01 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@alias('fetchSelfTokenPolicies.lastSuccessful.value') selfTokenPolicies;
|
2020-01-20 20:57:01 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
@task(function* () {
|
2020-01-20 20:57:01 +00:00
|
|
|
yield this.fetchSelfToken.perform();
|
2020-01-31 00:07:43 +00:00
|
|
|
if (this.aclEnabled) {
|
|
|
|
yield this.fetchSelfTokenPolicies.perform();
|
|
|
|
}
|
2020-06-10 13:49:16 +00:00
|
|
|
})
|
|
|
|
fetchSelfTokenAndPolicies;
|
2020-01-20 20:57:01 +00:00
|
|
|
|
2018-08-08 23:10:22 +00:00
|
|
|
// All non Ember Data requests should go through authorizedRequest.
|
|
|
|
// However, the request that gets regions falls into that category.
|
|
|
|
// This authorizedRawRequest is necessary in order to fetch data
|
|
|
|
// with the guarantee of a token but without the automatic region
|
|
|
|
// param since the region cannot be known at this point.
|
2020-04-28 04:00:22 +00:00
|
|
|
authorizedRawRequest(url, options = {}) {
|
|
|
|
const credentials = 'include';
|
2017-10-07 00:14:08 +00:00
|
|
|
const headers = {};
|
2019-03-26 07:46:44 +00:00
|
|
|
const token = this.secret;
|
2017-10-07 00:14:08 +00:00
|
|
|
|
2022-08-17 15:12:43 +00:00
|
|
|
if (!token && !Ember.testing) return null;
|
|
|
|
|
2017-10-07 00:14:08 +00:00
|
|
|
if (token) {
|
|
|
|
headers['X-Nomad-Token'] = token;
|
|
|
|
}
|
|
|
|
|
2020-04-28 04:00:22 +00:00
|
|
|
return fetch(url, assign(options, { headers, credentials }));
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-08-08 23:10:22 +00:00
|
|
|
|
|
|
|
authorizedRequest(url, options) {
|
2018-08-09 02:39:32 +00:00
|
|
|
if (this.get('system.shouldIncludeRegion')) {
|
|
|
|
const region = this.get('system.activeRegion');
|
2021-01-28 22:30:20 +00:00
|
|
|
if (region && url.indexOf('region=') === -1) {
|
2018-08-09 02:39:32 +00:00
|
|
|
url = addParams(url, { region });
|
|
|
|
}
|
2018-08-08 23:10:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.authorizedRawRequest(url, options);
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2020-01-31 00:06:35 +00:00
|
|
|
|
|
|
|
reset() {
|
|
|
|
this.fetchSelfToken.cancelAll({ resetState: true });
|
|
|
|
this.fetchSelfTokenPolicies.cancelAll({ resetState: true });
|
|
|
|
this.fetchSelfTokenAndPolicies.cancelAll({ resetState: true });
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-08 23:10:22 +00:00
|
|
|
|
|
|
|
function addParams(url, params) {
|
|
|
|
const paramsStr = queryString.stringify(params);
|
|
|
|
const delimiter = url.includes('?') ? '&' : '?';
|
|
|
|
return `${url}${delimiter}${paramsStr}`;
|
|
|
|
}
|