2023-04-10 15:36:59 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2017-12-15 21:39:18 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
2017-10-13 22:17:51 +00:00
|
|
|
import { default as ApplicationAdapter, namespace } from './application';
|
2021-04-01 18:21:30 +00:00
|
|
|
import OTTExchangeError from '../utils/ott-exchange-error';
|
2022-03-08 17:28:36 +00:00
|
|
|
import classic from 'ember-classic-decorator';
|
2022-12-15 18:11:28 +00:00
|
|
|
import { singularize } from 'ember-inflector';
|
2017-10-13 22:17:51 +00:00
|
|
|
|
2022-03-08 17:28:36 +00:00
|
|
|
@classic
|
2020-06-11 21:23:00 +00:00
|
|
|
export default class TokenAdapter extends ApplicationAdapter {
|
2020-06-10 13:49:16 +00:00
|
|
|
@service store;
|
2017-10-14 19:42:14 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
namespace = namespace + '/acl';
|
2017-10-14 19:42:14 +00:00
|
|
|
|
2022-12-15 18:11:28 +00:00
|
|
|
createRecord(_store, type, snapshot) {
|
|
|
|
let data = this.serialize(snapshot);
|
|
|
|
data.Policies = data.PolicyIDs;
|
|
|
|
return this.ajax(`${this.buildURL()}/token`, 'POST', { data });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete at /token instead of /tokens
|
|
|
|
urlForDeleteRecord(identifier, modelName) {
|
|
|
|
return `${this.buildURL()}/${singularize(modelName)}/${identifier}`;
|
|
|
|
}
|
|
|
|
|
2023-03-29 19:06:25 +00:00
|
|
|
async findSelf() {
|
|
|
|
const response = await this.ajax(`${this.buildURL()}/token/self`, 'GET');
|
|
|
|
const normalized = this.store.normalize('token', response);
|
|
|
|
const tokenRecord = this.store.push(normalized);
|
|
|
|
return tokenRecord;
|
|
|
|
}
|
2017-10-14 19:42:14 +00:00
|
|
|
|
2023-03-29 19:06:25 +00:00
|
|
|
async loginJWT(LoginToken, AuthMethodName) {
|
|
|
|
const response = await this.ajax(`${this.buildURL()}/login`, 'POST', {
|
|
|
|
data: {
|
|
|
|
AuthMethodName,
|
|
|
|
LoginToken,
|
|
|
|
},
|
2017-10-14 19:42:14 +00:00
|
|
|
});
|
2023-03-29 19:06:25 +00:00
|
|
|
const normalized = this.store.normalize('token', response);
|
|
|
|
const tokenRecord = this.store.push(normalized);
|
|
|
|
return tokenRecord;
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2021-04-01 18:21:30 +00:00
|
|
|
|
|
|
|
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-04-01 18:21:30 +00:00
|
|
|
|
2021-12-28 16:08:12 +00:00
|
|
|
return store.peekRecord(
|
|
|
|
'token',
|
|
|
|
store.normalize('token', token).data.id
|
|
|
|
);
|
2021-12-28 14:45:20 +00:00
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
throw new OTTExchangeError();
|
|
|
|
});
|
2021-04-01 18:21:30 +00:00
|
|
|
}
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|