github.com/hernad/nomad@v1.6.112/ui/app/adapters/token.js (about) 1 /** 2 * Copyright (c) HashiCorp, Inc. 3 * SPDX-License-Identifier: MPL-2.0 4 */ 5 6 import { inject as service } from '@ember/service'; 7 import { default as ApplicationAdapter, namespace } from './application'; 8 import OTTExchangeError from '../utils/ott-exchange-error'; 9 import classic from 'ember-classic-decorator'; 10 import { singularize } from 'ember-inflector'; 11 12 @classic 13 export default class TokenAdapter extends ApplicationAdapter { 14 @service store; 15 16 namespace = namespace + '/acl'; 17 18 createRecord(_store, type, snapshot) { 19 let data = this.serialize(snapshot); 20 data.Policies = data.PolicyIDs; 21 return this.ajax(`${this.buildURL()}/token`, 'POST', { data }); 22 } 23 24 // Delete at /token instead of /tokens 25 urlForDeleteRecord(identifier, modelName) { 26 return `${this.buildURL()}/${singularize(modelName)}/${identifier}`; 27 } 28 29 async findSelf() { 30 const response = await this.ajax(`${this.buildURL()}/token/self`, 'GET'); 31 const normalized = this.store.normalize('token', response); 32 const tokenRecord = this.store.push(normalized); 33 return tokenRecord; 34 } 35 36 async loginJWT(LoginToken, AuthMethodName) { 37 const response = await this.ajax(`${this.buildURL()}/login`, 'POST', { 38 data: { 39 AuthMethodName, 40 LoginToken, 41 }, 42 }); 43 const normalized = this.store.normalize('token', response); 44 const tokenRecord = this.store.push(normalized); 45 return tokenRecord; 46 } 47 48 exchangeOneTimeToken(oneTimeToken) { 49 return this.ajax(`${this.buildURL()}/token/onetime/exchange`, 'POST', { 50 data: { 51 OneTimeSecretID: oneTimeToken, 52 }, 53 }) 54 .then(({ Token: token }) => { 55 const store = this.store; 56 store.pushPayload('token', { 57 tokens: [token], 58 }); 59 60 return store.peekRecord( 61 'token', 62 store.normalize('token', token).data.id 63 ); 64 }) 65 .catch(() => { 66 throw new OTTExchangeError(); 67 }); 68 } 69 }