open-nomad/ui/app/adapters/token.js
Buck Doyle 6d67e90763
Add exchange of one-time token on UI load (#10066)
This adds UI support for receiving the one-time token passed via query parameter, as in #10134
and related PRs, and exchanging it for its corresponding secret ID. When this works, it’s mostly
invisible, with a brief flash of the OTT onscreen.

The authentication failure message now suggests the -authenticate flag.

When OTT exchange fails, it shows a whole-page error.

This includes a known UX shortcoming in that the OTT will not disappear from the URL when an
identifier is specified on the command line, like nomad ui -authenticate jobname. The goal is to
address that shortcoming in a forthcoming pull request.
2021-04-01 13:21:30 -05:00

38 lines
1 KiB
JavaScript

import { inject as service } from '@ember/service';
import { default as ApplicationAdapter, namespace } from './application';
import OTTExchangeError from '../utils/ott-exchange-error';
export default class TokenAdapter extends ApplicationAdapter {
@service store;
namespace = namespace + '/acl';
findSelf() {
return this.ajax(`${this.buildURL()}/token/self`, 'GET').then(token => {
const store = this.store;
store.pushPayload('token', {
tokens: [token],
});
return store.peekRecord('token', store.normalize('token', token).data.id);
});
}
exchangeOneTimeToken(oneTimeToken) {
return this.ajax(`${this.buildURL()}/token/onetime/exchange`, 'POST', {
data: {
OneTimeSecretID: oneTimeToken,
},
}).then(({ Token: token }) => {
const store = this.store;
store.pushPayload('token', {
tokens: [token],
});
return store.peekRecord('token', store.normalize('token', token).data.id);
}).catch(() => {
throw new OTTExchangeError();
});
}
}