open-nomad/ui/app/adapters/token.js

40 lines
1.1 KiB
JavaScript
Raw Normal View History

import { inject as service } from '@ember/service';
2017-10-13 22:17:51 +00:00
import { default as ApplicationAdapter, namespace } from './application';
import OTTExchangeError from '../utils/ott-exchange-error';
2017-10-13 22:17:51 +00:00
export default class TokenAdapter extends ApplicationAdapter {
@service store;
namespace = namespace + '/acl';
findSelf() {
2021-12-28 14:45:20 +00:00
return this.ajax(`${this.buildURL()}/token/self`, 'GET').then((token) => {
2019-03-26 07:46:44 +00:00
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,
},
2021-12-28 14:45:20 +00:00
})
.then(({ Token: token }) => {
const store = this.store;
store.pushPayload('token', {
tokens: [token],
});
2021-12-28 14:45:20 +00:00
return store.peekRecord('token', store.normalize('token', token).data.id);
})
.catch(() => {
throw new OTTExchangeError();
});
}
}