js-openstack-lib/src/keystone.js

47 lines
1.1 KiB
JavaScript

import log from 'loglevel';
import Http from './util/http';
log.setLevel('INFO');
export default class Keystone {
constructor (cloudConfig) {
// Sanity checks.
if (!cloudConfig) {
throw new Error('A configuration is required.');
}
// Clone the config, so that this instance is immutable
// at runtime (no modifying the config after the fact).
this.cloudConfig = Object.assign({}, cloudConfig);
this.http = new Http();
}
authenticate() {
const body = {
auth: {
identity: {
methods: ['password'],
password: {
user: {
name: this.cloudConfig.auth.username,
password: this.cloudConfig.auth.password
}
}
}
}
};
return this.http.httpPost(this.cloudConfig.auth.auth_url, body)
.then((res) => {
this.token = res.headers.get('X-Subject-Token');
return res.json(); // This returns a promise...
})
.then((body) => {
this.catalog = body.catalog || {};
})
.catch((reason) => {
return reason;
});
}
}